Cotton code
86 lines · 5.0 KB · TypeScriptRaw
/** * RULE (e2e rulings R1, R2; 16 Nov 2026): events are * sealed. The Google Calendar pull writes a new event sealed at arrival to * the person's arrival key, and a change Google makes to an event the * object holds sealed as an arrival patch beside it: only its dates, ids and * marks are clear, its title, notes and place never. An event Google did not * change (its etag stands) takes nothing. The object judges no kind by words * it cannot read. */import { env, runDurableObjectAlarm, runInDurableObject } from "cloudflare:test";import { afterEach, describe, expect, it } from "vitest";import { macRowId } from "../src/google_calendar.ts";import { bodyAad } from "../src/seal.ts";import { Store, type StoredRow } from "../src/store.ts";import { openArrivalRow } from "./arrival_keys.ts";import { connectAccount, FakeCalendar, google, primary, quiet, revoked, tokens } from "./google.ts";import { DATABASE_ID, http, personalObject } from "./personal.ts";import { holdsWord } from "./clear-word.ts";afterEach(() => {  google(tokens, primary, quiet, revoked);});const stub = (slug: string) => env.BUSINESS.get(env.BUSINESS.idFromName(slug));const UID = (id: string) => `${id}@google.com`;async function phase(slug: string, owner: string): Promise<string> {  const res = await http<{ accounts: { calendar: { phase: string } }[] }>(slug, "GET", "/v1/google/status", undefined, { authorization: `Bearer ${owner}` });  return res.body.accounts[0]!.calendar.phase;}async function settle(slug: string, owner: string, asked: () => boolean = () => true): Promise<void> {  for (let i = 0; i < 12; i++) {    await runDurableObjectAlarm(stub(slug));    if ((await phase(slug, owner)) === "synced" && asked()) return;  }  throw new Error("the pass never settled");}async function event(slug: string, id: string): Promise<StoredRow> {  return runInDurableObject(stub(slug), (_instance, state) => new Store(state.storage.sql, () => new Date()).get("event", id)!);}/** An event as a device holding the data key opens it: the arrival row, each arrival patch folded over it in order. */async function opened(row: StoredRow): Promise<Record<string, unknown>> {  const aad = bodyAad(DATABASE_ID, "event", row.id);  const out = await openArrivalRow(row.data, aad, "event", row.id);  for (const p of (row.data.arrival_patches as Record<string, unknown>[] | undefined) ?? []) Object.assign(out, await openArrivalRow(p, aad, "event", row.id));  return out;}describe("the Google Calendar pull on sealed events", () => {  it("lands a new event sealed at arrival and a change as an arrival patch, and an unchanged one takes nothing", async () => {    const slug = "gcsealed";    const { owner } = await personalObject(slug, { arrival: true });    const cal = new FakeCalendar();    cal.put({ id: "parade", iCalUID: UID("parade"), summary: "Quillon parade", description: "Bring the brass drum", location: "Ferrous lane", start: { dateTime: "2026-10-28T10:00:00Z" }, end: { dateTime: "2026-10-28T11:00:00Z" } });    google(tokens, primary, cal.route, revoked);    await connectAccount(slug, owner);    await settle(slug, owner);    const id = await macRowId("calendar", UID("parade"));    const first = await event(slug, id);    expect(String(first.data.dek_enc)).toMatch(/^a1:/);    expect(first.data).toMatchObject({ gcal_id: "parade", starts_at: "2026-10-28T10:00:00.000Z", account: expect.any(String), on_google: true });    for (const k of ["title", "notes", "location"]) expect(first.data, k).not.toHaveProperty(k);    expect(await opened(first)).toMatchObject({ title: "Quillon parade", notes: "Bring the brass drum", location: "Ferrous lane" });    expect(first.data.kind ?? null, "no kind judged by sealed words").toBeNull();    const asked = cal.asked.length;    cal.put({ id: "parade", iCalUID: UID("parade"), summary: "Quillon parade, moved", description: "Bring the brass drum", location: "Cobalt square", start: { dateTime: "2026-10-28T12:00:00Z" }, end: { dateTime: "2026-10-28T13:00:00Z" } });    await settle(slug, owner, () => cal.asked.length > asked);    const changed = await event(slug, id);    expect(changed.data.starts_at).toBe("2026-10-28T12:00:00.000Z");    expect(changed.data.sealed_enc, "the sealed half the phone sealed stays as it was").toBe(first.data.sealed_enc);    expect(changed.data.arrival_patches).toHaveLength(1);    expect(await opened(changed)).toMatchObject({ title: "Quillon parade, moved", location: "Cobalt square", notes: "Bring the brass drum" });    const kept = await runInDurableObject(stub(slug), (_i, state) => JSON.stringify([state.storage.sql.exec("SELECT data FROM rows WHERE table_id = 'event'").toArray(), state.storage.sql.exec("SELECT before, after FROM change WHERE table_id = 'event'").toArray()]));    for (const w of ["Quillon", "brass", "Ferrous", "Cobalt"]) expect(holdsWord(kept, w), w).toBe(false);    // A pass that brings nothing new writes no patch.    const again = cal.asked.length;    await settle(slug, owner, () => cal.asked.length > again);    expect((await event(slug, id)).data.arrival_patches).toHaveLength(1);  });});