Cotton code
43 lines · 2.7 KB · TypeScriptRaw
/** * LIVE CPU 4 (coordinator, 24 Nov 2026): a request the platform resets for its CPU dies with no event of its own. A * request that may carry much work is marked, durably, before it starts; the next start logs what was in flight. */import { env, runInDurableObject } from "cloudflare:test";import { describe, expect, it, vi } from "vitest";import { Business, REQUEST_MARK_ROWS, bulkOf } from "../src/business.ts";import { http, personalObject } from "./personal.ts";const marks = (s: DurableObjectState) => s.storage.sql.exec<{ key: string; value: string }>("SELECT key, value FROM meta WHERE key LIKE 'request_running:%'").toArray();describe("the request mark", () => {  it("names an act with a long list of args, and nothing shorter", () => {    expect(bulkOf({ act: "import_rows", args: { rows: new Array(500).fill({}) } })).toBe("import_rows rows=500");    expect(bulkOf({ act: "import_rows", args: { rows: new Array(REQUEST_MARK_ROWS - 1).fill({}) } })).toBeNull();    expect(bulkOf({ act: "create", args: { table: "todo", data: {} } })).toBeNull();    expect(bulkOf(null)).toBeNull();  });  it("is held only while the request runs, and a start that finds one says which request it was, then forgets it", async () => {    const slug = "request-mark";    const { credential } = await personalObject(slug);    const stub = env.BUSINESS.get(env.BUSINESS.idFromName(slug));    // A pass page and a long import leave no mark once answered.    expect((await http(slug, "GET", "/v1/sync?since=0&limit=50", undefined, { authorization: `Bearer ${credential}` })).status).toBe(200);    const rows = Array.from({ length: REQUEST_MARK_ROWS }, (_, i) => ({ table: "person", id: `mark-${i}`, retract: true }));    const landed = await http(slug, "POST", "/v1/act", { act: "import_rows", args: { rows } }, { authorization: `Bearer ${credential}` });    expect(landed.status, JSON.stringify(landed.body)).toBe(200);    expect(await runInDurableObject(stub, (_i, s) => marks(s))).toEqual([]);    // One left behind, as a reset leaves it: the next start logs it and clears it.    const logged = vi.spyOn(console, "error");    const after = await runInDurableObject(stub, (_i, s) => {      s.storage.sql.exec("INSERT INTO meta (key, value) VALUES ('request_running:x', 'POST /v1/act import_rows rows=500 begun 2026-11-24T05:16:50.000Z')");      new Business(s, env);      return marks(s);    });    expect(after).toEqual([]);    expect(logged.mock.calls.map((c) => String(c[0])).filter((l) => l.includes("was reset inside a request"))).toEqual([`cottondb: ${slug} was reset inside a request: POST /v1/act import_rows rows=500 begun 2026-11-24T05:16:50.000Z`]);    logged.mockRestore();  });});