import { env, runInDurableObject } from "cloudflare:test"; import { describe, expect, it } from "vitest"; import type { Business } from "../src/business.ts"; import { ADMIN, DATABASE_ID, http, personalObject, put } from "./personal.ts"; const sha = (n: number): string => n.toString(16).padStart(64, "0"); /** RULE (owner, 21 Nov 2026): a whole page of the prune (200 photos, all missing) answers, on a dry run and applied: a statement binds at most 100 values on the object (the live dry run's 500, 21 Nov 2026), tested here under 50. */ describe("the photo prune, a whole page", () => { const slug = "photo-prune-page"; it("counts and bins 250 missing photos in pages of 200", async () => { await personalObject(slug); const rows: Record[] = []; for (let i = 1; i <= 250; i++) rows.push({ ref: `m${i}`, table: "photo", data: { key: `${DATABASE_ID}/messages/t/${sha(i)}.jpg`, sha256: sha(i), taken_at: "2024-03-01T10:00:00Z" } }); for (let i = 1; i <= 150; i++) rows.push({ table: "sighting", data: { record_kind: "photo", record_id: `@m${i}`, source: "whatsapp", source_id: `s${i}`, photo_id: `@m${i}` } }); await put(slug, rows); // The object's SQL refuses a statement binding more than 50 values, as the live one refuses past 100 ("too many SQL variables"). let most = 0; await runInDurableObject(env.BUSINESS.get(env.BUSINESS.idFromName(slug)), (instance: Business) => { const store = (instance as unknown as { store: { sql: SqlStorage } }).store; const real = store.sql; store.sql = new Proxy(real, { get(target, name) { if (name !== "exec") return Reflect.get(target, name, target); return (query: string, ...bindings: unknown[]) => { most = Math.max(most, bindings.length); if (bindings.length > 50) throw new Error(`too many SQL variables: ${bindings.length}`); return target.exec(query, ...bindings); }; }, }); }); for (const apply of [false, true]) { let after: string | null = null; let binned = 0; let missing = 0; let sightings = 0; do { const res: { status: number; body: { missing: number; binned: number; referred: { sighting: number }; next: string | null } } = await http<{ missing: number; binned: number; referred: { sighting: number }; next: string | null }>(slug, "POST", "/v1/admin/photos/prune", { folder: "messages", after, apply }, ADMIN); expect(res.status, JSON.stringify(res.body)).toBe(200); missing += res.body.missing; binned += res.body.binned; sightings += res.body.referred.sighting; after = res.body.next; } while (after !== null); expect(missing).toBe(250); expect(sightings).toBe(150); expect(binned).toBe(apply ? 250 : 0); } expect(most, "the most values one statement bound").toBeLessThanOrEqual(50); }); });