/** * JOBS OUT 5: Send's pass (offers past their time, a large Keep's copy, kept sends landing) as a step job on the * JOBS OUT M1 door (src/step_jobs.ts). Its own switch, off by default: off, the alarm runs it as before; on, the alarm * never runs it, a send's call or the watchdog asks for a run, and the run steps it through the door. Invented data. */ import { env, runInDurableObject, SELF } from "cloudflare:test"; import { describe, expect, it } from "vitest"; import type { Business } from "../src/business.ts"; import { runStepJob } from "../src/jobs/step_job.ts"; import { ADMIN, http, personalObject } from "./personal.ts"; const stub = (slug: string) => env.BUSINESS.get(env.BUSINESS.idFromName(slug)); const door = (slug: string, path: string, init: RequestInit) => SELF.fetch(`https://jobs.internal/b/${slug}${path}`, init); const messageOf = (slug: string) => runInDurableObject(stub(slug), (instance: Business) => instance.stepJobMessage("send")); const DAY = 86_400_000; /** An offer two months old: Send's pass takes it away. */ async function staleOffer(slug: string, id: string): Promise { await runInDurableObject(stub(slug), (_i, state) => { state.storage.sql.exec("INSERT INTO send_in (id, peer, peer_slug, payload, title, root_table, bytes, state, at) VALUES (?, '+15550100001', 'p-invented', '{}', 'Invented', 'note', 0, 'offered', ?)", id, Date.now() - 60 * DAY); }); } const offers = (slug: string) => runInDurableObject(stub(slug), (_i, state) => state.storage.sql.exec<{ id: string }>("SELECT id FROM send_in").toArray().map((r) => r.id)); /** One alarm pass, run here and waited for (a due alarm the runtime fires itself would not be). */ async function alarm(slug: string): Promise { await runInDurableObject(stub(slug), async (instance: Business, state) => { await state.storage.deleteAlarm(); await instance.alarm(); }); } describe("Send's pass as a step job", { timeout: 400_000 }, () => { it("off: the alarm runs it as before", async () => { const slug = "step-send-off"; await personalObject(slug); await staleOffer(slug, "offer-off"); await alarm(slug); expect(await offers(slug)).toEqual([]); }); it("on: the alarm leaves it, and the run steps it through the door", async () => { const slug = "step-send-on"; await personalObject(slug); const on = await http(slug, "PUT", "/v1/admin/business", { jobs: { send: "on" } }, ADMIN); expect(on.status, JSON.stringify(on.body)).toBe(200); await staleOffer(slug, "offer-on"); await alarm(slug); expect(await offers(slug), "the alarm no longer runs Send's pass").toEqual(["offer-on"]); const job = await messageOf(slug); expect(await runStepJob({ door }, job)).toBe("ran"); expect(await offers(slug), "the run's step took it away").toEqual([]); expect(await runStepJob({ door }, job), "its message is used up").toBe("gone"); }); it("off again: the alarm runs it at once", async () => { const slug = "step-send-back"; await personalObject(slug); expect((await http(slug, "PUT", "/v1/admin/business", { jobs: { send: "on" } }, ADMIN)).status).toBe(200); expect((await http(slug, "PUT", "/v1/admin/business", { jobs: { send: null } }, ADMIN)).status).toBe(200); await staleOffer(slug, "offer-back"); await alarm(slug); expect(await offers(slug)).toEqual([]); }); });