import { describe, it } from "node:test"; import { expect } from "expect"; import { runCheck } from "../../src/checks/check.js"; import { writeTruthmarkConfig } from "../helpers/truthmark-config.js"; import { runInit } from "../../src/init/init.js"; import { createWorktreeRepo } from "../helpers/worktree-repo.js"; describe("branch-scoped truth integration", () => { it("reports branch name and head sha for a normal branch checkout", async () => { const repo = await createWorktreeRepo(); try { await writeTruthmarkConfig(repo.rootDir); await runInit(repo.rootDir); await repo.runGit(["add", "."]); await repo.runGit(["commit", "-m", "test: baseline"]); const result = await runCheck(repo.rootDir); const branchScope = result.data?.branchScope as | { branchName: string | null; headSha: string | null; } | undefined; expect(branchScope?.branchName).toBe("main"); expect(branchScope?.headSha).toMatch(/^[0-9a-f]{40}$/); } finally { await repo.cleanup(); } }); it("reports detached checkout identity cleanly", async () => { const repo = await createWorktreeRepo(); try { await writeTruthmarkConfig(repo.rootDir); await runInit(repo.rootDir); await repo.runGit(["add", "."]); await repo.runGit(["commit", "-m", "test: baseline"]); const headSha = (await repo.runGit(["rev-parse", "HEAD"])).stdout.trim(); await repo.runGit(["checkout", "--detach"]); const result = await runCheck(repo.rootDir); const branchScope = result.data?.branchScope as | { identity: string; branchName: string | null; } | undefined; expect(branchScope?.branchName).toBeNull(); expect(branchScope?.identity).toBe(`detached:${headSha}`); } finally { await repo.cleanup(); } }); it("reports the active worktree path without leaking the primary checkout context", async () => { const repo = await createWorktreeRepo(); try { await writeTruthmarkConfig(repo.rootDir); await runInit(repo.rootDir); await repo.runGit(["add", "."]); await repo.runGit(["commit", "-m", "test: baseline"]); const secondary = await repo.addWorktree("feature/worktree"); const result = await runCheck(secondary.rootDir); const branchScope = result.data?.branchScope as | { branchName: string | null; worktreePath: string; } | undefined; expect(branchScope?.branchName).toBe("feature/worktree"); expect(branchScope?.worktreePath).toBe(secondary.rootDir); expect(branchScope?.worktreePath).not.toBe(repo.rootDir); } finally { await repo.cleanup(); } }); it("keeps truth surface changes isolated to the checked out branch", async () => { const repo = await createWorktreeRepo(); try { await writeTruthmarkConfig(repo.rootDir); await runInit(repo.rootDir); await repo.runGit(["add", "."]); await repo.runGit(["commit", "-m", "test: baseline"]); const secondary = await repo.addWorktree("feature/docs"); await secondary.writeFile( "docs/truthmark/routes/areas.md", `${await secondary.readFile("docs/truthmark/routes/areas.md")}\n## Feature Branch Notes\nOnly here.\n`, ); const primaryResult = await runCheck(repo.rootDir); const secondaryResult = await runCheck(secondary.rootDir); const primaryHash = ( primaryResult.data?.branchScope as { relevantFileHashes: Record; } ).relevantFileHashes["docs/truthmark/routes/areas.md"]; const secondaryHash = ( secondaryResult.data?.branchScope as { relevantFileHashes: Record; } ).relevantFileHashes["docs/truthmark/routes/areas.md"]; expect(primaryHash).toBeTruthy(); expect(secondaryHash).toBeTruthy(); expect(primaryHash).not.toBe(secondaryHash); expect( await repo.readFile("docs/truthmark/routes/areas.md"), ).not.toContain("Feature Branch Notes"); expect( await secondary.readFile("docs/truthmark/routes/areas.md"), ).toContain("Feature Branch Notes"); } finally { await repo.cleanup(); } }); });