import { afterEach, describe, expect, it } from "bun:test"; import { execFile } from "node:child_process"; import { mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { promisify } from "node:util"; import { parsePatchFiles } from "@pierre/diffs"; import { parseArgs } from "../args.js"; import { collectDiff } from "../git.js"; import { openBrowser } from "../open-browser.js"; const execFileAsync = promisify(execFile); const tempDirs: string[] = []; afterEach(async () => { await Promise.all( tempDirs.splice(0).map((dir) => rm(dir, { recursive: true, force: true })), ); }); async function tempRepo() { const repo = await mkdtemp(join(tmpdir(), "diff-review-core-")); tempDirs.push(repo); await git(repo, ["init", "-q"]); await git(repo, ["config", "user.email", "review@example.invalid"]); await git(repo, ["config", "user.name", "Review"]); return repo; } async function git(cwd: string, args: string[]) { await execFileAsync("git", args, { cwd, maxBuffer: 20 * 1024 * 1024 }); } describe("diff-review core regressions", () => { it("rejects option-shaped base refs before invoking git", () => { expect(() => parseArgs("--base=--output=/tmp/diff-review-pwn")).toThrow( "--base must be a git revision, not an option", ); }); it("does not spawn browser openers during tests", async () => { await expect(openBrowser("http://127.0.0.1:1/")).resolves.toEqual({ ok: false, error: "Browser opener disabled", }); }); it("preserves staged rename metadata when collecting per-file diffs", async () => { const repo = await tempRepo(); await writeFile(join(repo, "old.txt"), "one\n"); await git(repo, ["add", "old.txt"]); await git(repo, ["commit", "-qm", "initial"]); await git(repo, ["mv", "old.txt", "new.txt"]); const diff = await collectDiff(repo, parseArgs("--staged")); expect(diff.diffText).toContain("diff --git a/old.txt b/new.txt"); expect(diff.files).toEqual([ { path: "new.txt", prevPath: "old.txt", status: "renamed", additions: 0, deletions: 0, }, ]); }); it("rejects binary-only diffs instead of launching an empty review", async () => { const repo = await tempRepo(); await writeFile(join(repo, "bin.dat"), Buffer.from([0, 1, 2])); await git(repo, ["add", "bin.dat"]); await git(repo, ["commit", "-qm", "initial"]); await writeFile(join(repo, "bin.dat"), Buffer.from([3, 4, 5])); await expect(collectDiff(repo, parseArgs(""))).rejects.toThrow( "No renderable diff; skipped oversized/binary files: bin.dat", ); }); it("normalizes quoted git paths into Pierre-parseable diff headers", async () => { const repo = await tempRepo(); const path = "a\tb.txt"; await writeFile(join(repo, path), "one\n"); await git(repo, ["add", path]); await git(repo, ["commit", "-qm", "initial"]); await writeFile(join(repo, path), "two\n"); const diff = await collectDiff(repo, parseArgs("")); const files = parsePatchFiles(diff.diffText, "quoted-path", true).flatMap( (patch) => patch.files, ); expect(diff.files[0]?.path).toBe(String.raw`a\tb.txt`); expect(files[0]?.name).toBe(String.raw`a\tb.txt`); }); });