Skip to content

Use Garry Tan's exact Claude Code setup: 23 opinionated tools that serve as CEO, Designer, Eng Manager, Release Manager, Doc Engineer, and QA

TypeScript
5.4 KB
Raw Blame
/**
 * HARN-108: inside a linked worktree `.git` is a file (`gitdir: …`), so every gstack file kept under the git directory must
 * come from `git rev-parse --git-path`; the literal `.git/<name>` fallback used when git failed or timed out wrote through
 * that file and died with ENOTDIR (gstack-review-plan's consumer scan on `.git/gstack-consumers`).
 */
import { afterEach, describe, expect, test } from 'bun:test';
import { spawnSync } from 'node:child_process';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import { gitPath } from '../lib/git-path';
import { collectLive } from '../lib/review-collect';
import { readMechanicalLines, writeMechanicalRecord } from '../lib/ship-mechanical';
import { patchId } from '../lib/ship-receipt-patch';

const dirs: string[] = [];
const savedPath = process.env.PATH;
const savedScan = process.env.GSTACK_CONSUMER_SCAN_TIMEOUT_MS;
afterEach(() => {
  process.env.PATH = savedPath;
  if (savedScan === undefined) delete process.env.GSTACK_CONSUMER_SCAN_TIMEOUT_MS; else process.env.GSTACK_CONSUMER_SCAN_TIMEOUT_MS = savedScan;
  while (dirs.length) fs.rmSync(dirs.pop()!, { recursive: true, force: true });
});

function worktree() {
  const root = fs.mkdtempSync(path.join(os.tmpdir(), 'git-path-'));
  dirs.push(root);
  const main = path.join(root, 'main'), wt = path.join(root, 'wt');
  fs.mkdirSync(main);
  const git = (cwd: string, ...a: string[]) => spawnSync('git', a, { cwd, encoding: 'utf8', timeout: 30_000 });
  git(main, 'init', '-q', '-b', 'main'); git(main, 'config', 'user.email', 't@t'); git(main, 'config', 'user.name', 't'); git(main, 'config', 'commit.gpgsign', 'false');
  fs.writeFileSync(path.join(main, 'README.md'), '# x\n'); git(main, 'add', '.'); git(main, 'commit', '-qm', 'base');
  expect(git(main, 'worktree', 'add', '-q', '-b', 'feat', wt).status).toBe(0);
  fs.mkdirSync(path.join(wt, 'src')); fs.writeFileSync(path.join(wt, 'src', 'a.ts'), 'export const a = 1;\n');
  git(wt, 'add', '.'); git(wt, 'commit', '-qm', 'feat');
  return { main, wt, git };
}
/** A PATH whose git fails the calls whose arguments match the shell pattern `only` (every call by default), as a hung or killed git looks to the callers. */
function brokenGit(only = '*'): void {
  const real = path.join(spawnSync('git', ['--exec-path'], { encoding: 'utf8', timeout: 10_000 }).stdout.trim(), 'git');
  const bin = fs.mkdtempSync(path.join(os.tmpdir(), 'git-path-bin-'));
  dirs.push(bin);
  fs.writeFileSync(path.join(bin, 'git'), `#!/bin/sh\ncase "$*" in ${only}) echo "fatal: simulated failure" >&2; exit 128;; esac\nexec '${real}' "$@"\n`, { mode: 0o755 });
  process.env.PATH = `${bin}${path.delimiter}${savedPath}`;
}

// Value: protects=gstack files under the git dir land in the worktree's own git dir; fails_when=a caller writes through the
//   .git file (ENOTDIR) or guesses .git/<name> after git fails; seam=lib/git-path.ts and its callers in a real git worktree
describe('git-dir paths in a linked worktree (HARN-108)', () => {
  test('a real worktree: the paths resolve under its gitdir and the writers succeed', () => {
    const { wt, main, git } = worktree();
    expect(fs.statSync(path.join(wt, '.git')).isFile()).toBe(true);
    const at = gitPath(wt, 'gstack-consumers');
    expect(at.ok).toBe(true);
    if (!at.ok) return;
    expect(at.path.startsWith(path.join(fs.realpathSync(main), '.git')) || at.path.startsWith(path.join(main, '.git'))).toBe(true);
    fs.mkdirSync(at.path, { recursive: true });
    const base = git(wt, 'rev-parse', 'main').stdout.trim(), head = git(wt, 'rev-parse', 'HEAD').stdout.trim();
    expect(typeof patchId(wt, base, head, ['.'])).toBe('string');
    const tree = git(wt, 'rev-parse', 'HEAD^{tree}').stdout.trim();
    expect(writeMechanicalRecord(wt, tree, { 'src/a.ts': 'golden, regenerated by x' })).toBeNull();
    expect(readMechanicalLines(wt, tree)).toEqual(['Mechanical retake: src/a.ts (golden, regenerated by x)']);
  });

  test('git failing: every writer names the failure and nothing is written through the .git file', () => {
    const { wt, git } = worktree();
    const base = git(wt, 'rev-parse', 'main').stdout.trim(), head = git(wt, 'rev-parse', 'HEAD').stdout.trim();
    const tree = git(wt, 'rev-parse', 'HEAD^{tree}').stdout.trim();
    brokenGit();
    const at = gitPath(wt, 'gstack-patch-id');
    expect(at).toEqual({ ok: false, error: expect.stringContaining('gstack does not guess .git/gstack-patch-id') });
    const id = patchId(wt, base, head, ['.']);
    expect(id).toEqual({ error: expect.stringContaining('git rev-parse --git-path gstack-patch-id gave no path') });
    expect(JSON.stringify(id)).not.toContain('ENOTDIR');
    expect(writeMechanicalRecord(wt, tree, { 'src/a.ts': 'x' })).toContain('gave no path');
    expect(fs.statSync(path.join(wt, '.git')).isFile()).toBe(true);
  });

  test('the consumer scan in a worktree whose git dir lookup fails names it, not ENOTDIR on .git/gstack-consumers', () => {
    const { wt, git } = worktree();
    fs.mkdirSync(path.join(wt, 'data')); fs.writeFileSync(path.join(wt, 'data', 'rows.json'), '[1]\n');
    fs.writeFileSync(path.join(wt, 'src', 'read.ts'), "export const f = 'data/rows.json';\n");
    git(wt, 'add', '.'); git(wt, 'commit', '-qm', 'data');
    brokenGit('*"--git-path gstack-consumers"*');
    const errors = JSON.stringify(collectLive(wt, 'main').errors);
    expect(errors).toContain('git rev-parse --git-path gstack-consumers gave no path');
    expect(errors).not.toContain('ENOTDIR');
  });
});