pi-mono/packages/chord/test/link/errors.test.ts
Mario Zechner e9c0ff4d84 feat(chord): Chord v2 is the package's root
src/next is src (the main export), src/next-node is src/node with short subpaths (/http, /workers, /plugins,
/plugins/runtime, /app, /surfaces, /web, /react, /tui, /tui/launcher); the foundations get /json and /watch;
Chord v1 moves to src/old (/old, /old/node) until p2 runs on v2; tests and docs move likewise (test/, docs/,
test/old, docs/old). Durable and env import the foundations from their subpaths.
2026-11-02 22:36:55 +00:00

35 lines
1.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { ChordError, PluginError } from "../../src/data/index.ts";
import { errorFromWire, wireError } from "../../src/link/internal.ts";
describe("errors on the wire", () => {
it("carry a ChordError as its JSON, without stack or cause", () => {
const error = new ChordError({ code: "invalid", reason: "args", message: "bad" }, { cause: new Error("x") });
const wire = wireError(error);
expect(wire).toEqual({ kind: "chord", error: { code: "invalid", reason: "args", message: "bad" } });
const back = errorFromWire(JSON.parse(JSON.stringify(wire)));
expect(back).toBeInstanceOf(ChordError);
expect(back).toMatchObject({ code: "invalid", reason: "args", message: "bad" });
});
it("carry a PluginError's code, message and details only; the consumer rethrows a PluginError", () => {
const error = new PluginError("quota", "too many notes", { limit: 3 });
const wire = wireError(error);
expect(wire).toEqual({ kind: "plugin", code: "quota", message: "too many notes", details: { limit: 3 } });
const back = errorFromWire(wire);
expect(back).toBeInstanceOf(PluginError);
expect(back).toMatchObject({ code: "quota", message: "too many notes", details: { limit: 3 } });
expect(wireError(new PluginError("c", "m"))).toEqual({ kind: "plugin", code: "c", message: "m" });
});
it("carry anything else as the plugin code internal with its message", () => {
expect(wireError(new TypeError("boom"))).toEqual({ kind: "plugin", code: "internal", message: "boom" });
expect(wireError("plain")).toEqual({ kind: "plugin", code: "internal", message: "plain" });
});
it("throw on a malformed ChordError envelope (a protocol error)", () => {
expect(() => errorFromWire({ kind: "chord", error: { code: "nope", message: "m" } })).toThrow(
"Invalid ChordError JSON",
);
});
});