Restore terminal log event stream

This commit is contained in:
npc0-hue
2026-08-10 23:55:23 +08:00
parent 0154f42485
commit eb3c708697
6 changed files with 29 additions and 6 deletions
+25 -2
View File
@@ -1,7 +1,10 @@
package api
import (
"context"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
@@ -41,7 +44,12 @@ func TestLogEventsSSEReplaysHistory(t *testing.T) {
batch := validLogBatchRequest(t, hello.SessionToken, 1, 2)
assertStatus(t, performJSON(t, router, http.MethodPost, "/api/v1/run/logs/batches", batch), http.StatusOK)
assertStatus(t, performJSON(t, router, http.MethodGet, "/api/v1/server-instances/server-1/logs/events?historyLimit=2", nil), http.StatusNotFound)
recorder := performCancelledSSE(t, router, "/api/v1/server-instances/server-1/logs/events?historyLimit=2")
assertStatus(t, recorder, http.StatusOK)
body := recorder.Body.String()
if !strings.Contains(recorder.Header().Get("Content-Type"), "text/event-stream") || !strings.Contains(body, "event: stream") || !strings.Contains(body, "event: log") || !strings.Contains(body, "event: ready") || !strings.Contains(body, `"seq":1`) || !strings.Contains(body, `"seq":2`) {
t.Fatalf("expected stream, history log, and ready SSE events, headers=%v body=%s", recorder.Header(), body)
}
}
func TestLogEventsSSEUsesServerWideNewestHistory(t *testing.T) {
@@ -54,7 +62,22 @@ func TestLogEventsSSEUsesServerWideNewestHistory(t *testing.T) {
assertStatus(t, performJSON(t, router, http.MethodPost, "/api/v1/run/logs/batches", validLogBatchRequestForStream(t, hello.SessionToken, "log-1", "stdout", 1, 2, 0)), http.StatusOK)
assertStatus(t, performJSON(t, router, http.MethodPost, "/api/v1/run/logs/batches", validLogBatchRequestForStream(t, hello.SessionToken, "log-2", "stderr", 1, 2, 10)), http.StatusOK)
assertStatus(t, performJSON(t, router, http.MethodGet, "/api/v1/server-instances/server-1/logs/events?historyLimit=2", nil), http.StatusNotFound)
recorder := performCancelledSSE(t, router, "/api/v1/server-instances/server-1/logs/events?historyLimit=2")
assertStatus(t, recorder, http.StatusOK)
body := recorder.Body.String()
if strings.Count(body, "event: log") != 2 || !strings.Contains(body, `"streamId":"log-2"`) || strings.Contains(body, `"streamId":"log-1"`) {
t.Fatalf("expected server-wide newest history across streams, body=%s", body)
}
}
func performCancelledSSE(t *testing.T, router http.Handler, path string) *httptest.ResponseRecorder {
t.Helper()
ctx, cancel := context.WithCancel(context.Background())
cancel()
req := httptest.NewRequest(http.MethodGet, path, nil).WithContext(ctx)
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
return rec
}
func TestLogIngestAPIDuplicateAndErrors(t *testing.T) {