import { describe, expect, it } from "vitest"; import type { HeldAnswer, SyncAnswer } from "../src/protocol.ts"; import { ADMIN, http, personalObject, put } from "./personal.ts"; /** A table the test brings beside the package's. */ const DIARY = { format_version: 1, id: "diary", name: "Diary", plural: "Diaries", columns: [{ key: "line", type: "text", required: true }] }; async function held(slug: string, query: string, credential: string): Promise { const res = await http(slug, "GET", `/v1/held?${query}`, undefined, { authorization: `Bearer ${credential}` }); expect(res.status, JSON.stringify(res.body)).toBe(200); return res.body; } /** Every page, as a phone's top-up asks. */ async function everyPage(slug: string, credential: string, query: string): Promise { const pages = [await held(slug, query, credential)]; while (pages[pages.length - 1]!.next) pages.push(await held(slug, `cursor=${pages[pages.length - 1]!.next!}`, credential)); return pages; } /** RULE (topup-ids, 11 Nov 2026): a top-up asks the object which ids it holds, never every row's data. */ describe("GET /v1/held: the ids the object holds, ids only", () => { const SLUG = "held-personal"; let credential = ""; it("answers every id of the tables named, live and binned apart, paged without a gap or a repeat, and never a row's data", async () => { ({ credential } = await personalObject(SLUG)); expect((await http(SLUG, "POST", "/v1/admin/definitions/tables", { tables: [DIARY] }, ADMIN)).status).toBe(200); const rows = Array.from({ length: 25 }, (_, i) => ({ verb: "put", table: "diary", id: `d-${String(i).padStart(2, "0")}`, data: { line: `day ${i}` } })); await put(SLUG, [...rows, { verb: "put", table: "todo", id: "td-1", data: { title: "Ask for ids" } }]); const del = await http(SLUG, "POST", "/v1/act", { act: "delete", args: { table: "diary", id: "d-03" } }, { authorization: `Bearer ${credential}` }); expect(del.status, JSON.stringify(del.body)).toBe(200); const pages = await everyPage(SLUG, credential, "tables=diary&limit=10"); expect(pages.length).toBe(3); const live = pages.flatMap((p) => p.held.diary?.live ?? []); const binned = pages.flatMap((p) => p.held.diary?.binned ?? []); expect(binned).toEqual(["d-03"]); expect(live).toEqual(rows.map((r) => r.id).filter((id) => id !== "d-03")); expect(pages.every((p) => Object.keys(p.held).every((t) => t === "diary")), "only the tables named").toBe(true); expect(JSON.stringify(pages)).not.toContain("day 1"); }); it("answers what a whole pull of the same tables holds, in far fewer bytes", async () => { const pulled = new Set(); let query = "since=0&tables=diary,todo"; let bytes = 0; for (;;) { const res = await http(SLUG, "GET", `/v1/sync?${query}`, undefined, { authorization: `Bearer ${credential}` }); bytes += JSON.stringify(res.body).length; for (const r of res.body.rows) pulled.add(`${r.table}\t${r.id}`); if (!res.body.next) break; query = `cursor=${res.body.next}`; } const pages = await everyPage(SLUG, credential, "tables=diary,todo"); const asked = new Set(pages.flatMap((p) => Object.entries(p.held).flatMap(([t, h]) => [...h.live, ...h.binned].map((id) => `${t}\t${id}`)))); expect([...asked].sort()).toEqual([...pulled].sort()); expect(JSON.stringify(pages).length).toBeLessThan(bytes); }); it("answers nothing for a table the object does not define, and refuses no tables, a bad limit and a forged cursor", async () => { expect((await held(SLUG, "tables=no_such_table", credential)).held).toEqual({}); const auth = { authorization: `Bearer ${credential}` }; expect((await http(SLUG, "GET", "/v1/held", undefined, auth)).status).toBe(400); expect((await http(SLUG, "GET", "/v1/held?tables=diary&limit=0", undefined, auth)).status).toBe(400); expect((await http(SLUG, "GET", "/v1/held?tables=Diary!", undefined, auth)).status).toBe(400); expect((await http(SLUG, "GET", "/v1/held?cursor=bm90IGEgY3Vyc29y", undefined, auth)).status).toBe(400); }); it("answers the owner alone", async () => { const res = await http<{ error?: { code: string } }>(SLUG, "GET", "/v1/held?tables=diary"); expect(res.status).toBe(403); }); });