import { describe, it, expect, afterEach, beforeEach } from "vitest"; import { mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { DatabaseSync } from "node:sqlite"; import { MailboxStore, RenameError } from "../src/db.js"; function backdate(dbPath: string, name: string, minutesAgo: number): void { const db = new DatabaseSync(dbPath); const iso = new Date(Date.now() - minutesAgo * 60_000).toISOString(); db.prepare("UPDATE mailboxes SET last_seen_at = ? WHERE name = ?").run(iso, name); db.close(); } let dir: string; let dbPath: string; beforeEach(() => { dir = mkdtempSync(join(tmpdir(), "claude-mailbox-test-")); dbPath = join(dir, "test.db"); }); afterEach(() => { rmSync(dir, { recursive: true, force: true }); }); describe("schema", () => { it("creates fresh tables and is idempotent on re-open", () => { const a = new MailboxStore(dbPath); a.upsertMailbox("alice"); a.close(); const b = new MailboxStore(dbPath); const list = b.listMailboxes(); expect(list.map((m) => m.name)).toEqual(["alice"]); b.close(); }); }); describe("send / peek / check round-trip", () => { it("delivers a message exactly once", () => { const store = new MailboxStore(dbPath); try { const result = store.send("alice", "bob", "hello bob"); expect(result.id).toBeGreaterThan(0); const peek1 = store.peek("bob"); expect(peek1.pending).toBe(1); expect(peek1.oldestAt).toBeInstanceOf(Date); const pulled = store.checkInbox("bob"); expect(pulled).toHaveLength(1); expect(pulled[0]!.from_mailbox).toBe("alice"); expect(pulled[0]!.body).toBe("hello bob"); const peek2 = store.peek("bob"); expect(peek2.pending).toBe(0); expect(peek2.oldestAt).toBeNull(); const empty = store.checkInbox("bob"); expect(empty).toEqual([]); } finally { store.close(); } }); it("checkInbox returns all pending in order and marks them delivered atomically", () => { const store = new MailboxStore(dbPath); try { for (let i = 0; i < 10; i++) { store.send("alice", "bob", `msg ${i}`); } const first = store.checkInbox("bob"); expect(first).toHaveLength(10); expect(first.map((m) => m.body)).toEqual( Array.from({ length: 10 }, (_, i) => `msg ${i}`), ); const second = store.checkInbox("bob"); expect(second).toEqual([]); } finally { store.close(); } }); }); describe("rename", () => { it("renames a mailbox and transfers undelivered messages", () => { const store = new MailboxStore(dbPath); try { store.send("alice", "bob-old", "hi"); store.send("alice", "bob-old", "again"); const r = store.rename("bob-old", "bob-new"); expect(r.from).toBe("bob-old"); expect(r.to).toBe("bob-new"); expect(r.messagesTransferred).toBe(2); // Old name is gone. const list = store.listMailboxes().map((m) => m.name); expect(list).toContain("bob-new"); expect(list).not.toContain("bob-old"); // Messages still pending under the new name. const peek = store.peek("bob-new"); expect(peek.pending).toBe(2); // checkInbox under the new name yields the original bodies and the original from. const pulled = store.checkInbox("bob-new"); expect(pulled.map((m) => m.body)).toEqual(["hi", "again"]); } finally { store.close(); } }); it("also rewrites the from-side when the renamed mailbox was a sender", () => { const store = new MailboxStore(dbPath); try { store.send("sender-old", "bob", "msg-1"); store.rename("sender-old", "sender-new"); const pulled = store.checkInbox("bob"); expect(pulled).toHaveLength(1); expect(pulled[0]!.from_mailbox).toBe("sender-new"); } finally { store.close(); } }); it("treats rename-to-same-name as a no-op touch", () => { const store = new MailboxStore(dbPath); try { store.upsertMailbox("alice"); const r = store.rename("alice", "alice"); expect(r.messagesTransferred).toBe(0); expect(store.listMailboxes().map((m) => m.name)).toEqual(["alice"]); } finally { store.close(); } }); it("rejects when target already exists", () => { const store = new MailboxStore(dbPath); try { store.upsertMailbox("alice"); store.upsertMailbox("bob"); expect(() => store.rename("alice", "bob")).toThrow(RenameError); try { store.rename("alice", "bob"); } catch (e) { expect((e as RenameError).reason).toBe("target-exists"); } // Source still present after the failed attempt. expect(store.listMailboxes().map((m) => m.name)).toEqual(["alice", "bob"]); } finally { store.close(); } }); it("rejects when source is missing", () => { const store = new MailboxStore(dbPath); try { try { store.rename("nope", "fresh"); } catch (e) { expect(e).toBeInstanceOf(RenameError); expect((e as RenameError).reason).toBe("source-missing"); } } finally { store.close(); } }); }); describe("listMailboxes", () => { it("returns mailboxes alphabetically with pendingForYou for the caller", () => { const store = new MailboxStore(dbPath); try { store.send("alice", "bob", "x"); store.send("alice", "bob", "y"); store.send("carol", "bob", "z"); const fromBob = store.listMailboxes("bob"); expect(fromBob.map((m) => m.name)).toEqual(["alice", "bob", "carol"]); const bobRow = fromBob.find((m) => m.name === "bob"); expect(bobRow?.pendingForYou).toBe(3); } finally { store.close(); } }); it("hides mailboxes older than hideAfterMinutes when filter is active", () => { const store = new MailboxStore(dbPath); try { store.upsertMailbox("recent"); store.upsertMailbox("stale"); store.close(); backdate(dbPath, "stale", 90); const store2 = new MailboxStore(dbPath); try { const filtered = store2.listMailboxes(undefined, { hideAfterMinutes: 60 }); expect(filtered.map((m) => m.name)).toEqual(["recent"]); const unfiltered = store2.listMailboxes(); expect(unfiltered.map((m) => m.name).sort()).toEqual(["recent", "stale"]); } finally { store2.close(); } } catch (e) { store.close(); throw e; } }); it("always includes the caller and senders with pending messages, even if stale", () => { const store = new MailboxStore(dbPath); try { store.send("stale-sender", "me", "you have mail"); store.upsertMailbox("recent-other"); store.upsertMailbox("stale-other"); store.close(); backdate(dbPath, "stale-sender", 120); backdate(dbPath, "stale-other", 120); backdate(dbPath, "me", 120); const store2 = new MailboxStore(dbPath); try { const filtered = store2.listMailboxes("me", { hideAfterMinutes: 60 }); const names = filtered.map((m) => m.name).sort(); expect(names).toContain("me"); expect(names).toContain("stale-sender"); expect(names).toContain("recent-other"); expect(names).not.toContain("stale-other"); } finally { store2.close(); } } catch (e) { store.close(); throw e; } }); }); describe("pruneStale", () => { it("deletes idle mailboxes with no pending messages and wipes their delivered history", () => { const store = new MailboxStore(dbPath); try { store.send("alice", "bob", "old"); store.checkInbox("bob"); store.upsertMailbox("fresh"); store.close(); backdate(dbPath, "alice", 60 * 24 * 8); backdate(dbPath, "bob", 60 * 24 * 8); const store2 = new MailboxStore(dbPath); try { const r = store2.pruneStale(60 * 24 * 7); expect(r.deletedMailboxes).toBe(2); expect(r.deletedMessages).toBe(1); const remaining = store2.listMailboxes().map((m) => m.name); expect(remaining).toEqual(["fresh"]); } finally { store2.close(); } } catch (e) { store.close(); throw e; } }); it("never deletes a mailbox that still has pending messages, even if idle", () => { const store = new MailboxStore(dbPath); try { store.send("alice", "bob", "still pending"); store.close(); backdate(dbPath, "alice", 60 * 24 * 30); backdate(dbPath, "bob", 60 * 24 * 30); const store2 = new MailboxStore(dbPath); try { const r = store2.pruneStale(60 * 24 * 7); expect(r.deletedMailboxes).toBe(0); expect(r.deletedMessages).toBe(0); expect(store2.peek("bob").pending).toBe(1); } finally { store2.close(); } } catch (e) { store.close(); throw e; } }); it("returns zero when deleteAfterMinutes is 0 (disabled)", () => { const store = new MailboxStore(dbPath); try { store.upsertMailbox("x"); store.close(); backdate(dbPath, "x", 60 * 24 * 365); const store2 = new MailboxStore(dbPath); try { const r = store2.pruneStale(0); expect(r).toEqual({ deletedMailboxes: 0, deletedMessages: 0 }); expect(store2.listMailboxes().map((m) => m.name)).toEqual(["x"]); } finally { store2.close(); } } catch (e) { store.close(); throw e; } }); });