|
| 1 | +import { afterEach, beforeEach, describe, expect, it, spyOn } from "bun:test"; |
| 2 | +import { mkdirSync, mkdtempSync, rmSync } from "node:fs"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join } from "node:path"; |
| 5 | + |
| 6 | +import { resolveCodemapConfig } from "../config"; |
| 7 | +import { closeDb, createTables, openDb, setMeta } from "../db"; |
| 8 | +import { initCodemap } from "../runtime"; |
| 9 | +import { buildContextEnvelope } from "./context-engine"; |
| 10 | +import * as indexEngine from "./index-engine"; |
| 11 | +import { computeIndexFreshness } from "./index-freshness"; |
| 12 | +import { _resetWatchStateForTests, runWatchLoop } from "./watcher"; |
| 13 | +import type { WatchBackend } from "./watcher"; |
| 14 | + |
| 15 | +let benchDir: string; |
| 16 | + |
| 17 | +beforeEach(() => { |
| 18 | + benchDir = mkdtempSync(join(tmpdir(), "index-freshness-")); |
| 19 | + mkdirSync(join(benchDir, ".codemap"), { recursive: true }); |
| 20 | + initCodemap(resolveCodemapConfig(benchDir, undefined)); |
| 21 | + _resetWatchStateForTests(); |
| 22 | +}); |
| 23 | + |
| 24 | +afterEach(() => { |
| 25 | + rmSync(benchDir, { recursive: true, force: true }); |
| 26 | + _resetWatchStateForTests(); |
| 27 | +}); |
| 28 | + |
| 29 | +function withEmptyDb<T>(fn: (db: ReturnType<typeof openDb>) => T): T { |
| 30 | + const db = openDb(); |
| 31 | + try { |
| 32 | + createTables(db); |
| 33 | + return fn(db); |
| 34 | + } finally { |
| 35 | + closeDb(db); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +function fakeBackend(): WatchBackend & { |
| 40 | + fire: (kind: "add" | "change" | "unlink", abs: string) => void; |
| 41 | +} { |
| 42 | + let onEvent: |
| 43 | + | ((k: "add" | "change" | "unlink", p: string) => void) |
| 44 | + | undefined; |
| 45 | + return { |
| 46 | + start(opts) { |
| 47 | + onEvent = opts.onEvent; |
| 48 | + }, |
| 49 | + async stop() {}, |
| 50 | + fire(kind, abs) { |
| 51 | + onEvent?.(kind, abs); |
| 52 | + }, |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +describe("computeIndexFreshness", () => { |
| 57 | + it("reports no warning when HEAD matches last_indexed_commit", () => { |
| 58 | + const head = "abc123def456789012345678901234567890abcd"; |
| 59 | + const revParse = spyOn(indexEngine, "getCurrentCommit").mockReturnValue( |
| 60 | + head, |
| 61 | + ); |
| 62 | + |
| 63 | + try { |
| 64 | + withEmptyDb((db) => { |
| 65 | + setMeta(db, "last_indexed_commit", head); |
| 66 | + const f = computeIndexFreshness(db); |
| 67 | + expect(f.commit_drift).toBe(false); |
| 68 | + expect(f.warning).toBeNull(); |
| 69 | + expect(f.head_commit).toBe(head); |
| 70 | + }); |
| 71 | + } finally { |
| 72 | + revParse.mockRestore(); |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + it("warns on commit drift", () => { |
| 77 | + const revParse = spyOn(indexEngine, "getCurrentCommit").mockReturnValue( |
| 78 | + "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", |
| 79 | + ); |
| 80 | + |
| 81 | + try { |
| 82 | + withEmptyDb((db) => { |
| 83 | + setMeta( |
| 84 | + db, |
| 85 | + "last_indexed_commit", |
| 86 | + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", |
| 87 | + ); |
| 88 | + const f = computeIndexFreshness(db); |
| 89 | + expect(f.commit_drift).toBe(true); |
| 90 | + expect(f.warning).toContain("HEAD is bbbbbbb"); |
| 91 | + }); |
| 92 | + } finally { |
| 93 | + revParse.mockRestore(); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + it("reports pending_sync when the watcher debouncer has queued paths", async () => { |
| 98 | + spyOn(indexEngine, "getCurrentCommit").mockReturnValue(""); |
| 99 | + mkdirSync(join(benchDir, "src"), { recursive: true }); |
| 100 | + const backend = fakeBackend(); |
| 101 | + const handle = runWatchLoop({ |
| 102 | + root: benchDir, |
| 103 | + excludeDirNames: new Set(["node_modules", ".git", "dist"]), |
| 104 | + onChange: () => {}, |
| 105 | + debounceMs: 60_000, |
| 106 | + backend, |
| 107 | + }); |
| 108 | + |
| 109 | + backend.fire("change", join(benchDir, "src/a.ts")); |
| 110 | + |
| 111 | + withEmptyDb((db) => { |
| 112 | + const f = computeIndexFreshness(db); |
| 113 | + expect(f.pending_sync).toBe(true); |
| 114 | + expect(f.pending_paths).toBe(1); |
| 115 | + expect(f.warning).toContain("pending"); |
| 116 | + }); |
| 117 | + |
| 118 | + await handle.stop(); |
| 119 | + }); |
| 120 | +}); |
| 121 | + |
| 122 | +describe("buildContextEnvelope", () => { |
| 123 | + it("includes index_freshness with disk drift opt-in", () => { |
| 124 | + const head = "cccccccccccccccccccccccccccccccccccccccc"; |
| 125 | + const revParse = spyOn(indexEngine, "getCurrentCommit").mockReturnValue( |
| 126 | + head, |
| 127 | + ); |
| 128 | + const changedFiles = spyOn(indexEngine, "getChangedFiles").mockReturnValue({ |
| 129 | + changed: [], |
| 130 | + deleted: [], |
| 131 | + existingPaths: new Set(), |
| 132 | + sourceCache: new Map(), |
| 133 | + existingHashes: new Map(), |
| 134 | + }); |
| 135 | + |
| 136 | + try { |
| 137 | + withEmptyDb((db) => { |
| 138 | + setMeta(db, "last_indexed_commit", head); |
| 139 | + const envelope = buildContextEnvelope(db, benchDir, { |
| 140 | + compact: true, |
| 141 | + intent: null, |
| 142 | + }); |
| 143 | + expect(envelope.index_freshness.head_commit).toBe(head); |
| 144 | + expect(envelope.index_freshness.disk_ahead_of_index).toBe(false); |
| 145 | + expect(envelope.index_freshness.warning).toBeNull(); |
| 146 | + }); |
| 147 | + } finally { |
| 148 | + revParse.mockRestore(); |
| 149 | + changedFiles.mockRestore(); |
| 150 | + } |
| 151 | + }); |
| 152 | +}); |
0 commit comments