import { env, runInDurableObject } from "cloudflare:test"; import { describe, expect, it } from "vitest"; import { Store } from "../src/store.ts"; import { http, personalObject, put, TODOS_OPEN, PEOPLE_OPEN } from "./personal.ts"; const stub = (slug: string) => env.BUSINESS.get(env.BUSINESS.idFromName(slug)); interface Restored { restored: { table: string; id: string; landed: string }[]; } async function restore(slug: string, credential: string, rows: Record[]): Promise<{ status: number; body: Restored & { code?: string } }> { return http(slug, "POST", "/v1/act", { act: "restore", args: { rows } }, { authorization: `Bearer ${credential}` }); } async function held(slug: string, table: string, id: string) { return runInDurableObject(stub(slug), (_i, state) => new Store(state.storage.sql, () => new Date()).any(table, id)); } /** RULE (restore-from-phone, 10 Nov 2026): a phone's copy of a database that is gone comes back whole into the new object, every row with its own id and times, and a held id is answered, never refused. */ describe("restore", () => { const slug = "restore-rows"; let credential = ""; it("makes the rows the object lacks with their own ids and times", async () => { ({ credential } = await personalObject(slug, { open: [...TODOS_OPEN, ...PEOPLE_OPEN.open] })); const res = await restore(slug, credential, [ { table: "project", id: "proj-invented-1", data: { name: "An invented project", slug: "an-invented-project" }, created_at: "2026-09-01T08:00:00.000Z", updated_at: "2026-09-02T08:00:00.000Z" }, { table: "todo", id: "todo-invented-1", data: { title: "An invented errand", project: "proj-invented-1", status: "done", completed_at: "2026-09-03T08:00:00.000Z" }, created_at: "2026-09-01T09:00:00.000Z", updated_at: "2026-09-03T08:00:00.000Z" }, ]); expect(res.status, JSON.stringify(res.body)).toBe(200); expect(res.body.restored).toEqual([ { table: "project", id: "proj-invented-1", landed: "made" }, { table: "todo", id: "todo-invented-1", landed: "made" }, ]); const todo = await held(slug, "todo", "todo-invented-1"); expect(todo?.data.title).toBe("An invented errand"); expect(todo?.data.completed_at).toBe("2026-09-03T08:00:00.000Z"); expect([todo?.created_at, todo?.updated_at]).toEqual(["2026-09-01T09:00:00.000Z", "2026-09-03T08:00:00.000Z"]); }); it("sent again, changes nothing and refuses nothing", async () => { const res = await restore(slug, credential, [{ table: "todo", id: "todo-invented-1", data: { title: "An invented errand", project: "proj-invented-1", status: "done", completed_at: "2026-09-03T08:00:00.000Z" }, updated_at: "2026-09-03T08:00:00.000Z" }]); expect(res.status, JSON.stringify(res.body)).toBe(200); expect(res.body.restored).toEqual([{ table: "todo", id: "todo-invented-1", landed: "unchanged" }]); }); it("the newer updated_at wins over a row imported from a backup, and an older one leaves the object's", async () => { await put(slug, [ { verb: "put", table: "person", id: "person-invented-older", data: { full_name: "From the backup" }, updated_at: "2026-10-23T08:00:00.000Z" }, { verb: "put", table: "person", id: "person-invented-newer", data: { full_name: "From the backup, edited since" }, updated_at: "2026-11-05T08:00:00.000Z" }, ]); const res = await restore(slug, credential, [ { table: "person", id: "person-invented-older", data: { full_name: "From the phone" }, updated_at: "2026-11-01T08:00:00.000Z" }, { table: "person", id: "person-invented-newer", data: { full_name: "From the phone, older" }, updated_at: "2026-11-01T08:00:00.000Z" }, ]); expect(res.status, JSON.stringify(res.body)).toBe(200); expect(res.body.restored.map((row) => row.landed)).toEqual(["written", "standing"]); expect((await held(slug, "person", "person-invented-older"))?.data.full_name).toBe("From the phone"); expect((await held(slug, "person", "person-invented-older"))?.updated_at).toBe("2026-11-01T08:00:00.000Z"); expect((await held(slug, "person", "person-invented-newer"))?.data.full_name).toBe("From the backup, edited since"); }); it("a row the object deleted stays deleted", async () => { const made = await http<{ made?: { id: string } }>(slug, "POST", "/v1/act", { act: "create", args: { table: "todo", id: "todo-invented-gone", data: { title: "Deleted here" } } }, { authorization: `Bearer ${credential}` }); expect(made.status, JSON.stringify(made.body)).toBe(200); await http(slug, "POST", "/v1/act", { act: "delete", args: { table: "todo", id: "todo-invented-gone" } }, { authorization: `Bearer ${credential}` }); const res = await restore(slug, credential, [{ table: "todo", id: "todo-invented-gone", data: { title: "Deleted here" }, updated_at: "2026-11-09T08:00:00.000Z" }]); expect(res.body.restored).toEqual([{ table: "todo", id: "todo-invented-gone", landed: "deleted" }]); expect((await held(slug, "todo", "todo-invented-gone"))?.deleted).toBe(true); }); it("a link to a row the object lacks refuses the whole call and keeps nothing", async () => { const res = await restore(slug, credential, [ { table: "todo", id: "todo-invented-2", data: { title: "Kept only with its project" } }, { table: "todo", id: "todo-invented-3", data: { title: "Its project is not here", project: "proj-invented-missing" } }, ]); expect(res.status).toBe(400); expect(res.body.code).toBe("bad_link"); expect(await held(slug, "todo", "todo-invented-2")).toBeNull(); }); });