Files
suaveplan/packages/api-stack/api/api-plugin-example/src/wasm/start.test.ts
2026-05-27 23:29:53 +01:00

58 lines
1.6 KiB
TypeScript

import { beforeEach, describe, expect, it } from "@suaveplan/testing/runner";
import { dispose } from "./dispose.js";
import { handleRequest } from "./handle-request.js";
import { start } from "./start.js";
import { resetState } from "./wasm-state.js";
beforeEach(() => {
resetState();
});
function emptyReq(url: string): Parameters<typeof handleRequest>[0] {
return {
url,
method: "GET",
headers: [],
bodyBase64: "",
principalId: null,
tenantId: null,
capabilities: ["example:read", "example:write"],
requestId: "r",
timestamp: 0,
};
}
function decode(b64url: string): string {
const padLen = (4 - (b64url.length % 4)) % 4;
const padded =
b64url.replace(/-/g, "+").replace(/_/g, "/") + "=".repeat(padLen);
return atob(padded);
}
describe("wasm/start", () => {
it("start is idempotent", { timeout: 5000 }, () => {
start();
start();
expect(true).toBe(true);
});
it("start re-primes after dispose", { timeout: 5000 }, async () => {
start();
const res = await handleRequest(emptyReq("http://t/example/invoices"));
expect(res.status).toBe(200);
const body = JSON.parse(decode(res.bodyBase64)) as {
invoices: readonly unknown[];
};
expect(body.invoices).toEqual([]);
dispose();
start();
const after = await handleRequest(
emptyReq("http://t/example/invoices")
);
const body2 = JSON.parse(decode(after.bodyBase64)) as {
invoices: readonly unknown[];
};
expect(body2.invoices).toEqual([]);
});
});