|
| 1 | +import type { |
| 2 | + Args, |
| 3 | + CommandRunInput, |
| 4 | + On, |
| 5 | + ProcessRunResult, |
| 6 | + RenderElement, |
| 7 | + RenderInput, |
| 8 | + SessionStartInput, |
| 9 | +} from 'claude-code' |
| 10 | +import { |
| 11 | + clock, |
| 12 | + expect, |
| 13 | + memoryStore, |
| 14 | + seat, |
| 15 | + test, |
| 16 | + textOf, |
| 17 | +} from 'claude-code/testing' |
| 18 | + |
| 19 | +seat('builtin') |
| 20 | + |
| 21 | +const SESSION: SessionStartInput = { |
| 22 | + surface: 'terminal', |
| 23 | + isInteractive: true, |
| 24 | + cwd: '/work', |
| 25 | +} |
| 26 | +const DIFF: CommandRunInput = { |
| 27 | + command: 'diff', |
| 28 | + args: '', |
| 29 | + origin: { kind: 'composer' }, |
| 30 | +} |
| 31 | +const CLEAR: CommandRunInput = { |
| 32 | + command: 'clear', |
| 33 | + args: '', |
| 34 | + origin: { kind: 'composer' }, |
| 35 | +} |
| 36 | +const PANE: RenderInput<'Pane'> = { |
| 37 | + component: 'Pane', |
| 38 | + surface: 'terminal', |
| 39 | + requestId: 'diff', |
| 40 | + viewport: { columns: 160, rows: 40 }, |
| 41 | + props: { |
| 42 | + title: 'Diff', |
| 43 | + isFocused: false, |
| 44 | + bodyColumns: 80, |
| 45 | + placement: 'dock', |
| 46 | + scroll: { offset: 0, bodyRows: 30 }, |
| 47 | + }, |
| 48 | +} |
| 49 | +const HINT: RenderInput<'PromptHint'> = { |
| 50 | + component: 'PromptHint', |
| 51 | + surface: 'terminal', |
| 52 | + requestId: 'hint', |
| 53 | + viewport: { columns: 160, rows: 40 }, |
| 54 | + props: { isDraft: false, isWorking: false, hint: '' }, |
| 55 | +} |
| 56 | +const HINT_DRAWN: RenderElement = { |
| 57 | + type: 'Text', |
| 58 | + children: ['? for shortcuts'], |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * A repository at /work with one changed file: an invocation of git whose |
| 63 | + * command line holds a key answers that output, and any other fails as git |
| 64 | + * does outside a repository. |
| 65 | + */ |
| 66 | +const REPOSITORY: Readonly<Record<string, string>> = { |
| 67 | + 'rev-parse --path-format=absolute': '/work\n/work/.git\n/work/.git\n', |
| 68 | + 'HEAD --shortstat': ' 1 file changed, 1 insertion(+)', |
| 69 | + 'HEAD --numstat': '1\t0\tapp.ts\0', |
| 70 | + 'ls-files': '', |
| 71 | + '-- app.ts': '@@ -1 +1 @@\n-const a = 1\n+const a = 2\n', |
| 72 | +} |
| 73 | + |
| 74 | +function gitIn(argv: readonly string[]): ProcessRunResult { |
| 75 | + const line = argv.join(' ') |
| 76 | + const found = Object.entries(REPOSITORY).find(([key]) => line.includes(key)) |
| 77 | + |
| 78 | + return found |
| 79 | + ? { exitCode: 0, stdout: found[1], stderr: '' } |
| 80 | + : { exitCode: 128, stdout: '', stderr: 'fatal: not a git repository' } |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * A session in that repository: /diff registers, git answers, the store |
| 85 | + * starts empty, the engine draws the prompt's hint, and what the plugin |
| 86 | + * asks of the world (each git run, each pane opened or closed) is kept. |
| 87 | + */ |
| 88 | +function inRepository(on: On) { |
| 89 | + const runs: Args<'process.run'>[] = [] |
| 90 | + const opened: Args<'ui.open'>[] = [] |
| 91 | + const closed: Args<'ui.close'>[] = [] |
| 92 | + on('session.start', ($, e) => ({ cwd: e.cwd })) |
| 93 | + on('command.register', ($, e) => ({ value: { command: e.name } })) |
| 94 | + on('process.run', ($, e) => { |
| 95 | + runs.push(e) |
| 96 | + return { value: gitIn(e.argv) } |
| 97 | + }) |
| 98 | + on('ui.open', ($, e) => { |
| 99 | + opened.push(e) |
| 100 | + return { value: undefined } |
| 101 | + }) |
| 102 | + on('ui.close', ($, e) => { |
| 103 | + closed.push(e) |
| 104 | + return { value: undefined } |
| 105 | + }) |
| 106 | + on('ui.invalidate', () => ({ value: undefined })) |
| 107 | + on('ui.render', { component: 'PromptHint' }, () => HINT_DRAWN) |
| 108 | + on('session.messages', () => ({ value: [] })) |
| 109 | + memoryStore(on) |
| 110 | + |
| 111 | + return { runs, opened, closed } |
| 112 | +} |
| 113 | + |
| 114 | +test('/diff opens the pane over the session’s changes', async ($, on) => { |
| 115 | + const world = inRepository(on) |
| 116 | + |
| 117 | + await $.session.start(SESSION) |
| 118 | + |
| 119 | + expect(await $.command.run(DIFF)).toEqual({}) |
| 120 | + expect(world.opened.map(pane => pane.id)).toEqual(['diff']) |
| 121 | + |
| 122 | + await clock.advance(1000) |
| 123 | + const drawn = textOf(await $.ui.render(PANE)) |
| 124 | + |
| 125 | + expect(drawn).toContain('1 file changed') |
| 126 | + expect(drawn).toContain('app.ts') |
| 127 | +}) |
| 128 | + |
| 129 | +test('every git child is pinned to the repository and reads the C locale', async ($, on) => { |
| 130 | + const world = inRepository(on) |
| 131 | + |
| 132 | + await $.session.start(SESSION) |
| 133 | + await $.command.run(DIFF) |
| 134 | + await clock.advance(1000) |
| 135 | + const [discovery, ...pinned] = world.runs |
| 136 | + |
| 137 | + expect(discovery?.argv).toContain('--show-toplevel') |
| 138 | + expect(discovery?.init?.cwd).toBeUndefined() |
| 139 | + expect(pinned.length).toBeGreaterThan(0) |
| 140 | + |
| 141 | + for (const run of pinned) { |
| 142 | + expect(run.argv.slice(0, 3)).toEqual([ |
| 143 | + 'git', |
| 144 | + '--git-dir=/work/.git', |
| 145 | + '--work-tree=/work', |
| 146 | + ]) |
| 147 | + expect(run.init).toMatchObject({ cwd: '/work', env: { LC_ALL: 'C' } }) |
| 148 | + } |
| 149 | +}) |
| 150 | + |
| 151 | +test('Claude’s first edit opens the pane on a wide terminal', async ($, on) => { |
| 152 | + const world = inRepository(on) |
| 153 | + on('tool.call', () => ({ result: 'edited' })) |
| 154 | + |
| 155 | + await $.session.start(SESSION) |
| 156 | + await $.ui.render(HINT) |
| 157 | + await $.tool.call({ |
| 158 | + tool: 'Edit', |
| 159 | + file_path: '/work/app.ts', |
| 160 | + old_string: '1', |
| 161 | + new_string: '2', |
| 162 | + }) |
| 163 | + await clock.advance(1000) |
| 164 | + |
| 165 | + expect(world.opened.map(pane => pane.id)).toEqual(['diff']) |
| 166 | +}) |
| 167 | + |
| 168 | +test('/clear closes the pane it finds open', async ($, on) => { |
| 169 | + const world = inRepository(on) |
| 170 | + on('command.run', { command: 'clear' }, () => ({})) |
| 171 | + |
| 172 | + await $.session.start(SESSION) |
| 173 | + await $.command.run(DIFF) |
| 174 | + await $.command.run(CLEAR) |
| 175 | + |
| 176 | + expect(world.closed.map(pane => pane.id)).toEqual(['diff']) |
| 177 | +}) |
0 commit comments