103 lines · 9.2 KB · TypeScriptRaw
/** * RESTORE 2 (coordinator for the owner, 27 Nov 2026; assets/design/shared/RESTORE-2.md and RESTORE-2-CONTRACT.md): the * server half. The generation on every sync answer and /v1/me; a phone's write from another generation refused 409 * `generation_moved` with nothing landed; the freeze (503 `account_frozen` to all but the admin token), kept outside * the object; the export; `finish` raising the version past anything held and writing the new generation. Point-in-time * recovery itself does not run here (no durable log locally): the restore's own step is proved on the dev Worker by * scripts/restore-runbook.ts; here a restore asked is stood in for by its marker, as the restore route writes it. */import { env } from "cloudflare:test";import { describe, expect, it } from "vitest";import { directory } from "../src/directory.ts";import { markerKey } from "../src/restore.ts";import { aadOf, importKey, openPart } from "../src/snapshots.ts";import { ADMIN, http, personalObject, TODOS_OPEN } from "./personal.ts";const phone = (credential: string, generation?: string) => ({ authorization: `Bearer ${credential}`, ...(generation ? { "x-cotton-generation": generation } : {}) });const todo = (title: string) => ({ act: "create", args: { table: "todo", data: { title } } });describe("RESTORE 2, the server half", () => { it("answers one generation on every sync page and on /v1/me, and refuses a write from another before it lands", async () => { const slug = "restore2-generation"; const { credential } = await personalObject(slug, { open: TODOS_OPEN }); const first = await http<{ generation: string; restored_to?: string; version: number }>(slug, "GET", "/v1/sync?since=0&limit=1", undefined, phone(credential)); expect(first.body.generation).toMatch(/^[A-Za-z0-9_-]{22}$/); expect(first.body.restored_to).toBeUndefined(); const again = await http<{ generation: string }>(slug, "GET", "/v1/sync?since=0", undefined, phone(credential)); expect(again.body.generation).toBe(first.body.generation); const me = await http<{ generation: string }>(slug, "GET", "/v1/me", undefined, phone(credential)); expect(me.body.generation).toBe(first.body.generation); // The same generation, or none (an assistant, the Mac, an older build): the write lands. expect((await http(slug, "POST", "/v1/act", todo("same generation"), phone(credential, first.body.generation))).status).toBe(200); expect((await http(slug, "POST", "/v1/act", todo("no generation"), phone(credential))).status).toBe(200); const before = (await http<{ version: number }>(slug, "GET", "/v1/me", undefined, phone(credential))).body.version; const moved = await http<{ code: string; generation: string; restored_to: string | null }>(slug, "POST", "/v1/act", todo("another generation"), phone(credential, "AAAAAAAAAAAAAAAAAAAAAA")); expect(moved.status).toBe(409); expect(moved.body).toMatchObject({ code: "generation_moved", generation: first.body.generation, restored_to: null }); expect((await http<{ version: number }>(slug, "GET", "/v1/me", undefined, phone(credential))).body.version, "nothing landed").toBe(before); }); it("freezes at the door: 503 account_frozen with Retry-After to all but the admin token, the freeze kept outside the object", async () => { const slug = "restore2-freeze"; const { credential, owner } = await personalObject(slug, { open: TODOS_OPEN }); expect((await http(slug, "POST", "/v1/admin/restore/freeze", { reason: "an invented test" }, { authorization: `Bearer ${owner}` })).status).toBe(403); expect((await http(slug, "POST", "/v1/admin/restore/freeze", { reason: "an invented test" }, ADMIN)).status).toBe(200); const res = await env.BUSINESS.get(env.BUSINESS.idFromName(slug)).fetch(new Request("https://x/v1/sync?since=0", { headers: { ...phone(credential), "x-cotton-business": slug } })); expect(res.status).toBe(503); expect(res.headers.get("retry-after")).toBe("30"); expect(await res.json()).toMatchObject({ code: "account_frozen", retry_after_s: 30 }); expect((await http<{ code: string }>(slug, "POST", "/v1/act", todo("frozen"), phone(credential))).body.code).toBe("account_frozen"); expect((await http<{ code: string }>(slug, "POST", "/mcp", { jsonrpc: "2.0", id: 1, method: "tools/list" }, { authorization: `Bearer ${owner}` })).body.code).toBe("account_frozen"); const state = await http<{ frozen: boolean; reason: string }>(slug, "GET", "/v1/admin/restore/state", undefined, ADMIN); expect(state.body).toMatchObject({ frozen: true, reason: "an invented test" }); expect(await env.PHOTOS.head(markerKey(slug)), "kept in R2, outside the object").not.toBeNull(); // A restore is refused unless frozen; frozen, it needs its bookmark and moment. expect((await http(slug, "POST", "/v1/admin/restore", {}, ADMIN)).status).toBe(400); expect((await http<{ code: string }>(slug, "POST", "/v1/admin/restore/finish", {}, ADMIN)).body.code).toBe("no_restore"); expect((await http(slug, "POST", "/v1/admin/restore/unfreeze", {}, ADMIN)).status).toBe(200); expect(await env.PHOTOS.head(markerKey(slug))).toBeNull(); expect((await http(slug, "GET", "/v1/sync?since=0", undefined, phone(credential))).status).toBe(200); expect((await http<{ code: string }>(slug, "POST", "/v1/admin/restore", { bookmark: "x", restored_to: new Date().toISOString() }, ADMIN)).body.code).toBe("not_frozen"); }); it("exports today's rows as stored, page by page, with a manifest", async () => { const slug = "restore2-export"; const { credential } = await personalObject(slug, { open: TODOS_OPEN }); for (let i = 0; i < 3; i++) await http(slug, "POST", "/v1/act", todo(`invented ${i}`), phone(credential)); expect((await http<{ code: string }>(slug, "POST", "/v1/admin/restore/export", {}, ADMIN)).body.code).toBe("not_frozen"); await http(slug, "POST", "/v1/admin/restore/freeze", {}, ADMIN); const done = await http<{ done: boolean; stamp: string; manifest: { rows: number; parts: { key: string; rows: number; sha256: string }[] } }>(slug, "POST", "/v1/admin/restore/export", {}, ADMIN); expect(done.body.done).toBe(true); expect(done.body.manifest.rows).toBeGreaterThanOrEqual(3); // Sealed like a snapshot (L5), under restore-exports/ where the lifecycle rule ends it. const part = new Uint8Array(await (await env.MEDIA.get(`_restore-exports/${slug}/${done.body.stamp}/${done.body.manifest.parts[0]!.key}`))!.arrayBuffer()); expect(new TextDecoder().decode(part)).not.toContain("invented"); const keys = await importKey((await directory(env).snapshotKey(slug, false))!); const lines = (await openPart(keys, aadOf(slug, "export", done.body.stamp, 1), part)).map((l) => JSON.parse(l) as { table: string; data: { title?: string } }); expect(lines.filter((l) => l.table === "todo" && l.data.title?.startsWith("invented")).length).toBe(3); expect(await env.MEDIA.head(`_restore-exports/${slug}/${done.body.stamp}/manifest.json`)).not.toBeNull(); await http(slug, "POST", "/v1/admin/restore/unfreeze", {}, ADMIN); }); it("finishes a restore: the version above anything held, a new generation and restored_to, then the old generation is refused", async () => { const slug = "restore2-finish"; const { credential } = await personalObject(slug, { open: TODOS_OPEN }); const old = (await http<{ generation: string; version: number }>(slug, "GET", "/v1/me", undefined, phone(credential))).body; await http(slug, "POST", "/v1/admin/restore/freeze", {}, ADMIN); // The restore route writes this marker, then point-in-time recovery restarts the object (not available locally). const restoredTo = "2026-11-27T10:00:00.000Z"; await env.PHOTOS.put(markerKey(slug), JSON.stringify({ frozen: true, frozen_at: new Date().toISOString(), reason: null, pending: { version_before: old.version + 500, generation: "BBBBBBBBBBBBBBBBBBBBBB", restored_to: restoredTo, bookmark_hash: "0", erased_before: false, asked_at: new Date().toISOString() } })); expect((await http<{ code: string }>(slug, "POST", "/v1/admin/restore/unfreeze", {}, ADMIN)).body.code).toBe("restore_unfinished"); const finished = await http<{ version: number; generation: string; restored_to: string }>(slug, "POST", "/v1/admin/restore/finish", {}, ADMIN); expect(finished.status, JSON.stringify(finished.body)).toBe(200); expect(finished.body).toMatchObject({ version: old.version + 500 + 1000, generation: "BBBBBBBBBBBBBBBBBBBBBB", restored_to: restoredTo }); expect((await http(slug, "POST", "/v1/admin/restore/unfreeze", {}, ADMIN)).status).toBe(200); const sync = await http<{ generation: string; restored_to: string; version: number }>(slug, "GET", "/v1/sync?since=0&limit=1", undefined, phone(credential)); expect(sync.body).toMatchObject({ generation: "BBBBBBBBBBBBBBBBBBBBBB", restored_to: restoredTo }); expect((await http<{ code: string }>(slug, "POST", "/v1/act", todo("old copy"), phone(credential, old.generation))).body.code).toBe("generation_moved"); const made = await http(slug, "POST", "/v1/act", todo("new copy"), phone(credential, "BBBBBBBBBBBBBBBBBBBBBB")); expect(made.status).toBe(200); expect((await http<{ version: number }>(slug, "GET", "/v1/me", undefined, phone(credential))).body.version, "a new write goes past every version held").toBe(old.version + 1501); });});