opencode-copilot-delegate 0.8.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,536 @@
1
+ import { describe, expect, it } from 'bun:test'
2
+ import type { TuiDialogProps } from '@opencode-ai/plugin/tui'
3
+ import { type JSX, testRender } from '@opentui/solid'
4
+
5
+ type TaskStatus = 'running' | 'cancelling' | 'complete' | 'failed' | 'cancelled'
6
+
7
+ type TaskRow = {
8
+ taskId: string
9
+ status: TaskStatus
10
+ agent: string
11
+ model: string
12
+ elapsedMs: number
13
+ toolCallCount: number
14
+ startedAt: number
15
+ }
16
+
17
+ type TasksListResponse = {
18
+ tasks: TaskRow[]
19
+ }
20
+
21
+ type ModalListClock = {
22
+ now(): number
23
+ setInterval(fn: () => void, delayMs: number): unknown
24
+ clearInterval(handle: unknown): void
25
+ }
26
+
27
+ type ModalListProps = {
28
+ Dialog: (props: TuiDialogProps) => JSX.Element
29
+ rpc: {
30
+ tasksList(): Promise<TasksListResponse>
31
+ }
32
+ onClose: () => void
33
+ onCancelTask?: (task: TaskRow) => void
34
+ clock?: ModalListClock
35
+ }
36
+
37
+ type ModalListComponent = (props: ModalListProps) => JSX.Element
38
+
39
+ type Deferred<T> = {
40
+ promise: Promise<T>
41
+ resolve(value: T): void
42
+ reject(error: unknown): void
43
+ }
44
+
45
+ class TestClock {
46
+ private time: number
47
+ private nextId = 1
48
+ private readonly intervals = new Map<
49
+ number,
50
+ {
51
+ delayMs: number
52
+ nextAt: number
53
+ fn: () => void
54
+ }
55
+ >()
56
+ readonly cleared: number[] = []
57
+
58
+ constructor(startAt = 0) {
59
+ this.time = startAt
60
+ }
61
+
62
+ now(): number {
63
+ return this.time
64
+ }
65
+
66
+ setInterval(fn: () => void, delayMs: number): number {
67
+ const id = this.nextId
68
+ this.nextId += 1
69
+ this.intervals.set(id, {
70
+ delayMs,
71
+ nextAt: this.time + delayMs,
72
+ fn,
73
+ })
74
+ return id
75
+ }
76
+
77
+ clearInterval(handle: unknown): void {
78
+ const id = Number(handle)
79
+ this.cleared.push(id)
80
+ this.intervals.delete(id)
81
+ }
82
+
83
+ advance(delayMs: number): void {
84
+ const target = this.time + delayMs
85
+
86
+ while (true) {
87
+ let nextId: number | undefined
88
+ let nextAt = Number.POSITIVE_INFINITY
89
+
90
+ for (const [id, interval] of this.intervals) {
91
+ if (interval.nextAt < nextAt) {
92
+ nextAt = interval.nextAt
93
+ nextId = id
94
+ }
95
+ }
96
+
97
+ if (nextId === undefined || nextAt > target) {
98
+ break
99
+ }
100
+
101
+ const interval = this.intervals.get(nextId)
102
+ if (!interval) {
103
+ break
104
+ }
105
+
106
+ this.time = interval.nextAt
107
+ interval.fn()
108
+
109
+ const updated = this.intervals.get(nextId)
110
+ if (updated) {
111
+ updated.nextAt += updated.delayMs
112
+ }
113
+ }
114
+
115
+ this.time = target
116
+ }
117
+
118
+ get activeIntervalCount(): number {
119
+ return this.intervals.size
120
+ }
121
+
122
+ adapter(): ModalListClock {
123
+ return {
124
+ now: () => this.now(),
125
+ setInterval: (fn, delayMs) => this.setInterval(fn, delayMs),
126
+ clearInterval: (handle) => this.clearInterval(handle),
127
+ }
128
+ }
129
+ }
130
+
131
+ async function loadModalList(): Promise<ModalListComponent> {
132
+ await import('@opentui/solid/runtime-plugin-support')
133
+
134
+ const module = await import('../components/modal-list')
135
+ const modalList =
136
+ Reflect.get(module, 'ModalList') ?? Reflect.get(module, 'default')
137
+
138
+ expect(typeof modalList).toBe('function')
139
+
140
+ return modalList as ModalListComponent
141
+ }
142
+
143
+ function createDeferred<T>(): Deferred<T> {
144
+ let resolveFn: (value: T) => void = () => {}
145
+ let rejectFn: (error: unknown) => void = () => {}
146
+ const promise = new Promise<T>((resolve, reject) => {
147
+ resolveFn = resolve
148
+ rejectFn = reject
149
+ })
150
+
151
+ return {
152
+ promise,
153
+ resolve(value: T) {
154
+ resolveFn(value)
155
+ },
156
+ reject(error: unknown) {
157
+ rejectFn(error)
158
+ },
159
+ }
160
+ }
161
+
162
+ function createDialogStub() {
163
+ let onClose: (() => void) | undefined
164
+
165
+ const Dialog = (props: TuiDialogProps) => {
166
+ onClose = props.onClose
167
+ return <box flexDirection="column">{props.children}</box>
168
+ }
169
+
170
+ return {
171
+ Dialog,
172
+ getOnClose: () => onClose,
173
+ }
174
+ }
175
+
176
+ function makeTask(taskId: string, overrides: Partial<TaskRow> = {}): TaskRow {
177
+ return {
178
+ taskId,
179
+ status: 'running',
180
+ agent: 'default',
181
+ model: 'gpt-5',
182
+ elapsedMs: 0,
183
+ toolCallCount: 0,
184
+ startedAt: 0,
185
+ ...overrides,
186
+ }
187
+ }
188
+
189
+ type CaptureSpans = Awaited<ReturnType<typeof testRender>>['captureSpans']
190
+ type CapturedFrame = ReturnType<CaptureSpans>
191
+
192
+ function findLineAttributesContaining(frame: CapturedFrame, text: string) {
193
+ return frame.lines.find((line) =>
194
+ line.spans.some((span) => span.text.includes(text)),
195
+ )
196
+ }
197
+
198
+ function lineHasInverse(
199
+ line: CapturedFrame['lines'][number] | undefined,
200
+ ): boolean {
201
+ if (!line) {
202
+ return false
203
+ }
204
+
205
+ return line.spans.some(
206
+ (span) => span.text.trim().length > 0 && span.attributes !== 0,
207
+ )
208
+ }
209
+
210
+ async function settle(
211
+ renderOnce: () => Promise<void>,
212
+ idle: () => Promise<void>,
213
+ ) {
214
+ await Promise.resolve()
215
+ await new Promise((resolve) => setTimeout(resolve, 0))
216
+ await renderOnce()
217
+ await idle()
218
+ await renderOnce()
219
+ }
220
+
221
+ async function waitForFrame(options: {
222
+ renderOnce: () => Promise<void>
223
+ idle: () => Promise<void>
224
+ captureCharFrame: () => string
225
+ includes: string
226
+ attempts?: number
227
+ }): Promise<string> {
228
+ const attempts = options.attempts ?? 10
229
+
230
+ for (let index = 0; index < attempts; index += 1) {
231
+ await settle(options.renderOnce, options.idle)
232
+ const frame = options.captureCharFrame()
233
+
234
+ if (frame.includes(options.includes)) {
235
+ return frame
236
+ }
237
+ }
238
+
239
+ return options.captureCharFrame()
240
+ }
241
+
242
+ describe('modal list', () => {
243
+ it('renders a loading state before tasksList resolves', async () => {
244
+ const ModalList = await loadModalList()
245
+ const deferred = createDeferred<TasksListResponse>()
246
+ const dialog = createDialogStub()
247
+ let calls = 0
248
+
249
+ const { renderOnce, captureCharFrame } = await testRender(
250
+ () => (
251
+ <ModalList
252
+ Dialog={dialog.Dialog}
253
+ rpc={{
254
+ tasksList: () => {
255
+ calls += 1
256
+ return deferred.promise
257
+ },
258
+ }}
259
+ onClose={() => {}}
260
+ />
261
+ ),
262
+ { width: 120, height: 20, useThread: false },
263
+ )
264
+
265
+ await renderOnce()
266
+
267
+ const frame = captureCharFrame()
268
+ expect(calls).toBe(1)
269
+ expect(frame).toContain('Loading delegations')
270
+ expect(frame).toContain('Esc close')
271
+ })
272
+
273
+ it('renders an empty state after tasksList returns no tasks', async () => {
274
+ const ModalList = await loadModalList()
275
+ const dialog = createDialogStub()
276
+
277
+ const { renderOnce, captureCharFrame, renderer } = await testRender(
278
+ () => (
279
+ <ModalList
280
+ Dialog={dialog.Dialog}
281
+ rpc={{ tasksList: async () => ({ tasks: [] }) }}
282
+ onClose={() => {}}
283
+ />
284
+ ),
285
+ { width: 120, height: 20, useThread: false },
286
+ )
287
+
288
+ await renderOnce()
289
+
290
+ const frame = await waitForFrame({
291
+ renderOnce,
292
+ idle: () => renderer.idle(),
293
+ captureCharFrame,
294
+ includes: '0 running · 0 recent',
295
+ })
296
+ expect(frame).toContain('0 running · 0 recent')
297
+ expect(frame).toContain('No Copilot delegations are running.')
298
+ expect(frame).toContain(
299
+ 'Start one with the copilot_delegate tool, then reopen /copilot-status.',
300
+ )
301
+ expect(frame).toContain('Esc close')
302
+ })
303
+
304
+ it('renders an error state with the rpc rejection message', async () => {
305
+ const ModalList = await loadModalList()
306
+ const dialog = createDialogStub()
307
+
308
+ const { renderOnce, captureCharFrame, renderer } = await testRender(
309
+ () => (
310
+ <ModalList
311
+ Dialog={dialog.Dialog}
312
+ rpc={{
313
+ tasksList: async () => {
314
+ throw new Error('rpc exploded on localhost')
315
+ },
316
+ }}
317
+ onClose={() => {}}
318
+ />
319
+ ),
320
+ { width: 120, height: 20, useThread: false },
321
+ )
322
+
323
+ await renderOnce()
324
+
325
+ const frame = await waitForFrame({
326
+ renderOnce,
327
+ idle: () => renderer.idle(),
328
+ captureCharFrame,
329
+ includes: 'Status unavailable.',
330
+ })
331
+ expect(frame).toContain('Status unavailable.')
332
+ expect(frame).toContain('rpc exploded on localhost')
333
+ expect(frame).toContain('Esc close')
334
+ })
335
+
336
+ it('renders running rows with focused styling, defaults, and key navigation', async () => {
337
+ const ModalList = await loadModalList()
338
+ const dialog = createDialogStub()
339
+ const clock = new TestClock(2_000)
340
+ const cancelled: TaskRow[] = []
341
+ const tasks = [
342
+ makeTask('cpl_alpha', {
343
+ startedAt: 0,
344
+ toolCallCount: 3,
345
+ }),
346
+ makeTask('cpl_beta', {
347
+ agent: '',
348
+ model: '',
349
+ startedAt: 1_000,
350
+ toolCallCount: 1,
351
+ }),
352
+ makeTask('cpl_gamma', {
353
+ agent: 'reviewer',
354
+ model: 'claude-sonnet-4.7',
355
+ startedAt: 2_000,
356
+ toolCallCount: 5,
357
+ }),
358
+ ]
359
+
360
+ const { renderOnce, captureCharFrame, captureSpans, mockInput, renderer } =
361
+ await testRender(
362
+ () => (
363
+ <ModalList
364
+ Dialog={dialog.Dialog}
365
+ rpc={{ tasksList: async () => ({ tasks }) }}
366
+ onClose={() => {}}
367
+ onCancelTask={(task) => cancelled.push(task)}
368
+ clock={clock.adapter()}
369
+ />
370
+ ),
371
+ { width: 120, height: 20, useThread: false },
372
+ )
373
+
374
+ await renderOnce()
375
+
376
+ const frame = await waitForFrame({
377
+ renderOnce,
378
+ idle: () => renderer.idle(),
379
+ captureCharFrame,
380
+ includes: '3 running · 0 recent',
381
+ })
382
+ expect(frame).toContain('3 running · 0 recent')
383
+ expect(frame).toContain('↑↓ navigate · c cancel · Esc close')
384
+ expect(frame).toContain('cpl_alpha')
385
+ expect(frame).toContain('cpl_beta')
386
+ expect(frame).toContain('cpl_gamma')
387
+ expect(frame).toContain('default')
388
+ expect(frame).toContain('reviewer')
389
+ expect(frame).toContain('claude-sonnet-4.7')
390
+ expect(frame).toContain('3 calls')
391
+ expect(frame).toContain('5 calls')
392
+ expect(frame).toContain('0s')
393
+
394
+ let spans = captureSpans()
395
+ const firstLine = findLineAttributesContaining(spans, 'cpl_alpha')
396
+ expect(firstLine).toBeDefined()
397
+ expect(lineHasInverse(firstLine)).toBe(true)
398
+
399
+ mockInput.pressKey('j')
400
+ await settle(renderOnce, () => renderer.idle())
401
+ expect(captureCharFrame()).toContain('cpl_beta')
402
+ spans = captureSpans()
403
+ const secondLine = findLineAttributesContaining(spans, 'cpl_beta')
404
+ expect(secondLine).toBeDefined()
405
+ expect(lineHasInverse(secondLine)).toBe(true)
406
+
407
+ mockInput.pressArrow('up')
408
+ await settle(renderOnce, () => renderer.idle())
409
+ spans = captureSpans()
410
+ const wrappedLine = findLineAttributesContaining(spans, 'cpl_alpha')
411
+ expect(wrappedLine).toBeDefined()
412
+ expect(lineHasInverse(wrappedLine)).toBe(true)
413
+
414
+ mockInput.pressKey('k')
415
+ await settle(renderOnce, () => renderer.idle())
416
+ spans = captureSpans()
417
+ const lastLine = findLineAttributesContaining(spans, 'cpl_gamma')
418
+ expect(lastLine).toBeDefined()
419
+ expect(lineHasInverse(lastLine)).toBe(true)
420
+
421
+ mockInput.pressKey('c')
422
+ await settle(renderOnce, () => renderer.idle())
423
+
424
+ expect(cancelled).toEqual([tasks[2]])
425
+ })
426
+
427
+ it('keeps cancelling tasks visible until runtime removes them', async () => {
428
+ const ModalList = await loadModalList()
429
+ const dialog = createDialogStub()
430
+
431
+ const { renderOnce, captureCharFrame, renderer } = await testRender(
432
+ () => (
433
+ <ModalList
434
+ Dialog={dialog.Dialog}
435
+ rpc={{
436
+ tasksList: async () => ({
437
+ tasks: [makeTask('cpl_cancelling', { status: 'cancelling' })],
438
+ }),
439
+ }}
440
+ onClose={() => {}}
441
+ />
442
+ ),
443
+ { width: 120, height: 20, useThread: false },
444
+ )
445
+
446
+ await renderOnce()
447
+
448
+ const frame = await waitForFrame({
449
+ renderOnce,
450
+ idle: () => renderer.idle(),
451
+ captureCharFrame,
452
+ includes: 'cpl_cancelling',
453
+ })
454
+
455
+ expect(frame).toContain('1 running · 0 recent')
456
+ expect(frame).toContain('cancelling')
457
+ expect(frame).toContain('cpl_cancelling')
458
+ })
459
+
460
+ it('updates elapsed time from an injected clock without waiting on wall time', async () => {
461
+ const ModalList = await loadModalList()
462
+ const dialog = createDialogStub()
463
+ const clock = new TestClock(0)
464
+
465
+ const { renderOnce, captureCharFrame, renderer } = await testRender(
466
+ () => (
467
+ <ModalList
468
+ Dialog={dialog.Dialog}
469
+ rpc={{
470
+ tasksList: async () => ({
471
+ tasks: [
472
+ makeTask('cpl_elapsed', {
473
+ startedAt: 0,
474
+ }),
475
+ ],
476
+ }),
477
+ }}
478
+ onClose={() => {}}
479
+ clock={clock.adapter()}
480
+ />
481
+ ),
482
+ { width: 120, height: 20, useThread: false },
483
+ )
484
+
485
+ await renderOnce()
486
+ const readyFrame = await waitForFrame({
487
+ renderOnce,
488
+ idle: () => renderer.idle(),
489
+ captureCharFrame,
490
+ includes: '0s',
491
+ })
492
+ expect(readyFrame).toContain('0s')
493
+
494
+ clock.advance(1_100)
495
+ await renderOnce()
496
+
497
+ expect(captureCharFrame()).toContain('1s')
498
+ })
499
+
500
+ it('clears its refresh interval on teardown', async () => {
501
+ const ModalList = await loadModalList()
502
+ const dialog = createDialogStub()
503
+ const clock = new TestClock(0)
504
+
505
+ const { renderer, renderOnce } = await testRender(
506
+ () => (
507
+ <ModalList
508
+ Dialog={dialog.Dialog}
509
+ rpc={{
510
+ tasksList: async () => ({
511
+ tasks: [makeTask('cpl_cleanup')],
512
+ }),
513
+ }}
514
+ onClose={() => {}}
515
+ clock={clock.adapter()}
516
+ />
517
+ ),
518
+ { width: 120, height: 20, useThread: false },
519
+ )
520
+
521
+ await renderOnce()
522
+ await waitForFrame({
523
+ renderOnce,
524
+ idle: () => renderer.idle(),
525
+ captureCharFrame: () => String(clock.activeIntervalCount),
526
+ includes: '1',
527
+ })
528
+
529
+ expect(clock.activeIntervalCount).toBe(1)
530
+
531
+ renderer.destroy()
532
+
533
+ expect(clock.activeIntervalCount).toBe(0)
534
+ expect(clock.cleared).toHaveLength(1)
535
+ })
536
+ })