/** * JOBS OUT C1: the Google Calendar pull (src/google_calendar.ts `pullAll`) as a step job on M1's door * (src/step_jobs.ts, kind `calendar_sync`), behind its own per-account switch, off by default. Off, the alarm pulls * exactly as before; on, the alarm only watches, and each run pulls one bounded pass (PAGES_PER_PASS pages) through * the door, at the pull's own cadence. Invented calendars and accounts only; Google is the fake in test/google.ts. */ import { env, runDurableObjectAlarm, runInDurableObject, SELF } from "cloudflare:test"; import { afterEach, describe, expect, it } from "vitest"; import type { Business } from "../src/business.ts"; import { CARRY_ON_MS, PAGES_PER_PASS } from "../src/google_calendar.ts"; import { runStepJob, type StepJobMessage } from "../src/jobs/step_job.ts"; import { Store } from "../src/store.ts"; import { connectAccount, FakeCalendar, google, primary, quiet, revoked, tokens, type Call } from "./google.ts"; import { ADMIN, EVENTS_OPEN, http, personalObject } from "./personal.ts"; afterEach(() => { google(tokens, primary, quiet, revoked); }); const KIND = "calendar_sync"; const stub = (slug: string) => env.BUSINESS.get(env.BUSINESS.idFromName(slug)); /** The door, as the queue's consumer reaches it: through the front Worker, on /b//…. */ const door = (slug: string, path: string, init: RequestInit) => SELF.fetch(`https://jobs.internal/b/${slug}${path}`, init); const messageOf = (slug: string): Promise => runInDurableObject(stub(slug), (instance: Business) => instance.stepJobMessage(KIND)); const jobRow = (slug: string) => runInDurableObject(stub(slug), (_i, state) => state.storage.sql.exec>("SELECT * FROM step_job WHERE kind = ?", KIND).toArray()[0] ?? null); const alarmIn = (slug: string) => runInDurableObject(stub(slug), async (_i, state) => ((await state.storage.getAlarm()) ?? Infinity) - Date.now()); const UID = (id: string) => `${id}@google.com`; const listings = (calls: Call[]) => calls.filter((c) => c.method === "GET" && /\/calendar\/v3\/calendars\/[^/]+\/events$/.test(c.url.pathname)).length; /** Every live event row: Google's id → its title. */ async function events(slug: string): Promise> { return runInDurableObject(stub(slug), (_i, state) => { const store = new Store(state.storage.sql, () => new Date()); return new Map([...store.scan("event")].filter((r) => !r.deleted).map((r) => [String(r.data.gcal_id), r.data.title ?? null])); }); } function calendarOf(count: number): FakeCalendar { const cal = new FakeCalendar(); for (let i = 0; i < count; i++) cal.put({ id: `e${i}`, iCalUID: UID(`e${i}`), summary: `Invented E${i}`, start: { dateTime: `2026-11-${String((i % 28) + 1).padStart(2, "0")}T10:00:00Z` } }); return cal; } describe("JOBS OUT C1: the Google Calendar pull as a step job", { timeout: 120_000 }, () => { it("is switched per account by the admin alone, on M1's switch, and is off by default", async () => { const slug = "c1-switch"; const { owner } = await personalObject(slug, EVENTS_OPEN); expect((await http(slug, "PUT", "/v1/admin/business", { jobs: { [KIND]: "on" } }, { authorization: `Bearer ${owner}` })).status).toBe(403); expect((await http(slug, "PUT", "/v1/admin/business", { jobs: { [KIND]: "yes" } }, ADMIN)).status).toBe(400); // Off by default: the door opens nothing, and a credential that does not open answers as it would with the kind on // (AUDIT F1/F2: the door never says whether a kind is on). const r = await door(slug, `/v1/jobs/step/${KIND}`, { method: "POST", headers: { authorization: "CottonJob j1.x.y" } }); expect(r.status).toBe(401); expect((await http(slug, "PUT", "/v1/admin/business", { jobs: { [KIND]: "on" } }, ADMIN)).status).toBe(200); expect((await http(slug, "PUT", "/v1/admin/business", { jobs: { [KIND]: null } }, ADMIN)).status).toBe(200); }); it("off: the alarm pulls as before, and a credential minted while it was on opens nothing", async () => { const slug = "c1-off"; const { owner } = await personalObject(slug, EVENTS_OPEN); google(tokens, primary, calendarOf(3).route, revoked); await http(slug, "PUT", "/v1/admin/business", { jobs: { [KIND]: "on" } }, ADMIN); const stale = await messageOf(slug); await http(slug, "PUT", "/v1/admin/business", { jobs: { [KIND]: null } }, ADMIN); await connectAccount(slug, owner); for (let i = 0; i < 4 && (await events(slug)).size < 3; i++) await runDurableObjectAlarm(stub(slug)); expect((await events(slug)).size).toBe(3); expect(await runStepJob({ door }, stale)).toBe("gone"); }); it("on: the alarm calls no Google; each run pulls one bounded pass, the next asked for at the pull's cadence, and the rows are the old path's", async () => { const total = 2 * PAGES_PER_PASS + 3; // The old path, for what it lands. const off = "c1-same-off"; { const { owner } = await personalObject(off, EVENTS_OPEN); google(tokens, primary, calendarOf(total).route, revoked); await connectAccount(off, owner); for (let i = 0; i < 12 && (await events(off)).size < total; i++) await runDurableObjectAlarm(stub(off)); } const slug = "c1-same-on"; const { owner } = await personalObject(slug, EVENTS_OPEN); expect((await http(slug, "PUT", "/v1/admin/business", { jobs: { [KIND]: "on" } }, ADMIN)).status).toBe(200); const cal = calendarOf(total); expect(cal.pageSize).toBe(2); const calls = google(tokens, primary, cal.route, revoked); await connectAccount(slug, owner); for (let i = 0; i < 3; i++) await runDurableObjectAlarm(stub(slug)); expect(listings(calls), "with the switch on the alarm lists nothing").toBe(0); expect((await events(slug)).size).toBe(0); // The alarm's watchdog asked for the first run on the queue. expect((await jobRow(slug))!.sent_at).not.toBeNull(); const m1 = await messageOf(slug); expect(await runStepJob({ door }, m1)).toBe("ran"); expect(listings(calls), "one run reads at most one pass's pages").toBe(PAGES_PER_PASS); expect((await events(slug)).size).toBe(2 * PAGES_PER_PASS); // Pages left: the next pass is the pull's carry-on, 30 s on, not at once; the alarm comes then to ask for it. expect(await alarmIn(slug)).toBeLessThanOrEqual(CARRY_ON_MS + 1000); // The same message delivered again runs nothing. expect(await runStepJob({ door }, m1)).toBe("gone"); expect(await runStepJob({ door }, await messageOf(slug))).toBe("ran"); expect((await events(slug)).size).toBe(total); expect([...(await events(slug)).entries()].sort()).toEqual([...(await events(off)).entries()].sort()); expect(listings(calls)).toBe(PAGES_PER_PASS + 2); }); });