import { describe, expect, it } from "vitest"; import { answer, http, loggedOnly, mint, NOTES_OPEN, personalObject, put, refusal } from "./personal.ts"; interface Rec { table: string; id: string; data: Record; deleted_at: string | null; } interface Write { change: { verb: string; before: Record | null; after: Record | null; receipt: { kind: string; said: string } }; record: Rec; } /** RULE (owner, 26 Oct 2026): what delete_record bins, the bin lists, puts back and erases — over the one mark delete_record sets. */ /** The bins this object keeps that an assistant never empties (EMPTY BIN 1, below), in the object's table order. */ const KEPT = ["message", "photo", "thread"]; describe("the bin", () => { const slug = "binned"; let owner = ""; let reader = ""; const notes: Record = {}; it("holds what delete_record binned, the latest first, and nothing a list reads", async () => { const made = await personalObject(slug, NOTES_OPEN); owner = made.owner; reader = await mint(slug, made.credential, "a reader", ["read"]); for (const title of ["Groceries", "Old plan", "Trip ideas"]) notes[title] = (await answer(slug, owner, "create_record", { table: "note", data: { title } })).record.id; const { thought } = await put(slug, [{ ref: "thought", table: "person", data: { full_name: "Binned Person" } }]); for (const [table, id] of [["note", notes["Old plan"]!], ["note", notes["Groceries"]!], ["person", thought!]] as const) { const gone = await answer(slug, owner, "delete_record", { table, id }); expect(gone.change.receipt.kind).toBe("deleted"); } const all = await answer<{ records: Rec[]; more: boolean }>(slug, reader, "list_bin"); expect(all.records.map((r) => [r.table, r.data.title ?? r.data.full_name])).toEqual([ ["person", "Binned Person"], ["note", "Groceries"], ["note", "Old plan"], ]); expect(all.records.every((r) => typeof r.deleted_at === "string")).toBe(true); const page = await answer<{ records: Rec[]; more: boolean }>(slug, reader, "list_bin", { table: "note", limit: 1 }); expect(page.records.map((r) => r.data.title)).toEqual(["Groceries"]); expect(page.more).toBe(true); const listed = await answer<{ records: Rec[] }>(slug, owner, "list_records", { table: "note" }); expect(listed.records.map((r) => r.data.title)).toEqual(["Trip ideas"]); }); it("puts one back in one write, and refuses what is not in the bin or a table with no bin", async () => { const versionBefore = await version(); const back = await answer(slug, owner, "restore_from_bin", { table: "note", id: notes["Groceries"]! }); expect(back.record.deleted_at).toBeNull(); expect(back.change.receipt).toEqual({ kind: "put_back", said: "Put back Groceries from the bin", words: [] }); expect(back.change.after).toEqual({ trashed_at: null }); expect(await version()).toBe(versionBefore + 1); const listed = await answer<{ records: Rec[] }>(slug, owner, "list_records", { table: "note" }); expect(listed.records.map((r) => r.data.title).sort()).toEqual(["Groceries", "Trip ideas"]); expect(await answer(slug, owner, "update_record", { table: "note", id: notes["Groceries"]!, patch: { title: "Groceries for Sunday" } })).toBeTruthy(); expect(await refusal(slug, owner, "restore_from_bin", { table: "note", id: notes["Trip ideas"]! })).toBe("That note is not in the bin"); expect(await refusal(slug, owner, "restore_from_bin", { table: "note", id: "no-such-note" })).toBe("That note is no longer here"); expect(await refusal(slug, owner, "restore_from_bin", { table: "memory", id: "x" })).toBe("Memories have no bin: a deleted memory is gone for good"); expect(await refusal(slug, reader, "restore_from_bin", { table: "note", id: notes["Old plan"]! })).toBe('this token does not have "write" access. Ask the owner to reconnect with write enabled.'); }); it("erases for good only what is binned, before older_than when it is given", async () => { expect(await answer(slug, owner, "empty_bin", { older_than: "2000-01-01" })).toEqual({ erased: 0, tables: {}, more: false, left_for_the_owner: KEPT }); expect(await refusal(slug, owner, "empty_bin", { older_than: "last week" })).toBe(`older_than is a day (YYYY-MM-DD, in the owner's zone) or an instant with its offset (2026-10-25T09:00:00Z); "last week" is not one`); // AUDIT C 3: binned today, so within the 30 days an assistant leaves for the owner's devices: nothing goes. expect(await answer(slug, owner, "empty_bin", { table: "note" })).toEqual({ erased: 0, tables: {}, more: false }); expect(await answer(slug, owner, "empty_bin", { older_than: "2030-01-01" })).toEqual({ erased: 0, tables: {}, more: false, left_for_the_owner: KEPT }); // Binned long ago: it goes. const { gone } = await put(slug, [{ ref: "gone", table: "note", data: { title: "Long gone", trashed_at: "2026-01-01T00:00:00.000Z" } }]); const versionBefore = await version(); expect(await answer(slug, owner, "empty_bin", { table: "note" })).toEqual({ erased: 1, tables: { note: 1 }, more: false, ...loggedOnly() }); expect(await version()).toBe(versionBefore + 1); expect(await refusal(slug, owner, "restore_from_bin", { table: "note", id: gone! })).toBe("That note is no longer here"); expect((await answer<{ records: Rec[] }>(slug, reader, "list_bin")).records.map((r) => r.data.title ?? r.data.full_name).sort()).toEqual(["Binned Person", "Old plan"]); // What was never binned stays. expect((await answer<{ records: Rec[] }>(slug, owner, "list_records", { table: "note" })).records).toHaveLength(2); }); async function version(): Promise { const r = await http<{ version: number }>(slug, "GET", "/v1/sync?since=0", undefined, { authorization: `Bearer ${owner}` }); return r.body.version; } }); /** * EMPTY BIN 1 (coordinator, 27 Nov 2026): an assistant's empty_bin never erases a Bin that holds records the person * did not bin (a chat's timer, a declined group, the photo prune): messages, chats, photos; named or not, older_than or * not. The owner's device empties them as before. Invented rows only. */ describe("the bins an assistant never empties", () => { it("leaves messages, chats and photos for the owner's devices, and empties the rest", async () => { const slug = "binkept"; const { credential, owner } = await personalObject(slug, { open: ["note", "message", "thread", "photo"] }); // Binned past the 30 days an assistant leaves (AUDIT C 3). const at = "2026-01-01T08:00:00.000Z"; const ids = await put(slug, [ { ref: "thread", table: "thread", data: { channel: "whatsapp", title: "Invented group", trashed_at: at } }, { ref: "timed", table: "message", data: { thread_id: "@thread", channel: "whatsapp", body: "ended by the timer", sent_at: at, trashed_at: at } }, { ref: "photo", table: "photo", data: { key: "never-came-up", trashed_at: at } }, { ref: "note", table: "note", data: { title: "Binned by the owner", trashed_at: at } }, ]); const kept = ["message", "thread", "photo"]; for (const table of kept) { for (const args of [{ table }, { table, older_than: "2030-01-01" }]) { expect(await refusal(slug, owner, "empty_bin", args), JSON.stringify(args)).toMatch(/Bin is emptied only on the owner's devices/); } } const emptied = await answer<{ erased: number; tables: Record; left_for_the_owner: string[] }>(slug, owner, "empty_bin", { older_than: "2030-01-01" }); expect(emptied.erased).toBe(1); expect(emptied.tables).toEqual({ note: 1 }); expect([...emptied.left_for_the_owner].sort()).toEqual([...kept].sort()); const binned = await answer<{ records: Rec[] }>(slug, owner, "list_bin"); expect(binned.records.map((r) => r.id).sort()).toEqual([ids.thread, ids.timed, ids.photo].sort()); // Nor does a token erase one at /v1/act, as the phone's Bin does (with token scopes off a token reaches /v1/act). const byToken = await http<{ code: string }>(slug, "POST", "/v1/act", { act: "delete", args: { table: "message", id: ids.timed, erase: "bin" } }, { authorization: `Bearer ${owner}` }); expect([byToken.status, byToken.body.code]).toEqual([403, "owner_only"]); // The owner's device erases a message from the Bin as before. const erased = await http(slug, "POST", "/v1/act", { act: "delete", args: { table: "message", id: ids.timed, erase: "bin" } }, { authorization: `Bearer ${credential}` }); expect(erased.status, JSON.stringify(erased.body)).toBe(200); expect((await answer<{ records: Rec[] }>(slug, owner, "list_bin", { table: "message" })).records).toEqual([]); }); });