Skip to content

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.

TypeScript

mods/diff: tests for the docked and inline layouts, the rename row, the moved-in file and /diff's answers

poteat778a2eb5e91e
diff --git a/mods/diff/tests/fixtures/git-in.ts b/mods/diff/tests/fixtures/git-in.ts
index 49e6ff7e..f30e9b37 100644
--- a/mods/diff/tests/fixtures/git-in.ts
+++ b/mods/diff/tests/fixtures/git-in.ts
@@ -1,18 +1,21 @@
import type { ProcessRunResult } from 'claude-code'
import { NOT_A_REPOSITORY } from './not-a-repository.js'
-import { REPOSITORY } from './repository.js'
/**
- * What git answers in /work (REPOSITORY); an invocation it does not know
- * fails as git does outside a repository.
+ * What git answers in /work; an invocation the answers do not know fails
+ * as git does outside a repository.
*
* @param argv the invocation, program first
+ * @param answers git's output by a key the invocation's command line holds
* @returns git's exit code and output
*/
-export function gitIn(argv: readonly string[]): ProcessRunResult {
+export function gitIn(
+ argv: readonly string[],
+ answers: Readonly<Record<string, string>>,
+): ProcessRunResult {
const line = argv.join(' ')
- const found = Object.entries(REPOSITORY).find(([key]) => line.includes(key))
+ const found = Object.entries(answers).find(([key]) => line.includes(key))
return found
? { exitCode: 0, stdout: found[1], stderr: '' }
diff --git a/mods/diff/tests/fixtures/in-repository.ts b/mods/diff/tests/fixtures/in-repository.ts
index fa27ed2b..2e731120 100644
--- a/mods/diff/tests/fixtures/in-repository.ts
+++ b/mods/diff/tests/fixtures/in-repository.ts
@@ -4,6 +4,7 @@ import { mock } from 'claude-code/testing'
import { gitIn } from './git-in.js'
import { HINT_DRAWN } from './hint-drawn.js'
import { keeping } from './keeping.js'
+import { REPOSITORY } from './repository.js'
import { startsSession } from './starts-session.js'
/**
@@ -13,9 +14,13 @@ import { startsSession } from './starts-session.js'
* The store starts empty, the clock at 0, and the engine draws the hint.
*
* @param on the test's `on`
+ * @param answers git's output by invocation key; one changed file when absent
* @returns the runs, the panes opened and closed, and the session's clock
*/
-export function inRepository(on: On) {
+export function inRepository(
+ on: On,
+ answers: Readonly<Record<string, string>> = REPOSITORY,
+) {
const runs: Args<'process.run'>[] = []
const opened = keeping<Args<'ui.open'>>()
const closed = keeping<Args<'ui.close'>>()
@@ -24,7 +29,7 @@ export function inRepository(on: On) {
on('process.run', ($, e) => {
runs.push(e)
- return { value: gitIn(e.argv) }
+ return { value: gitIn(e.argv, answers) }
})
on('ui.open', opened.hook)
diff --git a/mods/diff/tests/fixtures/index.ts b/mods/diff/tests/fixtures/index.ts
index 72d50c73..7c40d700 100644
--- a/mods/diff/tests/fixtures/index.ts
+++ b/mods/diff/tests/fixtures/index.ts
@@ -6,13 +6,22 @@ export * from './git-in.js'
export * from './hint.js'
export * from './hint-drawn.js'
export * from './in-repository.js'
+export * from './inline-pane.js'
export * from './keeping.js'
+export * from './many-file-count.js'
+export * from './many-files.js'
+export * from './moved-in.js'
+export * from './narrow-inline-pane.js'
export * from './not-a-repository.js'
+export * from './old-files.js'
export * from './pane.js'
+export * from './renamed.js'
export * from './repository.js'
+export * from './repository-of.js'
export * from './session.js'
export * from './settle-ms.js'
export * from './starts-session.js'
export * from './text-of.js'
+export * from './two-files.js'
export * as default from '.'
diff --git a/mods/diff/tests/fixtures/inline-pane.ts b/mods/diff/tests/fixtures/inline-pane.ts
new file mode 100644
index 00000000..7acab4a5
--- /dev/null
+++ b/mods/diff/tests/fixtures/inline-pane.ts
@@ -0,0 +1,12 @@
+import type { RenderInput } from 'claude-code'
+
+import { PANE } from './pane.js'
+
+/**
+ * The diff pane seated inline above the prompt on a 160-column terminal,
+ * as a session without the fullscreen layout seats it.
+ */
+export const INLINE_PANE: RenderInput<'Pane'> = {
+ ...PANE,
+ props: { ...PANE.props, placement: 'inline', bodyColumns: 156 },
+}
diff --git a/mods/diff/tests/fixtures/many-file-count.ts b/mods/diff/tests/fixtures/many-file-count.ts
new file mode 100644
index 00000000..25abf20b
--- /dev/null
+++ b/mods/diff/tests/fixtures/many-file-count.ts
@@ -0,0 +1,5 @@
+/**
+ * How many files MANY_FILES changes: two more than the docked list shows at
+ * once.
+ */
+export const MANY_FILE_COUNT = 10
diff --git a/mods/diff/tests/fixtures/many-files.ts b/mods/diff/tests/fixtures/many-files.ts
new file mode 100644
index 00000000..44246288
--- /dev/null
+++ b/mods/diff/tests/fixtures/many-files.ts
@@ -0,0 +1,19 @@
+import { MANY_FILE_COUNT } from './many-file-count.js'
+import { repositoryOf } from './repository-of.js'
+
+/**
+ * Git's output in /work where MANY_FILE_COUNT files changed a line each:
+ * more rows than the docked list shows at once.
+ */
+export const MANY_FILES = repositoryOf(
+ Array.from(
+ { length: MANY_FILE_COUNT },
+ (_, at) => `1\t1\tfile${at}.ts\0`,
+ ).join(''),
+ Object.fromEntries(
+ Array.from({ length: MANY_FILE_COUNT }, (_, at) => [
+ `file${at}.ts`,
+ `@@ -1 +1 @@\n-const v = ${at}\n+const v = ${at + 1}\n`,
+ ]),
+ ),
+)
diff --git a/mods/diff/tests/fixtures/moved-in.ts b/mods/diff/tests/fixtures/moved-in.ts
new file mode 100644
index 00000000..a7f210ed
--- /dev/null
+++ b/mods/diff/tests/fixtures/moved-in.ts
@@ -0,0 +1,14 @@
+import { repositoryOf } from './repository-of.js'
+
+/**
+ * Git's output in /work where two old files differ from HEAD: one dirty
+ * before the session began, one moved into place since (not dirty then).
+ */
+export const MOVED_IN = repositoryOf(
+ '1\t1\told.ts\0' + '1\t1\tmoved.ts\0',
+ {
+ 'old.ts': '@@ -1 +1 @@\n-a\n+b\n',
+ 'moved.ts': '@@ -1 +1 @@\n-c\n+d\n',
+ },
+ ['old.ts'],
+)
diff --git a/mods/diff/tests/fixtures/narrow-inline-pane.ts b/mods/diff/tests/fixtures/narrow-inline-pane.ts
new file mode 100644
index 00000000..f17e77cb
--- /dev/null
+++ b/mods/diff/tests/fixtures/narrow-inline-pane.ts
@@ -0,0 +1,13 @@
+import type { RenderInput } from 'claude-code'
+
+import { INLINE_PANE } from './inline-pane.js'
+
+/**
+ * The diff pane seated inline on a 100-column terminal: too narrow for the
+ * built-in panel, which shows only its wider-terminal line there.
+ */
+export const NARROW_INLINE_PANE: RenderInput<'Pane'> = {
+ ...INLINE_PANE,
+ viewport: { columns: 100, rows: 30 },
+ props: { ...INLINE_PANE.props, bodyColumns: 96 },
+}
diff --git a/mods/diff/tests/fixtures/old-files.ts b/mods/diff/tests/fixtures/old-files.ts
new file mode 100644
index 00000000..4d71a937
--- /dev/null
+++ b/mods/diff/tests/fixtures/old-files.ts
@@ -0,0 +1,18 @@
+import type { On } from 'claude-code'
+
+/**
+ * Answers `$.fs` over /work holding MOVED_IN's two files, both last
+ * written at time 0, before any session began.
+ *
+ * @param on the test's `on`
+ */
+export function oldFiles(on: On) {
+ on('fs.list', () => ({
+ value: [
+ { name: 'old.ts', kind: 'file', size: 2 },
+ { name: 'moved.ts', kind: 'file', size: 2 },
+ ],
+ }))
+
+ on('fs.stat', () => ({ value: { kind: 'file', size: 2, mtimeMs: 0 } }))
+}
diff --git a/mods/diff/tests/fixtures/renamed.ts b/mods/diff/tests/fixtures/renamed.ts
new file mode 100644
index 00000000..13661d9b
--- /dev/null
+++ b/mods/diff/tests/fixtures/renamed.ts
@@ -0,0 +1,10 @@
+import { repositoryOf } from './repository-of.js'
+
+/**
+ * Git's output in /work where one file was renamed inside its directory,
+ * its contents untouched.
+ */
+export const RENAMED = repositoryOf(
+ '0\t0\t\0docs/notes.txt\0docs/renamed-notes.txt\0',
+ {},
+)
diff --git a/mods/diff/tests/fixtures/repository-of.ts b/mods/diff/tests/fixtures/repository-of.ts
new file mode 100644
index 00000000..3977d0d4
--- /dev/null
+++ b/mods/diff/tests/fixtures/repository-of.ts
@@ -0,0 +1,27 @@
+/**
+ * Git's output in /work for each invocation whose command line holds the
+ * key, over the given `--numstat -z` records and per-file bodies.
+ *
+ * `status` lists the paths dirty at the start (the baseline: every path
+ * with a body unless given); a body is keyed by the `-- <path>` its diff is
+ * asked with.
+ *
+ * @param numstat the `--numstat -z` records, joined
+ * @param bodies each path's unified diff after its header
+ * @param dirty the paths `status` lists as dirty when the session starts
+ * @returns the answers by key
+ */
+export const repositoryOf = (
+ numstat: string,
+ bodies: Readonly<Record<string, string>>,
+ dirty: readonly string[] = Object.keys(bodies),
+): Readonly<Record<string, string>> => ({
+ 'rev-parse --path-format=absolute': '/work\n/work/.git\n/work/.git\n',
+ 'status --porcelain': dirty.map(path => ` M ${path}\0`).join(''),
+ 'HEAD --shortstat': ` ${Object.keys(bodies).length} files changed`,
+ 'HEAD --numstat': numstat,
+ 'ls-files': '',
+ ...Object.fromEntries(
+ Object.entries(bodies).map(([path, body]) => [`-- ${path}`, body]),
+ ),
+})
diff --git a/mods/diff/tests/fixtures/settle-ms.ts b/mods/diff/tests/fixtures/settle-ms.ts
index 94a4bd99..820789ad 100644
--- a/mods/diff/tests/fixtures/settle-ms.ts
+++ b/mods/diff/tests/fixtures/settle-ms.ts
@@ -1,5 +1,5 @@
/**
* Longer than every wait the pane schedules before it fetches and draws
- * (its open probe, refresh debounce and redraw), short of its HEAD poll.
+ * (its refresh debounce and redraw), short of its HEAD poll.
*/
export const SETTLE_MS = 1000
diff --git a/mods/diff/tests/fixtures/starts-session.ts b/mods/diff/tests/fixtures/starts-session.ts
index 6321927a..9ec206a2 100644
--- a/mods/diff/tests/fixtures/starts-session.ts
+++ b/mods/diff/tests/fixtures/starts-session.ts
@@ -7,11 +7,12 @@ import type { MockClock } from 'claude-code/testing'
* directory, the time, and each command it registers, under its name.
*
* @param on the test's `on`
- * @returns the clock the session reads, at 0 until the test moves it
+ * @param now where the clock starts, in milliseconds (0 when not given)
+ * @returns the clock the session reads, at `now` until the test moves it
*/
-export function startsSession(on: On): MockClock {
+export function startsSession(on: On, now = 0): MockClock {
on('session.start', ($, e) => ({ cwd: e.cwd }))
on('command.register', ($, e) => ({ value: { command: e.name } }))
- return mock.clock(on)
+ return mock.clock(on, { now })
}
diff --git a/mods/diff/tests/fixtures/text-of.ts b/mods/diff/tests/fixtures/text-of.ts
index 5c89ed67..e2806207 100644
--- a/mods/diff/tests/fixtures/text-of.ts
+++ b/mods/diff/tests/fixtures/text-of.ts
@@ -1,5 +1,6 @@
/**
- * A rendered tree's text as it reads: its strings and its labels, in order.
+ * A rendered tree's text as it reads: its strings, its labels and its code
+ * sources, in order.
*
* @param tree what `$.ui.render` resolved to, or a part of it
* @returns the text
@@ -19,10 +20,14 @@ export function textOf(tree: unknown): string {
const props: unknown = Reflect.get(tree, 'props')
- const label: unknown =
- typeof props === 'object' && props ? Reflect.get(props, 'label') : undefined
+ function leadOf(prop: string): string {
+ const value: unknown =
+ typeof props === 'object' && props ? Reflect.get(props, prop) : undefined
- const lead = typeof label === 'string' ? label : ''
+ return typeof value === 'string' ? value : ''
+ }
+
+ const lead = `${leadOf('label')}${leadOf('source')}`
return `${lead}${textOf(Reflect.get(tree, 'children') ?? [])}`
}
diff --git a/mods/diff/tests/fixtures/two-files.ts b/mods/diff/tests/fixtures/two-files.ts
new file mode 100644
index 00000000..6e1e4ea2
--- /dev/null
+++ b/mods/diff/tests/fixtures/two-files.ts
@@ -0,0 +1,9 @@
+import { repositoryOf } from './repository-of.js'
+
+/**
+ * Git's output in /work where two files changed, a hunk each.
+ */
+export const TWO_FILES = repositoryOf('1\t1\tapp.ts\0' + '2\t0\tlib.ts\0', {
+ 'app.ts': '@@ -1 +1 @@\n-const a = 1\n+const a = 2\n',
+ 'lib.ts': '@@ -3,0 +4,2 @@\n+export const b = 1\n+export const c = 2\n',
+})
diff --git a/mods/diff/tests/git.test.ts b/mods/diff/tests/git.test.ts
index 0ec8f925..982f47f8 100644
--- a/mods/diff/tests/git.test.ts
+++ b/mods/diff/tests/git.test.ts
@@ -1,4 +1,4 @@
-import { describe, expect, test, tier } from 'claude-code/testing'
+import { describe, expect, mock, test, tier } from 'claude-code/testing'
import Fixtures from './fixtures'
@@ -28,4 +28,28 @@ describe('git', () => {
expect(run.init).toMatchObject({ cwd: '/work', env: { LC_ALL: 'C' } })
}
})
+
+ test('a file moved in since the start is session work', async ($, on) => {
+ const clock = Fixtures.startsSession(on, Fixtures.SETTLE_MS)
+
+ on('process.run', ($, e) => ({
+ value: Fixtures.gitIn(e.argv, Fixtures.MOVED_IN),
+ }))
+
+ on('ui.open', () => ({ value: undefined }))
+ on('ui.invalidate', () => ({ value: undefined }))
+ on('session.messages', () => ({ value: [] }))
+ Fixtures.oldFiles(on)
+ mock.store(on)
+
+ await $.session.start(Fixtures.SESSION)
+ await $.command.run(Fixtures.DIFF)
+ await clock.advance(Fixtures.SETTLE_MS)
+
+ const drawn = Fixtures.textOf(await $.ui.render(Fixtures.PANE))
+
+ expect(drawn).toContain('1 file changed +1 -1')
+ expect(drawn).toContain('moved.ts')
+ expect(drawn).toContain('+1 file edited before this session (show)')
+ })
})
diff --git a/mods/diff/tests/register.test.ts b/mods/diff/tests/register.test.ts
index 13057d2c..04983c1d 100644
--- a/mods/diff/tests/register.test.ts
+++ b/mods/diff/tests/register.test.ts
@@ -120,13 +120,16 @@ describe('register', () => {
])
})
- test('/diff opens the pane over the session changes', async ($, on) => {
+ test('/diff opens the pane, unfocused, and says so', async ($, on) => {
const world = Fixtures.inRepository(on)
await $.session.start(Fixtures.SESSION)
- expect(await $.command.run(Fixtures.DIFF)).toEqual({})
- expect(world.opened.map(pane => pane.id)).toEqual(['diff'])
+ expect(await $.command.run(Fixtures.DIFF)).toEqual({
+ text: 'Diff panel shown',
+ })
+
+ expect(world.opened).toEqual([{ id: 'diff', title: 'Diff' }])
await world.clock.advance(Fixtures.SETTLE_MS)
@@ -136,23 +139,17 @@ describe('register', () => {
expect(drawn).toContain('app.ts')
})
- test('the close button closes the pane; /diff reopens it', async ($, on) => {
+ test('/diff again closes the pane and says so', async ($, on) => {
const world = Fixtures.inRepository(on)
await $.session.start(Fixtures.SESSION)
await $.command.run(Fixtures.DIFF)
- await world.clock.advance(Fixtures.SETTLE_MS)
- await $.ui.render(Fixtures.PANE)
- expect(await $.ui.press({ plugin: 'diff', key: 'close' })).toEqual({
- element: 'close',
+ expect(await $.command.run(Fixtures.DIFF)).toEqual({
+ text: 'Diff panel hidden',
})
- await world.clock.settle()
-
expect(world.closed.map(pane => pane.id)).toEqual(['diff'])
- expect(await $.command.run(Fixtures.DIFF)).toEqual({})
- expect(world.opened.map(pane => pane.id)).toEqual(['diff', 'diff'])
})
test('a wide terminal opens the pane at the first edit', async ($, on) => {
diff --git a/mods/diff/tests/views.test.ts b/mods/diff/tests/views.test.ts
new file mode 100644
index 00000000..59d660dd
--- /dev/null
+++ b/mods/diff/tests/views.test.ts
@@ -0,0 +1,105 @@
+import { describe, expect, test, tier } from 'claude-code/testing'
+
+import Fixtures from './fixtures'
+
+tier('builtin')
+
+describe('views', () => {
+ test('docked, every file has its hunks under the list', async ($, on) => {
+ const world = Fixtures.inRepository(on, Fixtures.TWO_FILES)
+
+ await $.session.start(Fixtures.SESSION)
+ await $.command.run(Fixtures.DIFF)
+ await world.clock.advance(Fixtures.SETTLE_MS)
+
+ const drawn = Fixtures.textOf(await $.ui.render(Fixtures.PANE))
+
+ expect(drawn).toContain('2 files changed +3 -1')
+ expect(drawn).toContain('+const a = 2')
+ expect(drawn).toContain('+export const c = 2')
+ expect(drawn).not.toContain('❯')
+ })
+
+ test('inline, a row moves the pointer and the body to it', async ($, on) => {
+ const world = Fixtures.inRepository(on, Fixtures.TWO_FILES)
+
+ await $.session.start(Fixtures.SESSION)
+ await $.command.run(Fixtures.DIFF)
+ await world.clock.advance(Fixtures.SETTLE_MS)
+ await $.ui.render(Fixtures.INLINE_PANE)
+
+ expect(await $.ui.press({ plugin: 'diff', key: 'file:lib.ts' })).toEqual({
+ element: 'file:lib.ts',
+ })
+
+ const drawn = Fixtures.textOf(await $.ui.render(Fixtures.INLINE_PANE))
+
+ expect(drawn).toContain('\u276f lib.ts')
+ expect(drawn).toContain('+export const c = 2')
+ expect(drawn).not.toContain('+const a = 2')
+ })
+
+ test('past eight files the docked list counts the rest', async ($, on) => {
+ const world = Fixtures.inRepository(on, Fixtures.MANY_FILES)
+
+ await $.session.start(Fixtures.SESSION)
+ await $.command.run(Fixtures.DIFF)
+ await world.clock.advance(Fixtures.SETTLE_MS)
+
+ const drawn = Fixtures.textOf(await $.ui.render(Fixtures.PANE))
+
+ expect(drawn).toContain('10 files changed +10 -10')
+ expect(drawn).toContain('file7.ts')
+ expect(drawn).toContain('↓ 2 more below')
+
+ expect(drawn.indexOf('↓ 2 more below')).toBeLessThan(
+ drawn.indexOf('file8.ts'),
+ )
+ })
+
+ test('a rename lists as git prints it, and reads no body', async ($, on) => {
+ const world = Fixtures.inRepository(on, Fixtures.RENAMED)
+
+ await $.session.start(Fixtures.SESSION)
+ await $.command.run(Fixtures.DIFF)
+ await world.clock.advance(Fixtures.SETTLE_MS)
+
+ const drawn = Fixtures.textOf(await $.ui.render(Fixtures.PANE))
+
+ expect(drawn).toContain('1 file changed')
+ expect(drawn).toContain('docs/{notes.txt => renamed-notes.txt}')
+ expect(drawn).toContain('No diff content')
+
+ expect(
+ world.runs.filter(run => run.argv.includes('docs/renamed-notes.txt')),
+ ).toEqual([])
+ })
+
+ test('inline on a wide terminal, the rows round one file', async ($, on) => {
+ const world = Fixtures.inRepository(on, Fixtures.TWO_FILES)
+
+ await $.session.start(Fixtures.SESSION)
+ await $.command.run(Fixtures.DIFF)
+ await world.clock.advance(Fixtures.SETTLE_MS)
+
+ const drawn = Fixtures.textOf(await $.ui.render(Fixtures.INLINE_PANE))
+
+ expect(drawn).toContain('❯ app.ts')
+ expect(drawn).toContain('+const a = 2')
+ expect(drawn).not.toContain('+export const c = 2')
+ })
+
+ test('inline on a narrow terminal, the resize line', async ($, on) => {
+ const world = Fixtures.inRepository(on, Fixtures.TWO_FILES)
+
+ await $.session.start(Fixtures.SESSION)
+ await $.command.run(Fixtures.DIFF)
+ await world.clock.advance(Fixtures.SETTLE_MS)
+
+ expect(
+ Fixtures.textOf(await $.ui.render(Fixtures.NARROW_INLINE_PANE)),
+ ).toBe(
+ 'Resize your terminal to at least 110 columns to show the diff panel',
+ )
+ })
+})