Cotton code
45 lines · 2.1 KB · TypeScriptRaw
import { env, runInDurableObject } from "cloudflare:test";import { describe, expect, it } from "vitest";import { Store } from "../src/store.ts";/** * THE BIN FROM THE OBJECT (TRIM 3, 21 Nov 2026): a phone's copy keeps only the * last 7 days of its Bin, and lists the rest from `GET /v1/bin`: the binned * rows of the tables it names, a page at a time, in (table, id) order. A live * row, a deleted row and a row of another table are never in it. Invented rows. */describe("binned rows, a page at a time", () => {  it("walks every binned row of the named tables once, and nothing else", async () => {    const stub = env.BUSINESS.get(env.BUSINESS.idFromName("bin-page"));    await runInDurableObject(stub, (_instance, state) => {      const store = new Store(state.storage.sql, () => new Date("2026-11-21T10:00:00Z"));      let version = 0;      const want: string[] = [];      for (let n = 0; n < 90; n++) {        const table = ["photo", "note", "event"][n % 3]!;        const id = `r-${String(n).padStart(3, "0")}`;        const binned = n % 4 === 0;        const row = store.insert(table, id, { title: "invented", ...(binned ? { trashed_at: "2026-11-01T00:00:00Z" } : {}) }, null, ++version);        const gone = n % 8 === 0;        if (gone) store.tombstone(row, ++version);        if (binned && !gone && table !== "event") want.push(`${table}:${id}`);      }      want.sort();      const got: string[] = [];      let after: { table: string; id: string } | null = null;      for (;;) {        const page = store.binnedPage(["photo", "note"], after, 4);        got.push(...page.rows.map((r) => `${r.table}:${r.id}`));        expect(page.rows.every((r) => typeof r.data.trashed_at === "string")).toBe(true);        if (!page.more) break;        const last = page.rows[page.rows.length - 1]!;        after = { table: last.table, id: last.id };      }      expect(got).toEqual(want);      expect(want.length).toBe(8);      // Many tables named, no bound value per table.      expect(store.binnedPage([...Array(200).keys()].map((n) => `t${n}`).concat(["note"]), null, 1000).rows.length).toBe(want.filter((w) => w.startsWith("note:")).length);    });  });});