Repository navigation
Expand file tree
/
Copy pathstream_test.go
More file actions
434 lines (370 loc) · 15.2 KB
/
Copy pathstream_test.go
File metadata and controls
434 lines (370 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
package dispatch_test
import (
"context"
"errors"
"slices"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/kapetan-io/dispatch/internal/herdr"
"github.com/kapetan-io/dispatch/internal/herdrtest"
)
// stateRule comments the worker's state once per state, so a test reads what the rules saw.
const stateRule = `
whenever({ name: "state", each: "ticket", if: () => true,
then: (t, ctx) => {
const state = ctx.worker.of(t).state;
return ctx.ticket.queueComment(t, "state " + state, { once: ["state", t.id, state] });
} });`
// startStreaming starts the loop over a streaming herdr and returns once the stream is live and the loop has
// settled, so a change the test makes next is the only thing the loop is waiting on.
func (d *Dispatch) startStreaming() {
d.t.Helper()
d.Restart()
d.streamLive(1)
}
// streamLive waits for the nth stream to name its first panes and for the loop to take them.
func (d *Dispatch) streamLive(n int) {
d.t.Helper()
require.Eventually(d.t, func() bool { return d.tools.Multiplexer.Streams() >= n }, 5*time.Second, time.Millisecond)
d.settleLoop()
}
func (d *Dispatch) herdrCalls(name string) int { return len(d.tools.Calls("herdr", name)) }
// readLines is every read line in the newest history segment.
func (d *Dispatch) readLines() []string {
d.t.Helper()
_, lines := d.newestSegment()
var reads []string
for _, line := range lines {
if strings.HasPrefix(line, `{"line":"read"`) {
reads = append(reads, line)
}
}
return reads
}
func streamingWorker(t *testing.T) *Dispatch {
t.Helper()
d := newDispatch(t, dispatchOptions{Streaming: true})
d.Linear.Tickets(ticket("ENG-1", "In Progress", "dispatch"))
d.Workers.Running("ENG-1", "working")
d.startStreaming()
return d
}
func TestStreamShowsAnIdleWorkerAndTellsItsFollowerWithoutAResync(t *testing.T) {
t.Parallel()
d := streamingWorker(t)
d.Workers.Stop("ENG-1", stopTasks{})
d.settleLoop()
reads, panes := d.herdrCalls("read"), d.herdrCalls("agent-at")
d.Workers.SetStatus("ENG-1", "idle")
require.Eventually(t, func() bool { return slices.Contains(d.Workers.Renames(), "w-ENG-1|💤 ENG-1") }, 5*time.Second, 5*time.Millisecond)
d.settleLoop()
assert.Contains(t, d.Followed.Told(), "ENG-1's worker is idle with nothing running: ticket ENG-1")
assert.Equal(t, reads, d.herdrCalls("read"))
assert.Equal(t, panes+1, d.herdrCalls("agent-at"))
}
func TestStreamTellsTheFollowerAWorkerIsBlocked(t *testing.T) {
t.Parallel()
d := streamingWorker(t)
reads := d.herdrCalls("read")
d.Workers.SetStatus("ENG-1", "blocked")
require.Eventually(t, func() bool {
return slices.Contains(d.Followed.Told(), "ENG-1 is waiting on your answer in its workspace")
}, 5*time.Second, 5*time.Millisecond)
assert.Equal(t, reads, d.herdrCalls("read"))
}
func TestStreamShowsAWorkerGoneWhenItsClaudeExitsOrItsTabOrWorkspaceCloses(t *testing.T) {
t.Parallel()
for _, test := range []struct {
name string
gone func(d *Dispatch)
}{
{"ClaudeExits", func(d *Dispatch) { d.tools.Multiplexer.ExitAgent("w-ENG-1:p1") }},
{"TabCloses", func(d *Dispatch) {
require.NoError(d.t, d.tools.Multiplexer.CloseTab(context.Background(), "w-ENG-1:t1"))
}},
{"WorkspaceCloses", func(d *Dispatch) { d.Workers.Gone("ENG-1") }},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
d := newDispatch(t, dispatchOptions{Streaming: true, Rules: rule(stateRule)})
d.Linear.Tickets(ticket("ENG-1", "In Progress", "dispatch"))
d.Workers.Running("ENG-1", "working")
d.startStreaming()
require.Equal(t, "state working", lastReading(t, d))
test.gone(d)
require.Eventually(t, func() bool { return lastReadingOf(d, "ENG-1") == "state none" }, 5*time.Second, 5*time.Millisecond)
})
}
}
func TestStreamLeavesAWorkerAloneForAChangeToAPaneItNoLongerHolds(t *testing.T) {
t.Parallel()
d := newDispatch(t, dispatchOptions{Streaming: true, Rules: rule(stateRule)})
d.Linear.Tickets(ticket("ENG-1", "In Progress", "dispatch"))
d.Workers.Running("ENG-1", "working")
d.startStreaming()
d.Workers.Gone("ENG-1")
require.Eventually(t, func() bool { return lastReadingOf(d, "ENG-1") == "state none" }, 5*time.Second, 5*time.Millisecond)
d.settleLoop()
calls := len(d.tools.Journal.Calls())
d.tools.Multiplexer.Changed("w-ENG-1:p1")
d.settleLoop()
assert.Equal(t, calls, len(d.tools.Journal.Calls()))
}
func TestStreamAppliesAChangeThatArrivesDuringAReadAfterThatRead(t *testing.T) {
t.Parallel()
d := newDispatch(t, dispatchOptions{Streaming: true, Rules: rule(stateRule)})
d.Linear.Tickets(ticket("ENG-1", "In Progress", "dispatch"))
d.Workers.Running("ENG-1", "working")
d.startStreaming()
d.tools.Multiplexer.SetStatusAfterSnapshot("w-ENG-1:p1", "blocked")
require.NoError(t, d.TickAfterASecond())
require.Eventually(t, func() bool { return lastReadingOf(d, "ENG-1") == "state blocked" }, 5*time.Second, 5*time.Millisecond)
d.settleLoop()
assert.Equal(t, []string{"ENG-1: state working", "ENG-1: state blocked"}, d.Linear.Comments())
}
func TestStreamIgnoresAChangeToAPaneThatHoldsNoWorker(t *testing.T) {
t.Parallel()
d := newDispatch(t, dispatchOptions{Streaming: true})
d.Linear.Tickets(ticket("ENG-1", "In Progress", "dispatch"))
d.Workers.Running("ENG-1", "working")
d.startStreaming()
calls, receipts := len(d.tools.Journal.Calls()), len(d.Receipts("ENG-1"))
d.tools.Multiplexer.Changed("a-pane-of-the-users:p1")
d.settleLoop()
assert.Equal(t, calls, len(d.tools.Journal.Calls()))
assert.Len(t, d.Receipts("ENG-1"), receipts)
}
func TestStreamMakesNoCallForAChangeAfterAFailedReadOfTheMultiplexer(t *testing.T) {
t.Parallel()
d := newDispatch(t, dispatchOptions{Streaming: true})
d.Linear.Tickets(ticket("ENG-1", "In Progress", "dispatch"))
d.Workers.Running("ENG-1", "working")
d.startStreaming()
d.tools.Multiplexer.Fail(herdrtest.OpRead, errors.New("the server is down"))
require.NoError(t, d.TickAfterASecond())
d.settleLoop()
calls := len(d.tools.Journal.Calls())
d.Workers.SetStatus("ENG-1", "idle")
d.settleLoop()
assert.Equal(t, calls, len(d.tools.Journal.Calls()))
}
func TestStreamDownIsReportedAtEachTickAndReopenedWhenTheMultiplexerIsBack(t *testing.T) {
t.Parallel()
d := newDispatch(t, dispatchOptions{Streaming: true})
d.Linear.Tickets(ticket("ENG-1", "In Progress", "dispatch"))
d.Workers.Running("ENG-1", "working")
d.tools.Multiplexer.Fail(herdrtest.OpStream, herdr.ErrRexNotRunning)
d.Restart()
const downLine = "dispatch: the worker stream ended: Rex is not running, and dispatch never starts it: open Rex; workers update at each resync until it reopens\n"
waitForDown := func(n int) {
require.Eventually(t, func() bool { return strings.Count(d.loopErr(), downLine) == n }, 5*time.Second, time.Millisecond)
}
waitForDown(1)
require.NoError(t, d.TickAfterASecond())
waitForDown(2)
d.tools.Multiplexer.Fail(herdrtest.OpStream, nil)
require.NoError(t, d.TickAfterASecond())
d.streamLive(1)
assert.Equal(t, 3, d.herdrCalls("changes"))
d.Workers.SetStatus("ENG-1", "blocked")
require.Eventually(t, func() bool {
return slices.Contains(d.Followed.Told(), "ENG-1 is waiting on your answer in its workspace")
}, 5*time.Second, 5*time.Millisecond)
}
func TestStreamDroppedIsReportedOnceAndDoesNotReopenInPlace(t *testing.T) {
t.Parallel()
d := newDispatch(t, dispatchOptions{Streaming: true})
d.Linear.Tickets(ticket("ENG-1", "In Progress", "dispatch"))
d.Workers.Running("ENG-1", "working")
d.startStreaming()
d.tools.Multiplexer.DropStream(herdr.ErrRexEventsEnded)
require.Eventually(t, func() bool {
return strings.Contains(d.loopErr(), "the worker stream ended: rex events ended: the server disconnected; workers update at each resync until it reopens")
}, 5*time.Second, time.Millisecond)
d.settleLoop()
assert.Equal(t, 1, d.herdrCalls("changes"))
}
func TestStreamStoppedWithTheLoopPrintsNoStreamDownLine(t *testing.T) {
t.Parallel()
d := newDispatch(t, dispatchOptions{Streaming: true})
d.Linear.Tickets(ticket("ENG-1", "In Progress", "dispatch"))
d.Workers.Running("ENG-1", "working")
d.startStreaming()
d.Stop()
assert.NotContains(t, d.loopErr(), "the worker stream ended")
}
func TestReplayMatchesARunWhoseWorldsCameFromTheStream(t *testing.T) {
t.Parallel()
d := streamingWorker(t)
d.Workers.Stop("ENG-1", stopTasks{})
d.Workers.SetStatus("ENG-1", "idle")
require.Eventually(t, func() bool { return slices.Contains(d.Workers.Renames(), "w-ENG-1|💤 ENG-1") }, 5*time.Second, 5*time.Millisecond)
d.Workers.SetStatus("ENG-1", "blocked")
require.Eventually(t, func() bool {
return slices.Contains(d.Followed.Told(), "ENG-1 is waiting on your answer in its workspace")
}, 5*time.Second, 5*time.Millisecond)
d.settleLoop()
d.Stop()
cycles, matched := replayOf(t, d)
assert.Positive(t, cycles)
assert.Equal(t, cycles, matched)
}
// workersOf is the workers part of a read line, which holds what the rules decide a worker is.
func workersOf(t *testing.T, line string) string {
t.Helper()
_, after, ok := strings.Cut(line, `"workers":`)
require.True(t, ok)
before, _, _ := strings.Cut(after, `,"sessions":`)
return before
}
func TestTickAfterAStreamChangeLeavesTheWorkerAsTheStreamReadIt(t *testing.T) {
t.Parallel()
for _, test := range []struct {
name string
change func(d *Dispatch)
state string
}{
{"Idle", func(d *Dispatch) { d.Workers.SetStatus("ENG-1", "idle") }, "idle"},
{"ClaudeExits", func(d *Dispatch) { d.tools.Multiplexer.ExitAgent("w-ENG-1:p1") }, "none"},
{"TabCloses", func(d *Dispatch) {
require.NoError(d.t, d.tools.Multiplexer.CloseTab(context.Background(), "w-ENG-1:t1"))
}, "none"},
} {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
d := newDispatch(t, dispatchOptions{Streaming: true})
d.Linear.Tickets(ticket("ENG-1", "In Progress", "dispatch"))
d.Workers.Running("ENG-1", "working")
d.startStreaming()
test.change(d)
require.Eventually(t, func() bool {
reads := d.readLines()
return strings.Contains(workersOf(t, reads[len(reads)-1]), `"State":"`+test.state+`"`)
}, 5*time.Second, 5*time.Millisecond)
d.settleLoop()
before := d.readLines()
require.NoError(t, d.TickAfterASecond())
after := d.readLines()
assert.Equal(t, workersOf(t, before[len(before)-1]), workersOf(t, after[len(after)-1]))
})
}
}
func TestStreamShowsANudgedWorkerIdleOnceItWorkedAndKeepsItWorkingWhileItReadsIdle(t *testing.T) {
t.Parallel()
d := newDispatch(t, dispatchOptions{Streaming: true})
d.Linear.Tickets(ticket("ENG-1", "In Progress", "dispatch"))
d.Workers.Running("ENG-1", "idle")
d.Workers.Stop("ENG-1", stopTasks{})
d.startStreaming()
require.Equal(t, []string{"w-ENG-1|💤 ENG-1"}, d.Workers.Renames())
d.Advance(time.Second)
require.NoError(t, d.Run("tell", "ENG-1", "carry on").Err)
require.Eventually(t, func() bool { return len(d.Workers.Renames()) == 2 }, 5*time.Second, 5*time.Millisecond)
// A change that finds the worker still idle before Claude took the nudge keeps it working.
panes := d.herdrCalls("agent-at")
d.tools.Multiplexer.Changed("w-ENG-1:p1")
require.Eventually(t, func() bool { return d.herdrCalls("agent-at") > panes }, 5*time.Second, time.Millisecond)
d.settleLoop()
assert.Len(t, d.Workers.Renames(), 2)
panes = d.herdrCalls("agent-at")
d.Workers.SetStatus("ENG-1", "working")
require.Eventually(t, func() bool { return d.herdrCalls("agent-at") > panes }, 5*time.Second, time.Millisecond)
d.settleLoop()
d.Workers.SetStatus("ENG-1", "idle")
require.Eventually(t, func() bool {
renames := d.Workers.Renames()
return len(renames) > 2 && renames[len(renames)-1] == "w-ENG-1|💤 ENG-1"
}, 5*time.Second, 5*time.Millisecond)
}
func TestStreamTellsTheFollowerAWorkerIsIdleOnceTheStopRecordOfItsTurnLands(t *testing.T) {
t.Parallel()
d := newDispatch(t, dispatchOptions{Streaming: true})
d.Linear.Tickets(ticket("ENG-1", "In Progress", "dispatch"))
d.Workers.Running("ENG-1", "working")
// The previous turn ended with a background task still running.
d.Workers.Stop("ENG-1", stopTasks{Running: 1})
d.startStreaming()
// Claude reports idle just before its Stop hook lands the record of the turn that ended.
d.Workers.SetStatus("ENG-1", "idle")
d.settleLoop()
require.NotContains(t, d.Followed.Told(), "ENG-1's worker is idle with nothing running: ticket ENG-1")
d.Advance(time.Second)
d.Workers.Stop("ENG-1", stopTasks{})
require.Eventually(t, func() bool {
return slices.Contains(d.Followed.Told(), "ENG-1's worker is idle with nothing running: ticket ENG-1")
}, 5*time.Second, 5*time.Millisecond)
}
func TestStreamDownLeavesWorkersToTheNextTickWhichReopensIt(t *testing.T) {
t.Parallel()
d := newDispatch(t, dispatchOptions{Streaming: true})
d.Linear.Tickets(ticket("ENG-1", "In Progress", "dispatch"))
d.Workers.Running("ENG-1", "working")
d.startStreaming()
d.tools.Multiplexer.Fail(herdrtest.OpStream, herdr.ErrRexNotRunning)
d.tools.Multiplexer.DropStream(herdr.ErrRexEventsEnded)
require.Eventually(t, func() bool { return strings.Contains(d.loopErr(), "the worker stream ended") }, 5*time.Second, time.Millisecond)
d.Workers.SetStatus("ENG-1", "blocked")
d.settleLoop()
require.NotContains(t, d.Followed.Told(), "ENG-1 is waiting on your answer in its workspace")
d.tools.Multiplexer.Fail(herdrtest.OpStream, nil)
require.NoError(t, d.TickAfterASecond())
assert.Contains(t, d.Followed.Told(), "ENG-1 is waiting on your answer in its workspace")
d.streamLive(2)
assert.Equal(t, 2, d.herdrCalls("changes"))
}
func TestStreamNamesMadeDuringAFailedReadAreLeftToTheNextFullRead(t *testing.T) {
t.Parallel()
d := newDispatch(t, dispatchOptions{Streaming: true})
d.Linear.Tickets(ticket("ENG-1", "In Progress", "dispatch"))
d.Workers.Running("ENG-1", "working")
d.startStreaming()
d.tools.Multiplexer.Fail(herdrtest.OpRead, errors.New("the server is down"))
require.NoError(t, d.TickAfterASecond())
d.settleLoop()
d.Workers.SetStatus("ENG-1", "blocked")
d.settleLoop()
d.tools.Multiplexer.Fail(herdrtest.OpRead, nil)
require.NoError(t, d.TickAfterASecond())
assert.Contains(t, d.Followed.Told(), "ENG-1 is waiting on your answer in its workspace")
}
func TestStreamRunsACycleWhileChangesKeepArriving(t *testing.T) {
t.Parallel()
d := newDispatch(t, dispatchOptions{Streaming: true, Rules: rule(stateRule)})
d.Linear.Tickets(ticket("ENG-1", "In Progress", "dispatch"))
d.Workers.Running("ENG-1", "working")
d.startStreaming()
require.Equal(t, "state working", lastReading(t, d))
flooding, stop := make(chan struct{}), make(chan struct{})
go func() {
defer close(flooding)
for {
select {
case <-stop:
return
default:
d.tools.Multiplexer.Changed("w-ENG-1:p1")
}
}
}()
defer func() { close(stop); <-flooding }()
d.Workers.SetStatus("ENG-1", "blocked")
require.Eventually(t, func() bool { return lastReadingOf(d, "ENG-1") == "state blocked" }, 5*time.Second, 5*time.Millisecond)
}
func TestSettleAnswersWhenAStreamNameLeavesNoWorkBehind(t *testing.T) {
t.Parallel()
d := newDispatch(t, dispatchOptions{Streaming: true})
d.Linear.Tickets(ticket("ENG-1", "In Progress", "dispatch"))
d.Workers.Running("ENG-1", "working")
d.startStreaming()
d.tools.Multiplexer.Fail(herdrtest.OpRead, errors.New("the server is down"))
require.NoError(t, d.TickAfterASecond())
d.settleLoop()
// The failed read lists no panes, so a name the stream sends is dropped without starting a read.
for range 300 {
d.tools.Multiplexer.Changed("w-ENG-1:p1")
d.settleLoop()
}
}