pi-mono/packages/chord/test/next-node/workers/control.test.ts
Mario Zechner 8959ad6e68 feat(chord): Chord v2 slice 10, HTTP and workers
chord/next/http: a Web fetch handler built from the registry Http (routes, wraps,
sockets, hosts, relays, CSP sources) and Authenticators, responses as descriptions,
a security baseline on by default, Chord's own Node listener as a host-lifetime
object, nodeRequest for apps mounting it, links and plugin sockets.
chord/next/workers: a forked worker per address, started on demand, stopped when
idle, never restarted by itself, plugins built by the server and shipped.
Plugins: kinds with platform and host packages, createPluginRuntime, scripts(kind),
rollout results per host, imports of other plugins by name. Kernel: RouteHandle
(retry, suspend), no route backoff, anonymous host.root, origins only from relays.
ws is an optional peer dependency.
2026-10-31 20:40:36 +00:00

55 lines
2.1 KiB
TypeScript

// Tests of the control channel's crossings (slice 10 §3): errors cross as `{ message, code?, reason? }` and back, a
// reload report with its errors and parked causes.
import { describe, expect, it } from "vitest";
import { ChordError, isChordError } from "../../../src/next/data/index.ts";
import {
controlOf,
crossed,
crossedReport,
envelope,
uncrossed,
uncrossedReport,
} from "../../../src/next-node/workers/control.ts";
describe("errors", () => {
it("cross as message, code and reason, never a stack or cause; come back as ChordErrors when they had a code", () => {
const chord = new ChordError({ code: "unavailable", message: "gone" }, { cause: new Error("inner") });
expect(crossed(chord)).toEqual({ message: "gone", code: "unavailable" });
expect(crossed(new Error("plain"))).toEqual({ message: "plain" });
expect(crossed("text")).toEqual({ message: "text" });
const back = uncrossed({ message: "gone", code: "released", reason: "closed" });
expect(isChordError(back, "released") && back.reason).toBe("closed");
expect(uncrossed({ message: "odd", code: "no-such-code" })).toMatchObject({ message: "odd" });
});
it("crosses a report's failures and parked causes and back", () => {
const report = crossedReport({
plugins: { added: ["@a/x"], changed: [], removed: [] },
failed: [{ plugin: "@a/y", error: new Error("broken") }],
objects: [
{ object: "a.one", kind: "swapped" },
{
object: "a.two",
kind: "parked",
cause: { kind: "waiting", error: new ChordError({ code: "unavailable", message: "w" }) },
},
],
});
expect(JSON.parse(JSON.stringify(report))).toEqual(report);
const back = uncrossedReport(report);
expect(back.failed[0]!.error).toBeInstanceOf(Error);
expect(back.objects[1]).toMatchObject({
kind: "parked",
cause: { kind: "waiting", error: { code: "unavailable" } },
});
});
});
describe("envelopes", () => {
it("tells control messages from anything else on the channel", () => {
expect(controlOf(envelope({ kind: "started" }))).toEqual({ kind: "started" });
expect(controlOf(new Uint8Array(1))).toBeUndefined();
expect(controlOf({ kind: "started" })).toBeUndefined();
});
});