droboticflow-new/tests/frontend/git-hooks.test.mjs

41 lines
2.2 KiB
JavaScript

import assert from "node:assert/strict";
import test from "node:test";
import { mkdtempSync, mkdirSync, writeFileSync, symlinkSync, rmSync, existsSync } from "node:fs";
import { tmpdir } from "node:os";
import { join, resolve } from "node:path";
import { execFileSync } from "node:child_process";
import { ensureGitHooks } from "../../scripts/lib/git-hooks.mjs";
test("verification bootstrap repairs hooks in a new worktree sharing dependencies", (t) => {
const dir = mkdtempSync(join(tmpdir(), "hooks-worktree-"));
t.after(() => rmSync(dir, { recursive: true, force: true }));
const main = join(dir, "main"),
worktree = join(dir, "task");
mkdirSync(main);
const git = (cwd, ...args) => execFileSync("git", args, { cwd, stdio: "pipe" });
git(main, "init");
mkdirSync(join(main, ".husky"));
writeFileSync(join(main, ".husky/pre-commit"), "printf ran > hook-ran\n");
git(main, "add", ".husky/pre-commit");
git(main, "-c", "user.name=Test", "-c", "user.email=test@example.invalid", "commit", "-m", "fixture");
git(main, "worktree", "add", "-b", "task", worktree);
symlinkSync(resolve("node_modules"), join(worktree, "node_modules"));
const env = { ...process.env, CI: "", HUSKY: "1" };
assert.throws(() => ensureGitHooks(worktree, { env }));
assert.equal(ensureGitHooks(worktree, { install: true, env }), "active");
assert.ok(existsSync(join(worktree, ".husky/_/pre-commit")));
assert.equal(ensureGitHooks(worktree, { env }), "active");
writeFileSync(join(worktree, "verified"), "fixture");
git(worktree, "add", "verified");
git(worktree, "-c", "user.name=Test", "-c", "user.email=test@example.invalid", "commit", "-m", "verify hook");
assert.ok(existsSync(join(worktree, "hook-ran")), "commit must actually invoke the hook");
git(worktree, "config", "core.hooksPath", "/custom/user-hooks");
assert.throws(() => ensureGitHooks(worktree, { install: true, env }), /Custom hooksPath/);
assert.equal(git(worktree, "config", "--get", "core.hooksPath").toString().trim(), "/custom/user-hooks");
});
test("explicit hook opt-out and exported sources remain supported", () => {
assert.equal(ensureGitHooks("/missing/export", { env: {} }), "skipped");
assert.equal(ensureGitHooks(process.cwd(), { install: true, env: { HUSKY: "0" } }), "skipped");
});