kernel / core / lib / src / lib.test.tsmain at 949894f
// lib.test.ts — the live-state delta format (the one invariant that matters: applyPatch(a, diff(a, b))
// always deep-equals b — every shape below proves it, then asserts the op shapes we promised: append
// fast path, wholesale array replace, key remove), the same-origin check as `{ origin, becomes }`
// rows, and which deployment a hostname is (the environment favicon).
import { expect, test, vi } from "vitest";
import {
applyPatch,
causeLeavingTheStack,
cookieValueOf,
deploymentEnvironment,
diff,
environmentFaviconHref,
environmentFaviconSvg,
environmentTitle,
forwardIssues,
ITERATE_CAUSE_HEADER,
fetchLeavingTheStack,
leavingTheStack,
oneStepDeeper,
isSameOriginBrowserRequest,
type Issue,
releaseRpcSessions,
reportIssue,
} from "./lib.ts";
// ── our mark, as it leaves the stack ──
test("a mark that leaves the stack keeps its counts, and names no context of the stack", () => {
const mark = JSON.stringify({
chain: "2026-11-11T20:00:00.000Z with an event on /agents/billing/acme ~k3x9q",
depth: 3,
at: 1_794_430_000_000,
hops: 2,
parent: "/agents/billing/acme@41",
});
expect(JSON.parse(causeLeavingTheStack(mark)!)).toEqual({
chain: "2026-11-11T20:00:00.000Z with another stack ~k3x9q",
depth: 3,
at: 1_794_430_000_000,
hops: 2,
});
const sent = leavingTheStack(
new Request("https://api.example.net/v1", { headers: { [ITERATE_CAUSE_HEADER]: mark } }),
);
expect(sent.headers.get(ITERATE_CAUSE_HEADER)).not.toMatch(/agents|billing|acme|@41/);
});
test.for([
[
"a chain the kernel did not make keeps its counts alone",
'{"chain":"/secrets/x","depth":1}',
'{"chain":"another stack","depth":1}',
],
[
"a field that is no count is left out",
'{"chain":"x","depth":0,"at":"/agents/x","hops":-1}',
'{"chain":"another stack","depth":0}',
],
["a mark with no depth is no mark", '{"chain":"x","depth":"/agents/x"}', null],
["text that is not the kernel's JSON is no mark", "/agents/billing@41", null],
["no mark stays none", null, null],
] as const)("%s", ([, mark, outside]) => {
expect(causeLeavingTheStack(mark)).toBe(outside);
const request = new Request("https://api.example.net/", {
headers: mark ? { [ITERATE_CAUSE_HEADER]: mark } : {},
});
expect(leavingTheStack(request).headers.get(ITERATE_CAUSE_HEADER)).toBe(outside);
});
// ── diff + applyPatch ──
test("deep-equal values diff to undefined (the don't-emit signal)", () => {
expect(diff({ a: [1, { b: 2 }] }, { a: [1, { b: 2 }] })).toBeUndefined();
expect(diff(3, 3)).toBeUndefined();
});
test("scalar and key changes are replace/add/remove ops", () => {
expect(roundtrip({ count: 1 }, { count: 2 })).toEqual([
{ op: "replace", path: "/count", value: 2 },
]);
expect(roundtrip({ a: 1 }, { a: 1, b: 2 })).toEqual([{ op: "add", path: "/b", value: 2 }]);
expect(roundtrip({ a: 1, b: 2 }, { a: 1 })).toEqual([{ op: "remove", path: "/b" }]);
});
test("the chat-log fast path: pure array append becomes `add …/-` ops", () => {
const ops = roundtrip({ messages: [{ t: "hi" }] }, { messages: [{ t: "hi" }, { t: "again" }] });
expect(ops).toEqual([{ op: "add", path: "/messages/-", value: { t: "again" } }]);
});
test("tail truncation becomes remove ops; middle divergence replaces wholesale", () => {
expect(roundtrip({ xs: [1, 2, 3] }, { xs: [1] })).toEqual([
{ op: "remove", path: "/xs/2" },
{ op: "remove", path: "/xs/1" },
]);
expect(roundtrip({ xs: [1, 2, 3] }, { xs: [1, 9, 3] })).toEqual([
{ op: "replace", path: "/xs", value: [1, 9, 3] },
]);
});
test("nested recursion, type flips, and root replacement", () => {
roundtrip({ a: { b: { c: 1 } } }, { a: { b: { c: 2, d: 3 } } });
roundtrip({ a: [1] }, { a: { was: "array" } });
expect(roundtrip(1, { now: "object" })).toEqual([
{ op: "replace", path: "", value: { now: "object" } },
]);
});
test("JSON-Pointer escaping for keys containing / and ~", () => {
const ops = roundtrip({ "a/b": 1, "c~d": 2 }, { "a/b": 9, "c~d": 8 });
expect(ops.map((o) => o.path).sort()).toEqual(["/a~1b", "/c~0d"]);
});
test("applyPatch never mutates its input", () => {
const a = { messages: [{ t: "hi" }] };
applyPatch(a, [{ op: "add", path: "/messages/-", value: { t: "x" } }]);
expect(a.messages).toHaveLength(1);
});
test("keys shadowing Object.prototype members diff by OWN presence, not the chain", () => {
expect(roundtrip({ toString: "hi" }, {})).toEqual([{ op: "remove", path: "/toString" }]);
expect(roundtrip({}, { toString: "x" })).toEqual([{ op: "add", path: "/toString", value: "x" }]);
roundtrip({ constructor: "a", keep: 1 }, { keep: 1 });
});
test("diff sees JSON semantics: undefined keys vanish, Dates diff as their ISO strings", () => {
// a key "becoming undefined" is a REMOVAL on the wire, never a value-less op
expect(diff({ a: 1, b: 2 }, { a: 1, b: undefined })).toEqual([{ op: "remove", path: "/b" }]);
expect(diff({ a: undefined }, { a: undefined })).toBeUndefined();
// a changed Date emits a real patch (structural equal() alone would call them identical)
const ops = diff({ at: new Date(0) }, { at: new Date(1000) });
expect(ops).toEqual([{ op: "replace", path: "/at", value: new Date(1000).toISOString() }]);
// sparse-array holes normalize to null instead of producing holes in the ops array
const sparse = [1];
sparse[3] = 4;
expect(applyPatch([1], diff([1], sparse)!)).toEqual([1, null, null, 4]);
});
test("applyPatch cannot touch prototypes (patches arrive over the wire)", () => {
expect(() => applyPatch({}, [{ op: "add", path: "/__proto__/polluted", value: true }])).toThrow(
/__proto__/,
);
// traversal is own-property-only: an inherited member never resolves as a container
expect(() =>
applyPatch({}, [{ op: "add", path: "/constructor/prototype/polluted", value: true }]),
).toThrow(/missing path/);
expect(({} as Record<string, unknown>).polluted).toBeUndefined();
});
// ── origin ── the check a cookie-carrying request rides on (app-server.ts,
// core/boundary-worker/src/host-session.ts): `{ origin, becomes }` rows for a request to
// https://worker.example/api.
const originRows: { headers: Record<string, string>; becomes: boolean }[] = [
{ headers: {}, becomes: true }, // no Origin at all: a non-browser client
{ headers: { origin: "" }, becomes: false }, // a PRESENT but empty Origin is foreign — never "no Origin" (lib.ts's one null-vs-empty exception)
{ headers: { origin: "https://worker.example" }, becomes: true }, // the page is this origin
{ headers: { origin: "https://evil.example" }, becomes: false }, // another site drove the browser
{ headers: { origin: "https://site--prj.worker.example" }, becomes: false }, // same site is not same origin: a mind host
{ headers: { origin: "http://worker.example" }, becomes: false }, // the scheme is part of the origin
{ headers: { origin: "https://worker.example:8443" }, becomes: false }, // so is the port
{ headers: { origin: "null" }, becomes: false }, // an opaque origin (a sandboxed document) is foreign
{ headers: { origin: "not a url" }, becomes: false },
];
for (const { headers: sent, becomes } of originRows)
test(`Origin ${JSON.stringify(sent)} ⇒ ${becomes}`, () => {
const headers = new Headers(sent);
expect(isSameOriginBrowserRequest({ url: "https://worker.example/api", headers })).toBe(
becomes,
);
});
// ── cookies ── `cookieValueOf(header, name)`: `{ header, name, becomes }` rows.
const cookieRows: { header: string | null; name: string; becomes: string | null }[] = [
{ header: null, name: "a", becomes: null },
{ header: "a=1", name: "a", becomes: "1" },
{ header: "b=2; a=x=y", name: "a", becomes: "x=y" }, // a value may hold `=`
{ header: "a=", name: "a", becomes: "" },
{ header: "ab=1", name: "a", becomes: null }, // the whole name
{ header: "a", name: "a", becomes: null }, // no `=`: not a cookie
];
for (const { header, name, becomes } of cookieRows)
test(`cookieValueOf(${JSON.stringify(header)}, ${JSON.stringify(name)}) ⇒ ${JSON.stringify(becomes)}`, () => {
expect(cookieValueOf(header, name)).toBe(becomes);
});
test("reportIssue hands each issue to the forwarder — bounded attributes, the caught value itself — and a throwing forwarder never reaches the caller", () => {
const seen: Issue[] = [];
vi.spyOn(console, "error").mockImplementation(() => {});
const boom = new Error("boom");
forwardIssues((issue) => seen.push(issue));
reportIssue("site.a", boom, { mindId: "mind_1", skipped: undefined, long: "x".repeat(300) });
expect(seen).toEqual([
{
failureSite: "site.a",
caught: boom,
attributes: { mindId: "mind_1", long: "x".repeat(256) },
},
]);
forwardIssues(() => {
throw new Error("forwarder down");
});
expect(() => reportIssue("site.b", boom)).not.toThrow();
forwardIssues(() => {});
});
test("releaseRpcSessions releases each once, the last first; one that throws is reported, the rest still released", () => {
const released: string[] = [];
const session = (name: string, fail = false) => ({
[Symbol.dispose]() {
released.push(name);
if (fail) throw new Error(`${name} already gone`);
},
});
const reported = vi.spyOn(console, "error").mockImplementation(() => undefined);
expect(() =>
releaseRpcSessions([session("first"), session("middle", true), session("last")]),
).not.toThrow();
expect(released).toEqual(["last", "middle", "first"]);
expect(reported).toHaveBeenCalled();
});
// ── environment ──
test.each([
"os.iterate.com",
"dash.iterate.com",
"voice.iterate.com",
"k.iterate.com",
"agents.iterate.workers.dev",
"notes.iterate.workers.dev",
// the parents of the per-PR previews, and an experiment: not a PR
"os.iterate-dev-preview.workers.dev",
"exp-jonas-os.iterate-dev-preview.workers.dev",
// `pr` must lead the hostname, and the host must be workers.dev
"notpr12-os.iterate-dev-preview.workers.dev",
"pr12-x.iterate.com",
])("%s is production", (hostname) => {
expect(deploymentEnvironment(hostname)).toEqual({ kind: "production" });
});
test.each([
["pr2990-a1b2c3d-os.iterate-dev-preview.workers.dev", 2990, "pr2990-a1b2c3d"],
["pr2990-a1b2c3d-dash.iterate-dev-preview.workers.dev", 2990, "pr2990-a1b2c3d"],
["pr2990-os.iterate-dev-preview.workers.dev", 2990, "pr2990"],
["pr2990-dash.iterate-dev-preview.workers.dev", 2990, "pr2990"],
["pr7-kit.iterate-dev-preview.workers.dev", 7, "pr7"],
])("%s is PR %i's preview, deployment %s", (hostname, pr, deployment) => {
expect(deploymentEnvironment(hostname)).toEqual({ kind: "preview", pr, deployment });
});
test.each(["localhost", "petshop.localhost", "127.0.0.1"])("%s is dev", (hostname) => {
expect(deploymentEnvironment(hostname)).toEqual({ kind: "dev" });
});
test("the title is prefixed off production only", () => {
expect(environmentTitle({ kind: "production" }, "Dash")).toBe("Dash");
expect(
environmentTitle({ kind: "preview", pr: 2990, deployment: "pr2990-a1b2c3d" }, "Dash"),
).toBe("[pr2990] Dash");
expect(environmentTitle({ kind: "dev" }, "Sign in · Dash")).toBe("[dev] Sign in · Dash");
});
test("production keeps its own icon file", () => {
expect(environmentFaviconHref({ kind: "production" }, "/iterate-logo.svg")).toBe(
"/iterate-logo.svg",
);
});
test("a preview's icon is purple, with its PR number", () => {
const svg = decodeSvg(
environmentFaviconHref({ kind: "preview", pr: 2990, deployment: "pr2990-a1b2c3d" }, "/x.svg"),
);
expect(svg).toContain('fill="#7C3AED"');
expect(svg).toContain(">2990</text>");
});
test("dev's icon is teal, with the iterate mark", () => {
const svg = decodeSvg(environmentFaviconHref({ kind: "dev" }, "/x.svg"));
expect(svg).toContain('fill="#0F766E"');
expect(svg).toContain("<path");
expect(svg).not.toContain("<text");
});
test.each([
[7, 360],
[42, 360],
[123, 264],
[2990, 198],
[12345, 158],
])("PR %i's digits fit across the square at font-size %i", (pr, fontSize) => {
expect(environmentFaviconSvg({ kind: "preview", pr, deployment: `pr${pr}-a1b2c3d` })).toContain(
`font-size="${fontSize}"`,
);
});
test.for([
{ name: "no wait is a step on", depth: 3, waited: 0, deeper: 4, clockAge: 0 },
{
name: "a clock a moment ahead, on another machine, is one step",
depth: 3,
waited: -0.001,
deeper: 4,
clockAge: -0.001,
},
{ name: "a wait under two minutes keeps counting", depth: 3, waited: 1, deeper: 4, clockAge: 1 },
{
name: "five minutes takes two steps off, and keeps the minute over",
depth: 3,
waited: 5,
deeper: 2,
clockAge: 1,
},
{
name: "a day takes any chain back to its start, and its clock with it",
depth: 16,
waited: 1_440,
deeper: 1,
clockAge: 0,
},
])("oneStepDeeper: $name", ({ depth, waited, deeper, clockAge }) => {
const now = Date.parse("2035-01-01T12:00:00Z");
vi.useFakeTimers({ now, toFake: ["Date"] });
const minutes = (count: number) => count * 60_000;
expect(oneStepDeeper({ depth, at: now - minutes(waited) })).toEqual({
depth: deeper,
at: now - minutes(clockAge),
});
vi.useRealTimers();
});
test("oneStepDeeper: a cause with no clock starts one, and keeps what else it carries", () => {
const now = Date.parse("2035-01-01T12:00:00Z");
vi.useFakeTimers({ now, toFake: ["Date"] });
expect(oneStepDeeper({ chain: "c", depth: 3 })).toEqual({ chain: "c", depth: 4, at: now });
vi.useRealTimers();
});
// ── THE LAST HOP OF A REQUEST THAT LEAVES THE STACK (`fetchLeavingTheStack`) ──
test("a request that leaves a stack with an egress gateway goes through it or not at all", async () => {
const hop = lastHop();
const request = () =>
new Request("https://origin.example/x", { headers: { authorization: "Bearer the-secret" } });
const answer = await fetchLeavingTheStack(hop.env, request(), { redirect: "follow" });
expect(await answer.text()).toBe("carried");
// once, and a gateway follows no redirect whatever the caller's init says
expect(hop).toMatchObject({
carried: [{ url: "https://origin.example/x", init: { redirect: "manual" }, cause: null }],
});
expect(hop.carried).toHaveLength(1);
// a gateway that fails is the request's failure
const down = new Error("the gateway is down");
await expect(
fetchLeavingTheStack({ EGRESS_GATEWAY: { fetch: () => Promise.reject(down) } }, request()),
).rejects.toBe(down);
// and a gateway's own 502 comes back as it is
const refused = new Response("no way out\n", { status: 502 });
expect(await fetchLeavingTheStack(lastHop(() => refused).env, request())).toBe(refused);
expect(hop).toMatchObject({ platform: [] });
});
test("a stack with no egress gateway sends by the platform's fetch with the caller's own init, and calls nothing else", async () => {
const hop = lastHop();
const init = { redirect: "manual" } as const;
const answer = await fetchLeavingTheStack({}, new Request("https://origin.example/x"), init);
expect(await answer.text()).toBe("by the platform's fetch");
expect(hop).toMatchObject({
platform: [{ url: "https://origin.example/x", init, cause: null }],
});
// with no init the platform's fetch gets none, so it follows a redirect
await fetchLeavingTheStack({}, new Request("https://origin.example/y"));
expect(hop.platform[1]).toEqual({
url: "https://origin.example/y",
init: undefined,
cause: null,
});
expect(hop).toMatchObject({ carried: [] });
});
test("the cause mark leaves as causeLeavingTheStack gives it, through a gateway and without one", async () => {
const hop = lastHop();
const mark = JSON.stringify({ chain: "2026-11-19T00:00:00.000Z with a request ~n1", depth: 3 });
const marked = () =>
new Request("https://origin.example/x", { headers: { [ITERATE_CAUSE_HEADER]: mark } });
await fetchLeavingTheStack(hop.env, marked());
await fetchLeavingTheStack({}, marked());
const outside = causeLeavingTheStack(mark);
expect(outside).toContain("another stack");
expect([hop.carried[0]?.cause, hop.platform[0]?.cause]).toEqual([outside, outside]);
});
const roundtrip = (a: unknown, b: unknown) => {
const ops = diff(a, b);
expect(ops, `diff(${JSON.stringify(a)}, ${JSON.stringify(b)})`).toBeDefined();
expect(applyPatch(a, ops!)).toEqual(b);
return ops!;
};
function decodeSvg(href: string) {
const prefix = "data:image/svg+xml,";
expect(href.startsWith(prefix)).toBe(true);
return decodeURIComponent(href.slice(prefix.length));
}
/** A gateway that says what it was handed, and the platform's fetch, which says what it sent. */
function lastHop(answer: () => Promise<Response> | Response = () => new Response("carried")) {
const carried: { url: string; init?: RequestInit; cause: string | null }[] = [];
const platform: { url: string; init?: RequestInit; cause: string | null }[] = [];
const seen = (into: typeof carried) => (request: Request, init?: RequestInit) =>
void into.push({ url: request.url, init, cause: request.headers.get(ITERATE_CAUSE_HEADER) });
vi.stubGlobal("fetch", async (request: Request, init?: RequestInit) => {
seen(platform)(request, init);
return new Response("by the platform's fetch");
});
const gateway = {
fetch: async (request: Request, init?: RequestInit) => {
seen(carried)(request, init);
return answer();
},
};
return {
env: { EGRESS_GATEWAY: gateway },
carried,
platform,
};
}