/** * SLOW READ (coordinator, 26 Nov 2026): the front Worker says an answer slower than SLOW_ANSWER_MS with what the object * told about itself (src/http.ts `slowLine`): a cold instance, an alarm under way, and what the last alarm still had * to do. Paths only. */import { env, runInDurableObject, SELF } from"cloudflare:test";import { describe, expect, it } from"vitest";import { ALARM_BUSY_HEADER, ALARM_RUNNING_HEADER, BEFORE_HEADER, BEGAN_HEADER, COLD_HEADER, QUEUED_HEADER, SLOW_ANSWER_MS, slowLine, TIMING_ASK_HEADER, TOTAL_HEADER } from"../src/http.ts";import { routeFamily } from"../src/business.ts";describe("a slow answer is said with why", () => { it("says nothing under the line, and above it the object's cold start, its alarm and its busy jobs", () => {const quiet = newResponse("{}", { headers: { [COLD_HEADER]: "1" } }); expect(slowLine("POST", "/mcp", SLOW_ANSWER_MS - 1, quiet)).toBeNull(); expect(slowLine("POST", "/mcp", 9_700, quiet)).toBe("cottondb: slow answer 9700 ms POST /mcp 200: cold yes; alarm not running; alarm busy none; queued unknown; before nothing");const behind = newResponse("{}", { status: 200, headers: { [ALARM_RUNNING_HEADER]: "4", [ALARM_BUSY_HEADER]: "changes,acts" } }); expect(slowLine("POST", "/mcp", 3_100, behind)).toBe("cottondb: slow answer 3100 ms POST /mcp 200: cold no; alarm running 4 s; alarm busy changes,acts; queued unknown; before nothing");const queued = newResponse("{}", { headers: { [BEFORE_HEADER]: "GET /v1/sync 3 ms ago" } }); expect(slowLine("GET", "/v1/me", 2_400, queued, 2_100)).toBe("cottondb: slow answer 2400 ms GET /v1/me 200: cold no; alarm not running; alarm busy none; queued 2100 ms; before GET /v1/sync 3 ms ago"); }); it("the object's first answer after a start says it was cold, and the next does not; a running alarm is said in seconds", async () => {const stub = env.BUSINESS.get(env.BUSINESS.idFromName("slow-answer-cold"));const ask = () => stub.fetch("https://cottondb.test/v1/nothing-here", { headers: { "x-cotton-business": "slow-answer-cold" } });const first = await ask();await first.arrayBuffer(); expect(first.headers.get(COLD_HEADER)).toBe("1");const second = await ask();await second.arrayBuffer(); expect(second.headers.get(COLD_HEADER)).toBeNull(); expect(second.headers.get(ALARM_RUNNING_HEADER)).toBeNull();// An alarm marked as begun ten seconds ago (the object's own mark, `alarm_running`).await runInDurableObject(stub, (_i, s) => s.storage.sql.exec("INSERT INTO meta (key, value) VALUES ('alarm_running', ?) ON CONFLICT (key) DO UPDATE SET value = excluded.value", newDate(Date.now() - 10_000).toISOString()));const third = await ask();await third.arrayBuffer(); expect(Number(third.headers.get(ALARM_RUNNING_HEADER))).toBeGreaterThanOrEqual(9);await runInDurableObject(stub, (_i, s) => s.storage.sql.exec("DELETE FROM meta WHERE key = 'alarm_running'")); });// LIVE SLOW 2: every forwarded answer says the whole wait and how much of it the request queued for the object, and// the object says what it did just before; a route family is its plain words only, never an id, a key or a token. it("splits every forwarded answer into its wait in the queue and the rest, and names what came before", async () => { expect(routeFamily("/v1/sync")).toBe("/v1/sync"); expect(routeFamily("/v1/admin/open-tables-lock")).toBe("/v1/admin/open-tables-lock"); expect(routeFamily("/v1/media/0a1b2c3d-0000-4000-8000-000000000000/films/x/master.m3u8")).toBe("/v1/media"); expect(routeFamily("/v1/stream/AbCdEf_123/x")).toBe("/v1/stream");const ask = (headers: Record<string, string> = {}) => SELF.fetch("https://example.test/b/slow-answer-split/v1/nothing-here", { headers }); (await (await ask()).arrayBuffer());// LIVE SLOW 3: an outside caller gets nothing of the object's timing (when the phone is busy is the person's own).const outside = await ask();await outside.arrayBuffer();for (const h of [BEGAN_HEADER, BEFORE_HEADER, TOTAL_HEADER, QUEUED_HEADER]) expect(outside.headers.get(h), h).toBeNull(); expect((await ask({ [TIMING_ASK_HEADER]: "not-the-admin-token" })).headers.get(TOTAL_HEADER)).toBeNull();// The admin token asks for the split (busy-look).const res = await ask({ [TIMING_ASK_HEADER]: String(env.ADMIN_TOKEN) });await res.arrayBuffer(); expect(Number(res.headers.get(TOTAL_HEADER))).toBeGreaterThanOrEqual(0); expect(Number(res.headers.get(QUEUED_HEADER))).toBeGreaterThanOrEqual(0); expect(res.headers.get(BEGAN_HEADER), "the object's own clock never leaves").toBeNull(); expect(res.headers.get(BEFORE_HEADER)).toMatch(/^GET \/v1\/nothing-here \d+ ms ago$/); });});