|
| 1 | +import { execFileSync } from 'node:child_process'; |
| 2 | +import { mkdtempSync, mkdirSync, readFileSync, readdirSync, rmSync, lstatSync, symlinkSync, writeFileSync, renameSync, opendirSync } from 'node:fs'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import { join } from 'node:path'; |
| 5 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 6 | +import { createTaskClone } from '../git/clone.ts'; |
| 7 | + |
| 8 | +vi.mock('node:fs', async importOriginal => { |
| 9 | + const actual = await importOriginal<typeof import('node:fs')>(); |
| 10 | + return { ...actual, readdirSync: vi.fn(actual.readdirSync), opendirSync: vi.fn(actual.opendirSync) }; |
| 11 | +}); |
| 12 | +vi.mock('node:child_process', async importOriginal => { |
| 13 | + const actual = await importOriginal<typeof import('node:child_process')>(); |
| 14 | + return { ...actual, execFileSync: vi.fn(actual.execFileSync) }; |
| 15 | +}); |
| 16 | + |
| 17 | +const roots: string[] = []; |
| 18 | +const git = (cwd: string, ...args: string[]) => execFileSync('git', ['-c', 'core.hooksPath=/dev/null', ...args], |
| 19 | + { cwd, encoding: 'utf8', stdio: ['ignore', 'pipe', 'pipe'] }).trim(); |
| 20 | +function fixture() { |
| 21 | + const root = mkdtempSync(join(tmpdir(), 'clone-test-')); roots.push(root); |
| 22 | + const source = join(root, 'source'), parent = join(root, 'tasks'); |
| 23 | + mkdirSync(source); mkdirSync(parent); |
| 24 | + git(source, 'init'); git(source, 'config', 'user.name', 'Test'); git(source, 'config', 'user.email', 'test@example.com'); |
| 25 | + writeFileSync(join(source, 'file.txt'), 'trusted\n'); git(source, 'add', '.'); git(source, 'commit', '-m', 'baseline'); |
| 26 | + return { source, parent, head: git(source, 'rev-parse', 'HEAD'), taskId: 'task-1' }; |
| 27 | +} |
| 28 | +afterEach(() => { vi.restoreAllMocks(); vi.resetAllMocks(); for (const root of roots.splice(0)) rmSync(root, { recursive: true, force: true }); }); |
| 29 | +describe('isolated staging clone', () => { |
| 30 | + it('copies objects, ignores dirty source changes, and has no origin or shared metadata', () => { |
| 31 | + const input = fixture(); |
| 32 | + writeFileSync(join(input.source, 'file.txt'), 'uncommitted'); |
| 33 | + const clone = createTaskClone(input); |
| 34 | + expect(readFileSync(join(clone.directory, 'file.txt'), 'utf8')).toBe('trusted\n'); |
| 35 | + expect(lstatSync(join(clone.directory, '.git')).isDirectory()).toBe(true); |
| 36 | + expect(git(clone.directory, 'status', '--porcelain')).toBe(''); |
| 37 | + expect(git(clone.directory, 'remote')).toBe(''); |
| 38 | + const hash = git(input.source, 'rev-parse', 'HEAD:file.txt'); |
| 39 | + const path = join('objects', hash.slice(0, 2), hash.slice(2)); |
| 40 | + const sourceObject = join(input.source, '.git', path), cloneObject = join(clone.directory, '.git', path); |
| 41 | + const before = readFileSync(sourceObject); |
| 42 | + expect(lstatSync(cloneObject).nlink).toBe(1); |
| 43 | + expect(lstatSync(cloneObject).ino).not.toBe(lstatSync(sourceObject).ino); |
| 44 | + writeFileSync(cloneObject, 'corrupted disposable task object'); |
| 45 | + expect(readFileSync(sourceObject)).toEqual(before); |
| 46 | + expect(git(input.source, 'cat-file', '-p', hash)).toBe('trusted'); |
| 47 | + }); |
| 48 | + it('supports linked-worktree sources but produces standalone metadata', () => { |
| 49 | + const input = fixture(), linked = join(input.parent, 'linked'); |
| 50 | + git(input.source, 'worktree', 'add', '--detach', linked, input.head); |
| 51 | + const clone = createTaskClone({ ...input, source: linked }); |
| 52 | + expect(lstatSync(join(clone.directory, '.git')).isDirectory()).toBe(true); |
| 53 | + expect(git(clone.directory, 'rev-parse', 'HEAD')).toBe(input.head); |
| 54 | + }); |
| 55 | + it.each(['objects/info/alternates', 'objects/info/http-alternates', 'info/grafts', 'shallow'])('rejects %s before allocating a clone', name => { |
| 56 | + const input = fixture(); |
| 57 | + writeFileSync(join(input.source, '.git', name), ''); |
| 58 | + expect(() => createTaskClone(input)).toThrow('Unsupported Git storage'); |
| 59 | + expect(readdirSync(input.parent)).toEqual([]); |
| 60 | + }); |
| 61 | + it('rejects object symlinks and replacements', () => { |
| 62 | + const input = fixture(); |
| 63 | + symlinkSync('/tmp', join(input.source, '.git/objects/linked')); |
| 64 | + expect(() => createTaskClone(input)).toThrow('object entry'); |
| 65 | + rmSync(join(input.source, '.git/objects/linked')); |
| 66 | + git(input.source, 'update-ref', `refs/replace/${input.head}`, input.head); |
| 67 | + expect(() => createTaskClone(input)).toThrow('Replacement'); |
| 68 | + }); |
| 69 | + it('rejects nested storage, including paths through symlinks', () => { |
| 70 | + const input = fixture(), nested = join(input.source, 'tasks'), alias = join(input.parent, 'alias'); |
| 71 | + mkdirSync(nested); symlinkSync(nested, alias); |
| 72 | + expect(() => createTaskClone({ ...input, parent: alias })).toThrow('outside'); |
| 73 | + }); |
| 74 | + it('requires a full existing commit and finite time budget', () => { |
| 75 | + const input = fixture(); |
| 76 | + expect(() => createTaskClone({ ...input, head: 'HEAD' })).toThrow('full committed'); |
| 77 | + expect(() => createTaskClone({ ...input, head: 'f'.repeat(40) })).toThrow(); |
| 78 | + expect(() => createTaskClone({ ...input, timeoutMs: Infinity })).toThrow('deadline'); |
| 79 | + expect(readdirSync(input.parent)).toEqual([]); |
| 80 | + }); |
| 81 | + it('canonicalizes symlinked common metadata before checking storage containment', () => { |
| 82 | + const input = fixture(), metadata = join(input.parent, 'metadata'); |
| 83 | + renameSync(join(input.source, '.git'), metadata); |
| 84 | + symlinkSync(metadata, join(input.source, '.git')); |
| 85 | + const parent = join(metadata, 'tasks'); mkdirSync(parent); |
| 86 | + // Disputed intermediate state: Git reports a lexical path through the link. |
| 87 | + expect(git(input.source, 'rev-parse', '--git-common-dir')).toBe('.git'); |
| 88 | + expect(lstatSync(join(input.source, '.git')).isSymbolicLink()).toBe(true); |
| 89 | + expect(() => createTaskClone({ ...input, parent })).toThrow('outside source metadata'); |
| 90 | + expect(readdirSync(parent)).toEqual([]); |
| 91 | + }); |
| 92 | + it('never materializes an entire object directory before checking its entry budget', () => { |
| 93 | + const input = fixture(); |
| 94 | + // A bulk enumeration is forbidden even for a small fixture; this asserts |
| 95 | + // the disputed intermediate representation, not only a later limit error. |
| 96 | + vi.mocked(readdirSync).mockClear(); |
| 97 | + createTaskClone(input); |
| 98 | + expect(readdirSync).not.toHaveBeenCalled(); |
| 99 | + expect(opendirSync).toHaveBeenCalled(); |
| 100 | + }); |
| 101 | + it('rejects a successful final Git call that returns after the overall deadline', async () => { |
| 102 | + const input = fixture(); |
| 103 | + const actual = await vi.importActual<typeof import('node:child_process')>('node:child_process'); |
| 104 | + let elapsed = 0, lateResult = false; |
| 105 | + vi.spyOn(performance, 'now').mockImplementation(() => elapsed); |
| 106 | + vi.mocked(execFileSync).mockImplementation(((file: string, args: string[], options: object) => { |
| 107 | + const result = actual.execFileSync(file, args, options); |
| 108 | + if (args.at(-2) === 'rev-parse' && args.at(-1) === 'HEAD') { |
| 109 | + elapsed = 1001; lateResult = true; |
| 110 | + } |
| 111 | + return result; |
| 112 | + }) as typeof execFileSync); |
| 113 | + expect(() => createTaskClone({ ...input, timeoutMs: 1000 })).toThrow('deadline'); |
| 114 | + expect(lateResult).toBe(true); |
| 115 | + expect(readdirSync(input.parent)).toEqual([]); |
| 116 | + vi.mocked(execFileSync).mockImplementation(actual.execFileSync); |
| 117 | + }); |
| 118 | + it('does not inherit Git directory, index, configuration or object overrides', () => { |
| 119 | + const input = fixture(); |
| 120 | + const keys = ['GIT_DIR', 'GIT_INDEX_FILE', 'GIT_CONFIG_COUNT', 'GIT_CONFIG_KEY_0', 'GIT_CONFIG_VALUE_0']; |
| 121 | + const old = keys.map(key => process.env[key]); |
| 122 | + try { |
| 123 | + Object.assign(process.env, { GIT_DIR: '/missing', GIT_INDEX_FILE: '/missing', GIT_CONFIG_COUNT: '1', |
| 124 | + GIT_CONFIG_KEY_0: 'core.bare', GIT_CONFIG_VALUE_0: 'true' }); |
| 125 | + const clone = createTaskClone(input); |
| 126 | + expect(readFileSync(join(clone.directory, 'file.txt'), 'utf8')).toBe('trusted\n'); |
| 127 | + } finally { |
| 128 | + keys.forEach((key, i) => { if (old[i] === undefined) delete process.env[key]; else process.env[key] = old[i]; }); |
| 129 | + } |
| 130 | + }); |
| 131 | +}); |
0 commit comments