Cotton code
99 lines · 4.8 KB · TypeScriptRaw
/** * JOBS OUT 4, the dead-man check: the Worker's cron, not the account object's own alarm, looks at each account's alarm * and sets again one that is missing or stands more than 10 minutes in the past; one that stands in the future is left. * Every account here is invented. */import { env, runDurableObjectAlarm, runInDurableObject } from "cloudflare:test";import { describe, expect, it, vi } from "vitest";import { ALARM_STALE_MS, alarmStale, watchAlarms } from "../src/alarm_watch.ts";import { directory } from "../src/directory.ts";import { markerKey } from "../src/restore.ts";import { bearer, open, signUp } from "./calling.ts";import { DATABASE_ID, OWNER_PHONE, personalObject } from "./personal.ts";vi.setConfig({ testTimeout: 300_000 });function object(slug: string) {  return env.BUSINESS.get(env.BUSINESS.idFromName(slug));}describe("the dead-man check", () => {  it("knows a dead alarm: none, or past by more than ten minutes", () => {    const now = Date.parse("2026-11-28T04:25:00Z");    expect(alarmStale(null, now)).toBe(true);    expect(alarmStale(now - ALARM_STALE_MS - 1, now)).toBe(true);    expect(alarmStale(now - 60_000, now)).toBe(false);    expect(alarmStale(now + 60_000, now)).toBe(false);  });  it("re-arms an account whose alarm is gone, from the cron, and leaves a live one alone", async () => {    const slug = "alarm-watch-dead";    await personalObject(slug);    const entered = await directory(env).enter({ databaseId: DATABASE_ID, slug, phone: OWNER_PHONE });    expect(entered, JSON.stringify(entered)).toHaveProperty("ok");    await runInDurableObject(object(slug), async (_instance, state) => {      await state.storage.deleteAlarm();    });    const before = Date.now();    const run = await watchAlarms(env);    expect(run.rearmed).toContain(slug);    const at = await runInDurableObject(object(slug), (_instance, state) => state.storage.getAlarm());    expect(at).not.toBeNull();    expect(at!).toBeGreaterThanOrEqual(before);    expect(at!).toBeLessThan(before + 60_000);    // Live again: the next look changes nothing.    await runInDurableObject(object(slug), async (_instance, state) => {      await state.storage.setAlarm(Date.now() + 3_600_000);    });    const again = await watchAlarms(env);    expect(again.rearmed).not.toContain(slug);    const seen = await object(slug).alarmWatch();    expect(seen.rearmed).toBe(false);    expect(seen.alarm_at).not.toBeNull();  });  it("an idle live account always holds an alarm: a pass with nothing to do sets the next", async () => {    const slug = "alarm-watch-idle";    await personalObject(slug);    await runInDurableObject(object(slug), async (_instance, state) => {      await state.storage.setAlarm(Date.now());    });    await runDurableObjectAlarm(object(slug));    const at = await runInDurableObject(object(slug), (_instance, state) => state.storage.getAlarm());    expect(at, "every pass sets the next").not.toBeNull();    expect(at!).toBeGreaterThan(Date.now());  });  it("leaves alone an account being deleted whose alarm its own code left unset", async () => {    const credential = await signUp("+15550109901", "watch-delete");    const slug = `p-${credential.split("_")[1]!}`;    const code = await open<{ challenge_id: string }>("POST", "/v1/account/delete/code", {}, bearer(credential));    expect(code.status, JSON.stringify(code.body)).toBe(200);    const asked = await open("POST", "/v1/account/delete", { challenge_id: code.body.challenge_id, code: "123456", confirm: "delete" }, bearer(credential));    expect(asked.status, JSON.stringify(asked.body)).toBe(202);    await runInDurableObject(object(slug), async (_instance, state) => {      await state.storage.deleteAlarm();    });    const run = await watchAlarms(env);    expect(run.rearmed).not.toContain(slug);    expect(await runInDurableObject(object(slug), (_instance, state) => state.storage.getAlarm())).toBeNull();    expect((await object(slug).alarmWatch()).left).toBe("deleting");  });  it("leaves alone an account mid-restore (frozen, or an owner's restore stepping on its own alarm)", async () => {    const credential = await signUp("+15550109902", "watch-restore");    const slug = `p-${credential.split("_")[1]!}`;    await env.PHOTOS.put(markerKey(slug), JSON.stringify({ frozen: true, owner: true }));    await runInDurableObject(object(slug), async (instance, state) => {      (instance as unknown as { restoreMarker: unknown }).restoreMarker = null;      await state.storage.deleteAlarm();    });    const run = await watchAlarms(env);    expect(run.rearmed).not.toContain(slug);    expect(await runInDurableObject(object(slug), (_instance, state) => state.storage.getAlarm())).toBeNull();    expect((await object(slug).alarmWatch()).left).toBe("restoring");    await env.PHOTOS.delete(markerKey(slug));  });});