Cotton code
45 lines · 2.7 KB · TypeScriptRaw
import { env, runInDurableObject } from "cloudflare:test";import { describe, expect, it } from "vitest";import { Store } from "../src/store.ts";/** * OLDER ROWS, A PAGE AT A TIME (MAIL.md, 21 Nov 2026): Comms keeps a window of * mail on the phone, and `GET /v1/older` pages what is older, newest first by * the table's clear date column, as a person scrolls past the window; with * `eq` one conversation's mail. Invented rows. */describe("a dated table's older rows, newest first, a page at a time", () => {  it("walks every live row older than the window once, in order, and one conversation alone with eq", async () => {    const stub = env.BUSINESS.get(env.BUSINESS.idFromName("older-page"));    await runInDurableObject(stub, (_instance, state) => {      const store = new Store(state.storage.sql, () => new Date("2026-11-21T10:00:00Z"));      let version = 0;      const at = (n: number): string => new Date(Date.UTC(2026, 0, 1) + n * 3_600_000).toISOString();      // 60 emails over 6 conversations; two share each moment; every seventh is binned.      for (let n = 0; n < 60; n++) {        const row = store.insert("email", `e-${String(n).padStart(3, "0")}`, { thread_id: `et-${n % 6}`, sent_at: at(Math.floor(n / 2)), sealed_enc: "s1:x" }, null, ++version);        if (n % 7 === 0) store.tombstone(row, ++version);      }      const live = (n: number): boolean => n % 7 !== 0;      const window = at(20);      const want = [...Array(60).keys()].filter((n) => live(n) && at(Math.floor(n / 2)) < window).sort((a, b) => Math.floor(b / 2) - Math.floor(a / 2) || b - a).map((n) => `e-${String(n).padStart(3, "0")}`);      const got: string[] = [];      let before: [string, string] = [window, ""];      for (;;) {        const page = store.olderPage("email", "sent_at", { before, from: null, to: null, eq: null, limit: 7 });        got.push(...page.map((r) => r.id));        if (page.length < 7) break;        const last = page[page.length - 1]!;        before = [String(last.data.sent_at), last.id];      }      expect(got).toEqual(want);      const one = store.olderPage("email", "sent_at", { before: null, from: null, to: null, eq: { column: "thread_id", value: "et-2" }, limit: 100 }).map((r) => r.id);      expect(one).toEqual(want.concat([...Array(60).keys()].filter((n) => live(n) && at(Math.floor(n / 2)) >= window).map((n) => `e-${String(n).padStart(3, "0")}`)).filter((id) => Number(id.slice(2)) % 6 === 2).sort((a, b) => Number(b.slice(2)) - Number(a.slice(2))));      const span = store.olderPage("email", "sent_at", { before: null, from: at(5), to: at(6), eq: null, limit: 100 }).map((r) => r.id);      expect(span).toEqual(["e-013", "e-012", "e-011", "e-010"].filter((id) => live(Number(id.slice(2)))));    });  });});