/** * worktree/layout.ts 的单测:纯逻辑,不碰 git、不起 pi。 * * node --test clients/pi/extensions/worktree/layout.test.ts */ import assert from "node:assert/strict"; import test from "node:test"; import { EXCLUDE_ENTRY, branchFor, continuationText, validateName, worktreePath } from "./layout.ts"; test("合法名原样通过(含斜杠,如 feat/x)", () => { assert.deepEqual(validateName("feat/x"), { ok: true, name: "feat/x" }); assert.deepEqual(validateName(" fix-auth "), { ok: true, name: "fix-auth" }); assert.deepEqual(validateName("a.b_c-d"), { ok: true, name: "a.b_c-d" }); }); test("拒绝路径穿越、绝对路径与非法字符", () => { for (const bad of ["", " ", ".", "..", "../x", "a/../b", "/abs", "a/", "a//b", "a\u0000b", "a\nb", "-x", "a b"]) { const r = validateName(bad); assert.equal(r.ok, false, `expected reject: ${JSON.stringify(bad)}`); if (!r.ok) assert.ok(r.reason.length > 0, "拒绝时必须有 reason"); } }); test("路径与分支推导", () => { assert.equal(worktreePath("/r", "feat/x"), "/r/.worktrees/feat/x"); assert.equal(worktreePath("/r/", "x"), "/r/.worktrees/x", "尾部斜杠不产生双斜杠"); assert.equal(branchFor("feat/x"), "feat/x"); assert.equal(EXCLUDE_ENTRY, ".worktrees/"); }); test("续句带路径与分支,且是英文", () => { const t = continuationText("/r/.worktrees/x", "feat/x"); assert.match(t, /\/r\/\.worktrees\/x/); assert.match(t, /feat\/x/); assert.doesNotMatch(t, /[\u4e00-\u9fff]/); }); test("分支未知时续句不提分支,但仍然点明新目录与「续原任务」", () => { const t = continuationText("/r", ""); assert.match(t, /\/r/); assert.doesNotMatch(t, /branch \./, "不能留下 'branch .' 那种空分支残句"); assert.match(t, /continue the original task/); });