Add persistent Run control stream
This commit is contained in:
@@ -136,6 +136,45 @@ func TestPlatformClientSignsRunChannelRequestsWithUniqueNonce(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestPlatformClientStreamsSignedControlEvents(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost || r.URL.Path != "/api/v1/run/control/events" {
|
||||
t.Fatalf("unexpected request %s %s", r.Method, r.URL.Path)
|
||||
}
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("read stream body: %v", err)
|
||||
}
|
||||
verifyRunRequestSignature(t, r, body, "run-local", "session-token")
|
||||
var request protocol.RunControlStreamRequest
|
||||
if err := json.Unmarshal(body, &request); err != nil {
|
||||
t.Fatalf("decode stream request: %v", err)
|
||||
}
|
||||
if request.LastEventSeq != 7 {
|
||||
t.Fatalf("unexpected stream cursor: %+v", request)
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
_, _ = w.Write([]byte("event: job.changed\nid: 8\ndata: {\"runEndpointId\":\"run-local\",\"sequence\":8,\"type\":\"job.changed\",\"serverTime\":\"2026-07-03T12:00:00Z\"}\n\n"))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client, err := NewPlatformClient(server.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("new client: %v", err)
|
||||
}
|
||||
events := []protocol.RunControlEvent{}
|
||||
err = client.StreamControlEvents(context.Background(), protocol.RunControlStreamRequest{RunEndpointID: "run-local", SessionToken: "session-token", LastEventSeq: 7}, func(event protocol.RunControlEvent) error {
|
||||
events = append(events, event)
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("stream events: %v", err)
|
||||
}
|
||||
if len(events) != 1 || events[0].Type != protocol.RunControlEventTypeJobChanged || events[0].Sequence != 8 {
|
||||
t.Fatalf("unexpected control events: %+v", events)
|
||||
}
|
||||
}
|
||||
|
||||
func verifyRunRequestSignature(t *testing.T, request *http.Request, body []byte, endpoint string, token string) {
|
||||
t.Helper()
|
||||
if request.Header.Get("X-Run-Endpoint") != endpoint {
|
||||
|
||||
Reference in New Issue
Block a user