spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

claude-code / mods / diff / tests / command.test.ts
88 lines · 2.5 KBRaw
1import type {
2 CommandRunInput,
3 ProcessRunResult,
4 SessionStartInput,
5} from 'claude-code'
6import { expect, seat, test } from 'claude-code/testing'
7
8seat('builtin')
9
10const SESSION: SessionStartInput = {
11 surface: 'terminal',
12 isInteractive: true,
13 cwd: '/work',
14}
15const DIFF: CommandRunInput = {
16 command: 'diff',
17 args: '',
18 origin: { kind: 'composer' },
19}
20const NOT_A_REPOSITORY: ProcessRunResult = {
21 exitCode: 128,
22 stdout: '',
23 stderr: 'fatal: not a git repository',
24}
25const BUILTIN_HOLDS = '"/diff" refused: it is the built-in /diff'
26
27test('outside a git repository /diff says so and opens nothing', async ($, on) => {
28 const opened: string[] = []
29 on('session.start', ($, e) => ({ cwd: e.cwd }))
30 on('command.register', ($, e) => ({ value: { command: e.name } }))
31 on('process.run', () => ({ value: NOT_A_REPOSITORY }))
32 on('ui.open', ($, e, next) => {
33 opened.push(e.id)
34 return next(e)
35 })
36
37 await $.session.start(SESSION)
38 const { text } = await $.command.run(DIFF)
39
40 expect(text).toContain('isn’t in a git repository')
41 expect(opened).toEqual([])
42})
43
44test('a git that never answers is not “no repository”', async ($, on) => {
45 on('session.start', ($, e) => ({ cwd: e.cwd }))
46 on('command.register', ($, e) => ({ value: { command: e.name } }))
47 on('process.run', () => ({
48 deny: 'git aborted: still running after 5000ms',
49 }))
50
51 await $.session.start(SESSION)
52 const { text } = await $.command.run(DIFF)
53
54 expect(text).toContain('git didn’t answer')
55})
56
57test('with the built-in holding /diff, the plugin stands down', async ($, on) => {
58 const logged: string[] = []
59 on('session.start', ($, e) => ({ cwd: e.cwd }))
60 on('command.register', () => ({ deny: BUILTIN_HOLDS }))
61 on('command.run', () => ({ text: 'the built-in /diff ran' }))
62 on('ui.log', ($, e) => {
63 logged.push(e.text)
64 return { value: undefined }
65 })
66
67 await $.session.start(SESSION)
68
69 expect(await $.command.run(DIFF)).toEqual({ text: 'the built-in /diff ran' })
70 expect(logged).toEqual([])
71})
72
73test('a refusal the built-in did not cause is said aloud', async ($, on) => {
74 const logged: string[] = []
75 on('session.start', ($, e) => ({ cwd: e.cwd }))
76 on('command.register', () => ({ deny: '32 commands are registered already' }))
77 on('ui.log', ($, e) => {
78 logged.push(e.text)
79 return { value: undefined }
80 })
81
82 await $.session.start(SESSION)
83
84 expect(logged).toEqual([
85 'could not register /diff: diff: $.command.register: 32 commands are ' +
86 'registered already; the diff panel is unavailable this session',
87 ])
88})