|
1 | | -import { describe, expect, it } from "bun:test"; |
| 1 | +import { describe, expect, it, spyOn } from "bun:test"; |
| 2 | +import { mkdirSync, mkdtempSync, writeFileSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
2 | 5 |
|
| 6 | +import { resolveCodemapConfig } from "../config"; |
3 | 7 | import { createTables, insertFile, upsertQueryBaseline } from "../db"; |
4 | 8 | import type { CodemapDatabase } from "../db"; |
5 | 9 | import { diffRows } from "../diff-rows"; |
| 10 | +import { configureResolver } from "../resolver"; |
| 11 | +import { getProjectRoot, initCodemap } from "../runtime"; |
6 | 12 | import { openCodemapDatabase } from "../sqlite-db"; |
| 13 | +import { installCodemapTestTeardown } from "../test-helpers/runtime-reset"; |
7 | 14 | import { |
8 | 15 | buildFindingKeySet, |
9 | 16 | collapseAuditEnvelopeForSummary, |
10 | 17 | computeDelta, |
11 | 18 | findingKey, |
| 19 | + makeWorktreeReindex, |
12 | 20 | runAudit, |
13 | 21 | tagAddedWithAttribution, |
14 | 22 | V1_DELTAS, |
15 | 23 | } from "./audit-engine"; |
16 | 24 | import type { AuditEnvelope } from "./audit-engine"; |
| 25 | +import * as runIndex from "./run-index"; |
| 26 | +import type { IndexResult } from "./types"; |
| 27 | + |
| 28 | +installCodemapTestTeardown(); |
17 | 29 |
|
18 | 30 | function freshDb(): CodemapDatabase { |
19 | 31 | const db = openCodemapDatabase(":memory:"); |
@@ -488,3 +500,34 @@ describe("computeDelta — files", () => { |
488 | 500 | } |
489 | 501 | }); |
490 | 502 | }); |
| 503 | + |
| 504 | +describe("makeWorktreeReindex", () => { |
| 505 | + it("swaps roots under runtime bracket and restores live config", async () => { |
| 506 | + const liveRoot = mkdtempSync(join(tmpdir(), "audit-reindex-live-")); |
| 507 | + const wtRoot = mkdtempSync(join(tmpdir(), "audit-reindex-wt-")); |
| 508 | + writeFileSync(join(liveRoot, "package.json"), "{}"); |
| 509 | + writeFileSync(join(wtRoot, "package.json"), "{}"); |
| 510 | + mkdirSync(join(wtRoot, ".codemap"), { recursive: true }); |
| 511 | + |
| 512 | + initCodemap(resolveCodemapConfig(liveRoot, {})); |
| 513 | + configureResolver(liveRoot, null); |
| 514 | + |
| 515 | + const spy = spyOn(runIndex, "runCodemapIndex").mockResolvedValue({ |
| 516 | + mode: "full", |
| 517 | + indexed: 0, |
| 518 | + skipped: 0, |
| 519 | + elapsedMs: 0, |
| 520 | + stats: { files: 0 }, |
| 521 | + } as IndexResult); |
| 522 | + try { |
| 523 | + await makeWorktreeReindex()(wtRoot); |
| 524 | + expect(getProjectRoot()).toBe(liveRoot); |
| 525 | + expect(() => |
| 526 | + initCodemap(resolveCodemapConfig("/other-root", {})), |
| 527 | + ).toThrow(/cannot switch project root/); |
| 528 | + expect(spy).toHaveBeenCalled(); |
| 529 | + } finally { |
| 530 | + spy.mockRestore(); |
| 531 | + } |
| 532 | + }); |
| 533 | +}); |
0 commit comments