import { Database } from "bun:sqlite"; import { describe, expect, it } from "bun:test"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { getCopilotSessionsStreaming } from "./copilot"; function createCopilotDb(dbPath: string, sessionId: string): void { const db = new Database(dbPath); try { db.run("CREATE TABLE ItemTable (key TEXT PRIMARY KEY, value TEXT)"); db.run("INSERT INTO ItemTable(key, value) VALUES (?, ?)", [ "interactive.sessions", JSON.stringify([ { sessionId, requests: [ { message: { text: sessionId }, response: [{ value: `reply-${sessionId}` }], timestamp: Date.UTC(2026, 1, 9, 12), }, ], }, ]), ]); } finally { db.close(); } } describe("copilot ingestion", () => { it("filters default-location sessions by workspace path and keeps unresolved paths", async () => { const storageRoot = await mkdtemp( join(tmpdir(), "scribe-copilot-filter-test-"), ); const fixtures = [ { id: "inside", path: "/Users/test/code/project" }, { id: "outside", path: "/Users/test/other/project" }, { id: "unresolved", path: undefined }, ]; try { for (const fixture of fixtures) { const hashDir = join(storageRoot, fixture.id); await mkdir(hashDir, { recursive: true }); if (fixture.path) { await writeFile( join(hashDir, "workspace.json"), JSON.stringify({ folder: `file://${fixture.path}` }), "utf8", ); } createCopilotDb(join(hashDir, "state.vscdb"), fixture.id); } const filtered = []; for await (const session of getCopilotSessionsStreaming( ["/Users/test/code"], undefined, { dataRoots: [storageRoot] }, )) { filtered.push(session.externalThreadId); } expect(filtered.sort()).toEqual(["inside", "unresolved"]); const unfiltered = []; for await (const session of getCopilotSessionsStreaming([], undefined, { dataRoots: [storageRoot], })) { unfiltered.push(session.externalThreadId); } expect(unfiltered.sort()).toEqual(["inside", "outside", "unresolved"]); } finally { await rm(storageRoot, { recursive: true, force: true }); } }); });