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[]): string { return execFileSync(process.execPath, [CLI, ...args, "--no-update-check"], { encoding: "utf8", 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)); });