repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/git-safe/__tests__/git-policy.test.ts

Raw
File outline
import { describe, expect, it } from "vitest";
import { applyGitPolicy, blockedGitCommandReason } from "../git-policy";

describe("git-safe agent Git policy", () => {
	it("blocks literal command-git no-verify bypasses", () => {
		for (const command of [
			"command git commit --no-verify",
			"command -p git push --no-verify",
			"  command -- git rebase --no-verify",
		]) {
			expect(blockedGitCommandReason(command)).toContain("blocked --no-verify");
		}
	});

	it("does not classify ordinary Git or quoted documentation as command bypasses", () => {
		for (const command of [
			"git commit --no-verify",
			"printf '%s' 'command git commit --no-verify'",
			"command git status",
			"command git commit -m 'Document --no-verify usage'",
			"command git merge --no-verify-signatures topic",
		]) {
			expect(blockedGitCommandReason(command)).toBeUndefined();
		}
	});

	it("routes Git, command, and env through the executable wrapper", () => {
		const runtime = { wrapperDir: "/tmp/git policy", realGit: "/usr/bin/git" };
		const wrapped = applyGitPolicy("git status", runtime);

		expect(wrapped).toContain("'/tmp/git policy'");
		expect(wrapped).toContain("'/tmp/git policy/git'");
		expect(wrapped).not.toContain("PI_GIT_SAFE_REAL_GIT");
		expect(applyGitPolicy(wrapped, runtime)).not.toBe(wrapped);
	});

	it("converts Windows paths for Git Bash", () => {
		const wrapped = applyGitPolicy("git status", {
			wrapperDir: "C:\\Users\\me\\AppData\\Local\\Temp\\git-safe",
			realGit: "C:\\Program Files\\Git\\cmd\\git.exe",
		});

		expect(wrapped).toContain(
			"export PATH='/c/Users/me/AppData/Local/Temp/git-safe':\"$PATH\"",
		);
		expect(wrapped).toContain(
			"'/c/Users/me/AppData/Local/Temp/git-safe/git' \"$@\"",
		);
	});

	it("does not trust an agent-supplied policy marker", () => {
		const command = "# pi-git-safe-policy-v1\ngit commit --no-verify";
		const wrapped = applyGitPolicy(command, {
			wrapperDir: "/tmp/wrapper",
			realGit: "/usr/bin/git",
		});

		expect(wrapped).not.toBe(command);
		expect(wrapped.endsWith(command)).toBe(true);
	});
});