Cotton code
394 lines · 26.5 KB · TypeScriptRaw
import { describe, expect, it } from "vitest";import { appReports } from "../src/device_reports.ts";import type { SyncAnswer, WireRow } from "../src/protocol.ts";import { ADMIN, CLEAR_IMPORT_OPEN, DATABASE_ID, http, OWNER_PHONE, PERSONAL_TABLES, PERSONAL_TOOLS, personalObject, put } from "./personal.ts";const FACTS = `device_name=${encodeURIComponent("Deck walk")}&platform=ios&app_version=${encodeURIComponent("1.0.0 (1)")}`;const SYNC_COLUMNS = ["synced_at", "sync_version", "sync_rows_total", "sync_rows_done", "sync_bytes", "caught_up", "stalled_at", "stalled_why"];async function page(slug: string, query: string, credential: string): Promise<SyncAnswer> {  const res = await http<SyncAnswer>(slug, "GET", `/v1/sync?${query}`, undefined, { authorization: `Bearer ${credential}` });  expect(res.status, JSON.stringify(res.body)).toBe(200);  return res.body;}async function pass(slug: string, credential: string, query: string): Promise<SyncAnswer[]> {  const pages = [await page(slug, query, credential)];  while (pages[pages.length - 1]!.next) pages.push(await page(slug, `cursor=${pages[pages.length - 1]!.next!}`, credential));  return pages;}async function deviceRows(slug: string): Promise<WireRow[]> {  const res = await http<{ rows: WireRow[] }>(slug, "GET", "/v1/admin/export?limit=10000", undefined, ADMIN);  return res.body.rows.filter((r) => r.table === "device" && !r.deleted);}/** The owner signed in again from a phone whose sign-in names `deviceId`. */async function signIn(slug: string, deviceId: string): Promise<string> {  const code = await http<{ challenge_id: string }>(slug, "POST", "/v1/code", { phone: OWNER_PHONE, device_id: deviceId });  const signed = await http<{ credential: string }>(slug, "POST", "/v1/sign-in", { challenge_id: code.body.challenge_id, code: "123456", device_id: deviceId });  expect(signed.status, JSON.stringify(signed.body)).toBe(200);  return signed.body.credential;}describe("a signed-in device is filed under its credential's device_id", () => {  const SLUG = "sync-device-file";  it("makes the row on the first page that names the phone, writes the pass on it, and never a second", async () => {    const { credential } = await personalObject(SLUG);    await put(SLUG, Array.from({ length: 8 }, (_, i) => ({ verb: "put", table: "todo", id: `fd-${i}`, data: { title: `todo ${i}`, status: "open" } })));    const pages = await pass(SLUG, credential, `since=0&limit=3&${FACTS}`);    const id = pages[0]!.device;    expect(id, "the first page names the row it made").toEqual(expect.any(String));    expect(pages.every((p) => p.device === id), "every page of the pass names it").toBe(true);    const rows = (await deviceRows(SLUG)).filter((r) => r.data.device_id === "coordinator");    expect(rows.map((r) => r.id)).toEqual([id]);    expect(rows[0]!.data).toMatchObject({ device_id: "coordinator", name: "Deck walk", platform: "ios", app_version: "1.0.0 (1)", caught_up: true, sync_rows_total: pages[0]!.total, sync_rows_done: pages[0]!.total });    const again = await pass(SLUG, credential, `since=0&${FACTS}`);    expect(again[0]!.device, "a later pass finds the same row").toBe(id);    expect((await deviceRows(SLUG)).filter((r) => r.data.device_id === "coordinator")).toHaveLength(1);  });  it("restates a synthetic phone's running build on its next launch", async () => {    const credential = await signIn(SLUG, "two-launch-phone");    const first = `since=0&device_name=${encodeURIComponent("Synthetic phone")}&platform=ios&app_version=${encodeURIComponent("1.0.0 (328)")}`;    const second = `since=0&device_name=${encodeURIComponent("Synthetic phone")}&platform=ios&app_version=${encodeURIComponent("1.0.0 (329)")}`;    const made = await page(SLUG, first, credential);    const auth = { authorization: `Bearer ${credential}` };    expect((await http(SLUG, "POST", "/v1/act", { act: "update", args: { table: "device", id: made.device, data: { reported_state: { build: "328", sha: "synthetic328", schema: 137 } } } }, auth)).status).toBe(200);    const [before] = (await deviceRows(SLUG)).filter((r) => r.data.device_id === "two-launch-phone");    expect(before!.data.app_version).toBe("1.0.0 (328)");    const launchedAgain = await page(SLUG, second, credential);    expect((await http(SLUG, "POST", "/v1/act", { act: "update", args: { table: "device", id: launchedAgain.device, data: { reported_state: { build: "329", sha: "synthetic329", schema: 138 } } } }, auth)).status).toBe(200);    const [after, ...more] = (await deviceRows(SLUG)).filter((r) => r.data.device_id === "two-launch-phone");    expect(launchedAgain.device).toBe(made.device);    expect(more).toEqual([]);    expect(after!.data.app_version).toBe("1.0.0 (329)");    expect(after!.data.reported_state).toEqual({ build: "329", sha: "synthetic329", schema: 138 });  });  it("takes the phone's bytes, stall and why it stalled on that row through the general update, and clears them", async () => {    const credential = await signIn(SLUG, "coordinator");    const id = (await page(SLUG, `since=0&${FACTS}`, credential)).device!;    const stalled = new Date().toISOString();    const why = "this phone's copy could not take the object's columns of transaction (an invented reason)";    const write = (data: Record<string, unknown>) => http<{ changed: WireRow[] }>(SLUG, "POST", "/v1/act", { act: "update", args: { table: "device", id, data } }, { authorization: `Bearer ${credential}` });    const res = await write({ sync_bytes: 206_300_000, stalled_at: stalled, stalled_why: why });    expect(res.status, JSON.stringify(res.body)).toBe(200);    const row = (await deviceRows(SLUG)).find((r) => r.id === id)!;    expect(row.data).toMatchObject({ sync_bytes: 206_300_000, stalled_at: stalled, stalled_why: why });    const cleared = await write({ stalled_at: null, stalled_why: null });    expect(cleared.status, JSON.stringify(cleared.body)).toBe(200);    const after = (await deviceRows(SLUG)).find((r) => r.id === id)!;    expect([after.data.stalled_at ?? null, after.data.stalled_why ?? null]).toEqual([null, null]);  });  it("adopts the row a phone already filed under its device_id", async () => {    const credential = await signIn(SLUG, "phone-key-2");    const made = await http(SLUG, "POST", "/v1/act", { act: "create", args: { table: "device", id: "filed-by-phone", data: { device_id: "phone-key-2", name: "iPhone", platform: "ios", approved_at: new Date().toISOString() } } }, { authorization: `Bearer ${credential}` });    expect(made.status, JSON.stringify(made.body)).toBe(200);    expect((await page(SLUG, `since=0&${FACTS}`, credential)).device).toBe("filed-by-phone");    expect((await deviceRows(SLUG)).filter((r) => r.data.device_id === "phone-key-2").map((r) => r.id)).toEqual(["filed-by-phone"]);  });  it("makes nothing for a page that does not say what the device is called", async () => {    const credential = await signIn(SLUG, "unnamed");    expect((await page(SLUG, "since=0", credential)).device).toBeNull();    expect((await deviceRows(SLUG)).filter((r) => r.data.device_id === "unnamed")).toEqual([]);  });});describe("a caught-up phone's sync passes go quiet", () => {  const SLUG = "sync-device-quiet";  it("writes nothing on a caught-up pass whose phone names itself as its row already says", async () => {    const { credential } = await personalObject(SLUG);    const facts = `device_name=${encodeURIComponent("Synthetic quiet phone")}&platform=ios&app_version=${encodeURIComponent("1.0.0 (330)")}`;    // The passes that carry its own row's writes back to it settle in two.    let caughtUp = 0;    for (let i = 0; i < 3; i++) {      const passed = await pass(SLUG, credential, `since=${caughtUp}&${facts}`);      caughtUp = passed[passed.length - 1]!.version;    }    const [held] = (await deviceRows(SLUG)).filter((r) => r.data.device_id === "coordinator");    const next = await pass(SLUG, credential, `since=${caughtUp}&${facts}`);    expect(next.flatMap((p) => p.rows), "nothing changed, so nothing is sent").toEqual([]);    expect(next[next.length - 1]!.version, "and the object's version stands").toBe(caughtUp);    const [after] = (await deviceRows(SLUG)).filter((r) => r.data.device_id === "coordinator");    expect(after!.version).toBe(held!.version);  });});describe("a personal object keeps the shipped package's tables", () => {  const SLUG = "sync-device-package";  it("gives an object made before the device's sync columns those columns, so the writes naming them keep", async () => {    const device = PERSONAL_TABLES.find((t) => t.id === "device")!;    const before = { ...device, columns: (device.columns as { key: string }[]).filter((c) => !SYNC_COLUMNS.includes(c.key)) };    const meta = await http(SLUG, "PUT", "/v1/admin/business", { name: "Personal", kind: "personal", owner_phone: OWNER_PHONE, database_id: DATABASE_ID, timezone: "Asia/Kolkata" }, ADMIN);    expect(meta.status).toBe(200);    const tables = PERSONAL_TABLES.map((t) => (t.id === "device" ? before : t));    expect((await http(SLUG, "PUT", "/v1/admin/definitions", { tools: PERSONAL_TOOLS, tables, flavour: { flavour: "personal" } }, ADMIN)).status).toBe(200);    const credential = await signIn(SLUG, "old-object-phone");    const defs = await http<{ tables: { id: string; columns: { key: string }[] }[] }>(SLUG, "GET", "/v1/definitions", undefined, ADMIN);    const held = defs.body.tables.find((t) => t.id === "device")!;    expect(SYNC_COLUMNS.filter((k) => !held.columns.some((c) => c.key === k)), "the object took the columns the package grew").toEqual([]);    const answer = await page(SLUG, `since=0&${FACTS}`, credential);    const row = (await deviceRows(SLUG)).find((r) => r.id === answer.device)!;    expect(row.data).toMatchObject({ caught_up: true, sync_rows_total: answer.total, sync_version: answer.version });  });  it("gives an object made with the storage-only package the faces the phone draws", async () => {    const slug = "sync-device-faces";    const thin = PERSONAL_TABLES.map((t) =>      t.engine ? { id: t.id, name: t.name, plural: t.plural, visibility: "owner", columns: (t.columns as { key: string; type: string }[]).map(({ key, type }) => ({ key, type, readonly: true })) } : t,    );    expect((await http(slug, "PUT", "/v1/admin/business", { name: "Personal", kind: "personal", owner_phone: OWNER_PHONE, database_id: DATABASE_ID, timezone: "Asia/Kolkata" }, ADMIN)).status).toBe(200);    expect((await http(slug, "PUT", "/v1/admin/definitions", { tools: PERSONAL_TOOLS, tables: thin, flavour: { flavour: "personal" } }, ADMIN)).status).toBe(200);    await signIn(slug, "faces-phone");    const defs = await http<{ tables: Record<string, unknown>[] }>(slug, "GET", "/v1/definitions", undefined, ADMIN);    for (const shipped of PERSONAL_TABLES.filter((t) => t.engine)) {      expect(defs.body.tables.find((t) => t.id === shipped.id), `${String(shipped.id)} as the package defines it`).toEqual(shipped);    }  });});describe("a device that imports and never syncs is filed by the first call of its pass", () => {  const SLUG = "import-device-file";  const MAC = `device_name=${encodeURIComponent("Asha's Mac mini")}&platform=mac&app_version=${encodeURIComponent("0.3.0 (12)")}`;  it("makes the Mac's row from an import call that names it, adopts it after, and leaves a call naming none alone", async () => {    await personalObject(SLUG, CLEAR_IMPORT_OPEN);    const credential = await signIn(SLUG, "mac-install-1");    const auth = { authorization: `Bearer ${credential}` };    const bare = await http(SLUG, "POST", "/v1/import/calls", { calls: [] }, auth);    expect(bare.status, JSON.stringify(bare.body)).toBe(200);    expect((await deviceRows(SLUG)).filter((r) => r.data.device_id === "mac-install-1")).toEqual([]);    const first = await http(SLUG, "POST", `/v1/import/calls?${MAC}`, { calls: [] }, auth);    expect(first.status, JSON.stringify(first.body)).toBe(200);    const rows = (await deviceRows(SLUG)).filter((r) => r.data.device_id === "mac-install-1");    expect(rows).toHaveLength(1);    expect(rows[0]!.data).toMatchObject({ device_id: "mac-install-1", name: "Asha's Mac mini", platform: "mac", app_version: "0.3.0 (12)" });    const next = await http(SLUG, "GET", `/v1/outbox?status=queued&${MAC}`, undefined, auth);    expect(next.status, JSON.stringify(next.body)).toBe(200);    expect((await deviceRows(SLUG)).filter((r) => r.data.device_id === "mac-install-1").map((r) => r.id)).toEqual([rows[0]!.id]);  });});describe("GET /v1/me with the naming fields keeps the Mac's row fresh", () => {  const SLUG = "me-device-refresh";  const named = (name: string, version: string) =>    `device_name=${encodeURIComponent(name)}&platform=mac&app_version=${encodeURIComponent(version)}`;  const macRows = async (deviceId: string) => (await deviceRows(SLUG)).filter((r) => r.data.device_id === deviceId);  it("makes the row when the object holds none, as any naming call does", async () => {    await personalObject(SLUG, CLEAR_IMPORT_OPEN);    const credential = await signIn(SLUG, "mac-me-1");    const me = await http(SLUG, "GET", `/v1/me?${named("Mac mini", "0.3.0 (12)")}`, undefined, { authorization: `Bearer ${credential}` });    expect(me.status, JSON.stringify(me.body)).toBe(200);    const rows = await macRows("mac-me-1");    expect(rows).toHaveLength(1);    expect(rows[0]!.data).toMatchObject({ name: "Mac mini", platform: "mac", app_version: "0.3.0 (12)" });  });  it("restates a held row's name, platform and version, and moves updated_at even when nothing differs", async () => {    const credential = await signIn(SLUG, "mac-me-2");    const auth = { authorization: `Bearer ${credential}` };    expect((await http(SLUG, "GET", `/v1/me?${named("Old name", "0.3.0 (12)")}`, undefined, auth)).status).toBe(200);    const [made] = await macRows("mac-me-2");    await new Promise((r) => setTimeout(r, 5));    expect((await http(SLUG, "GET", `/v1/me?${named("Mac mini", "0.4.0 (13)")}`, undefined, auth)).status).toBe(200);    const [restated, ...more] = await macRows("mac-me-2");    expect(more, "the same row, never a second").toEqual([]);    expect(restated!.id).toBe(made!.id);    expect(restated!.data).toMatchObject({ name: "Mac mini", platform: "mac", app_version: "0.4.0 (13)" });    expect(restated!.updated_at > made!.updated_at).toBe(true);    await new Promise((r) => setTimeout(r, 5));    expect((await http(SLUG, "GET", `/v1/me?${named("Mac mini", "0.4.0 (13)")}`, undefined, auth)).status).toBe(200);    const [touched] = await macRows("mac-me-2");    expect(touched!.data).toMatchObject(restated!.data);    expect(touched!.updated_at > restated!.updated_at, "a quiet pass still moves it on").toBe(true);    expect(touched!.version).toBeGreaterThan(restated!.version);  });  it("leaves a held row alone on any other naming call, and on /v1/me naming nothing", async () => {    const credential = await signIn(SLUG, "mac-me-2");    const auth = { authorization: `Bearer ${credential}` };    const [before] = await macRows("mac-me-2");    expect((await http(SLUG, "POST", `/v1/import/calls?${named("Renamed", "9")}`, { calls: [] }, auth)).status).toBe(200);    expect((await http(SLUG, "GET", "/v1/me", undefined, auth)).status).toBe(200);    const [after] = await macRows("mac-me-2");    expect(after!.updated_at).toBe(before!.updated_at);    expect(after!.data.name).toBe("Mac mini");  });});// Owner's decision, 22 Nov 2026: kept chat pictures are resealed on a phone, and an app from before reads a kept picture// plain, so the reseal waits for every device that pulls messages to say it opens the sealed ones (`opens=messages`).describe("the reseal of kept chat pictures waits for every device that reads messages", () => {  const SLUG = "sync-device-opens";  const facts = (name: string) => `device_name=${encodeURIComponent(name)}&platform=android&app_version=${encodeURIComponent("1.0 (20)")}`;  const plain = async (credential: string) => (await http<{ files: unknown[]; waiting?: string[] }>(SLUG, "GET", "/v1/media-plain?kind=messages", undefined, { authorization: `Bearer ${credential}` })).body;  it("names an older app that pulls messages, never one that does not, and goes on once it has updated", async () => {    const { credential } = await personalObject(SLUG);    await pass(SLUG, credential, `since=0&${facts("New Comms")}&opens=messages`);    expect((await plain(credential)).waiting ?? []).toEqual([]);    // Words, from before, pulls no messages: it holds nothing back.    const words = await signIn(SLUG, "old-words");    await pass(SLUG, words, `since=0&tables=note&${facts("Old Words")}`);    expect((await plain(credential)).waiting ?? []).toEqual([]);    // Comms on a second phone, from before: named, and nothing listed.    const old = await signIn(SLUG, "old-comms");    await pass(SLUG, old, `since=0&tables=message,thread&${facts("Old Comms")}`);    expect(await plain(credential)).toEqual({ files: [], next: null, waiting: ["Old Comms"] });    // Updated: it says so on its next pass, and the reseal goes on.    await pass(SLUG, old, `since=0&tables=message,thread&${facts("Old Comms")}&opens=messages`);    expect((await plain(credential)).waiting).toBeUndefined();  });});// AVATAR SEAL, 27 Nov 2026: faces (kind people) are sealed under the person table's files root, so their reseal waits on its// own for every device that pulls person to say it opens sealed faces (`opens=people`); saying messages alone is not enough.describe("the reseal of kept faces waits for every device that reads person, on its own", () => {  const SLUG = "sync-device-opens-people";  const facts = (name: string) => `device_name=${encodeURIComponent(name)}&platform=android&app_version=${encodeURIComponent("1.0 (20)")}`;  const plain = async (credential: string, kind: string) => (await http<{ files: unknown[]; waiting?: string[] }>(SLUG, "GET", `/v1/media-plain?kind=${kind}`, undefined, { authorization: `Bearer ${credential}` })).body;  it("names an app that opens sealed chat pictures but not faces, for faces only, and goes on once it says people", async () => {    const { credential } = await personalObject(SLUG);    await pass(SLUG, credential, `since=0&${facts("New Comms")}&opens=messages,people`);    expect((await plain(credential, "people")).waiting ?? []).toEqual([]);    // Comms from 22 Nov: opens sealed chat pictures, not faces. Held back for faces, not for chat pictures.    const mid = await signIn(SLUG, "mid-comms");    await pass(SLUG, mid, `since=0&tables=message,thread,person&${facts("Mid Comms")}&opens=messages`);    expect((await plain(credential, "messages")).waiting).toBeUndefined();    expect(await plain(credential, "people")).toEqual({ files: [], next: null, waiting: ["Mid Comms"] });    // Updated: it says people too, and the faces' reseal goes on.    await pass(SLUG, mid, `since=0&tables=message,thread,person&${facts("Mid Comms")}&opens=messages,people`);    expect((await plain(credential, "people")).waiting).toBeUndefined();    // AVATAR SEAL 2 (4): opens is a list, read by membership: a kind this Worker does not know yet, in any place in    // it, holds back neither chat pictures nor faces.    await pass(SLUG, mid, `since=0&tables=message,thread,person&${facts("Mid Comms")}&opens=${encodeURIComponent("albums,people,messages")}`);    expect((await plain(credential, "messages")).waiting).toBeUndefined();    expect((await plain(credential, "people")).waiting).toBeUndefined();  });});describe("apps that share one device credential each keep their own report (DEVICE ROWS)", () => {  const SLUG = "sync-device-apps";  it("keeps each bundle's newest report under apps, never replaced by an older one, the top level the newest", async () => {    await personalObject(SLUG);    const credential = await signIn(SLUG, "shared-keychain-phone");    const auth = { authorization: `Bearer ${credential}` };    const id = (await page(SLUG, `since=0&device_name=${encodeURIComponent("Synthetic iPhone")}&platform=ios&app_version=${encodeURIComponent("1.0.0 (31)")}`, credential)).device!;    const report = (reported_state: Record<string, unknown>) => http(SLUG, "POST", "/v1/act", { act: "update", args: { table: "device", id, data: { reported_state } } }, auth);    const held = async () => (await deviceRows(SLUG)).find((r) => r.id === id)!.data.reported_state as Record<string, unknown> & { apps: Record<string, Record<string, unknown>> };    expect((await report({ bundleId: "com.cottondb.notes", build: "31", last_exchange_at: "2026-11-25T19:27:00Z", last_answer: "ok", unsent: 0 })).status).toBe(200);    expect((await report({ bundleId: "com.cottondb.messages", build: "37", last_exchange_at: "2026-11-25T19:41:00Z", last_answer: "ok", unsent: 10 })).status).toBe(200);    // An older report from Words lands after Comms': it replaces neither Words' entry nor the top level.    expect((await report({ bundleId: "com.cottondb.notes", build: "30", last_exchange_at: "2026-11-25T19:00:00Z", last_answer: "offline", unsent: 4 })).status).toBe(200);    // A write that names apps itself is not believed: the map is the object's.    expect((await report({ bundleId: "com.cottondb.books", build: "19", last_exchange_at: "2026-11-25T19:50:00Z", last_answer: "ok", apps: { "com.cottondb.fake": { build: "1" } } })).status).toBe(200);    const state = await held();    expect(Object.keys(state.apps).sort()).toEqual(["com.cottondb.books", "com.cottondb.messages", "com.cottondb.notes"]);    expect(state.apps["com.cottondb.notes"]).toMatchObject({ build: "31", last_exchange_at: "2026-11-25T19:27:00Z", last_answer: "ok", unsent: 0 });    expect(state.apps["com.cottondb.messages"]).toMatchObject({ build: "37", unsent: 10 });    expect(state).toMatchObject({ bundleId: "com.cottondb.books", build: "19" });    // Older than the top level now held: the top stays Books', Comms' entry still moves only forward.    expect((await report({ bundleId: "com.cottondb.messages", build: "37", last_exchange_at: "2026-11-25T19:45:00Z", last_answer: "ok", unsent: 0 })).status).toBe(200);    const later = await held();    expect(later).toMatchObject({ bundleId: "com.cottondb.books", build: "19" });    expect(later.apps["com.cottondb.messages"]).toMatchObject({ last_exchange_at: "2026-11-25T19:45:00Z", unsent: 0 });    // list_devices' apps: one line per bundle, newest report first.    const lines = appReports(later);    expect(lines.map((l) => l.bundleId)).toEqual(["com.cottondb.messages", "com.cottondb.books", "com.cottondb.notes"]);    expect(lines[0]).toMatchObject({ build: "37", lastExchangeAt: "2026-11-25T19:45:00Z", lastAnswer: "ok", unsent: 0 });  });  it("keeps what waits on each app by table and by code, names and counts only, and says the oldest wait's age (UNSENT SEEN)", async () => {    const credential = await signIn(SLUG, "a-phone-that-waits");    const id = (await page(SLUG, `since=0&device_name=${encodeURIComponent("Synthetic iPhone")}&platform=ios&app_version=${encodeURIComponent("1.0.0 (38)")}`, credential)).device!;    const report = {      bundleId: "com.cottondb.messages",      build: "38",      last_exchange_at: "2026-11-26T04:36:00Z",      last_answer: "ok",      unsent: 3,      unsent_ops: 10,      waiting_by_table: { cotton_messages: 2, cotton_reactions: 1, "Not a table name": 5, cotton_bad: -1 },      waiting_by_code: { seal_key_missing: 8, queued: 2, cotton_secret_words: "a value" },      oldest_waiting_at: "2026-11-25T18:00:00Z",      oldest_seal_waiting_at: "2026-11-25T18:00:00Z",    };    expect((await http(SLUG, "POST", "/v1/act", { act: "update", args: { table: "device", id, data: { reported_state: report } } }, { authorization: `Bearer ${credential}` })).status).toBe(200);    const held = (await deviceRows(SLUG)).find((r) => r.id === id)!.data.reported_state as { apps: Record<string, Record<string, unknown>> };    const entry = held.apps["com.cottondb.messages"]!;    expect(entry).toMatchObject({ unsent: 3, unsent_ops: 10, waiting_by_table: { cotton_messages: 2, cotton_reactions: 1 }, waiting_by_code: { seal_key_missing: 8, queued: 2 } });    expect(JSON.stringify(entry)).not.toContain("a value");    expect(JSON.stringify(entry)).not.toContain("Not a table name");    const [line] = appReports(held, Date.parse("2026-11-26T06:00:00Z"));    expect(line).toMatchObject({      bundleId: "com.cottondb.messages",      unsent: 3,      unsentOps: 10,      waitingByTable: { cotton_messages: 2, cotton_reactions: 1 },      waitingByCode: { seal_key_missing: 8, queued: 2 },      oldestWaitingAt: "2026-11-25T18:00:00Z",      oldestWaitingMinutes: 720,      oldestSealWaitingMinutes: 720,    });  });  it("keeps a first pull's progress as whole counts only, said every few pages (FIRST SYNC FAST)", async () => {    const credential = await signIn(SLUG, "a-phone-in-its-first-pull");    const id = (await page(SLUG, `since=0&device_name=${encodeURIComponent("Synthetic iPhone")}&platform=ios&app_version=${encodeURIComponent("1.0.0 (39)")}`, credential)).device!;    const report = { bundleId: "com.cottondb.mail", build: "39", pass_done: 14_425, pass_total: 280_062, parked: 9_000 };    expect((await http(SLUG, "POST", "/v1/act", { act: "update", args: { table: "device", id, data: { reported_state: report } } }, { authorization: `Bearer ${credential}` })).status).toBe(200);    let held = (await deviceRows(SLUG)).find((r) => r.id === id)!.data.reported_state as { apps: Record<string, Record<string, unknown>> };    expect(held.apps["com.cottondb.mail"]).toMatchObject({ pass_done: 14_425, pass_total: 280_062, parked: 9_000 });    expect(appReports(held)[0]).toMatchObject({ passDone: 14_425, passTotal: 280_062, parked: 9_000 });    const odd = { bundleId: "com.cottondb.mail", build: "39", pass_done: "a value", pass_total: -1, parked: 1.5 };    expect((await http(SLUG, "POST", "/v1/act", { act: "update", args: { table: "device", id, data: { reported_state: odd } } }, { authorization: `Bearer ${credential}` })).status).toBe(200);    held = (await deviceRows(SLUG)).find((r) => r.id === id)!.data.reported_state as { apps: Record<string, Record<string, unknown>> };    const entry = held.apps["com.cottondb.mail"]!;    expect([entry.pass_done, entry.pass_total, entry.parked]).toEqual([undefined, undefined, undefined]);  });  it("writes a report without a bundleId exactly as before", async () => {    const credential = await signIn(SLUG, "a-mac");    const id = (await page(SLUG, `since=0&device_name=Mac&platform=macos&app_version=0.11.0`, credential)).device!;    expect((await http(SLUG, "POST", "/v1/act", { act: "update", args: { table: "device", id, data: { reported_state: { build: "11", schema: 140 } } } }, { authorization: `Bearer ${credential}` })).status).toBe(200);    expect((await deviceRows(SLUG)).find((r) => r.id === id)!.data.reported_state).toEqual({ build: "11", schema: 140 });  });});