repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/diff-review/__tests__/core.test.ts

Raw
File outline
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 { afterEach, describe, expect, it } from "vitest";
import { parseArgs } from "../args.js";
import { collectDiff, normalizeDiffHeaders } from "../git.js";
import { openBrowser } from "../open-browser.js";

const execFileAsync = promisify(execFile);
const tempDirs: string[] = [];
const GIT_LOCAL_ENV = [
	"GIT_ALTERNATE_OBJECT_DIRECTORIES",
	"GIT_CONFIG",
	"GIT_CONFIG_PARAMETERS",
	"GIT_CONFIG_COUNT",
	"GIT_OBJECT_DIRECTORY",
	"GIT_DIR",
	"GIT_WORK_TREE",
	"GIT_IMPLICIT_WORK_TREE",
	"GIT_GRAFT_FILE",
	"GIT_INDEX_FILE",
	"GIT_NO_REPLACE_OBJECTS",
	"GIT_REPLACE_REF_BASE",
	"GIT_PREFIX",
	"GIT_SHALLOW_FILE",
	"GIT_COMMON_DIR",
] as const;

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,
		env: cleanGitEnv(),
		maxBuffer: 20 * 1024 * 1024,
	});
}

function cleanGitEnv() {
	const env = { ...process.env };
	for (const key of GIT_LOCAL_ENV) delete env[key];
	return env;
}

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("does not spawn browser openers in isolated Vitest harness env", async () => {
		const oldNodeEnv = process.env.NODE_ENV;
		const oldVitest = process.env.VITEST;
		try {
			delete process.env.NODE_ENV;
			process.env.VITEST = "true";
			await expect(openBrowser("http://127.0.0.1:1/")).resolves.toEqual({
				ok: false,
				error: "Browser opener disabled",
			});
		} finally {
			if (oldNodeEnv === undefined) delete process.env.NODE_ENV;
			else process.env.NODE_ENV = oldNodeEnv;
			if (oldVitest === undefined) delete process.env.VITEST;
			else process.env.VITEST = oldVitest;
		}
	});

	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", () => {
		const diff = normalizeDiffHeaders(
			[
				String.raw`diff --git "a/a\tb.txt" "b/a\tb.txt"`,
				String.raw`--- "a/a\tb.txt"`,
				String.raw`+++ "b/a\tb.txt"`,
				"@@ -1 +1 @@",
				"-one",
				"+two",
				"",
			].join("\n"),
		);
		const files = parsePatchFiles(diff, "quoted-path", true).flatMap(
			(patch) => patch.files,
		);

		expect(files[0]?.name).toBe(String.raw`a\tb.txt`);
	});
});