import assert from "node:assert/strict";
import test from "node:test";
import type { ExtensionContext, SessionEntry } from "@earendil-works/pi-coding-agent";
import { onLiveBranch } from "../src/liveness.ts";
const STALE = "This extension ctx is stale after session replacement or reload.";
const entry = (id: string): SessionEntry =>
({
type: "custom",
id,
parentId: null,
timestamp: "2026-09-12T00:00:00.000Z",
customType: "unrelated",
data: {},
}) as SessionEntry;
const context = (branch: SessionEntry[] | (() => never)): ExtensionContext =>
({
get sessionManager() {
return typeof branch === "function"
? {
getBranch: branch,
}
: { getBranch: () => branch };
},
}) as unknown as ExtensionContext;
test("a boundary still on the branch is live", () => {
assert.equal(onLiveBranch(context([entry("a"), entry("b")]), "a"), true);
});
test("a boundary that left the branch is not live", () => {
assert.equal(onLiveBranch(context([entry("a")]), "b"), false);
});
test("a replaced session is not a live branch and does not throw", () => {
const replaced = context(() => {
throw new Error(STALE);
});
assert.equal(onLiveBranch(replaced, "a"), false);
assert.equal(onLiveBranch(replaced, null), false);
});