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
| 1 | import { describe, expect, test, tier } from 'claude-code/testing' |
| 2 | |
| 3 | import Git from '../../../hooks/git' |
| 4 | import Fixtures from '../../fixtures' |
| 5 | |
| 6 | tier('builtin') |
| 7 | |
| 8 | describe('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 | stampOf: (path: string) => number = () => Fixtures.STAMP, |
| 20 | ): Recorded { |
| 21 | const statted: string[] = [] |
| 22 | const read: string[] = [] |
| 23 | |
| 24 | const git = Fixtures.scriptedGitOf({ |
| 25 | 'rev-parse': Fixtures.ok(`${Fixtures.HEAD_SHA}\n`), |
| 26 | }) |
| 27 | |
| 28 | const depsOf = (): Git.HeadKeyDeps => ({ |
| 29 | run: git.run, |
| 30 | entryKindsOf: dir => Promise.resolve(listings[dir] ?? null), |
| 31 | mtimeOf: path => { |
| 32 | statted.push(path) |
| 33 | |
| 34 | return Promise.resolve(stampOf(path)) |
| 35 | }, |
| 36 | readFile: path => { |
| 37 | read.push(path) |
| 38 | |
| 39 | return Promise.resolve(head) |
| 40 | }, |
| 41 | }) |
| 42 | |
| 43 | return { deps: depsOf(), statted, read, ran: git.argvs } |
| 44 | } |
| 45 | |
| 46 | const plainGitDir: ReadonlyMap<string, Git.EntryKind> = new Map([ |
| 47 | ['HEAD', 'file'], |
| 48 | ['packed-refs', 'file'], |
| 49 | ['refs', 'dir'], |
| 50 | ]) |
| 51 | |
| 52 | const refs: ReadonlyMap<string, Git.EntryKind> = new Map([['heads', 'dir']]) |
| 53 | const heads: ReadonlyMap<string, Git.EntryKind> = new Map([['main', 'file']]) |
| 54 | |
| 55 | const healthy = { |
| 56 | '/r/.git': plainGitDir, |
| 57 | '/r/.git/refs': refs, |
| 58 | '/r/.git/refs/heads': heads, |
| 59 | } |
| 60 | |
| 61 | const logged = { |
| 62 | '/r/.git': new Map<string, Git.EntryKind>([ |
| 63 | ...plainGitDir, |
| 64 | ['logs', 'dir'], |
| 65 | ]), |
| 66 | '/r/.git/logs': new Map<string, Git.EntryKind>([['HEAD', 'file']]), |
| 67 | } |
| 68 | |
| 69 | async function idleOn(branch: string) { |
| 70 | const probe = recorded(logged, `ref: refs/heads/${branch}\n`) |
| 71 | |
| 72 | return { |
| 73 | keys: new Set([ |
| 74 | await Git.headKeyOf(probe.deps, Fixtures.CHECKOUT), |
| 75 | await Git.headKeyOf(probe.deps, Fixtures.CHECKOUT), |
| 76 | await Git.headKeyOf(probe.deps, Fixtures.CHECKOUT), |
| 77 | ]).size, |
| 78 | ran: probe.ran, |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | test('a plain checkout reads HEAD, its ref file, packed-refs', async () => { |
| 83 | const probe = recorded(healthy, 'ref: refs/heads/main\n') |
| 84 | const key = await Git.headKeyOf(probe.deps, Fixtures.CHECKOUT) |
| 85 | |
| 86 | expect(probe.statted).toEqual([ |
| 87 | '/r/.git/HEAD', |
| 88 | '/r/.git/refs/heads/main', |
| 89 | '/r/.git/packed-refs', |
| 90 | ]) |
| 91 | |
| 92 | expect(key).toContain('refs/heads/main') |
| 93 | expect(probe.ran).toEqual([]) |
| 94 | }) |
| 95 | |
| 96 | test('a climbing ref: is never stat-ed; git answers instead', async () => { |
| 97 | const probe = recorded(healthy, 'ref: ../../x\n') |
| 98 | const key = await Git.headKeyOf(probe.deps, Fixtures.CHECKOUT) |
| 99 | |
| 100 | expect(probe.statted).toEqual([]) |
| 101 | expect(key).toBe(`rev:${Fixtures.HEAD_SHA}`) |
| 102 | }) |
| 103 | |
| 104 | test('a branch named with a # starts no git while idle', async () => { |
| 105 | expect(await idleOn('fix/#12')).toEqual({ ran: [], keys: 1 }) |
| 106 | }) |
| 107 | |
| 108 | test('a branch named outside ASCII starts no git while idle', async () => { |
| 109 | expect(await idleOn('höher')).toEqual({ ran: [], keys: 1 }) |
| 110 | }) |
| 111 | |
| 112 | test('on such a branch a moved HEAD is seen at the next look', async () => { |
| 113 | let moves = 0 |
| 114 | |
| 115 | const probe = recorded(logged, 'ref: refs/heads/fix/#12\n', path => |
| 116 | path.endsWith('/logs/HEAD') ? Fixtures.STAMP + moves : Fixtures.STAMP, |
| 117 | ) |
| 118 | |
| 119 | const before = await Git.headKeyOf(probe.deps, Fixtures.CHECKOUT) |
| 120 | |
| 121 | moves += 1 |
| 122 | |
| 123 | expect(await Git.headKeyOf(probe.deps, Fixtures.CHECKOUT)).not.toBe(before) |
| 124 | expect(probe.ran, 'dated by the log of HEAD, no git started').toEqual([]) |
| 125 | }) |
| 126 | |
| 127 | test('with no log of HEAD such a branch is read through git', async () => { |
| 128 | const probe = recorded(healthy, 'ref: refs/heads/fix/#12\n') |
| 129 | const key = await Git.headKeyOf(probe.deps, Fixtures.CHECKOUT) |
| 130 | |
| 131 | expect(probe.statted).toEqual([]) |
| 132 | expect(key).toBe(`rev:${Fixtures.HEAD_SHA}`) |
| 133 | }) |
| 134 | |
| 135 | test('a HEAD that is a symbolic link is never read or stat-ed', async () => { |
| 136 | const probe = recorded( |
| 137 | { |
| 138 | ...healthy, |
| 139 | '/r/.git': new Map<string, Git.EntryKind>([ |
| 140 | ['HEAD', 'other'], |
| 141 | ['packed-refs', 'file'], |
| 142 | ['refs', 'dir'], |
| 143 | ]), |
| 144 | }, |
| 145 | 'ref: refs/heads/main\n', |
| 146 | ) |
| 147 | |
| 148 | const key = await Git.headKeyOf(probe.deps, Fixtures.CHECKOUT) |
| 149 | |
| 150 | expect(probe.read).toEqual([]) |
| 151 | expect(probe.statted).toEqual([]) |
| 152 | expect(key).toBe(`rev:${Fixtures.HEAD_SHA}`) |
| 153 | }) |
| 154 | |
| 155 | test('a symlinked refs directory hides the ref file', async () => { |
| 156 | const probe = recorded( |
| 157 | { |
| 158 | ...healthy, |
| 159 | '/r/.git': new Map<string, Git.EntryKind>([ |
| 160 | ['HEAD', 'file'], |
| 161 | ['refs', 'other'], |
| 162 | ]), |
| 163 | }, |
| 164 | 'ref: refs/heads/main\n', |
| 165 | ) |
| 166 | |
| 167 | await Git.headKeyOf(probe.deps, Fixtures.CHECKOUT) |
| 168 | |
| 169 | expect(probe.statted).toEqual(['/r/.git/HEAD']) |
| 170 | }) |
| 171 | }) |