diff options
| author | user <user@clank> | 2026-08-12 22:24:55 +0200 |
|---|---|---|
| committer | user <user@clank> | 2026-08-12 22:24:55 +0200 |
| commit | 2cd247828d863cdc779c7178c988152835096ca2 (patch) | |
| tree | 867b63f58057e81ac8ab4d242e73b733b8372c9e /cli/test | |
| parent | docs(cli): record the fork gap the design turned up (diff) | |
| download | sl0ppi-2cd247828d863cdc779c7178c988152835096ca2.tar.gz sl0ppi-2cd247828d863cdc779c7178c988152835096ca2.tar.xz sl0ppi-2cd247828d863cdc779c7178c988152835096ca2.zip | |
cli: up, ls, open, close
Slice 2 of docs/cli-design.md. The commands read the world (discovery +
list-tabs), compute a plan, then apply it -- never mutating while walking, which
is how `up` stays idempotent.
sl0ppij.ts process wrapper; every piece of state comes back out of
list-tabs/list-panes, so there is nothing to desync
layout.ts composes the tab node around a project's fragment
root.ts root resolution, session naming, the root registry
workspace.ts up / ls / open / close
index.ts dispatch + the fail-open wrapper
Four things this turned up that reading the code would not have:
- `attach --create-background` fails with "There is no active session!" when
the caller is itself inside a session -- which sl0ppi almost always is,
being run from the user's daily-driver terminal. Session creation now runs
with the SL0PPIJ*/ZELLIJ* markers stripped: we are creating a new detached
session, not nesting.
- falling open by passing our own argv to sl0ppij just produces "unrecognized
subcommand: up" and still leaves the user without a terminal. Our verbs and
flags are stripped first, so what is handed over is something sl0ppij
actually understands.
- attaching panics in the client when stdin is not a tty, so `up` from a
script or CI step died. It now reports the session is ready and exits.
- a start_suspended pane reports `is_held`, not `exited`. Conflating them made
`ls` claim every agent was running when none had started. Plugin panes (the
status bar, the link handler) also inflated pane counts and are now excluded
as chrome.
Index conventions were verified against a live session rather than inferred,
because guessing closes the wrong tab: list-tabs `position` and
`close-tab --tab-id` are 0-based, `go-to-tab` is 1-based, and closing renumbers
every tab after it, so batched closes run in descending position order.
Verified end to end against a real session (three projects, one shipping a
custom fragment): up creates the session with all tabs and their purposes; up
again reports "up to date" with tabs unchanged; ls reports panes and
agent:suspended correctly; open creates-and-focuses and resolves unique
prefixes; close removes a tab; deleting a project auto-closes its tab; and with
the root itself missing, nothing is closed at all.
64 tests green.
Diffstat (limited to 'cli/test')
| -rw-r--r-- | cli/test/cli.test.ts | 95 | ||||
| -rw-r--r-- | cli/test/layout.test.ts | 87 | ||||
| -rw-r--r-- | cli/test/root.test.ts | 73 |
3 files changed, 255 insertions, 0 deletions
diff --git a/cli/test/cli.test.ts b/cli/test/cli.test.ts new file mode 100644 index 0000000..464ade4 --- /dev/null +++ b/cli/test/cli.test.ts @@ -0,0 +1,95 @@ +import { describe, expect, test } from "bun:test"; +import { parseArgs, passthroughArgs } from "../src/index.ts"; +import { agentStateOf } from "../src/commands/workspace.ts"; +import { withoutSessionEnv } from "../src/core/sl0ppij.ts"; + +describe("parseArgs", () => { + test("defaults to up", () => { + expect(parseArgs([]).command).toBe("up"); + expect(parseArgs(["--root", "/x"]).command).toBe("up"); + }); + + test("recognises the verbs", () => { + for (const verb of ["up", "ls", "open", "close", "help"]) { + expect(parseArgs([verb]).command).toBe(verb); + } + }); + + test("keeps the project name as an argument", () => { + const args = parseArgs(["open", "myproj", "--root", "/x"]); + expect(args.command).toBe("open"); + expect(args.rest).toEqual(["myproj"]); + expect(args.rootFlag).toBe("/x"); + }); + + test("accepts both flag spellings", () => { + expect(parseArgs(["--root=/x"]).rootFlag).toBe("/x"); + expect(parseArgs(["--depth=3"]).depth).toBe(3); + expect(parseArgs(["--depth", "2"]).depth).toBe(2); + expect(parseArgs(["-n"]).dryRun).toBe(true); + }); + + test("a bad depth falls back to 1 rather than NaN", () => { + expect(parseArgs(["--depth", "banana"]).depth).toBe(1); + }); +}); + +// Falling open is only useful if what we hand over is something sl0ppij +// understands. Passing `up` through just yields "unrecognized subcommand" and +// still leaves the user without a terminal. +describe("passthroughArgs", () => { + test("strips our verbs and flags", () => { + expect(passthroughArgs(["up", "--root", "/x"])).toEqual([]); + expect(passthroughArgs(["ls", "--depth=2"])).toEqual([]); + expect(passthroughArgs(["open", "proj", "-n"])).toEqual(["proj"]); + }); + + test("keeps arguments sl0ppij would understand", () => { + expect(passthroughArgs(["attach", "mysession"])).toEqual(["attach", "mysession"]); + expect(passthroughArgs(["--root", "/x", "list-sessions"])).toEqual(["list-sessions"]); + }); + + test("only the leading verb is dropped", () => { + // `sl0ppij action ... close` style words further along must survive + expect(passthroughArgs(["action", "close-tab"])).toEqual(["action", "close-tab"]); + }); +}); + +describe("agentStateOf", () => { + // A start_suspended pane reports is_held, not exited: the command is staged + // but has never run. Conflating the two made `ls` claim twelve agents were + // running when none were. + test("held means suspended", () => { + expect(agentStateOf({ is_held: true })).toBe("suspended"); + }); + test("exited means exited", () => { + expect(agentStateOf({ exited: true })).toBe("exited"); + }); + test("neither means running", () => { + expect(agentStateOf({})).toBe("running"); + }); + test("absent means none", () => { + expect(agentStateOf(undefined)).toBe("none"); + }); +}); + +describe("withoutSessionEnv", () => { + // `attach --create-background` fails with "There is no active session!" when + // the caller is inside a session, and sl0ppi is nearly always run from one. + test("drops both spellings of the session markers", () => { + const cleaned = withoutSessionEnv({ + ZELLIJ: "0", + ZELLIJ_SESSION_NAME: "inventive-drum", + SL0PPIJ: "0", + SL0PPIJ_SESSION_NAME: "dev", + SL0PPIJ_PANE_ID: "3", + PATH: "/usr/bin", + }); + expect(cleaned.ZELLIJ).toBeUndefined(); + expect(cleaned.ZELLIJ_SESSION_NAME).toBeUndefined(); + expect(cleaned.SL0PPIJ).toBeUndefined(); + expect(cleaned.SL0PPIJ_SESSION_NAME).toBeUndefined(); + expect(cleaned.SL0PPIJ_PANE_ID).toBeUndefined(); + expect(cleaned.PATH).toBe("/usr/bin"); + }); +}); diff --git a/cli/test/layout.test.ts b/cli/test/layout.test.ts new file mode 100644 index 0000000..4d8f7ce --- /dev/null +++ b/cli/test/layout.test.ts @@ -0,0 +1,87 @@ +import { describe, expect, test } from "bun:test"; +import { + DEFAULT_FRAGMENT, + kdlString, + sessionLayoutKdl, + singleTabLayoutKdl, + tabKdl, +} from "../src/core/layout.ts"; + +const spec = { name: "foo", relPath: "foo", cwd: "/home/user/dev/foo" }; + +describe("kdlString", () => { + test("escapes quotes and backslashes", () => { + expect(kdlString('a"b')).toBe('"a\\"b"'); + expect(kdlString("a\\b")).toBe('"a\\\\b"'); + }); +}); + +describe("tabKdl", () => { + test("carries name, purpose and cwd", () => { + const kdl = tabKdl(spec); + expect(kdl).toContain('name="foo"'); + expect(kdl).toContain('purpose="project:foo"'); + expect(kdl).toContain('cwd="/home/user/dev/foo"'); + }); + + // Single-line KDL needs `;` between nodes; multi-line does not. Emitting + // multi-line is how we avoid the whole class of parse error. + test("is always multi-line", () => { + const kdl = tabKdl({ ...spec, fragment: "pane\npane" }); + expect(kdl.split("\n").length).toBeGreaterThan(3); + expect(kdl).not.toMatch(/\{[^\n]*\}/); // no braces opened and closed on one line + }); + + test("uses the default fragment when a project ships none", () => { + expect(tabKdl(spec)).toContain(DEFAULT_FRAGMENT); + }); + + test("keeps a project's fragment verbatim, just indented", () => { + const fragment = 'pane split_direction="vertical" {\n pane\n pane\n}'; + const kdl = tabKdl({ ...spec, fragment }); + for (const line of fragment.split("\n")) { + expect(kdl).toContain(line.trim()); + } + }); + + test("a directory with a quote in its name cannot break out of the string", () => { + const kdl = tabKdl({ name: 'ev"il', relPath: 'ev"il', cwd: '/tmp/ev"il' }); + expect(kdl).toContain('name="ev\\"il"'); + expect(kdl).toContain('cwd="/tmp/ev\\"il"'); + + // The name does not survive the fork's sanitiser, so the purpose takes the + // lossy-marked form rather than silently becoming a *different* valid path + // (`project:evil`) that could collide with a real directory called "evil". + expect(kdl).toMatch(/purpose="project:\.evil\.[0-9a-f]{8}"/); + + // Nothing escaped the quoting: on the tab's own line, once escaped quotes + // are removed, exactly six delimiters remain (name, purpose, cwd). + const head = kdl.split("\n")[0] as string; + expect((head.replace(/\\"/g, "").match(/"/g) ?? []).length).toBe(6); + }); +}); + +describe("layout wrappers", () => { + test("session layout holds every tab", () => { + const kdl = sessionLayoutKdl([ + spec, + { name: "bar", relPath: "bar", cwd: "/home/user/dev/bar" }, + ]); + expect(kdl.startsWith("layout {\n")).toBe(true); + expect(kdl.trimEnd().endsWith("}")).toBe(true); + expect(kdl).toContain('purpose="project:foo"'); + expect(kdl).toContain('purpose="project:bar"'); + }); + + test("single tab layout holds exactly one", () => { + const kdl = singleTabLayoutKdl(spec); + expect(kdl.match(/\btab name=/g)).toHaveLength(1); + }); + + test("braces balance", () => { + const kdl = sessionLayoutKdl([spec, { ...spec, relPath: "bar", name: "bar" }]); + const open = (kdl.match(/\{/g) ?? []).length; + const close = (kdl.match(/\}/g) ?? []).length; + expect(open).toBe(close); + }); +}); diff --git a/cli/test/root.test.ts b/cli/test/root.test.ts new file mode 100644 index 0000000..b212050 --- /dev/null +++ b/cli/test/root.test.ts @@ -0,0 +1,73 @@ +import { afterAll, describe, expect, test } from "bun:test"; +import { mkdirSync, mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { resolveRoot, sessionNameFor } from "../src/core/root.ts"; + +const base = mkdtempSync(join(tmpdir(), "sl0ppi-root-")); +afterAll(() => rmSync(base, { recursive: true, force: true })); + +const dev = join(base, "dev"); +const project = join(dev, "myproj"); +mkdirSync(join(project, ".sl0ppi"), { recursive: true }); +const plain = join(base, "plain"); +mkdirSync(plain, { recursive: true }); + +describe("sessionNameFor", () => { + test("uses the root's basename", () => { + expect(sessionNameFor("/home/user/dev")).toBe("dev"); + }); + + test("sanitises awkward names", () => { + expect(sessionNameFor("/tmp/my project!")).toBe("my-project"); + expect(sessionNameFor("/tmp/...")).toBe("root"); + }); + + test("is stable for the same root", () => { + const registry = { dev: "/home/user/dev" }; + expect(sessionNameFor("/home/user/dev", registry)).toBe("dev"); + }); + + // Two checkouts both called `api` must not fight over one session. + test("disambiguates when another root already claims the name", () => { + const registry = { api: "/home/user/work/api" }; + const name = sessionNameFor("/home/user/dev/api", registry); + expect(name).not.toBe("api"); + expect(name.startsWith("api-")).toBe(true); + }); +}); + +describe("resolveRoot", () => { + test("--root wins", () => { + const r = resolveRoot({ rootFlag: dev, cwd: plain }); + expect(r.root).toBe(dev); + expect(r.session).toBe("dev"); + }); + + test("inside a project, the root is its parent and the project is remembered", () => { + const r = resolveRoot({ cwd: project }); + expect(r.root).toBe(dev); + expect(r.project).toBe("myproj"); + }); + + test("inside a session, the registered root is used", () => { + const r = resolveRoot({ + cwd: plain, + sessionEnv: "dev", + registry: { dev }, + }); + expect(r.root).toBe(dev); + expect(r.session).toBe("dev"); + }); + + test("otherwise the cwd is the root", () => { + const r = resolveRoot({ cwd: plain }); + expect(r.root).toBe(plain); + expect(r.session).toBe("plain"); + }); + + test("an unregistered session name does not hijack the cwd", () => { + const r = resolveRoot({ cwd: plain, sessionEnv: "someone-elses-session", registry: {} }); + expect(r.root).toBe(plain); + }); +}); |
