Files
ClaudeMailbox/node/tests/db.test.ts
Mika Kuns b10ac36ed0 feat(naming)!: auto-derive mailbox name from project + runtime rename
Mailbox names are now built as <project>-<session-short>, where <project>
is the sanitized git-repo basename (or cwd basename) — no more env-var
prefix step. Sessions can re-tag themselves at runtime via the new
mcp__mailbox__rename tool (POST /v1/rename), which transfers all
pending messages to the new name in a single transaction. Peers using
the old name re-discover via list_mailboxes.

BREAKING: \$CLAUDE_MAILBOX_NAME is no longer read. Existing setups that
relied on the env-var prefix should remove it from .claude/settings.json;
the prefix now comes from the working directory automatically.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-20 13:14:15 +02:00

182 lines
5.3 KiB
TypeScript

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 { MailboxStore, RenameError } from "../src/db.js";
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();
}
});
});