Skip to content

Commit 936b929

Browse files
committed
mods: tests sit in one describe each, diff's shared inputs in a fixtures folder, every decision and duplicate named
1 parent 97f86b9 commit 936b929

10 files changed

Lines changed: 450 additions & 403 deletions

File tree

‎mods/diff/tests/boot-probe.test.ts‎

Lines changed: 32 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,41 @@
1-
import type {
2-
CommandRunInput,
3-
ProcessRunResult,
4-
SessionStartInput,
5-
} from 'claude-code'
6-
import { clock, expect, test, tier } from 'claude-code/testing'
1+
import { clock, describe, expect, test, tier } from 'claude-code/testing'
2+
3+
import Limits from '../hooks/limits'
4+
import Fixtures from './fixtures'
5+
6+
const GIT_HUNG = `git aborted: still running after ${Limits.GIT_TIMEOUT_MS}ms`
77

88
tier('builtin')
99

10-
const SESSION: SessionStartInput = {
11-
surface: 'terminal',
12-
isInteractive: true,
13-
cwd: '/work',
14-
}
15-
const DIFF: CommandRunInput = {
16-
command: 'diff',
17-
args: '',
18-
origin: { kind: 'composer' },
19-
}
20-
const NOT_A_REPOSITORY: ProcessRunResult = {
21-
exitCode: 128,
22-
stdout: '',
23-
stderr: 'fatal: not a git repository',
24-
}
25-
const GIT_TIMEOUT_MS = 5000
26-
const GIT_HUNG = `git aborted: still running after ${GIT_TIMEOUT_MS}ms`
27-
28-
test('/diff at boot joins the boot probe, then asks again', async ($, on) => {
29-
const probes: (readonly string[])[] = []
30-
on('session.start', ($, e) => ({ cwd: e.cwd }))
31-
on('command.register', ($, e) => ({ value: { command: e.name } }))
32-
on('process.run', async ($, e) => {
33-
probes.push(e.argv)
34-
35-
if (probes.length === 1) {
36-
await clock.sleep(GIT_TIMEOUT_MS)
37-
38-
return { deny: GIT_HUNG }
39-
}
40-
41-
return { value: NOT_A_REPOSITORY }
42-
})
10+
describe('boot-probe', () => {
11+
test('/diff at boot joins the boot probe, then asks again', async ($, on) => {
12+
const probes: (readonly string[])[] = []
13+
Fixtures.startsSession(on)
14+
on('process.run', async ($, e) => {
15+
probes.push(e.argv)
16+
17+
if (probes.length === 1) {
18+
await clock.sleep(Limits.GIT_TIMEOUT_MS)
19+
20+
return { deny: GIT_HUNG }
21+
}
22+
23+
return { value: Fixtures.NOT_A_REPOSITORY }
24+
})
4325

44-
const booting = $.session.start(SESSION)
45-
await clock.advance(0)
46-
const ran = $.command.run(DIFF)
47-
await clock.advance(0)
26+
const booting = $.session.start(Fixtures.SESSION)
27+
await clock.advance(0)
28+
const ran = $.command.run(Fixtures.DIFF)
29+
await clock.advance(0)
4830

49-
expect(probes, 'the boot probe, which /diff joined').toHaveLength(1)
31+
expect(probes, 'the boot probe, which /diff joined').toHaveLength(1)
5032

51-
await clock.advance(GIT_TIMEOUT_MS)
52-
await booting
33+
await clock.advance(Limits.GIT_TIMEOUT_MS)
34+
await booting
5335

54-
expect(await ran).toEqual({
55-
text: expect.stringContaining("isn't in a git repository"),
36+
expect(await ran).toEqual({
37+
text: expect.stringContaining("isn't in a git repository"),
38+
})
39+
expect(probes, 'then one more of its own').toHaveLength(2)
5640
})
57-
expect(probes, 'then one more of its own').toHaveLength(2)
5841
})

‎mods/diff/tests/command.test.ts‎

Lines changed: 59 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,77 @@
1-
import type {
2-
CommandRunInput,
3-
ProcessRunResult,
4-
SessionStartInput,
5-
} from 'claude-code'
6-
import { expect, test, tier } from 'claude-code/testing'
1+
import { describe, expect, test, tier } from 'claude-code/testing'
72

8-
tier('builtin')
3+
import Fixtures from './fixtures'
94

10-
const SESSION: SessionStartInput = {
11-
surface: 'terminal',
12-
isInteractive: true,
13-
cwd: '/work',
14-
}
15-
const DIFF: CommandRunInput = {
16-
command: 'diff',
17-
args: '',
18-
origin: { kind: 'composer' },
19-
}
20-
const NOT_A_REPOSITORY: ProcessRunResult = {
21-
exitCode: 128,
22-
stdout: '',
23-
stderr: 'fatal: not a git repository',
24-
}
255
const BUILTIN_HOLDS = '"/diff" refused: it is the built-in /diff'
266

27-
test('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-
})
7+
tier('builtin')
368

37-
await $.session.start(SESSION)
38-
const { text } = await $.command.run(DIFF)
9+
describe('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)
3916

40-
expect(text).toContain("isn't in a git repository")
41-
expect(opened).toEqual([])
42-
})
17+
return next(e)
18+
})
4319

44-
test('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-
}))
20+
await $.session.start(Fixtures.SESSION)
21+
const { text } = await $.command.run(Fixtures.DIFF)
5022

51-
await $.session.start(SESSION)
52-
const { text } = await $.command.run(DIFF)
23+
expect(text).toContain("isn't in a git repository")
24+
expect(opened).toEqual([])
25+
})
5326

54-
expect(text).toContain("git didn't answer")
55-
})
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+
}))
5632

57-
test('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 }
33+
await $.session.start(Fixtures.SESSION)
34+
const { text } = await $.command.run(Fixtures.DIFF)
35+
36+
expect(text).toContain("git didn't answer")
6537
})
6638

67-
await $.session.start(SESSION)
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)
6846

69-
expect(await $.command.run(DIFF)).toEqual({ text: 'the built-in /diff ran' })
70-
expect(logged).toEqual([])
71-
})
47+
return { value: undefined }
48+
})
49+
50+
await $.session.start(Fixtures.SESSION)
7251

73-
test('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 }
52+
expect(await $.command.run(Fixtures.DIFF)).toEqual({
53+
text: 'the built-in /diff ran',
54+
})
55+
expect(logged).toEqual([])
8056
})
8157

82-
await $.session.start(SESSION)
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)
8366

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-
])
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+
})
8877
})

‎mods/diff/tests/fixtures/diff.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { CommandRunInput } from 'claude-code'
2+
3+
/**
4+
* The command as the person types it, with no arguments.
5+
*/
6+
export const DIFF: CommandRunInput = {
7+
command: 'diff',
8+
args: '',
9+
origin: { kind: 'composer' },
10+
}

‎mods/diff/tests/fixtures/index.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export * from './diff.js'
2+
export * from './not-a-repository.js'
3+
export * from './session.js'
4+
export * from './starts-session.js'
5+
6+
export * as default from '.'
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { ProcessRunResult } from 'claude-code'
2+
3+
/**
4+
* How git fails in a directory outside any repository.
5+
*/
6+
export const NOT_A_REPOSITORY: ProcessRunResult = {
7+
exitCode: 128,
8+
stdout: '',
9+
stderr: 'fatal: not a git repository',
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import type { SessionStartInput } from 'claude-code'
2+
3+
/**
4+
* An interactive terminal session in /work.
5+
*/
6+
export const SESSION: SessionStartInput = {
7+
surface: 'terminal',
8+
isInteractive: true,
9+
cwd: '/work',
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import type { On } from 'claude-code'
2+
3+
/**
4+
* Answers what every /diff session asks first: its start, with its own
5+
* directory, and each command it registers, under the name asked for.
6+
*
7+
* @param on the test's `on`
8+
*/
9+
export function startsSession(on: On) {
10+
on('session.start', ($, e) => ({ cwd: e.cwd }))
11+
on('command.register', ($, e) => ({ value: { command: e.name } }))
12+
}

0 commit comments

Comments
 (0)