import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import fs from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { after, test } from "node:test"; const CLI = fileURLToPath(new URL("../dist/index.js", import.meta.url)); const built = await fs .access(CLI) .then(() => true) .catch(() => false); const home = await fs.mkdtemp(path.join(os.tmpdir(), "designpaca-cli-")); after(async () => { await fs.rm(home, { recursive: true, force: true }); }); /** 빌드 산출물을 사용자가 실행하는 방식 그대로 돌린다 */ function run(args: string[], cwd?: string): string { return execFileSync(process.execPath, [CLI, ...args, "--no-update-check"], { encoding: "utf8", ...(cwd ? { cwd } : {}), env: { ...process.env, HOME: home, USERPROFILE: home, FORCE_COLOR: "0", DESIGNPACA_STATE_DIR: path.join(home, ".designpaca"), }, }); } test("빌드 산출물이 있어야 한다", { skip: built ? false : "pnpm build 먼저 실행해라" }, () => { assert.ok(built); }); test("--version 은 스킬 버전과 같다", { skip: !built }, async () => { const stamp = ( await fs.readFile(fileURLToPath(new URL("../dist/skill/.designpaca_version", import.meta.url)), "utf8") ).trim(); assert.equal(run(["--version"]).trim(), stamp); }); test("도움말에 모든 명령이 나온다", { skip: !built }, () => { const out = run(["--help"]); for (const cmd of ["install", "update", "uninstall", "doctor", "list"]) { assert.ok(out.includes(cmd), `도움말에 ${cmd} 가 없다`); } }); test("dry-run 은 아무 파일도 쓰지 않는다", { skip: !built }, async () => { const before = await fs.readdir(home); const out = run(["install", "-t", "claude-code", "-s", "user", "-y", "--dry-run"]); assert.ok(out.includes("설치 계획")); assert.deepEqual(await fs.readdir(home), before); }); test("알 수 없는 대상은 종료 코드 2 로 거부한다", { skip: !built }, () => { assert.throws( () => run(["install", "-t", "emacs", "-y"]), (err: { status?: number }) => err.status === 2, ); }); test("update --dry-run 은 파일도 매니페스트도 건드리지 않는다", { skip: !built }, async () => { run(["install", "-t", "claude-code", "-s", "user", "-y"]); const manifestFile = path.join(home, ".designpaca", "manifest.json"); const skillMd = path.join(home, ".claude", "skills", "designpaca", "SKILL.md"); // 업데이트 대상이 있는 상태를 만든다 const manifest = JSON.parse(await fs.readFile(manifestFile, "utf8")); manifest.installs[0].version = "0.0.1"; await fs.writeFile(manifestFile, JSON.stringify(manifest, null, 2) + "\n", "utf8"); const manifestBefore = await fs.readFile(manifestFile, "utf8"); const skillBefore = await fs.readFile(skillMd, "utf8"); const statBefore = await fs.stat(skillMd); const out = run(["update", "--dry-run"]); assert.ok(out.includes("업데이트 계획")); assert.ok(out.includes("설치 계획")); assert.equal(await fs.readFile(manifestFile, "utf8"), manifestBefore); assert.equal(await fs.readFile(skillMd, "utf8"), skillBefore); const statAfter = await fs.stat(skillMd); assert.equal(statAfter.mtimeMs, statBefore.mtimeMs); // 대조군: 실제 update 는 반영돼야 한다 run(["update"]); const manifestAfter = JSON.parse(await fs.readFile(manifestFile, "utf8")); assert.equal(manifestAfter.installs[0].version, run(["--version"]).trim()); run(["uninstall", "-y"]); }); test("uninstall --dry-run 은 아무것도 지우지 않는다", { skip: !built }, async () => { run(["install", "-t", "claude-code", "-s", "user", "-y"]); const skillMd = path.join(home, ".claude", "skills", "designpaca", "SKILL.md"); const manifestFile = path.join(home, ".designpaca", "manifest.json"); const out = run(["uninstall", "--dry-run"]); assert.ok(out.includes("제거 계획")); await fs.access(skillMd); const manifest = JSON.parse(await fs.readFile(manifestFile, "utf8")); assert.equal(manifest.installs.length, 1); run(["uninstall", "-y"]); await assert.rejects(() => fs.access(skillMd)); }); test("설치 → doctor → 제거 왕복", { skip: !built }, async () => { run(["install", "-t", "claude-code", "-s", "user", "-y"]); const skillMd = path.join(home, ".claude", "skills", "designpaca", "SKILL.md"); assert.ok((await fs.readFile(skillMd, "utf8")).includes("name: designpaca")); assert.ok(run(["doctor"]).includes("claude-code")); run(["uninstall", "-y"]); await assert.rejects(() => fs.access(skillMd)); }); test("update 는 project 설치를 기록된 root 에서 갱신하고 cwd 에는 아무것도 쓰지 않는다", { skip: !built }, async () => { // 보안 감사 P0 재현: repoA 에 project 설치 → 버전을 낮춤 → 무관한 repoB 에서 update. // 예전에는 repoB 의 AGENTS.md 에 블록이 주입되고 사용자가 만든 스킬이 백업 없이 덮였다. const repoA = path.join(home, "repoA"); const repoB = path.join(home, "repoB"); await fs.mkdir(repoA, { recursive: true }); await fs.mkdir(path.join(repoB, ".claude", "skills", "designpaca"), { recursive: true }); await fs.writeFile(path.join(repoB, "AGENTS.md"), "# repoB\n", "utf8"); const userSkill = path.join(repoB, ".claude", "skills", "designpaca", "SKILL.md"); await fs.writeFile(userSkill, "user own skill\n", "utf8"); const listB = async () => (await fs.readdir(repoB, { recursive: true })).map(String).sort(); const beforeB = await listB(); run(["install", "-t", "agents-md,claude-code", "-s", "project", "-y"], repoA); const manifestFile = path.join(home, ".designpaca", "manifest.json"); const manifest = JSON.parse(await fs.readFile(manifestFile, "utf8")); assert.equal(manifest.installs.length, 2); for (const rec of manifest.installs) rec.version = "0.0.1"; await fs.writeFile(manifestFile, JSON.stringify(manifest, null, 2) + "\n", "utf8"); run(["update"], repoB); // repoB 는 그대로다 assert.deepEqual(await listB(), beforeB); assert.equal(await fs.readFile(path.join(repoB, "AGENTS.md"), "utf8"), "# repoB\n"); assert.equal(await fs.readFile(userSkill, "utf8"), "user own skill\n"); // repoA 기록만 새 버전으로 갱신됐고, 새 기록이 생기지 않았다 const after = JSON.parse(await fs.readFile(manifestFile, "utf8")); const version = run(["--version"]).trim(); assert.equal(after.installs.length, 2); for (const rec of after.installs) { assert.equal(rec.version, version); assert.ok(path.resolve(rec.root).startsWith(path.resolve(repoA)), `기록 root 가 repoA 밖: ${rec.root}`); } assert.ok((await fs.readFile(path.join(repoA, "AGENTS.md"), "utf8")).includes("designpaca:start")); run(["uninstall", "-y"], repoB); await assert.rejects(() => fs.access(path.join(repoA, ".claude", "skills", "designpaca", "SKILL.md"))); assert.equal(await fs.readFile(userSkill, "utf8"), "user own skill\n"); });