/** * Regression net for derived run-tabs (src/lib/run-tab.ts) — the identity * primitive of same-project parallel dispatch (phase 2 of worktree-per-agent). * * Everything in the dispatch loop is tab-keyed; parallel runs work by minting * a unique alias per extra run. These checks lock the alias algebra both the * server (dispatch route, ingestion, close-sweep) and the desktop runner * (force-isolation guard) depend on staying in agreement. * * Run: npx tsx scripts/test/run-tab.ts */ import { deriveRunTab, baseProjectKey, isDerivedRunTab, RUN_TAB_SEPARATOR } from "@/lib/run-tab"; function assert(condition: boolean, message: string): void { if (!condition) throw new Error(message); } let passed = 0; const check = (label: string, fn: () => void) => { fn(); passed += 1; console.log(` ✓ ${label}`); }; check("derive → base round-trips to the project key", () => { const tab = deriveRunTab("kivvi", "0b9c1d2e-3f45-6789-abcd-ef0123456789"); assert(baseProjectKey(tab) === "kivvi", `round-trip broke: ${tab}`); }); check("alias embeds a compact runId suffix (dashes stripped, 8 chars)", () => { const tab = deriveRunTab("kivvi", "0b9c1d2e-3f45-6789-abcd-ef0123456789"); assert(tab === `kivvi${RUN_TAB_SEPARATOR}0b9c1d2e`, `unexpected alias: ${tab}`); }); check("two runs of one project get distinct aliases (no coordination needed)", () => { const a = deriveRunTab("kivvi", "aaaaaaaa-1111-2222-3333-444444444444"); const b = deriveRunTab("kivvi", "bbbbbbbb-1111-2222-3333-444444444444"); assert(a !== b, "aliases collided"); assert(baseProjectKey(a) === baseProjectKey(b), "aliases lost their base"); }); check("isDerivedRunTab: true for aliases, false for plain tabs", () => { assert(isDerivedRunTab(deriveRunTab("revampit", "abc123")), "alias not detected"); assert(!isDerivedRunTab("revampit"), "plain tab misdetected"); assert(!isDerivedRunTab("revamp-it"), "hyphenated plain tab misdetected"); assert(!isDerivedRunTab("Tab With Spaces"), "spaced plain tab misdetected"); }); check("base of a plain tab is itself (safe to apply everywhere)", () => { assert(baseProjectKey("orangecat") === "orangecat", "identity broken"); assert(baseProjectKey("Tab With Spaces") === "Tab With Spaces", "identity broken for spaces"); }); check("base takes the FIRST separator (suffix content can never smuggle a fake base)", () => { const weird = `proj${RUN_TAB_SEPARATOR}abc${RUN_TAB_SEPARATOR}def`; assert(baseProjectKey(weird) === "proj", "multi-separator base wrong"); }); check("short/odd runIds still produce a usable alias", () => { const t1 = deriveRunTab("p", "ab"); assert(t1 === `p${RUN_TAB_SEPARATOR}ab` && isDerivedRunTab(t1), "short runId broke alias"); const t2 = deriveRunTab("p", "---"); assert( t2 === `p${RUN_TAB_SEPARATOR}run` && baseProjectKey(t2) === "p", "dash-only runId broke alias", ); }); check("alias is filesystem-safe for the session file convention (.md)", () => { const tab = deriveRunTab("kivvi", "0b9c1d2e-3f45"); assert(!/[/\\\0]/.test(tab), "alias contains path-hostile characters"); }); console.log(`\n${passed}/${passed} passed`);