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
77 lines · 2.2 KBRaw
1import { describe, expect, test, tier } from 'claude-code/testing'
2
3import Fixtures from './fixtures'
4
5const BUILTIN_HOLDS = '"/diff" refused: it is the built-in /diff'
6
7tier('builtin')
8
9describe('command', () => {
10 test('outside a repository /diff says so, opens nothing', async ($, on) => {
11 const opened: string[] = []
12 Fixtures.startsSession(on)
13 on('process.run', () => ({ value: Fixtures.NOT_A_REPOSITORY }))
14 on('ui.open', ($, e, next) => {
15 opened.push(e.id)
16
17 return next(e)
18 })
19
20 await $.session.start(Fixtures.SESSION)
21 const { text } = await $.command.run(Fixtures.DIFF)
22
23 expect(text).toContain("isn't in a git repository")
24 expect(opened).toEqual([])
25 })
26
27 test('a git that never answers is not "no repository"', async ($, on) => {
28 Fixtures.startsSession(on)
29 on('process.run', () => ({
30 deny: 'git aborted: still running after 5000ms',
31 }))
32
33 await $.session.start(Fixtures.SESSION)
34 const { text } = await $.command.run(Fixtures.DIFF)
35
36 expect(text).toContain("git didn't answer")
37 })
38
39 test('when the built-in holds /diff, the mod stands down', async ($, on) => {
40 const logged: string[] = []
41 on('session.start', ($, e) => ({ cwd: e.cwd }))
42 on('command.register', () => ({ deny: BUILTIN_HOLDS }))
43 on('command.run', () => ({ text: 'the built-in /diff ran' }))
44 on('ui.log', ($, e) => {
45 logged.push(e.text)
46
47 return { value: undefined }
48 })
49
50 await $.session.start(Fixtures.SESSION)
51
52 expect(await $.command.run(Fixtures.DIFF)).toEqual({
53 text: 'the built-in /diff ran',
54 })
55 expect(logged).toEqual([])
56 })
57
58 test('a refusal the built-in did not cause is said aloud', async ($, on) => {
59 const logged: string[] = []
60 on('session.start', ($, e) => ({ cwd: e.cwd }))
61 on('command.register', () => ({
62 deny: '32 commands are registered already',
63 }))
64 on('ui.log', ($, e) => {
65 logged.push(e.text)
66
67 return { value: undefined }
68 })
69
70 await $.session.start(Fixtures.SESSION)
71
72 expect(logged).toEqual([
73 'could not register /diff: diff: $.command.register: 32 commands are ' +
74 'registered already; the diff panel is unavailable this session',
75 ])
76 })
77})