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 / fixtures / in-repository.ts
85 lines · 2.3 KBRaw
1import type { Args, On } from 'claude-code'
2import { mock } from 'claude-code/testing'
3
4import Beneath from './beneath'
5import { gitIn } from './git-in.js'
6import { HINT_DRAWN } from './hint-drawn.js'
7import { keeping } from './keeping.js'
8import { REPOSITORY } from './repository.js'
9import { startsSession } from './starts-session.js'
10
11/**
12 * A session in a repository git answers for from a script (REPOSITORY, in
13 * /work, when none is given), keeping what the plugin does there.
14 *
15 * Kept: each git run, ring move, pane opened, left waiting or closed, status
16 * line. The clock starts at 0 and the engine draws the hint; the rest of the
17 * world is the test's (Beneath). Rewriting the script changes git's answers.
18 *
19 * @param on the test's `on`
20 * @param script git's output for each invocation whose line holds the key
21 * @param beneath the store, settings, environment, whether an open is seated
22 * @returns the runs, the ring's moves, the panes opened, left waiting and
23 * closed, the statuses, the clock
24 */
25export function inRepository(
26 on: On,
27 script: Readonly<Record<string, string>> = REPOSITORY,
28 beneath: Beneath.Beneath = {},
29) {
30 const runs: Args<'process.run'>[] = []
31 const focused: Args<'ui.focus'>[] = []
32 const statuses: (string | undefined)[] = []
33 const waiting: Args<'ui.open'>[] = []
34 const opened = keeping<Args<'ui.open'>>()
35 const closed = keeping<Args<'ui.close'>>()
36 const clock = startsSession(on)
37
38 on('process.run', ($, e) => {
39 runs.push(e)
40
41 return { value: gitIn(e.argv, script) }
42 })
43
44 on('ui.focus', (_engine, e) => {
45 focused.push(e)
46
47 return {}
48 })
49
50 on('ui.status', ($, e) => {
51 statuses.push(e.text)
52
53 return { value: undefined }
54 })
55
56 on('ui.open', (engine, e) => {
57 const isWaiting = beneath.isLeftWaiting?.() === true
58
59 if (!isWaiting) {
60 return opened.hook(engine, e)
61 }
62
63 waiting.push(e)
64
65 return Beneath.LEFT_WAITING
66 })
67
68 on('ui.close', closed.hook)
69 on('ui.invalidate', () => ({ value: undefined }))
70 on('ui.render', { component: 'PromptHint' }, () => HINT_DRAWN)
71 on('session.messages', () => ({ value: [] }))
72 on('settings.read', () => ({ value: beneath.settings ?? {} }))
73 mock.store(on, beneath.stored ?? {})
74 mock.env(on, beneath.env ?? {})
75
76 return {
77 runs,
78 focused,
79 opened: opened.kept,
80 waiting,
81 closed: closed.kept,
82 statuses,
83 clock,
84 }
85}