73 lines · 5.4 KB · TypeScriptRaw
/** * An erase a phone sends (`delete` with `erase`: why, as its receipt says) ends the row for good on the object too: * the change log keeps nothing the message held, and its chat's logged list lines keep none of its words * (CONTRACT.md "WhatsApp through the Mac", 23 Nov 2026). Every id, name and word is invented. */import { env, runInDurableObject } from "cloudflare:test";import { describe, expect, it } from "vitest";import { http, personalObject } from "./personal.ts";import { holdsWord } from "./clear-word.ts";const stub = (slug: string) => env.BUSINESS.get(env.BUSINESS.idFromName(slug));async function act(slug: string, credential: string, name: string, args: Record<string, unknown>) { const res = await http<Record<string, unknown>>(slug, "POST", "/v1/act", { act: name, args }, { authorization: `Bearer ${credential}` }); expect(res.status, JSON.stringify(res.body)).toBe(200); return res.body;}/** Every logged change, as the object keeps it. */async function log(slug: string): Promise<{ table_id: string; record_id: string; kind: string; before: string | null; after: string | null }[]> { return runInDurableObject(stub(slug), (_i, state) => state.storage.sql.exec<{ table_id: string; record_id: string; kind: string; before: string | null; after: string | null }>("SELECT table_id, record_id, kind, before, after FROM change ORDER BY id").toArray());}/** The row the object holds, as text. */async function row(slug: string, table: string, id: string): Promise<string> { return runInDurableObject(stub(slug), (_i, state) => JSON.stringify(state.storage.sql.exec("SELECT data, deleted FROM rows WHERE table_id = ? AND id = ?", table, id).toArray()));}const WORDS = "zebra quartz lantern";const KEY = "messages/t-erase/fb2b-invented-photo.jpg";/** RULE (owner, 23 Nov 2026): an erased message leaves no words and no file ref in the object's change log, its own entries or its chat's; a delete that is no erase keeps its undo. */describe("an erase a phone sends", () => { it("leaves the message's words and file nowhere in the log, and its chat's list lines keep none of them", async () => { const slug = "erase-log"; const { credential } = await personalObject(slug, { open: ["thread", "message"] }); await act(slug, credential, "create", { table: "thread", id: "t-erase", data: { title: "Invented chat", last_message_preview: "an older hello", last_message_at: "2026-11-20T08:00:00.000Z" } }); await act(slug, credential, "create", { table: "message", id: "m-old", data: { thread_id: "t-erase", channel: "whatsapp", body: "an older hello", sent_at: "2026-11-20T08:00:00.000Z" } }); await act(slug, credential, "create", { table: "message", id: "m-gone", data: { thread_id: "t-erase", channel: "whatsapp", body: WORDS, sent_at: "2026-11-21T08:00:00.000Z", media: [{ key: KEY, contentType: "image/jpeg" }] } }); await act(slug, credential, "update", { table: "message", id: "m-gone", data: { body: `${WORDS}, edited` } }); await act(slug, credential, "update", { table: "thread", id: "t-erase", data: { last_message_preview: WORDS, last_message_at: "2026-11-21T08:00:00.000Z" } }); // The phone empties the list line first, then erases the message (src/engine.ts `erased`). await act(slug, credential, "update", { table: "thread", id: "t-erase", data: { last_message_preview: "" } }); expect(JSON.stringify(await log(slug))).toContain("zebra"); await act(slug, credential, "delete", { table: "message", id: "m-gone", erase: "disappeared" }); const after = await log(slug); const text = JSON.stringify(after); expect(holdsWord(text, "zebra"), "zebra").toBe(false); expect(holdsWord(text, "quartz"), "quartz").toBe(false); expect(holdsWord(text, "fb2b-invented-photo"), "fb2b-invented-photo").toBe(false); expect(after.filter((c) => c.record_id === "m-gone")).toEqual([{ table_id: "message", record_id: "m-gone", kind: "delete", before: null, after: null }]); // The tombstone keeps only why, so every copy that pulls it ends the row for good too. expect(JSON.parse(await row(slug, "message", "m-gone"))).toEqual([{ data: JSON.stringify({ $erase: "disappeared" }), deleted: 1 }]); const pulled = await http<{ rows: { id: string; deleted?: boolean; data: Record<string, unknown> }[] }>(slug, "GET", "/v1/sync?since=0", undefined, { authorization: `Bearer ${credential}` }); expect(pulled.body.rows.find((r) => r.id === "m-gone")).toMatchObject({ deleted: true, data: { $erase: "disappeared" } }); // The other message, and the chat's other changes, keep their undo. expect(after.some((c) => c.record_id === "m-old" && c.kind === "create")).toBe(true); expect(after.some((c) => c.record_id === "t-erase" && c.kind === "create")).toBe(true); }); it("leaves a plain delete its undo", async () => { const slug = "erase-log-plain"; const { credential } = await personalObject(slug, { open: ["thread", "message"] }); await act(slug, credential, "create", { table: "thread", id: "t-plain", data: { title: "Invented chat" } }); await act(slug, credential, "create", { table: "message", id: "m-plain", data: { thread_id: "t-plain", channel: "whatsapp", body: "binned, not erased", sent_at: "2026-11-21T08:00:00.000Z" } }); await act(slug, credential, "delete", { table: "message", id: "m-plain" }); const gone = (await log(slug)).find((c) => c.record_id === "m-plain" && c.kind === "delete"); expect(gone?.before).toContain("binned, not erased"); });});