aboutsummaryrefslogtreecommitdiffstats
path: root/cli/test/purpose.test.ts
diff options
context:
space:
mode:
authoruser <user@clank>2026-08-12 21:55:12 +0200
committeruser <user@clank>2026-08-12 21:55:12 +0200
commit72441afa75c058e4b1454108d962f2bebaee941b (patch)
treed668f1f61d8cbf19127013724db50cf680aa59c9 /cli/test/purpose.test.ts
parentdocs: agree the sl0ppi CLI design (diff)
downloadsl0ppi-72441afa75c058e4b1454108d962f2bebaee941b.tar.gz
sl0ppi-72441afa75c058e4b1454108d962f2bebaee941b.tar.xz
sl0ppi-72441afa75c058e4b1454108d962f2bebaee941b.zip
cli: the idempotency core (discovery, tab identity, plan diff)
Slice 1 of docs/cli-design.md: the only part with logic worth testing hard. Pure functions, no subprocesses, so the tests hit them directly. purpose.ts tab identity as a zellij tab purpose carrying the project's root-relative path discover.ts depth-1 scan for .sl0ppi/ markers plan.ts discovered projects vs list-tabs -> {create, close, keep, foreign} Identity carries the *path* because `list-panes --json` exposes no cwd (checked against a live session, not assumed), so there is otherwise no way back from a tab to a directory on disk. Slashes survive the fork's sanitiser, so nested paths encode fine. Two safety properties, both fuzzed over 5000 random names: - a purpose never exceeds 64 chars and always survives re-sanitising, so what we generate is what the terminal stores - projectRelPath() reconstructs the original path exactly or returns null -- never a different path. Wrong answers close the wrong tab; refusing only leaves a stale one. The fuzz caught a real bug in the second property. The first implementation detected lossy encodings by re-encoding and comparing, which silently failed: `project:<47 a's>.<hash8>` is itself 64 legal characters, so it re-encodes to itself, the check passed, and a bogus path was "reconstructed" -- which would have closed a live tab. Lossy purposes now carry a structural marker (a leading dot, which no discovered path can have) instead of relying on a comparison. Plus the mount guard from the design: when the ROOT itself is missing, close nothing. An unmounted filesystem is indistinguishable from mass deletion, and one of those two readings eats your whole session. 31 unit tests + 2 fuzz properties, 10178 assertions, green.
Diffstat (limited to 'cli/test/purpose.test.ts')
-rw-r--r--cli/test/purpose.test.ts140
1 files changed, 140 insertions, 0 deletions
diff --git a/cli/test/purpose.test.ts b/cli/test/purpose.test.ts
new file mode 100644
index 0000000..e036761
--- /dev/null
+++ b/cli/test/purpose.test.ts
@@ -0,0 +1,140 @@
+import { describe, expect, test } from "bun:test";
+import {
+ PURPOSE_MAX,
+ isProjectPurpose,
+ projectPurpose,
+ projectRelPath,
+ sanitizePurpose,
+} from "../src/core/purpose.ts";
+
+describe("sanitizePurpose (must match the fork)", () => {
+ test("keeps the fork's allowed set", () => {
+ expect(sanitizePurpose("a-b_c.d:e/f")).toBe("a-b_c.d:e/f");
+ });
+
+ // The fork's own doc uses this exact example.
+ test("strips shell metacharacters", () => {
+ expect(sanitizePurpose('rm -rf /; echo "x"')).toBe("rm-rf/echox");
+ });
+
+ test("strips non-ascii", () => {
+ expect(sanitizePurpose("café–π")).toBe("caf");
+ });
+
+ test("truncates to the fork's limit", () => {
+ expect(sanitizePurpose("x".repeat(200))).toHaveLength(PURPOSE_MAX);
+ });
+});
+
+describe("projectPurpose", () => {
+ test("is the plain relative path for ordinary names", () => {
+ expect(projectPurpose("sl0ppi")).toBe("project:sl0ppi");
+ expect(projectPurpose("group/nested")).toBe("project:group/nested");
+ });
+
+ test("never exceeds what the fork will store", () => {
+ for (const name of ["ok", "a".repeat(200), "wéîrd name!", "x/".repeat(50)]) {
+ expect(projectPurpose(name).length).toBeLessThanOrEqual(PURPOSE_MAX);
+ }
+ });
+
+ test("survives the fork's sanitiser unchanged (round-trip through the terminal)", () => {
+ for (const name of ["ok", "a".repeat(200), "wéîrd name!", "dots...", "x/".repeat(50)]) {
+ const purpose = projectPurpose(name);
+ expect(sanitizePurpose(purpose)).toBe(purpose);
+ }
+ });
+
+ test("distinct projects get distinct purposes, even when truncated", () => {
+ const a = projectPurpose(`${"a".repeat(80)}/one`);
+ const b = projectPurpose(`${"a".repeat(80)}/two`);
+ expect(a).not.toBe(b);
+ });
+
+ test("is stable across calls", () => {
+ expect(projectPurpose("foo")).toBe(projectPurpose("foo"));
+ expect(projectPurpose("wéîrd name!")).toBe(projectPurpose("wéîrd name!"));
+ });
+});
+
+describe("projectRelPath", () => {
+ test("reconstructs an ordinary path", () => {
+ expect(projectRelPath("project:sl0ppi")).toBe("sl0ppi");
+ expect(projectRelPath("project:group/nested")).toBe("group/nested");
+ });
+
+ test("refuses to reconstruct a truncated/hashed purpose", () => {
+ expect(projectRelPath(projectPurpose("a".repeat(200)))).toBeNull();
+ });
+
+ test("refuses non-project purposes", () => {
+ expect(projectRelPath("agent:main")).toBeNull();
+ expect(projectRelPath("project:")).toBeNull();
+ });
+
+ test("round-trips every reconstructable name", () => {
+ for (const name of ["foo", "group/nested", "dots.in.name", "a-b_c"]) {
+ expect(projectRelPath(projectPurpose(name))).toBe(name);
+ }
+ });
+});
+
+// The safety invariant, fuzzed. Closing a tab is destructive, so the one thing
+// projectRelPath must never do is return a path that is not the original.
+// Wrong answers close the wrong tab; refusing (null) only leaves a stale one.
+describe("projectRelPath never lies (fuzz)", () => {
+ const alphabet = "abzAZ09_-./: !@#\u00e9\u4e2d\"';";
+
+ function randomName(rng: () => number): string {
+ const len = 1 + Math.floor(rng() * 90);
+ let out = "";
+ for (let i = 0; i < len; i++) {
+ out += alphabet[Math.floor(rng() * alphabet.length)];
+ }
+ return out;
+ }
+
+ // Deterministic PRNG so a failure is reproducible.
+ function mulberry32(seed: number): () => number {
+ return () => {
+ seed = (seed + 0x6d2b79f5) | 0;
+ let t = Math.imul(seed ^ (seed >>> 15), 1 | seed);
+ t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
+ };
+ }
+
+ test("reconstructs exactly, or refuses — never something else", () => {
+ const rng = mulberry32(0x510991);
+ for (let i = 0; i < 5000; i++) {
+ const name = randomName(rng);
+ const purpose = projectPurpose(name);
+
+ expect(purpose.length).toBeLessThanOrEqual(PURPOSE_MAX);
+ expect(sanitizePurpose(purpose)).toBe(purpose);
+
+ const back = projectRelPath(purpose);
+ if (back !== null) expect(back).toBe(name);
+ }
+ });
+
+ test("distinct names never collide on a purpose", () => {
+ const rng = mulberry32(12345);
+ const seen = new Map<string, string>();
+ for (let i = 0; i < 5000; i++) {
+ const name = randomName(rng);
+ const purpose = projectPurpose(name);
+ const prior = seen.get(purpose);
+ if (prior !== undefined) expect(prior).toBe(name);
+ seen.set(purpose, name);
+ }
+ });
+});
+
+describe("isProjectPurpose", () => {
+ test("classifies", () => {
+ expect(isProjectPurpose("project:foo")).toBe(true);
+ expect(isProjectPurpose("agent:main")).toBe(false);
+ expect(isProjectPurpose(undefined)).toBe(false);
+ });
+});