|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +func TestStreamReplay_LastEventID(t *testing.T) { |
| 13 | + jr := newJobRunner("") |
| 14 | + id := randomID() |
| 15 | + |
| 16 | + // Create a dummy job with history |
| 17 | + j := &job{ |
| 18 | + id: id, |
| 19 | + history: make([]map[string]any, 0, 64), |
| 20 | + listeners: make([]chan struct{}, 0), |
| 21 | + } |
| 22 | + jr.jobs[id] = j |
| 23 | + |
| 24 | + j.trySend(map[string]any{"line": "hello"}) |
| 25 | + j.trySend(map[string]any{"line": "world"}) |
| 26 | + j.trySend(map[string]any{"done": true, "exit_code": 0}) |
| 27 | + |
| 28 | + // Test requesting from event 1 (skipping "hello", fetching "world" and "done") |
| 29 | + req := httptest.NewRequest(http.MethodGet, "/stream", nil) |
| 30 | + req.Header.Set("Last-Event-ID", "0") |
| 31 | + rec := httptest.NewRecorder() |
| 32 | + |
| 33 | + err := jr.streamSSE(rec, req, id) |
| 34 | + if err != nil { |
| 35 | + t.Fatalf("streamSSE error: %v", err) |
| 36 | + } |
| 37 | + |
| 38 | + out := rec.Body.String() |
| 39 | + if strings.Contains(out, "hello") { |
| 40 | + t.Fatalf("expected hello to be skipped, got: %s", out) |
| 41 | + } |
| 42 | + if !strings.Contains(out, "world") { |
| 43 | + t.Fatalf("expected world, got: %s", out) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestStreamReplay_GapWarning(t *testing.T) { |
| 48 | + jr := newJobRunner("") |
| 49 | + id := randomID() |
| 50 | + |
| 51 | + j := &job{ |
| 52 | + id: id, |
| 53 | + history: make([]map[string]any, 0, 64), |
| 54 | + listeners: make([]chan struct{}, 0), |
| 55 | + baseIndex: 50, // simulate that events 0-49 were evicted |
| 56 | + } |
| 57 | + jr.jobs[id] = j |
| 58 | + |
| 59 | + j.trySend(map[string]any{"line": "late event"}) |
| 60 | + j.trySend(map[string]any{"done": true, "exit_code": 0}) |
| 61 | + |
| 62 | + // Requesting from event 0, but base is 50, so gap warning should be emitted |
| 63 | + req := httptest.NewRequest(http.MethodGet, "/stream", nil) |
| 64 | + req.Header.Set("Last-Event-ID", "0") |
| 65 | + rec := httptest.NewRecorder() |
| 66 | + |
| 67 | + err := jr.streamSSE(rec, req, id) |
| 68 | + if err != nil { |
| 69 | + t.Fatalf("streamSSE error: %v", err) |
| 70 | + } |
| 71 | + |
| 72 | + out := rec.Body.String() |
| 73 | + if !strings.Contains(out, "stream replay gap") { |
| 74 | + t.Fatalf("expected gap warning, got: %s", out) |
| 75 | + } |
| 76 | + if !strings.Contains(out, "late event") { |
| 77 | + t.Fatalf("expected late event, got: %s", out) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestStreamReplay_LiveReconnection(t *testing.T) { |
| 82 | + jr := newJobRunner("") |
| 83 | + tmpDir := t.TempDir() |
| 84 | + t.Setenv("UKC_AGENT_WORKSPACE", tmpDir) |
| 85 | + id := jr.start(context.Background(), "echo 'first'; sleep 0.1; echo 'second'") |
| 86 | + |
| 87 | + // First connection: read "first", then disconnect |
| 88 | + ctx, cancel1 := context.WithCancel(context.Background()) |
| 89 | + req1, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/stream", nil) |
| 90 | + rec1 := httptest.NewRecorder() |
| 91 | + |
| 92 | + // We want to stream until we see "first", then cancel the context to simulate disconnect |
| 93 | + go func() { |
| 94 | + // Read body manually instead of streamSSE to interleave properly |
| 95 | + time.Sleep(50 * time.Millisecond) |
| 96 | + cancel1() |
| 97 | + }() |
| 98 | + |
| 99 | + _ = jr.streamSSE(rec1, req1, id) // this will return context.Canceled |
| 100 | + |
| 101 | + // Second connection: read the rest |
| 102 | + req2 := httptest.NewRequest(http.MethodGet, "/stream", nil) |
| 103 | + rec2 := httptest.NewRecorder() |
| 104 | + _ = jr.streamSSE(rec2, req2, id) |
| 105 | + |
| 106 | + out := rec2.Body.String() |
| 107 | + |
| 108 | + // We might get "first" again if we didn't use Last-Event-ID, but we should definitely get "second" |
| 109 | + if !strings.Contains(out, "second") { |
| 110 | + t.Fatalf("expected second to arrive in the stream: %s", out) |
| 111 | + } |
| 112 | +} |
0 commit comments