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 / git.test.ts
135 lines · 4.3 KBRaw
1import { describe, expect, mock, test, tier } from 'claude-code/testing'
2
3import Git from '../hooks/git'
4import Fixtures from './fixtures'
5
6tier('builtin')
7
8describe('git', () => {
9 test('each git child names the pin, in the C locale', async ($, on) => {
10 const world = Fixtures.inRepository(on, Fixtures.oneSecret())
11
12 await $.session.start(Fixtures.WORKTREE_SESSION)
13 await $.command.run(Fixtures.DIFF)
14 await world.clock.advance(Fixtures.SETTLE_MS)
15 await $.ui.render(Fixtures.PANE)
16 await $.ui.press({ plugin: 'diff', key: 'ask:.env' })
17 await world.clock.settle()
18
19 const [discovery, ...pinned] = world.runs
20 const diffs = pinned.filter(run => run.argv.includes('diff'))
21
22 expect(discovery?.argv).toContain('--show-toplevel')
23
24 expect(
25 discovery?.init?.cwd,
26 "found where the session started, wherever Claude's shell has gone",
27 ).toBe('/main/wt')
28
29 expect(diffs.length).toBeGreaterThanOrEqual(3)
30
31 for (const run of world.runs) {
32 expect(run.init?.env).toEqual(Git.GIT_CHILD_ENV)
33 }
34
35 for (const run of pinned) {
36 expect(run.argv.slice(0, Fixtures.PINNED_LEAD.length)).toEqual(
37 Fixtures.PINNED_LEAD,
38 )
39
40 expect(run.init).toMatchObject({ cwd: '/main/wt' })
41 }
42
43 expect(world.runs.some(run => run.argv.includes('config'))).toBe(false)
44 })
45
46 test('the working tree is walked once, by the first fetch', async ($, on) => {
47 const world = Fixtures.inRepository(on)
48
49 const walks = () =>
50 world.runs.filter(run => Fixtures.gitWordOf(run.argv) === 'status')
51
52 const fetches = () =>
53 world.runs.filter(
54 run => Fixtures.gitWordOf(run.argv) === 'diff --numstat',
55 )
56
57 on('tool.call', () => ({ result: 'ran' }))
58
59 await $.session.start(Fixtures.SESSION)
60 await $.command.run(Fixtures.DIFF)
61 await world.clock.advance(Fixtures.SETTLE_MS)
62
63 expect(walks(), 'the first fetch walked').toHaveLength(1)
64 expect(fetches()).toHaveLength(1)
65
66 await $.tool.call({ tool: 'Bash', command: 'make' })
67 await world.clock.advance(Fixtures.SETTLE_MS)
68
69 expect(fetches(), 'the command fetched again').toHaveLength(2)
70 expect(walks(), 'and read the walk it had').toHaveLength(1)
71 expect(walks()[0]?.init?.cwd, 'pinned like every child').toBe('/work')
72 })
73
74 test('a file moved in since the start is session work', async ($, on) => {
75 const clock = Fixtures.startsSession(on, Fixtures.SETTLE_MS)
76
77 on('process.run', ($, e) => ({
78 value: Fixtures.gitIn(e.argv, Fixtures.MOVED_IN),
79 }))
80
81 on('ui.open', () => ({ value: undefined }))
82 on('ui.invalidate', () => ({ value: undefined }))
83 on('session.messages', () => ({ value: [] }))
84 Fixtures.oldFiles(on)
85 mock.store(on)
86
87 await $.session.start(Fixtures.SESSION)
88 await $.command.run(Fixtures.DIFF)
89 await clock.advance(Fixtures.SETTLE_MS)
90
91 const drawn = Fixtures.textOf(await $.ui.render(Fixtures.PANE))
92
93 expect(drawn).toContain('1 file changed +1 -1')
94 expect(drawn).toContain('moved.ts')
95 expect(drawn).toContain('+1 file edited before this session (show)')
96 })
97
98 test("the session began when the engine says: a resumed one's first start, again at /clear", async ($, on) => {
99 let startedAt = 0
100
101 const clock = Fixtures.startsSession(on, Fixtures.SETTLE_MS)
102
103 on('process.run', ($, e) => ({
104 value: Fixtures.gitIn(e.argv, Fixtures.MOVED_IN),
105 }))
106
107 on('ui.open', () => ({ value: undefined }))
108 on('ui.invalidate', () => ({ value: undefined }))
109 on('session.messages', () => ({ value: [] }))
110 on('session.usage', () => ({ value: Fixtures.usageAt(startedAt) }))
111 on('command.run', { command: 'clear' }, () => ({}))
112 Fixtures.oldFiles(on)
113 mock.store(on)
114
115 await $.session.start(Fixtures.SESSION)
116 await $.command.run(Fixtures.DIFF)
117 await clock.advance(Fixtures.SETTLE_MS)
118
119 expect(
120 Fixtures.textOf(await $.ui.render(Fixtures.PANE)),
121 'resumed from before the old files were written: both are the ' +
122 "session's, as the built-in restores its start",
123 ).toContain('2 files changed +2 -2')
124
125 startedAt = clock.now()
126
127 await $.command.run(Fixtures.CLEAR)
128 await clock.advance(Fixtures.SETTLE_MS)
129
130 expect(
131 Fixtures.textOf(await $.ui.render(Fixtures.PANE)),
132 '/clear started the session over, past the dirty file',
133 ).toContain('+1 file edited before this session (show)')
134 })
135})