Skip to content

Commit 68799fc

Browse files
author
Token Goat Test
committed
test(cli_context_stats): isolate os.homedir() so claude_md_total pin is machine-independent
Codex peer review caught this: the prior 2530 pin was computed with the real developer machine's ~/.claude/CLAUDE.md folded into the total (findClaudeMdFiles reads it unconditionally, and isolate-home.ts only redirects HOME on macOS). That value would differ on any other machine and doesn't exist at all on a fresh CI runner, so the pin was silently machine-specific and would have failed on the very next CI run. Mock os.homedir() to the test's own isolated tempDir, matching the pattern already used by the other tests in this file, so claude_md_total is a pure function of the 400-byte fixture (100 tokens).
1 parent 53489f2 commit 68799fc

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

tests/cli_context_stats.test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,14 @@ describe('cli_context_stats', () => {
177177
fs.mkdirSync(project)
178178
fs.writeFileSync(path.join(project, 'CLAUDE.md'), 'x'.repeat(400))
179179

180+
// Pin homedir to the isolated tempDir (no .claude/CLAUDE.md there) so
181+
// claude_md_total is a pure function of this fixture. Without this, findClaudeMdFiles
182+
// also folds in the *real* developer's ~/.claude/CLAUDE.md (see src/cli_context_stats.ts),
183+
// which is machine-specific and absent entirely on a fresh CI runner -- an exact pin against
184+
// the unmocked value would be right on one machine and wrong everywhere else, including CI.
185+
const homedirMock = os.homedir as unknown as ReturnType<typeof vi.fn>
186+
homedirMock.mockReturnValue(tempDir)
187+
180188
let output = ''
181189
const orig = process.stdout.write.bind(process.stdout)
182190
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -188,8 +196,9 @@ describe('cli_context_stats', () => {
188196
;(process.stdout as any).write = orig
189197
}
190198

199+
// 400-byte fixture file / 4 = 100 tokens (see tok() in src/cli_context_stats.ts).
191200
const parsed = JSON.parse(output) as { claude_md_total: number }
192-
expect(parsed.claude_md_total).toBe(2530)
201+
expect(parsed.claude_md_total).toBe(100)
193202
})
194203

195204
it('--fix actually prunes MEMORY.md via memory_prune (not a no-op)', async () => {

0 commit comments

Comments
 (0)