repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by bugabinga
extensions/diff-review/__tests__/core.test.ts
RawFile outline
- execFileAsync line 12
- tempDirs line 13
- afterEach line 15
- tempRepo line 21
- git line 30
- diff-review core regressions line 34
- rejects option-shaped base refs before invoking git line 35
- does not spawn browser openers during tests line 41
- preserves staged rename metadata when collecting per-file diffs line 48
- rejects binary-only diffs instead of launching an empty review line 69
- normalizes quoted git paths into Pierre-parseable diff headers line 81
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`);
});
});