@@ -0,0 +1,67 @@
1+import { afterEach, beforeEach, describe, expect, test } from "bun:test";
2+import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
3+import { tmpdir } from "node:os";
4+import { join } from "node:path";
5+
6+const messageRepoModuleUrl = new URL("./message-repo.ts", import.meta.url).href;
7+const connectionModuleUrl = new URL("./connection.ts", import.meta.url).href;
8+
9+function runInHome(
10+ home: string,
11+ script: string,
12+): {
13+ exitCode: number;
14+ stdout: string;
15+ stderr: string;
16+} {
17+ const scriptPath = join(
18+ tmpdir(),
19+ `mc-message-repo-test-${process.pid}-${Date.now()}.mjs`,
20+ );
21+ writeFileSync(scriptPath, script);
22+ try {
23+ const result = Bun.spawnSync([process.execPath, scriptPath], {
24+ env: { ...process.env, HOME: home },
25+ stdout: "pipe",
26+ stderr: "pipe",
27+ });
28+ return {
29+ exitCode: result.exitCode,
30+ stdout: result.stdout.toString(),
31+ stderr: result.stderr.toString(),
32+ };
33+ } finally {
34+ rmSync(scriptPath, { force: true });
35+ }
36+}
37+
38+describe("loadMessages", () => {
39+ let fakeHome = "";
40+
41+ beforeEach(() => {
42+ fakeHome = mkdtempSync(join(tmpdir(), "mc-message-home-"));
43+ });
44+
45+ afterEach(() => {
46+ rmSync(fakeHome, { recursive: true, force: true });
47+ });
48+
49+ test("skips invalid message rows without writing CLI output", () => {
50+ const result = runInHome(
51+ fakeHome,
52+ `
53+ import { loadMessages } from ${JSON.stringify(messageRepoModuleUrl)};
54+ import { getDb } from ${JSON.stringify(connectionModuleUrl)};
55+ const db = getDb();
56+ db.run("INSERT INTO sessions (id, cwd, model, created_at, updated_at) VALUES (?, ?, ?, ?, ?)", ["s1", "/tmp", "zen/gpt-5.4", 1, 1]);
57+ db.run("INSERT INTO messages (session_id, payload, turn_index, created_at) VALUES (?, ?, ?, ?)", ["s1", "{not-json", 0, 1]);
58+ const messages = loadMessages("s1");
59+ console.log(JSON.stringify({ count: messages.length }));
60+ `,
61+ );
62+
63+ expect(result.exitCode).toBe(0);
64+ expect(result.stderr).toBe("");
65+ expect(result.stdout.trim()).toBe('{"count":0}');
66+ });
67+});