import { access, appendFile, readFile } from "node:fs/promises"; import { describe, expect, it } from "vitest"; import type { RecordChunk } from "../core"; import { CanonicalRecordLog } from "../record-log"; function record(chunk: number, of: number): RecordChunk { return { chunk, of, atoms: [ { key: `chunk-${chunk}`, text: `fact ${chunk}`, kind: "state", status: "active", }, ], }; } describe("canonical classifier JSONL", () => { it("tails coalesced appends, writes canonical private records, and removes them", async () => { const seen: RecordChunk[] = []; const log = await CanonicalRecordLog.create((item) => seen.push(item)); const directory = log.directory; await log.append(record(1, 2)); await log.append(record(2, 2)); await log.drain(); expect(log.records).toEqual([record(1, 2), record(2, 2)]); expect(seen).toEqual(log.records); expect(await readFile(log.path, "utf8")).toBe( `${JSON.stringify(record(1, 2))}\n${JSON.stringify(record(2, 2))}\n`, ); if (process.platform !== "win32") expect(await log.mode()).toBe(0o600); await log.close(); await expect(access(directory)).rejects.toThrow(); }); it("preserves UTF-8 when filesystem notifications split a multibyte record", async () => { const seen: RecordChunk[] = []; const log = await CanonicalRecordLog.create((item) => seen.push(item)); try { const unicode = record(1, 1); unicode.atoms[0].text = "präzise"; const line = Buffer.from(`${JSON.stringify(unicode)}\n`, "utf8"); const marker = Buffer.from("ä", "utf8"); const split = line.indexOf(marker) + 1; await appendFile(log.path, line.subarray(0, split)); await new Promise((resolve) => setTimeout(resolve, 20)); await appendFile(log.path, line.subarray(split)); await log.drain(); expect(log.records).toEqual([unicode]); expect(seen).toEqual([unicode]); } finally { await log.close(); } }); it("rejects externally appended invalid records", async () => { const log = await CanonicalRecordLog.create(() => {}); try { await appendFile(log.path, '{"invalid":true}\n', "utf8"); await expect(log.drain()).rejects.toThrow( "record log contains an invalid record", ); } finally { await log.close(); } }); it("rejects an incomplete final JSONL record", async () => { const log = await CanonicalRecordLog.create(() => {}); try { await appendFile(log.path, '{"chunk":', "utf8"); await expect(log.drain()).rejects.toThrow("incomplete JSONL record"); } finally { await log.close(); } }); });