droboticflow/tests/frontend/prepared-report.test.mjs
2026-11-13 22:31:44 -08:00

91 lines
3.3 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import fs from "node:fs";
import ts from "typescript";
import { buildAPIRequestHeaders } from "../../src/services/api/http.js";
const code = ts.transpileModule(fs.readFileSync(new URL("../../src/services/api/client.ts", import.meta.url), "utf8"), {
compilerOptions: { module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2022 },
}).outputText;
function client(fetcher, onDelay = () => {}) {
let now = 0;
const exports = {};
const window = {
setTimeout(fn, ms) {
return setImmediate(() => {
now += ms;
onDelay();
fn();
});
},
clearTimeout(id) {
clearImmediate(id);
},
};
new Function("exports", "require", "fetch", "Date", "window", code)(
exports,
(path) => (path === "./http.js" ? { buildAPIRequestHeaders } : { buildAPIURL: (p) => p }),
fetcher,
{ now: () => now },
window,
);
return exports;
}
const response = (body, status = 200) => new Response(JSON.stringify(body), { status });
test("prepared reports wait for publication and return only a completed report", async () => {
let calls = 0;
const c = client(async (_url, options) => {
assert.equal(options.credentials, "include");
assert.equal(new Headers(options.headers).get("Accept"), "application/vnd.droboticflow.prepared+json");
assert.equal(options.body, undefined);
return ++calls === 1 ? response({ status: "building", retryAfterMs: 1000 }, 202) : response({ rows: [1] });
});
assert.deepEqual(await c.preparedAPIRequest("/api/dashboard/workforce"), { rows: [1] });
assert.equal(calls, 2);
});
test("publication polling retains the original total 30 second budget", async () => {
let calls = 0;
const c = client(async () => {
calls++;
return response({ status: "building", retryAfterMs: 2000 }, 202);
});
await assert.rejects(c.preparedAPIRequest("/report"), { name: "APITimeoutError" });
assert.equal(calls, 15);
});
test("leaving the report cancels publication polling before another read", async () => {
const abort = new AbortController();
let calls = 0;
const c = client(
async () => {
calls++;
return response({ status: "building" }, 202);
},
() => abort.abort(),
);
await assert.rejects(c.preparedAPIRequest("/report", abort.signal), { name: "AbortError" });
assert.equal(calls, 1);
});
test("permission errors and unrelated 202 responses are never treated as finished data", async () => {
const denied = client(async () => response({ error: "forbidden" }, 403));
await assert.rejects(denied.preparedAPIRequest("/report"), { status: 403 });
const unknown = client(async () => response({ rows: [] }, 202));
await assert.rejects(unknown.preparedAPIRequest("/report"), { status: 202 });
});
test("prepared reads retain bounded transport retry and never retry cancellation", async () => {
let calls = 0;
const c = client(async () => {
if (++calls === 1) throw new TypeError("network");
return response({ items: [] });
});
assert.deepEqual(await c.preparedAPIRequest("/report"), { items: [] });
assert.equal(calls, 2);
calls = 0;
const aborted = client(async () => {
calls++;
throw new DOMException("canceled", "AbortError");
});
await assert.rejects(aborted.preparedAPIRequest("/report"), { name: "AbortError" });
assert.equal(calls, 1);
});