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 / probes / head-key-of.test.ts
118 lines · 3.1 KBRaw
1import { describe, expect, test, tier } from 'claude-code/testing'
2
3import Git from '../../../hooks/git'
4import Fixtures from '../../fixtures'
5
6tier('builtin')
7
8describe('head-key-of', () => {
9 type Recorded = {
10 deps: Git.HeadKeyDeps
11 statted: readonly string[]
12 read: readonly string[]
13 ran: readonly (readonly string[])[]
14 }
15
16 function recorded(
17 listings: Readonly<Record<string, ReadonlyMap<string, Git.EntryKind>>>,
18 head: string,
19 ): Recorded {
20 const statted: string[] = []
21 const read: string[] = []
22
23 const git = Fixtures.scriptedGitOf({
24 'rev-parse': Fixtures.ok(`${Fixtures.HEAD_SHA}\n`),
25 })
26
27 const depsOf = (): Git.HeadKeyDeps => ({
28 run: git.run,
29 entryKindsOf: dir => Promise.resolve(listings[dir] ?? null),
30 mtimeOf: path => {
31 statted.push(path)
32
33 return Promise.resolve(Fixtures.STAMP)
34 },
35 readFile: path => {
36 read.push(path)
37
38 return Promise.resolve(head)
39 },
40 })
41
42 return { deps: depsOf(), statted, read, ran: git.argvs }
43 }
44
45 const plainGitDir: ReadonlyMap<string, Git.EntryKind> = new Map([
46 ['HEAD', 'file'],
47 ['packed-refs', 'file'],
48 ['refs', 'dir'],
49 ])
50
51 const refs: ReadonlyMap<string, Git.EntryKind> = new Map([['heads', 'dir']])
52 const heads: ReadonlyMap<string, Git.EntryKind> = new Map([['main', 'file']])
53
54 const healthy = {
55 '/r/.git': plainGitDir,
56 '/r/.git/refs': refs,
57 '/r/.git/refs/heads': heads,
58 }
59
60 test('a plain checkout reads HEAD, its ref file, packed-refs', async () => {
61 const probe = recorded(healthy, 'ref: refs/heads/main\n')
62 const key = await Git.headKeyOf(probe.deps, Fixtures.CHECKOUT)
63
64 expect(probe.statted).toEqual([
65 '/r/.git/HEAD',
66 '/r/.git/refs/heads/main',
67 '/r/.git/packed-refs',
68 ])
69
70 expect(key).toContain('refs/heads/main')
71 expect(probe.ran).toEqual([])
72 })
73
74 test('a climbing ref: is never stat-ed; git answers instead', async () => {
75 const probe = recorded(healthy, 'ref: ../../x\n')
76 const key = await Git.headKeyOf(probe.deps, Fixtures.CHECKOUT)
77
78 expect(probe.statted).toEqual([])
79 expect(key).toBe(`rev:${Fixtures.HEAD_SHA}`)
80 })
81
82 test('a HEAD that is a symbolic link is never read or stat-ed', async () => {
83 const probe = recorded(
84 {
85 ...healthy,
86 '/r/.git': new Map<string, Git.EntryKind>([
87 ['HEAD', 'other'],
88 ['packed-refs', 'file'],
89 ['refs', 'dir'],
90 ]),
91 },
92 'ref: refs/heads/main\n',
93 )
94
95 const key = await Git.headKeyOf(probe.deps, Fixtures.CHECKOUT)
96
97 expect(probe.read).toEqual([])
98 expect(probe.statted).toEqual([])
99 expect(key).toBe(`rev:${Fixtures.HEAD_SHA}`)
100 })
101
102 test('a symlinked refs directory hides the ref file', async () => {
103 const probe = recorded(
104 {
105 ...healthy,
106 '/r/.git': new Map<string, Git.EntryKind>([
107 ['HEAD', 'file'],
108 ['refs', 'other'],
109 ]),
110 },
111 'ref: refs/heads/main\n',
112 )
113
114 await Git.headKeyOf(probe.deps, Fixtures.CHECKOUT)
115
116 expect(probe.statted).toEqual(['/r/.git/HEAD'])
117 })
118})