85 lines · 4.4 KB · TypeScriptRaw
/** * RULE (MAIL ALL ON PHONE, 24 Nov 2026): a sync page closes by its bytes as well as its rows. The owner's phone * takes all his mail (111,422 mails, bodies up to 65,536 characters, sealed and padded), and a page of 5,000 such * rows would hold hundreds of MB in an object of 128 MB. Each page here holds at most PAGE_BYTES and one read more, * and the pass still carries every row exactly once, in a first pass and in a pass of changes. All rows invented. */import { env, runInDurableObject } from "cloudflare:test";import { describe, expect, it } from "vitest";import { declarations, planFirst } from "../src/first.ts";import { PAGE_BYTES, READ_ROWS, Store, type Cursor, type FirstAt, type FirstPlan, type StoredRow, type SyncScope } from "../src/store.ts";const BIG = 60_000;const MAILS = 250;const owner: SyncScope = { kind: "all", except: new Set() };const DECLS = declarations({ test: { first: { email_thread: { by: "last_at", rows: 5, late: true }, email: { by: "sent_at", per: "thread_id", of: "email_thread", rows: 2, groups: 5, late: true }, }, },});const key = (r: { table: string; id: string }): string => `${r.table}/${r.id}`;const weight = (rows: StoredRow[]): number => rows.reduce((n, r) => n + JSON.stringify(r.data).length + 200, 0);function build(store: Store): number { let version = 0; for (let t = 0; t < 8; t++) store.insert("email_thread", `et-${t}`, { last_at: `2026-11-${String(10 + t).padStart(2, "0")}T00:00:00Z` }, null, ++version); // 250 invented mails, most of them long: about 12 MB in all, so a page of 5,000 rows would hold them all. for (let m = 0; m < MAILS; m++) store.insert("email", `em-${String(m).padStart(3, "0")}`, { thread_id: `et-${m % 8}`, sent_at: `2026-11-${String(1 + (m % 28)).padStart(2, "0")}T00:00:00Z`, body: "x".repeat(m % 5 === 0 ? 40 : BIG) }, null, ++version); for (let n = 0; n < 30; n++) store.insert("note", `n-${n}`, { n }, null, ++version); return version;}async function withStore<T>(name: string, run: (store: Store) => T): Promise<T> { const stub = env.BUSINESS.get(env.BUSINESS.idFromName(name)); return runInDurableObject(stub, (_instance, state) => run(new Store(state.storage.sql, () => new Date("2026-11-24T10:00:00Z"))));}describe("a sync page closes by its bytes", () => { it("a pass of changes: each page at most PAGE_BYTES and one read, every row once", async () => { await withStore("page-bytes-changed", (store) => { const upto = build(store); const seen: string[] = []; let pages = 0; for (let after: Cursor | null = null; ; ) { const page = store.changedPage({ since: 0, upto, limit: 5000, scope: owner, windows: [], small: ["note"], floor: null, after }); pages++; expect(weight(page.rows)).toBeLessThan(PAGE_BYTES + READ_ROWS * (BIG + 300)); seen.push(...page.rows.map(key)); if (!page.more) break; const last = page.rows[page.rows.length - 1]!; after = { version: last.version, table: last.table, id: last.id }; } expect(pages).toBeGreaterThan(2); expect(new Set(seen).size).toBe(seen.length); expect(seen.length).toBe(8 + MAILS + 30); }); }); it("a first pass: each page at most PAGE_BYTES and one read, every row once, the cuts kept", async () => { await withStore("page-bytes-first", (store) => { const upto = build(store); const specs = planFirst((t) => ({ email_thread: 8, email: MAILS, note: 30 })[t] ?? 0, "2026-11-24", 10, DECLS); store.firstIndexes(specs); store.firstPass("pass-bytes", 1000, 0); const small = store.smallFirst({ since: 0, upto, scope: owner, windows: [], most: 10 }).filter((t) => !specs.some((s) => s.table === t)); let cursor: { plan: FirstPlan; at: FirstAt | null } = { plan: { pass: "pass-bytes", small, specs, cuts: specs.map(() => null) }, at: null }; const seen: string[] = []; let pages = 0; for (;;) { const got = store.firstPage({ since: 0, upto, limit: 5000, scope: owner, windows: [], ...cursor }); pages++; expect(weight(got.rows)).toBeLessThan(PAGE_BYTES + READ_ROWS * (BIG + 300)); seen.push(...got.rows.map(key)); if (!got.more) break; cursor = JSON.parse(JSON.stringify({ plan: got.plan, at: got.at })) as typeof cursor; } expect(pages).toBeGreaterThan(2); expect(new Set(seen).size).toBe(seen.length); expect(seen.length).toBe(8 + MAILS + 30); }); });});