/** * ALARM DEAD (live, 28 Nov 2026): the owner's alarm stood six hours in the past (getAlarm() said 03:53 IST) and nothing * fired; a request did not bring it round, since "an alarm is set" never set one again. Here: an alarm the platform has * stopped retrying (getAlarm() a past time) is revived by the next request within seconds; a handler that throws six * times never propagates (no retry storm) and leaves an alarm a minute out each time; and the next alarm is set, durably, * before the handler's work begins, so a reset inside cannot leave the object without one. */ import { env, runInDurableObject } from "cloudflare:test"; import { describe, expect, it } from "vitest"; import { http, personalObject } from "./personal.ts"; const SLUG = "alarm-dead"; const stub = () => env.BUSINESS.get(env.BUSINESS.idFromName(SLUG)); type Live = { alarm(): Promise; alarmPass(): Promise }; type Storage = { getAlarm(): Promise; setAlarm(t: number): Promise }; describe("an alarm the platform stopped is revived (ALARM DEAD)", { timeout: 300_000 }, () => { it("a request revives an alarm six hours past within seconds", async () => { const { owner } = await personalObject(SLUG); const past = Date.now() - 6 * 3_600_000; // The platform's word, as on live: an alarm set for a time long gone, firing no more (the test pool would fire a // real past alarm at once, so its answer is held here until the object sets one). await runInDurableObject(stub(), async (_i, s) => { // No real alarm behind it: only a revival sets one. await s.storage.deleteAlarm(); const storage = s.storage as unknown as Storage & { __get?: Storage["getAlarm"]; __set?: Storage["setAlarm"] }; storage.__get = storage.getAlarm.bind(storage); storage.__set = storage.setAlarm.bind(storage); let stuck = true; storage.getAlarm = async () => (stuck ? past : storage.__get!()); storage.setAlarm = async (t: number) => { stuck = false; return storage.__set!(t); }; }); expect((await http(SLUG, "GET", "/v1/definitions", undefined, { authorization: `Bearer ${owner}` })).status).toBe(200); const next = await runInDurableObject(stub(), async (_i, s) => { const storage = s.storage as unknown as Storage & { __get?: Storage["getAlarm"]; __set?: Storage["setAlarm"] }; const at = await storage.__get!(); storage.getAlarm = storage.__get!; storage.setAlarm = storage.__set!; return at === null ? null : at - Date.now(); }); expect(next, "revived").not.toBeNull(); expect(next!).toBeLessThanOrEqual(30_000); expect(next!).toBeGreaterThan(-5_000); }); it("a handler that throws six times never propagates, and leaves an alarm a minute out each time; the next is set before the work", async () => { const outcomes = await runInDurableObject(stub(), async (instance, s) => { const l = instance as unknown as Live; const pass = l.alarmPass; const seen: { threw: boolean; setBefore: number | null; after: number | null }[] = []; try { for (let i = 0; i < 6; i++) { let setBefore: number | null = null; l.alarmPass = async () => { // Inside the work: the next alarm already stands, a minute out (a reset here would still leave it). const at = await s.storage.getAlarm(); setBefore = at === null ? null : at - Date.now(); throw new Error(`an invented failure ${i + 1}`); }; let threw = false; await s.storage.deleteAlarm(); try { await l.alarm(); } catch { threw = true; } const after = await s.storage.getAlarm(); seen.push({ threw, setBefore, after: after === null ? null : after - Date.now() }); } } finally { l.alarmPass = pass; } return seen; }); for (const o of outcomes) { expect(o.threw, "no throw out of the handler").toBe(false); expect(o.setBefore, "set before the work").not.toBeNull(); expect(o.setBefore!).toBeLessThanOrEqual(60_000); expect(o.after, "an alarm after").not.toBeNull(); expect(o.after!).toBeLessThanOrEqual(60_000); } }); });