import { test, expect } from "bun:test"; import { mkdtempSync, mkdirSync, symlinkSync, rmSync, readdirSync, realpathSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { probeDevicesimSkills, type RuntimeInfo } from "../src/devicesim_skills"; const SNAP = "abc1234def5678900000000000000000000000ff"; const GIT = "fff0000000000000000000000000000000000001"; const RUNTIME_SHA = "151b411c6bfa746e0254751b5d1756797f6265db"; const NAMES = ["airbuild", "catuse-cli", "cat-supervisor"]; const RUNTIME_OK: RuntimeInfo = { ok: true, sha: RUNTIME_SHA, path: "/opt/homebrew/bin/cat-supervisor" }; function sandbox() { const root = realpathSync(mkdtempSync(path.join(tmpdir(), "dsskills-"))); const home = path.join(root, "home"); const ws = path.join(home, "ai_workspace"); const skills = path.join(ws, ".claude", "skills"); const userSkills = path.join(home, ".claude", "skills"); mkdirSync(skills, { recursive: true }); const probe = (over: Partial[0]> = {}) => probeDevicesimSkills({ platform: "ios", aiWorkspace: ws, home, runtime: () => RUNTIME_OK, gitHead: (d) => (d.includes("DeviceSim") ? GIT : null), ...over, }); /** 在 `dir` 下为每个 skill 建一条指向 `target(name)` 的软链。 */ const link = (dir: string, target: (n: string) => string, names = NAMES) => { mkdirSync(dir, { recursive: true }); for (const n of names) { mkdirSync(target(n), { recursive: true }); symlinkSync(target(n), path.join(dir, n)); } }; return { root, home, skills, userSkills, probe, link }; } test("非 iOS 节点不上报", () => { const s = sandbox(); try { expect(s.probe({ platform: "android" })).toBeUndefined(); } finally { rmSync(s.root, { recursive: true, force: true }); } }); test("运行时版本单独报;没装 cat-supervisor 就不 ok,哪怕 skill 都在", () => { const s = sandbox(); try { s.link(s.skills, (n) => path.join(s.root, "DeviceSim", "skills", n)); const good = s.probe()!; expect(good).toMatchObject({ runtimeSha: RUNTIME_SHA.slice(0, 12), ok: true }); expect(good.detail).toContain("cat-supervisor 151b411"); const missing = s.probe({ runtime: () => ({ ok: false, reason: "cat-supervisor 未安装" }) })!; expect(missing.ok).toBe(false); expect(missing.runtimeSha).toBeUndefined(); expect(missing.detail).toContain("cat-supervisor 未安装"); } finally { rmSync(s.root, { recursive: true, force: true }); } }); test("skill 装在用户级 ~/.claude/skills 也认", () => { const s = sandbox(); try { s.link(s.userSkills, (n) => path.join(s.root, "DeviceSim", "skills", n)); const st = s.probe()!; expect(st).toMatchObject({ sha: GIT.slice(0, 12), ok: true }); expect(st.detail).toContain(`airbuild: 用户级 ${path.join(s.root, "DeviceSim", "skills", "airbuild")}@${GIT.slice(0, 7)}`); } finally { rmSync(s.root, { recursive: true, force: true }); } }); test("项目级还指着旧快照、用户级是新的:两处都列出来并点出版本不同", () => { const s = sandbox(); try { s.link(s.skills, (n) => path.join(s.root, ".streamline", "devicesim-skills", SNAP, n), ["airbuild", "catuse-cli"]); s.link(s.userSkills, (n) => path.join(s.root, "DeviceSim", "skills", n)); const st = s.probe()!; expect(st.ok).toBe(true); expect(st.detail).toContain(`airbuild: 项目级 ${path.join(s.root, ".streamline", "devicesim-skills", SNAP, "airbuild")}@${SNAP.slice(0, 7)}`); expect(st.detail).toContain("(两处版本不同)"); expect(st.detail).toContain("skill 版本不一致"); } finally { rmSync(s.root, { recursive: true, force: true }); } }); test("实体目录也照报", () => { const s = sandbox(); try { for (const n of NAMES) mkdirSync(path.join(s.skills, n)); const st = s.probe()!; expect(st.ok).toBe(true); expect(st.detail).toContain("catuse-cli: 项目级 实体目录"); } finally { rmSync(s.root, { recursive: true, force: true }); } }); test("缺失 / 断链记为不 ok,且一个文件都不写", () => { const s = sandbox(); try { symlinkSync(path.join(s.root, "nowhere"), path.join(s.skills, "airbuild")); const st = s.probe()!; expect(st).toMatchObject({ sha: "", ok: false }); expect(st.detail).toContain("airbuild: 项目级软链断了"); expect(st.detail).toContain("catuse-cli: 缺失"); expect(st.detail).toContain("cat-supervisor: 缺失"); expect(readdirSync(s.skills)).toEqual(["airbuild"]); expect(readdirSync(s.root).sort()).toEqual(["home"]); } finally { rmSync(s.root, { recursive: true, force: true }); } });