|
| 1 | +/** The manifest's git dirty-state probe must not spawn git for a directory outside every work tree. CI run 38987886278 failed tests/compact.test.ts on Windows with EBUSY from rmSync of a scratch project directory: the two failing tests were the only ones that both pass a cwd and exceed the manifest cap, which is what runs `git diff` and `git status` there under the 50 ms hints.git_hint_max_ms bound. A spawn that is killed at that bound is not finished with the directory it started in, and in a non-repository the spawn could only ever answer "nothing". */ |
| 2 | + |
| 3 | +import * as fs from 'node:fs' |
| 4 | +import * as os from 'node:os' |
| 5 | +import * as path from 'node:path' |
| 6 | + |
| 7 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' |
| 8 | + |
| 9 | +import { CAN_JUNCTION } from './helpers/can-symlink.js' |
| 10 | + |
| 11 | +import { importSessionState, recordFileEdit, recordFileRead } from '../src/session.js' |
| 12 | +import { saveSessionState } from '../src/session_store.js' |
| 13 | +import { buildManifest } from '../src/manifest.js' |
| 14 | +import { runGit } from '../src/util.js' |
| 15 | + |
| 16 | +// No real git runs in this file: the probe's argument and cwd are what is asserted, so the answer cannot depend on how fast a process starts. |
| 17 | +vi.mock('../src/util.js', async (importOriginal) => { |
| 18 | + const actual = await importOriginal<Record<string, unknown>>() |
| 19 | + return { ...actual, runGit: vi.fn(() => ({ stdout: '', stderr: '', exitCode: 0 })) } |
| 20 | +}) |
| 21 | + |
| 22 | +const runGitMock = vi.mocked(runGit) |
| 23 | + |
| 24 | +// HAND-DERIVED: every fixture is built on disk by this file (a bare `.git` marker, a junction to a folder holding one) and the expected probe calls follow from what git itself walks up for, not from reading the matcher. |
| 25 | + |
| 26 | +/** The nearest ancestor of `dir` that holds a `.git` entry, or null when none does. */ |
| 27 | +function gitAncestor(dir: string): string | null { |
| 28 | + for (let cur = path.resolve(dir); ; cur = path.dirname(cur)) { |
| 29 | + if (fs.existsSync(path.join(cur, '.git'))) return cur |
| 30 | + if (path.dirname(cur) === cur) return null |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/** A session whose manifest is well past the default 1600-character cap, so the cap engages and the adaptive bonus (the git probe) is consulted. */ |
| 35 | +function seedLongSession(sessionId: string): void { |
| 36 | + recordFileEdit('/proj/src/edited.ts') |
| 37 | + for (let i = 0; i < 45; i++) recordFileRead(`/proj/src/read-only-file-number-${i}.ts`) |
| 38 | + saveSessionState(sessionId) |
| 39 | +} |
| 40 | + |
| 41 | +describe('manifest git probe needs a work tree', () => { |
| 42 | + const dirs: string[] = [] |
| 43 | + let prevGitDir: string | undefined |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + importSessionState({ files: [], hintsShown: [], webFetches: [], bashOutputs: [], curlDownloads: [] }) |
| 47 | + runGitMock.mockClear() |
| 48 | + prevGitDir = process.env['GIT_DIR'] |
| 49 | + delete process.env['GIT_DIR'] |
| 50 | + }) |
| 51 | + |
| 52 | + afterEach(() => { |
| 53 | + if (prevGitDir === undefined) delete process.env['GIT_DIR'] |
| 54 | + else process.env['GIT_DIR'] = prevGitDir |
| 55 | + for (const dir of dirs.splice(0)) fs.rmSync(dir, { recursive: true, force: true }) |
| 56 | + }) |
| 57 | + |
| 58 | + function scratch(): string { |
| 59 | + const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'tg-manifest-gitprobe-')) |
| 60 | + dirs.push(dir) |
| 61 | + return dir |
| 62 | + } |
| 63 | + |
| 64 | + it('spawns no git for a directory outside every work tree, even when the cap engages', () => { |
| 65 | + const projectDir = scratch() |
| 66 | + const ancestor = gitAncestor(projectDir) |
| 67 | + expect(ancestor, `precondition: the temp directory ${os.tmpdir()} must sit outside every work tree, but ${String(ancestor)} holds a .git entry; point TMPDIR/TEMP at a folder outside it`).toBeNull() |
| 68 | + fs.writeFileSync(path.join(projectDir, 'package.json'), '{}') |
| 69 | + seedLongSession('no-worktree-session') |
| 70 | + |
| 71 | + const manifest = buildManifest('no-worktree-session', projectDir) |
| 72 | + |
| 73 | + expect(manifest, 'the cap must engage, or this asserts on a manifest that never consulted the probe').toContain('manifest truncated at ') |
| 74 | + expect(runGitMock).not.toHaveBeenCalled() |
| 75 | + }) |
| 76 | + |
| 77 | + it('still probes git when the directory itself is a work tree', () => { |
| 78 | + const projectDir = scratch() |
| 79 | + fs.mkdirSync(path.join(projectDir, '.git')) |
| 80 | + seedLongSession('worktree-session') |
| 81 | + |
| 82 | + const manifest = buildManifest('worktree-session', projectDir) |
| 83 | + |
| 84 | + expect(manifest).toContain('manifest truncated at ') |
| 85 | + const probes = runGitMock.mock.calls.map(([args, opts]) => [args[0], opts?.cwd]) |
| 86 | + expect(probes).toEqual([['diff', projectDir], ['status', projectDir]]) |
| 87 | + }) |
| 88 | + |
| 89 | + it('still probes git when a work tree is an ancestor of the directory', () => { |
| 90 | + const root = scratch() |
| 91 | + fs.writeFileSync(path.join(root, '.git'), 'gitdir: ../elsewhere') |
| 92 | + const projectDir = path.join(root, 'packages', 'app') |
| 93 | + fs.mkdirSync(projectDir, { recursive: true }) |
| 94 | + seedLongSession('nested-session') |
| 95 | + |
| 96 | + buildManifest('nested-session', projectDir) |
| 97 | + |
| 98 | + expect(runGitMock.mock.calls.map(([args]) => args[0])).toEqual(['diff', 'status']) |
| 99 | + }) |
| 100 | + |
| 101 | + it.skipIf(!CAN_JUNCTION)('still probes git when the directory is a junction into a work tree', () => { |
| 102 | + const repo = scratch() |
| 103 | + fs.mkdirSync(path.join(repo, '.git')) |
| 104 | + const target = path.join(repo, 'app') |
| 105 | + fs.mkdirSync(target) |
| 106 | + const outside = scratch() |
| 107 | + const link = path.join(outside, 'link') |
| 108 | + fs.symlinkSync(target, link, 'junction') |
| 109 | + expect(gitAncestor(outside), `precondition: ${outside} must sit outside every work tree, but ${String(gitAncestor(outside))} holds a .git entry`).toBeNull() |
| 110 | + seedLongSession('junction-session') |
| 111 | + |
| 112 | + try { |
| 113 | + buildManifest('junction-session', link) |
| 114 | + } finally { |
| 115 | + fs.rmSync(link, { recursive: true, force: true }) |
| 116 | + } |
| 117 | + |
| 118 | + expect(runGitMock.mock.calls.map(([args]) => args[0])).toEqual(['diff', 'status']) |
| 119 | + }) |
| 120 | + |
| 121 | + it('still probes git when GIT_DIR names the repository', () => { |
| 122 | + const projectDir = scratch() |
| 123 | + process.env['GIT_DIR'] = path.join(projectDir, 'elsewhere.git') |
| 124 | + seedLongSession('git-dir-session') |
| 125 | + |
| 126 | + buildManifest('git-dir-session', projectDir) |
| 127 | + |
| 128 | + expect(runGitMock.mock.calls.map(([args]) => args[0])).toEqual(['diff', 'status']) |
| 129 | + }) |
| 130 | +}) |
0 commit comments