File size: 18,997 Bytes
bdbd3b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
import type { Args, ResultOf } from 'claude-code'
import { describe, expect, mock, test, tier } from 'claude-code/testing'

import Limits from '../hooks/limits'
import Names from '../hooks/names'
import Fixtures from './fixtures'

tier('builtin')

describe('register', () => {
  test('the start asks nothing of git and registers /diff', async ($, on) => {
    const world = Fixtures.inRepository(on)

    await $.session.start(Fixtures.SESSION)

    expect(world.runs, "no git, as the built-in's start runs none").toEqual([])

    expect(await $.command.run(Fixtures.DIFF), '/diff was registered').toEqual({
      text: 'Diff panel shown',
    })
  })

  test("git runs at the built-in's moments and no others", async ($, on) => {
    const world = Fixtures.inRepository(on)

    let read = 0

    function spawnedSince(): string[] {
      const words = world.runs
        .slice(read)
        .map(run => Fixtures.gitWordOf(run.argv))
        .filter(word => word !== Fixtures.POLL_WORD)

      read = world.runs.length

      return words
    }

    const edit = () =>
      $.tool.call({
        tool: 'Edit',
        file_path: '/work/app.ts',
        old_string: '1',
        new_string: '2',
      })

    on('tool.call', () => ({ result: 'done' }))
    on('turn.complete', ($, e) => ({ text: e.answer }))
    on('command.run', { command: 'clear' }, () => ({}))

    await $.session.start(Fixtures.SESSION)
    await $.ui.render(Fixtures.hintAt(Limits.AUTO_OPEN_MIN_COLUMNS - 1))
    await world.clock.advance(Fixtures.SETTLE_MS)

    expect(spawnedSince(), 'the start and the footer: nothing').toEqual([])

    await edit()
    await world.clock.advance(Fixtures.SETTLE_MS)

    expect(
      spawnedSince(),
      'an edit with no room for a pane: nothing, the width is read first',
    ).toEqual([])

    await $.command.run(Fixtures.DIFF)
    await world.clock.advance(Fixtures.SETTLE_MS)

    expect(spawnedSince(), '/diff: found, then one fetch for the pane').toEqual(
      [
        'rev-parse --show-toplevel',
        'status',
        'diff --shortstat',
        'diff --numstat',
        'ls-files',
        'diff -- app.ts',
      ],
    )

    await edit()
    await world.clock.advance(Fixtures.SETTLE_MS)

    expect(spawnedSince(), 'an edit, the pane open: one fetch').toEqual([
      'diff --shortstat',
      'diff --numstat',
      'ls-files',
      'diff -- app.ts',
    ])

    await $.tool.call({ tool: 'Bash', command: 'make' })
    await world.clock.advance(Fixtures.SETTLE_MS)

    expect(spawnedSince(), 'a shell command, the pane open: one fetch').toEqual(
      ['diff --shortstat', 'diff --numstat', 'ls-files', 'diff -- app.ts'],
    )

    await $.turn.complete({
      answer: 'done',
      durationMs: 1,
      isAborted: false,
      turnId: 't1',
      reason: 'answer',
    })

    await world.clock.advance(Fixtures.SETTLE_MS)

    expect(spawnedSince(), "a turn's end: nothing").toEqual([])

    await $.command.run(Fixtures.DIFF)
    await edit()
    await $.tool.call({ tool: 'Bash', command: 'make' })
    await world.clock.advance(Fixtures.SETTLE_MS)

    expect(spawnedSince(), 'the pane closed by hand: nothing').toEqual([])

    await $.command.run(Fixtures.CLEAR)
    await $.command.run(Fixtures.DIFF)
    await world.clock.advance(Fixtures.SETTLE_MS)

    expect(spawnedSince()[0], '/clear forgot the repository').toBe(
      'rev-parse --show-toplevel',
    )
  })

  test('/diff whose probe never answers probes once more', async ($, on) => {
    const probes: (readonly string[])[] = []
    const clock = Fixtures.startsSession(on)

    on('process.run', async ($, e) => {
      probes.push(e.argv)

      if (probes.length === 1) {
        await clock.sleep(Limits.GIT_TIMEOUT_MS)

        return { deny: Fixtures.GIT_HUNG }
      }

      return { value: Fixtures.NOT_A_REPOSITORY }
    })

    await $.session.start(Fixtures.SESSION)

    const ran = $.command.run(Fixtures.DIFF)

    await clock.settle()

    expect(probes, "this /diff's probe, unanswered yet").toHaveLength(1)

    await clock.advance(Limits.GIT_TIMEOUT_MS)

    expect(await ran).toEqual({
      text: expect.stringContaining("isn't in a git repository"),
    })

    expect(probes, 'then one more, which answered').toHaveLength(2)
  })

  test('two /diff typed together probe the repository once', async ($, on) => {
    const world = Fixtures.inRepository(on)

    await $.session.start(Fixtures.SESSION)

    const [first, second] = await Promise.all([
      $.command.run(Fixtures.DIFF),
      $.command.run(Fixtures.DIFF),
    ])

    const probes = world.runs.filter(run =>
      run.argv.includes('--show-toplevel'),
    )

    expect(first.text).toMatch(/^Diff panel (shown|hidden)$/)
    expect(second.text).toMatch(/^Diff panel (shown|hidden)$/)
    expect(probes, 'the second joined the probe in flight').toHaveLength(1)
  })

  test('outside a repository /diff says so, opens nothing', async ($, on) => {
    const opened: string[] = []

    Fixtures.startsSession(on)
    on('process.run', () => ({ value: Fixtures.NOT_A_REPOSITORY }))

    on('ui.open', ($, e, next) => {
      opened.push(e.id)

      return next(e)
    })

    await $.session.start(Fixtures.SESSION)

    const { text } = await $.command.run(Fixtures.DIFF)

    expect(text).toContain("isn't in a git repository")
    expect(opened).toEqual([])
  })

  test('a git that never answers is not "no repository"', async ($, on) => {
    Fixtures.startsSession(on)
    on('process.run', () => ({ deny: Fixtures.GIT_HUNG }))

    await $.session.start(Fixtures.SESSION)

    const { text } = await $.command.run(Fixtures.DIFF)

    expect(text).toContain("git didn't answer")
  })

  test('when the built-in holds /diff, the mod stands down', async ($, on) => {
    const logged: string[] = []
    const runs: Args<'process.run'>[] = []

    mock.clock(on)
    on('session.start', ($, e) => ({ cwd: e.cwd }))
    on('command.register', () => ({ deny: Fixtures.BUILTIN_HOLDS }))
    on('command.run', () => ({ text: 'the built-in /diff ran' }))
    on('tool.call', () => ({ result: 'edited' }))

    on('process.run', ($, e) => {
      runs.push(e)

      return { value: Fixtures.gitIn(e.argv) }
    })

    on('ui.log', ($, e) => {
      logged.push(e.text)

      return { value: undefined }
    })

    await $.session.start(Fixtures.SESSION)

    expect(await $.command.run(Fixtures.DIFF)).toEqual({
      text: 'the built-in /diff ran',
    })

    await $.tool.call({
      tool: 'Edit',
      file_path: '/work/app.ts',
      old_string: '1',
      new_string: '2',
    })

    expect(logged).toEqual([])
    expect(runs, 'idle: no git for /diff or the edit').toEqual([])
  })

  test('a refusal the built-in did not cause is said aloud', async ($, on) => {
    const logged: string[] = []

    mock.clock(on)
    on('session.start', ($, e) => ({ cwd: e.cwd }))

    on('command.register', () => ({
      deny: '32 commands are registered already',
    }))

    on('ui.log', ($, e) => {
      logged.push(e.text)

      return { value: undefined }
    })

    await $.session.start(Fixtures.SESSION)

    expect(logged).toEqual([
      'could not register /diff: diff: $.command.register: 32 commands are ' +
        'registered already; the diff panel is unavailable this session',
    ])
  })

  test('/diff opens the pane, unfocused, and says so', async ($, on) => {
    const world = Fixtures.inRepository(on)

    await $.session.start(Fixtures.SESSION)

    expect(world.runs, 'the start ran nothing').toEqual([])

    expect(await $.command.run(Fixtures.DIFF)).toEqual({
      text: 'Diff panel shown',
    })

    expect(world.runs[0]?.argv, '/diff found the repository first').toContain(
      '--show-toplevel',
    )

    expect(world.opened).toEqual([
      { id: 'diff', title: 'Diff', holdToasts: true },
    ])

    await world.clock.advance(Fixtures.SETTLE_MS)

    const drawn = Fixtures.textOf(await $.ui.render(Fixtures.PANE))

    expect(drawn).toContain('1 file changed')
    expect(drawn).toContain('app.ts')
  })

  test('off fullscreen, /diff opens the dialog, focused', async ($, on) => {
    const world = Fixtures.inRepository(on)

    await $.session.start(Fixtures.SESSION)

    expect(await $.command.run(Fixtures.DIALOG_DIFF)).toEqual({})

    expect(world.opened[0]).toEqual({
      id: 'diff',
      title: 'Diff',
      holdToasts: true,
      closeOnEscape: true,
      rows: expect.any(Number),
      focus: true,
    })

    expect(await $.command.run(Fixtures.DIALOG_DIFF)).toEqual({
      text: 'Diff dialog dismissed',
    })
  })

  test('/diff again closes the pane and says so', async ($, on) => {
    const world = Fixtures.inRepository(on)

    await $.session.start(Fixtures.SESSION)
    await $.command.run(Fixtures.DIFF)

    expect(await $.command.run(Fixtures.DIFF)).toEqual({
      text: 'Diff panel hidden',
    })

    expect(world.closed.map(pane => pane.id)).toEqual(['diff'])
  })

  test('a wide terminal opens the pane at the first edit', async ($, on) => {
    const world = Fixtures.inRepository(on)

    on('tool.call', () => ({ result: 'edited' }))

    await $.session.start(Fixtures.SESSION)
    await $.ui.render(Fixtures.HINT)

    await $.tool.call({
      tool: 'Edit',
      file_path: '/work/app.ts',
      old_string: '1',
      new_string: '2',
    })

    await world.clock.advance(Fixtures.SETTLE_MS)

    expect(world.opened.map(pane => pane.id)).toEqual(['diff'])
  })

  test('on the main screen the first edit opens nothing, and /diff still opens the dialog', async ($, on) => {
    const world = Fixtures.inRepository(on)

    on('tool.call', () => ({ result: 'edited' }))

    await $.session.start(Fixtures.SESSION)
    await $.ui.render(Fixtures.MAIN_SCREEN_HINT)

    await $.tool.call({
      tool: 'Edit',
      file_path: '/work/app.ts',
      old_string: '1',
      new_string: '2',
    })

    await world.clock.advance(Fixtures.SETTLE_MS)

    expect(world.opened, 'a pane there would be an unasked dialog').toEqual([])
    expect(world.runs, 'so nothing was asked of the repository').toEqual([])

    expect(await $.command.run(Fixtures.DIALOG_DIFF)).toEqual({})

    expect(
      world.opened[0],
      "the person's /diff opens the dialog, not dismisses one",
    ).toMatchObject({ id: 'diff', focus: true })
  })

  test('a surface that does not say whether it docks a pane opens nothing at the first edit', async ($, on) => {
    const world = Fixtures.inRepository(on)

    on('tool.call', () => ({ result: 'edited' }))

    await $.session.start(Fixtures.SESSION)
    await $.ui.render(Fixtures.UNSAID_HINT)

    await $.tool.call({
      tool: 'Edit',
      file_path: '/work/app.ts',
      old_string: '1',
      new_string: '2',
    })

    await world.clock.advance(Fixtures.SETTLE_MS)

    expect(world.opened, 'unknown is not a dock').toEqual([])

    await $.ui.render(Fixtures.HINT)

    await $.tool.call({
      tool: 'Edit',
      file_path: '/work/app.ts',
      old_string: '2',
      new_string: '3',
    })

    await world.clock.advance(Fixtures.SETTLE_MS)

    expect(
      world.opened.map(pane => pane.id),
      'once a drawing says the layout docks, the next edit opens it',
    ).toEqual(['diff'])
  })

  test('an edit that failed or was refused opens nothing', async ($, on) => {
    const world = Fixtures.inRepository(on)

    const answers: ResultOf['tool.call'][] = [
      { isError: true, result: 'no such text' },
      { deny: 'not allowed' },
    ]

    const edit = () =>
      $.tool.call({
        tool: 'Edit',
        file_path: '/work/app.ts',
        old_string: '1',
        new_string: '2',
      })

    on('tool.call', () => answers.shift() ?? { result: 'edited' })

    await $.session.start(Fixtures.SESSION)
    await $.ui.render(Fixtures.HINT)
    await edit()
    await edit()
    await world.clock.advance(Fixtures.SETTLE_MS)

    expect(world.opened, 'neither edit landed').toEqual([])
    expect(world.runs, 'so nothing asked after the repository').toEqual([])

    await edit()
    await world.clock.advance(Fixtures.SETTLE_MS)

    expect(
      world.opened.map(pane => pane.id),
      'the third did',
    ).toEqual(['diff'])
  })

  test('/clear closes the pane it finds open', async ($, on) => {
    const world = Fixtures.inRepository(on)

    on('command.run', { command: 'clear' }, () => ({}))

    await $.session.start(Fixtures.SESSION)
    await $.command.run(Fixtures.DIFF)
    await $.command.run(Fixtures.CLEAR)

    expect(world.closed.map(pane => pane.id)).toEqual(['diff'])
  })

  test('ask attaches the hunks on screen and calls no tool', async ($, on) => {
    const world = Fixtures.inRepository(on, Fixtures.oneSecret())
    const called: Args<'tool.call'>[] = []

    on('tool.call', ($, e) => {
      called.push(e)

      return { result: 'called' }
    })

    on('prompt.submit', ($, e) => ({ text: e.text, context: e.context }))

    await $.session.start(Fixtures.WORKTREE_SESSION)
    await $.command.run(Fixtures.DIFF)
    await world.clock.advance(Fixtures.SETTLE_MS)
    await $.ui.render(Fixtures.PANE)
    await $.ui.press({ plugin: 'diff', key: 'ask:.env' })
    await world.clock.settle()

    const armed = Fixtures.jsonOf(await $.ui.render(Fixtures.PANE))

    const submitted = await $.prompt.submit(Fixtures.typedPromptOf('why?'))

    expect(armed).toContain('asked ✓')
    expect(called).toEqual([])

    expect(world.statuses.at(-2)).toBe(
      '.env rides your next prompt (press asked ✓ to drop it)',
    )

    expect(submitted).toMatchObject({
      context: [
        'The user attached the diff of .env from the diff pane to this ' +
          'prompt:\n@@ -1 +1 @@\n-KEY=old\n+KEY=new',
      ],
    })
  })

  test('an ask with no room in the context is dropped', async ($, on) => {
    const world = Fixtures.inRepository(on, Fixtures.oneSecret())
    const full = 'x'.repeat(Limits.PROMPT_CONTEXT_MAX_CHARS)
    const reached: (readonly string[] | undefined)[] = []

    on('prompt.submit', ($, e) => {
      reached.push(e.context)

      return { text: e.text }
    })

    await $.session.start(Fixtures.WORKTREE_SESSION)
    await $.command.run(Fixtures.DIFF)
    await world.clock.advance(Fixtures.SETTLE_MS)
    await $.ui.render(Fixtures.PANE)
    await $.ui.press({ plugin: 'diff', key: 'ask:.env' })
    await world.clock.settle()

    await $.prompt.submit(Fixtures.typedPromptOf('why?', [full]))
    await $.prompt.submit(Fixtures.typedPromptOf('and now?'))

    expect(reached).toEqual([[full], undefined])

    expect(world.statuses.at(-1)).toBe(
      ".env's diff did not fit in the prompt and was dropped",
    )
  })

  test('a worktree opens on the base it kept', async ($, on) => {
    const world = Fixtures.inRepository(on, Fixtures.oneSecret(), {
      'base:/main/wt': 'uncommitted',
    })

    await $.session.start(Fixtures.WORKTREE_SESSION)
    await $.command.run(Fixtures.DIFF)
    await world.clock.advance(Fixtures.SETTLE_MS)

    expect(
      Fixtures.stringsOf(await $.ui.render(Fixtures.PANE)),
      "the built-in's base line under the header",
    ).toContain('uncommitted (vs HEAD)')
  })

  test('no repository is an answer kept until /clear', async ($, on) => {
    const { 'rev-parse --path-format=absolute': worktree = '', ...notYet } =
      Fixtures.oneSecret()

    const script: Record<string, string> = notYet
    const world = Fixtures.inRepository(on, script)

    const probesOf = () =>
      world.runs.filter(run => run.argv.includes('--show-toplevel')).length

    on('command.run', { command: 'clear' }, () => ({}))

    await $.session.start(Fixtures.WORKTREE_SESSION)

    const before = await $.command.run(Fixtures.DIFF)

    script['rev-parse --path-format=absolute'] = worktree

    const again = await $.command.run(Fixtures.DIFF)
    const probesKept = probesOf()

    await $.command.run(Fixtures.CLEAR)

    const after = await $.command.run(Fixtures.DIFF)

    await world.clock.advance(Fixtures.SETTLE_MS)
    await $.ui.render(Fixtures.PANE)

    script['rev-parse --path-format=absolute'] = Fixtures.ELSEWHERE_LINES

    const third = $.command.run(Fixtures.DIFF)

    await world.clock.advance(Fixtures.SETTLE_MS)
    await third

    expect(before.text).toContain("isn't in a git repository")

    expect(again.text, "kept, as the built-in's answer is").toContain(
      "isn't in a git repository",
    )

    expect(probesKept, 'one probe answered both').toBe(1)
    expect(after.text, '/clear forgot it').toBe('Diff panel shown')
    expect(world.opened[0]?.id).toBe('diff')
    expect(probesOf(), 'then one more, pinned from there on').toBe(2)

    expect(
      world.runs.some(run => run.argv.includes('--git-dir=/else/.git')),
    ).toBe(false)

    expect(
      world.runs
        .filter(run => run.argv.includes('--numstat'))
        .every(run => run.argv[1] === Fixtures.PINNED_LEAD[1]),
    ).toBe(true)
  })

  test('a repository that appears later opens on first edit', async ($, on) => {
    const { 'rev-parse --path-format=absolute': worktree = '', ...notYet } =
      Fixtures.oneSecret()

    const script: Record<string, string> = notYet
    const world = Fixtures.inRepository(on, script)

    on('tool.call', () => ({ result: 'edited' }))

    await $.session.start(Fixtures.WORKTREE_SESSION)
    await $.ui.render(Fixtures.HINT)

    script['rev-parse --path-format=absolute'] = worktree

    await $.tool.call({
      tool: 'Edit',
      file_path: '/main/wt/.env',
      old_string: 'old',
      new_string: 'new',
    })

    await world.clock.advance(Fixtures.SETTLE_MS)

    expect(world.opened.map(pane => pane.id)).toEqual(['diff'])
  })

  test('a git that cannot start is an answer, not a timeout', async ($, on) => {
    const runs: Args<'process.run'>[] = []

    Fixtures.startsSession(on)

    on('process.run', ($, e) => {
      runs.push(e)

      return e.argv.includes('--show-toplevel')
        ? { deny: Fixtures.FAILED_START }
        : { value: Fixtures.gitIn(e.argv, Fixtures.oneSecret()) }
    })

    await $.session.start(Fixtures.WORKTREE_SESSION)

    const { text } = await $.command.run(Fixtures.DIFF)
    const probes = runs.filter(run => run.argv.includes('--show-toplevel'))

    expect(text).toContain("isn't in a git repository")
    expect(probes, "this /diff's alone; no retry").toHaveLength(1)
  })

  test('/diff on a narrow terminal: the resize line', async ($, on) => {
    const world = Fixtures.inRepository(on, Fixtures.oneSecret())

    await $.session.start(Fixtures.WORKTREE_SESSION)

    const narrow = await $.command.run(
      Fixtures.diffAt(Limits.OPEN_MIN_COLUMNS - 1),
    )

    const openedNarrow = [...world.opened]

    await $.command.run(Fixtures.diffAt(Limits.OPEN_MIN_COLUMNS))

    expect(narrow.text).toContain(
      'Resize your terminal to at least 110 columns to show the diff panel',
    )

    expect(openedNarrow).toEqual([])
    expect(world.opened.map(pane => pane.id)).toEqual([Names.PANE_ID])
  })
})