skillsService.test.ts 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  1. import {
  2. existsSync,
  3. mkdirSync,
  4. mkdtempSync,
  5. readFileSync,
  6. rmSync,
  7. symlinkSync,
  8. writeFileSync,
  9. } from "node:fs";
  10. import { tmpdir } from "node:os";
  11. import { join } from "node:path";
  12. import { afterEach, describe, expect, it, vi } from "vitest";
  13. import { MAX_SKILL_SCAN_DEPTH } from "@zcode/shared";
  14. import type { ISkillsService } from "../src/skills/skills.js";
  15. const originalHome = process.env.HOME;
  16. const originalProcessLabel = process.env.ZCODE_PROCESS_LABEL;
  17. const tempHomes: string[] = [];
  18. function makeTempHome(): string {
  19. const home = mkdtempSync(join(tmpdir(), "zcode-skills-home-"));
  20. tempHomes.push(home);
  21. return home;
  22. }
  23. async function createSkillsServiceInEnv(
  24. home: string,
  25. options?: { isDesktopRuntime?: boolean },
  26. ): Promise<ISkillsService> {
  27. process.env.HOME = home;
  28. vi.resetModules();
  29. const mod = await import("../src/skills/skillsService.js");
  30. return mod.createSkillsService(options);
  31. }
  32. function writeSkill(basePath: string, folder: string, content: string): void {
  33. const skillDir = join(basePath, folder);
  34. mkdirSync(skillDir, { recursive: true });
  35. writeFileSync(join(skillDir, "SKILL.md"), content, "utf-8");
  36. }
  37. function writeSkillMeta(basePath: string, folder: string, metadata: unknown): void {
  38. const skillDir = join(basePath, folder);
  39. mkdirSync(skillDir, { recursive: true });
  40. writeFileSync(join(skillDir, "_meta.json"), JSON.stringify(metadata, null, 2), "utf-8");
  41. }
  42. function workspaceSkillRoot(workspacePath: string): string {
  43. return join(workspacePath, ".zcode", "skills");
  44. }
  45. function userSkillRoot(home: string): string {
  46. return join(home, ".zcode", "skills");
  47. }
  48. function userAgentsSkillRoot(home: string): string {
  49. return join(home, ".agents", "skills");
  50. }
  51. function officialPluginRoot(home: string, pluginName: string, version = "0.1.0"): string {
  52. return join(
  53. home,
  54. ".zcode",
  55. "cli",
  56. "plugins",
  57. "cache",
  58. "zcode-plugins-official",
  59. pluginName,
  60. version,
  61. );
  62. }
  63. function marketplacePluginRoot(
  64. home: string,
  65. marketplace: string,
  66. pluginName: string,
  67. version = "0.0.0",
  68. ): string {
  69. return join(
  70. home,
  71. ".zcode",
  72. "cli",
  73. "plugins",
  74. "cache",
  75. marketplace,
  76. pluginName,
  77. version,
  78. );
  79. }
  80. function writePluginManifest(pluginRoot: string, manifest: Record<string, unknown>): void {
  81. mkdirSync(join(pluginRoot, ".zcode-plugin"), { recursive: true });
  82. writeFileSync(
  83. join(pluginRoot, ".zcode-plugin", "plugin.json"),
  84. JSON.stringify(manifest, null, 2),
  85. "utf-8",
  86. );
  87. }
  88. function writeClaudePluginManifest(pluginRoot: string, manifest: Record<string, unknown>): void {
  89. mkdirSync(join(pluginRoot, ".claude-plugin"), { recursive: true });
  90. writeFileSync(
  91. join(pluginRoot, ".claude-plugin", "plugin.json"),
  92. JSON.stringify(manifest, null, 2),
  93. "utf-8",
  94. );
  95. }
  96. function writeInstalledPluginRecord(
  97. home: string,
  98. record: {
  99. marketplace: string;
  100. name: string;
  101. installPath: string;
  102. version?: string;
  103. },
  104. ): void {
  105. const pluginsRoot = join(home, ".zcode", "cli", "plugins");
  106. mkdirSync(pluginsRoot, { recursive: true });
  107. writeFileSync(
  108. join(pluginsRoot, "installed_plugins.json"),
  109. JSON.stringify(
  110. {
  111. version: 1,
  112. plugins: [
  113. {
  114. id: `${record.name}@${record.marketplace}`,
  115. name: record.name,
  116. marketplace: record.marketplace,
  117. version: record.version ?? "0.0.0",
  118. installPath: record.installPath,
  119. installedAt: "2026-06-25T00:00:00.000Z",
  120. updatedAt: "2026-06-25T00:00:00.000Z",
  121. scope: "user",
  122. source: "./plugins/demo",
  123. },
  124. ],
  125. },
  126. null,
  127. 2,
  128. ),
  129. "utf-8",
  130. );
  131. }
  132. function writeCliConfig(home: string, config: Record<string, unknown>): void {
  133. const configPath = join(home, ".zcode", "cli", "config.json");
  134. mkdirSync(join(home, ".zcode", "cli"), { recursive: true });
  135. writeFileSync(configPath, JSON.stringify(config, null, 2), "utf-8");
  136. }
  137. afterEach(() => {
  138. process.env.HOME = originalHome;
  139. process.env.ZCODE_PROCESS_LABEL = originalProcessLabel;
  140. vi.doUnmock("node:fs/promises");
  141. vi.resetModules();
  142. while (tempHomes.length > 0) {
  143. const home = tempHomes.pop();
  144. if (home) {
  145. rmSync(home, { recursive: true, force: true });
  146. }
  147. }
  148. });
  149. describe("skillsService", () => {
  150. it("workspace 同层合并读取 .zcode 与 .agents,并忽略废弃和 Claude 技能目录", async () => {
  151. const home = makeTempHome();
  152. const workspacePath = join(home, "workspace");
  153. writeSkill(
  154. workspaceSkillRoot(workspacePath),
  155. "repo-review",
  156. `---
  157. name: repo-review
  158. description: from zcode
  159. ---
  160. zcode-body`,
  161. );
  162. writeSkill(
  163. join(workspacePath, ".zcode", "cli", "skills"),
  164. "old-glm",
  165. `---
  166. name: old-glm
  167. description: deprecated
  168. ---
  169. old-body`,
  170. );
  171. writeSkill(
  172. join(workspacePath, ".agents", "skills"),
  173. "agents-review",
  174. `---
  175. name: agents-review
  176. description: from agents
  177. ---
  178. agents-body`,
  179. );
  180. writeSkill(
  181. join(workspacePath, ".claude", "skills"),
  182. "claude-review",
  183. `---
  184. name: claude-review
  185. description: retired claude
  186. ---
  187. claude-body`,
  188. );
  189. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  190. const result = await service.list({ workspacePath, provider: "claude" });
  191. expect(result.skills.map((skill) => skill.name)).toEqual(["agents-review", "repo-review"]);
  192. expect(result.skills.find((skill) => skill.name === "agents-review")).toMatchObject({
  193. description: "from agents",
  194. scope: "workspace",
  195. enabled: true,
  196. });
  197. expect(result.skills.find((skill) => skill.name === "repo-review")).toMatchObject({
  198. description: "from zcode",
  199. scope: "workspace",
  200. enabled: true,
  201. });
  202. });
  203. it.skipIf(process.platform === "win32")(
  204. "workspace .zcode/skills 软链导入的技能可被 / 和 $ 面板发现",
  205. async () => {
  206. const home = makeTempHome();
  207. const workspacePath = join(home, "workspace");
  208. const sourceSkillParent = join(home, "external-agent", "skills");
  209. const sourceSkillDir = join(sourceSkillParent, "imported-skill");
  210. const linkedSkillDir = join(workspaceSkillRoot(workspacePath), "imported-skill");
  211. writeSkill(
  212. sourceSkillParent,
  213. "imported-skill",
  214. `---
  215. name: imported-skill
  216. description: imported via symlink
  217. ---
  218. imported-body`,
  219. );
  220. mkdirSync(workspaceSkillRoot(workspacePath), { recursive: true });
  221. symlinkSync(sourceSkillDir, linkedSkillDir, "dir");
  222. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  223. const result = await service.list({ workspacePath, provider: "glm" });
  224. expect(result.skills.map((skill) => skill.name)).toEqual(["imported-skill"]);
  225. expect(result.skills[0]).toMatchObject({
  226. description: "imported via symlink",
  227. scope: "workspace",
  228. });
  229. expect(result.skills[0]?.path).toContain("imported-skill/SKILL.md");
  230. },
  231. );
  232. it("workspace 只有 .agents/skills 时仍能发现技能", async () => {
  233. const home = makeTempHome();
  234. const workspacePath = join(home, "workspace");
  235. writeSkill(
  236. join(workspacePath, ".agents", "skills"),
  237. "agents-review",
  238. `---
  239. name: agents-review
  240. description: from agents
  241. ---
  242. agents-body`,
  243. );
  244. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  245. const result = await service.list({ workspacePath, provider: "claude" });
  246. expect(result.skills.map((skill) => skill.name)).toEqual(["agents-review"]);
  247. expect(result.skills[0]).toMatchObject({
  248. description: "from agents",
  249. scope: "workspace",
  250. });
  251. });
  252. it("递归发现分组目录,跳过隐藏目录但允许 .system 官方目录", async () => {
  253. const home = makeTempHome();
  254. const workspacePath = join(home, "workspace");
  255. const root = workspaceSkillRoot(workspacePath);
  256. writeSkill(
  257. join(root, "group-a"),
  258. "public-skill",
  259. `---
  260. name: public-skill
  261. description: visible
  262. ---
  263. public-body`,
  264. );
  265. writeSkill(
  266. join(root, "bundle", ".agents", "skills"),
  267. "vendored-skill",
  268. `---
  269. name: vendored-skill
  270. description: hidden
  271. ---
  272. vendored-body`,
  273. );
  274. writeSkill(
  275. join(root, ".system"),
  276. "bundled-system-skill",
  277. `---
  278. name: bundled-system-skill
  279. description: official
  280. ---
  281. system-body`,
  282. );
  283. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  284. const result = await service.list({ workspacePath, provider: "glm" });
  285. expect(result.skills.map((skill) => skill.name).sort()).toEqual([
  286. "bundled-system-skill",
  287. "public-skill",
  288. ]);
  289. });
  290. it.skipIf(process.platform === "win32")("递归扫描时跳过指向文件的软链", async () => {
  291. const home = makeTempHome();
  292. const workspacePath = join(home, "workspace");
  293. const root = workspaceSkillRoot(workspacePath);
  294. writeSkill(
  295. root,
  296. "zai-grafana-cli",
  297. `---
  298. name: zai-grafana-cli
  299. description: grafana cli
  300. ---
  301. body`,
  302. );
  303. const skillDir = join(root, "zai-grafana-cli");
  304. const binDir = join(skillDir, "bin");
  305. const executablePath = join(home, "gfctl");
  306. mkdirSync(binDir, { recursive: true });
  307. writeFileSync(executablePath, "#!/bin/sh\n", "utf-8");
  308. symlinkSync(executablePath, join(binDir, "gfctl"));
  309. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  310. const result = await service.list({ workspacePath, provider: "glm" });
  311. expect(result.skills.map((skill) => skill.name)).toEqual(["zai-grafana-cli"]);
  312. expect(result.diagnostics.some((diagnostic) => diagnostic.code === "skill_scan_failed")).toBe(
  313. false,
  314. );
  315. });
  316. it("读取 SKILL.md 时兼容 CRLF、多行 description 和中文冒号", async () => {
  317. const home = makeTempHome();
  318. const workspacePath = join(home, "workspace");
  319. writeSkill(
  320. workspaceSkillRoot(workspacePath),
  321. "windows-skill",
  322. "---\r\nname: windows-skill\r\ndescription: |\r\n first line\r\n second line\r\n---\r\nbody line",
  323. );
  324. writeSkill(
  325. workspaceSkillRoot(workspacePath),
  326. "cn-colon-skill",
  327. `---
  328. name: cn-colon-skill
  329. description: 学术数据查询。触发场景: 提到论文、学者、机构时使用。
  330. ---
  331. body`,
  332. );
  333. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  334. const result = await service.list({ workspacePath, provider: "glm" });
  335. expect(result.skills.find((skill) => skill.name === "windows-skill")).toMatchObject({
  336. description: "first line\nsecond line",
  337. body: "body line",
  338. });
  339. expect(result.skills.find((skill) => skill.name === "cn-colon-skill")).toMatchObject({
  340. description: "学术数据查询。触发场景: 提到论文、学者、机构时使用。",
  341. body: "body",
  342. });
  343. });
  344. it("读取技能时会合并同目录 _meta.json 的版本信息", async () => {
  345. const home = makeTempHome();
  346. const workspacePath = join(home, "workspace");
  347. const publishedAt = Date.now();
  348. writeSkill(
  349. workspaceSkillRoot(workspacePath),
  350. "feishu-drive",
  351. `---
  352. name: feishu-drive
  353. description: drive
  354. ---
  355. body`,
  356. );
  357. writeSkillMeta(workspaceSkillRoot(workspacePath), "feishu-drive", {
  358. ownerId: "owner-1",
  359. slug: "feishu-drive",
  360. version: "1.0.0",
  361. publishedAt,
  362. });
  363. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  364. const result = await service.list({ workspacePath, provider: "glm" });
  365. expect(result.skills[0]?.metadata).toEqual({
  366. ownerId: "owner-1",
  367. slug: "feishu-drive",
  368. version: "1.0.0",
  369. publishedAt,
  370. });
  371. });
  372. it("桌面 runtime 同时读取用户级 ~/.zcode/skills 和 ~/.agents/skills", async () => {
  373. const home = makeTempHome();
  374. const workspacePath = join(home, "workspace");
  375. writeSkill(
  376. userSkillRoot(home),
  377. "zcode-global",
  378. `---
  379. name: zcode-global
  380. description: user zcode
  381. ---
  382. body`,
  383. );
  384. writeSkill(
  385. userAgentsSkillRoot(home),
  386. "agents-global",
  387. `---
  388. name: agents-global
  389. description: user agents
  390. ---
  391. body`,
  392. );
  393. writeSkill(
  394. userSkillRoot(home),
  395. "copied-global",
  396. `---
  397. name: copied-global
  398. description: copied into zcode
  399. ---
  400. zcode-body`,
  401. );
  402. writeSkill(
  403. userAgentsSkillRoot(home),
  404. "copied-global",
  405. `---
  406. name: copied-global
  407. description: original agents copy
  408. ---
  409. agents-body`,
  410. );
  411. writeSkill(
  412. userSkillRoot(home),
  413. "current-review",
  414. `---
  415. name: shared-review
  416. description: zcode version
  417. ---
  418. zcode-review-body`,
  419. );
  420. writeSkill(
  421. userAgentsSkillRoot(home),
  422. "external-review",
  423. `---
  424. name: shared-review
  425. description: agents version
  426. ---
  427. agents-review-body`,
  428. );
  429. writeSkill(
  430. join(home, ".zcode", "cli", "skills"),
  431. "old-global",
  432. `---
  433. name: old-global
  434. description: deprecated
  435. ---
  436. body`,
  437. );
  438. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  439. const result = await service.list({ workspacePath, provider: "glm" });
  440. expect(result.capability).toEqual({ userScopeAvailable: true });
  441. expect(result.skills.map((skill) => skill.name)).toEqual([
  442. "agents-global",
  443. "copied-global",
  444. "shared-review",
  445. "zcode-global",
  446. ]);
  447. expect(result.skills.find((skill) => skill.name === "copied-global")?.path)
  448. .toContain(join(home, ".zcode", "skills", "copied-global"));
  449. expect(result.skills.find((skill) => skill.name === "shared-review")?.path)
  450. .toContain(join(home, ".zcode", "skills", "current-review"));
  451. expect(result.skills.every((skill) => skill.scope === "user")).toBe(true);
  452. });
  453. it("桌面 runtime 用户级 ~/.zcode/skills 没有技能时 fallback 到 ~/.agents/skills", async () => {
  454. const home = makeTempHome();
  455. const workspacePath = join(home, "workspace");
  456. writeSkill(
  457. userAgentsSkillRoot(home),
  458. "agents-global",
  459. `---
  460. name: agents-global
  461. description: user agents
  462. ---
  463. body`,
  464. );
  465. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  466. const result = await service.list({ workspacePath, provider: "glm" });
  467. expect(result.skills.map((skill) => skill.name)).toEqual(["agents-global"]);
  468. expect(result.skills[0]?.scope).toBe("user");
  469. });
  470. it("读取已启用官方 plugin 声明的技能目录", async () => {
  471. const home = makeTempHome();
  472. const workspacePath = join(home, "workspace");
  473. const browserUsePluginRoot = officialPluginRoot(home, "browser-use");
  474. const documentPluginRoot = officialPluginRoot(home, "documents");
  475. const zcodeGuidePluginRoot = officialPluginRoot(home, "zcode-guide");
  476. const iosPluginRoot = officialPluginRoot(home, "ios-simulator");
  477. const androidPluginRoot = officialPluginRoot(home, "android-emulator");
  478. writePluginManifest(browserUsePluginRoot, {
  479. name: "browser-use",
  480. skills: "skills",
  481. version: "0.1.0",
  482. });
  483. writePluginManifest(documentPluginRoot, {
  484. name: "documents",
  485. skills: "skills",
  486. version: "0.1.0",
  487. });
  488. writePluginManifest(zcodeGuidePluginRoot, {
  489. name: "zcode-guide",
  490. skills: "skills",
  491. version: "0.1.0",
  492. });
  493. writePluginManifest(iosPluginRoot, {
  494. name: "ios-simulator",
  495. skills: "skills",
  496. version: "0.1.0",
  497. });
  498. writePluginManifest(androidPluginRoot, {
  499. name: "android-emulator",
  500. skills: "skills",
  501. version: "0.1.0",
  502. });
  503. writeSkill(
  504. join(browserUsePluginRoot, "skills"),
  505. "control-browser",
  506. `---
  507. name: control-browser
  508. description: browser skill
  509. ---
  510. browser-body`,
  511. );
  512. writeSkill(
  513. join(documentPluginRoot, "skills"),
  514. "docx",
  515. `---
  516. name: docx
  517. description: docx skill
  518. ---
  519. docx-body`,
  520. );
  521. writeSkill(
  522. join(documentPluginRoot, "skills"),
  523. "pdf",
  524. `---
  525. name: pdf
  526. description: pdf skill
  527. ---
  528. pdf-body`,
  529. );
  530. writeSkill(
  531. join(zcodeGuidePluginRoot, "skills"),
  532. "diagnosing-plugins",
  533. `---
  534. name: diagnosing-plugins
  535. description: Diagnose plugin discovery and enablement problems
  536. ---
  537. diagnosing-body`,
  538. );
  539. writeSkill(
  540. join(iosPluginRoot, "skills"),
  541. "ios-simulator",
  542. `---
  543. name: ios-simulator
  544. description: ios skill
  545. ---
  546. ios-body`,
  547. );
  548. writeSkill(
  549. join(androidPluginRoot, "skills"),
  550. "android-emulator",
  551. `---
  552. name: android-emulator
  553. description: android skill
  554. ---
  555. android-body`,
  556. );
  557. writeCliConfig(home, {
  558. plugins: {
  559. enabledPlugins: {
  560. "ios-simulator@zcode-plugins-official": true,
  561. },
  562. },
  563. });
  564. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  565. const result = await service.list({ workspacePath, provider: "glm" });
  566. expect(result.skills.map((skill) => skill.name)).toEqual([
  567. "control-browser",
  568. "diagnosing-plugins",
  569. "docx",
  570. "ios-simulator",
  571. "pdf",
  572. ]);
  573. expect(result.skills.map((skill) => skill.scope)).toEqual([
  574. "plugin",
  575. "plugin",
  576. "plugin",
  577. "plugin",
  578. "plugin",
  579. ]);
  580. expect(result.skills.map((skill) => skill.pluginName).sort()).toEqual([
  581. "browser-use",
  582. "documents",
  583. "documents",
  584. "ios-simulator",
  585. "zcode-guide",
  586. ]);
  587. expect(result.skills.find((skill) => skill.name === "android-emulator")).toBeUndefined();
  588. });
  589. it("suppressed 内置插件不贡献任何 skill", async () => {
  590. const home = makeTempHome();
  591. const workspacePath = join(home, "workspace");
  592. const browserUsePluginRoot = officialPluginRoot(home, "browser-use");
  593. const documentPluginRoot = officialPluginRoot(home, "documents");
  594. const skillCreatorPluginRoot = officialPluginRoot(home, "skill-creator");
  595. const zcodeGuidePluginRoot = officialPluginRoot(home, "zcode-guide");
  596. writePluginManifest(browserUsePluginRoot, {
  597. name: "browser-use",
  598. skills: "skills",
  599. version: "0.1.0",
  600. });
  601. writePluginManifest(documentPluginRoot, {
  602. name: "documents",
  603. skills: "skills",
  604. version: "0.1.0",
  605. });
  606. writePluginManifest(skillCreatorPluginRoot, {
  607. name: "skill-creator",
  608. skills: "skills",
  609. version: "0.1.0",
  610. });
  611. writePluginManifest(zcodeGuidePluginRoot, {
  612. name: "zcode-guide",
  613. skills: "skills",
  614. version: "0.1.0",
  615. });
  616. writeSkill(
  617. join(browserUsePluginRoot, "skills"),
  618. "control-browser",
  619. `---
  620. name: control-browser
  621. description: browser skill
  622. ---
  623. browser-body`,
  624. );
  625. writeSkill(
  626. join(documentPluginRoot, "skills"),
  627. "docx",
  628. `---
  629. name: docx
  630. description: docx skill
  631. ---
  632. docx-body`,
  633. );
  634. writeSkill(
  635. join(skillCreatorPluginRoot, "skills"),
  636. "skill-creator",
  637. `---
  638. name: skill-creator
  639. description: skill creator skill
  640. ---
  641. creator-body`,
  642. );
  643. writeSkill(
  644. join(zcodeGuidePluginRoot, "skills"),
  645. "diagnosing-plugins",
  646. `---
  647. name: diagnosing-plugins
  648. description: Diagnose plugin discovery and enablement problems
  649. ---
  650. diagnosing-body`,
  651. );
  652. // browser、documents 和 zcode-guide 被卸载(suppressed),skill-creator 保持默认启用。
  653. writeCliConfig(home, {
  654. plugins: {
  655. suppressedBuiltins: [
  656. "browser-use@zcode-plugins-official",
  657. "documents@zcode-plugins-official",
  658. "zcode-guide@zcode-plugins-official",
  659. ],
  660. },
  661. });
  662. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  663. const result = await service.list({ workspacePath, provider: "glm" });
  664. expect(result.skills.map((skill) => skill.name)).toEqual(["skill-creator"]);
  665. expect(result.skills.find((skill) => skill.name === "control-browser")).toBeUndefined();
  666. expect(result.skills.find((skill) => skill.name === "docx")).toBeUndefined();
  667. expect(result.skills.find((skill) => skill.name === "diagnosing-plugins")).toBeUndefined();
  668. expect(result.skills.find((skill) => skill.pluginName === "browser")).toBeUndefined();
  669. expect(result.skills.find((skill) => skill.pluginName === "documents")).toBeUndefined();
  670. expect(result.skills.find((skill) => skill.pluginName === "zcode-guide")).toBeUndefined();
  671. });
  672. it("读取已安装 marketplace plugin 声明的技能目录", async () => {
  673. const home = makeTempHome();
  674. const workspacePath = join(home, "workspace");
  675. const pluginRoot = marketplacePluginRoot(
  676. home,
  677. "claude-plugins-official",
  678. "amazon-location-service",
  679. );
  680. writeClaudePluginManifest(pluginRoot, {
  681. name: "amazon-location-service",
  682. skills: "skills",
  683. version: "1.0.0",
  684. });
  685. writeSkill(
  686. join(pluginRoot, "skills"),
  687. "amazon-location-service",
  688. `---
  689. name: amazon-location-service
  690. description: amazon location skill
  691. ---
  692. amazon-body`,
  693. );
  694. writeInstalledPluginRecord(home, {
  695. installPath: pluginRoot,
  696. marketplace: "claude-plugins-official",
  697. name: "amazon-location-service",
  698. });
  699. writeCliConfig(home, {
  700. plugins: {
  701. enabledPlugins: {
  702. "amazon-location-service@claude-plugins-official": true,
  703. },
  704. },
  705. });
  706. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  707. const result = await service.list({ workspacePath, provider: "glm" });
  708. expect(result.skills).toEqual([
  709. expect.objectContaining({
  710. name: "amazon-location-service",
  711. pluginName: "amazon-location-service",
  712. pluginId: "amazon-location-service@claude-plugins-official",
  713. scope: "plugin",
  714. }),
  715. ]);
  716. });
  717. // Bugfix 回归(差一层目录):Claude 插件规范里 manifest skills 数组项指向「技能目录本身」
  718. // (项内直接是 SKILL.md),agent 端扫描已支持(adapters/src/skills/scan.ts);这里验证
  719. // 桌面 services 端技能列表对同一形态的消费,两端语义保持一致。
  720. it("读取 manifest skills 数组声明的插件技能(数组项即技能目录)", async () => {
  721. const home = makeTempHome();
  722. const workspacePath = join(home, "workspace");
  723. const pluginRoot = marketplacePluginRoot(
  724. home,
  725. "claude-plugins-official",
  726. "mattpocock-skills",
  727. );
  728. writeClaudePluginManifest(pluginRoot, {
  729. name: "mattpocock-skills",
  730. skills: ["./skills/engineering/tdd", "./skills/productivity/tdd", "./skills/bundle-root"],
  731. version: "1.0.0",
  732. });
  733. // 数组项内直接是 SKILL.md;engineering/tdd 与 productivity/tdd 叶子同名但 frontmatter
  734. // name 不同,验证不被提前按目录名去重;bundle-root 验证「根自身即技能」规则。
  735. writeSkill(
  736. join(pluginRoot, "skills", "engineering", "tdd"),
  737. "tdd",
  738. `---
  739. name: tdd
  740. description: engineering tdd skill
  741. ---
  742. tdd-body`,
  743. );
  744. writeSkill(
  745. join(pluginRoot, "skills", "productivity", "tdd"),
  746. "productivity-tdd",
  747. `---
  748. name: productivity-tdd
  749. description: productivity tdd skill
  750. ---
  751. productivity-tdd-body`,
  752. );
  753. writeSkill(
  754. join(pluginRoot, "skills", "bundle-root"),
  755. "bundle-root",
  756. `---
  757. name: bundle-root
  758. description: skill whose SKILL.md sits directly in the declared root
  759. ---
  760. bundle-root-body`,
  761. );
  762. writeInstalledPluginRecord(home, {
  763. installPath: pluginRoot,
  764. marketplace: "claude-plugins-official",
  765. name: "mattpocock-skills",
  766. });
  767. writeCliConfig(home, {
  768. plugins: {
  769. enabledPlugins: {
  770. "mattpocock-skills@claude-plugins-official": true,
  771. },
  772. },
  773. });
  774. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  775. const result = await service.list({ workspacePath, provider: "glm" });
  776. expect(result.skills.map((skill) => skill.name).sort()).toEqual([
  777. "bundle-root",
  778. "productivity-tdd",
  779. "tdd",
  780. ]);
  781. for (const skill of result.skills) {
  782. expect(skill.pluginName).toBe("mattpocock-skills");
  783. expect(skill.scope).toBe("plugin");
  784. }
  785. });
  786. it("非桌面 runtime 不读取用户级技能", async () => {
  787. const home = makeTempHome();
  788. const workspacePath = join(home, "workspace");
  789. writeSkill(
  790. userSkillRoot(home),
  791. "zcode-global",
  792. `---
  793. name: zcode-global
  794. description: user zcode
  795. ---
  796. body`,
  797. );
  798. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: false });
  799. const result = await service.list({ workspacePath, provider: "glm" });
  800. expect(result.capability).toEqual({
  801. userScopeAvailable: false,
  802. userScopeReason: "desktop_only",
  803. });
  804. expect(result.skills).toEqual([]);
  805. });
  806. // Windows 跳过:此测试失败原因是 CLI config 写入逻辑问题,非 Windows 特定
  807. const isWindows = process.platform === "win32";
  808. (isWindows ? it.skip : it)("setEnabled 会把技能开关按路径写入 CLI config 且不按 workspaceIdentity 隔离", async () => {
  809. const home = makeTempHome();
  810. const workspacePath = join(home, "workspace");
  811. writeSkill(
  812. workspaceSkillRoot(workspacePath),
  813. "repo-review",
  814. `---
  815. name: repo-review
  816. description: review
  817. ---
  818. body`,
  819. );
  820. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  821. const before = await service.list({
  822. workspacePath,
  823. workspaceIdentity: "remote:ssh:a:/workspace",
  824. provider: "glm",
  825. });
  826. const skill = before.skills[0]!;
  827. await service.setEnabled({
  828. workspacePath,
  829. workspaceIdentity: "remote:ssh:a:/workspace",
  830. provider: "glm",
  831. scope: skill.scope,
  832. skillId: skill.id,
  833. enabled: false,
  834. });
  835. const disabled = await service.list({
  836. workspacePath,
  837. workspaceIdentity: "remote:ssh:a:/workspace",
  838. provider: "glm",
  839. });
  840. const enabled = await service.list({
  841. workspacePath,
  842. workspaceIdentity: "remote:ssh:b:/workspace",
  843. provider: "glm",
  844. });
  845. const config = JSON.parse(
  846. readFileSync(join(home, ".zcode", "cli", "config.json"), "utf-8"),
  847. ) as { skills?: Record<string, { enable?: boolean }> };
  848. expect(disabled.skills[0]?.enabled).toBe(false);
  849. expect(enabled.skills[0]?.enabled).toBe(false);
  850. expect(config.skills?.[skill.path]).toEqual({ enable: false });
  851. await service.setEnabled({
  852. workspacePath,
  853. workspaceIdentity: "remote:ssh:a:/workspace",
  854. provider: "glm",
  855. scope: skill.scope,
  856. skillId: skill.id,
  857. enabled: true,
  858. });
  859. const reenabled = await service.list({
  860. workspacePath,
  861. workspaceIdentity: "remote:ssh:a:/workspace",
  862. provider: "glm",
  863. });
  864. const reenabledConfig = JSON.parse(
  865. readFileSync(join(home, ".zcode", "cli", "config.json"), "utf-8"),
  866. ) as { skills?: Record<string, { enable?: boolean }> };
  867. expect(reenabled.skills[0]?.enabled).toBe(true);
  868. expect(reenabledConfig.skills?.[skill.path]).toBeUndefined();
  869. });
  870. it("读取旧版 skills-state.json 时不会迁移或写入旧状态文件", async () => {
  871. const home = makeTempHome();
  872. const workspacePath = join(home, "workspace");
  873. const legacySkillId = "workspace:legacy-skill";
  874. const legacyStatePath = join(home, ".zcode", "v2", "skills-state.json");
  875. mkdirSync(join(home, ".zcode", "v2"), { recursive: true });
  876. writeFileSync(
  877. legacyStatePath,
  878. JSON.stringify({ disabledByWorkspace: { [workspacePath]: [legacySkillId] } }),
  879. "utf-8",
  880. );
  881. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  882. await service.list({ workspacePath, provider: "glm" });
  883. const state = JSON.parse(readFileSync(legacyStatePath, "utf-8")) as {
  884. disabledByWorkspace?: Record<string, string[]>;
  885. };
  886. expect(state.disabledByWorkspace).toEqual({ [workspacePath]: [legacySkillId] });
  887. expect(existsSync(join(home, ".zcode", "cli", "config.json"))).toBe(false);
  888. });
  889. it("buildPromptContext 只注入启用且被提及的技能正文", async () => {
  890. const home = makeTempHome();
  891. const workspacePath = join(home, "workspace");
  892. writeSkill(
  893. workspaceSkillRoot(workspacePath),
  894. "code-review",
  895. `---
  896. name: code-review
  897. description: Review code
  898. ---
  899. review-body`,
  900. );
  901. writeSkill(
  902. workspaceSkillRoot(workspacePath),
  903. "plan-maker",
  904. `---
  905. name: plan-maker
  906. description: Make plans
  907. ---
  908. plan-body`,
  909. );
  910. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  911. const context = await service.buildPromptContext({
  912. workspacePath,
  913. provider: "glm",
  914. prompt: "请先执行 $code-review",
  915. });
  916. expect(context.prompt).toContain("<available_skills>");
  917. expect(context.prompt).toContain("<activated_skill name=\"code-review\"");
  918. expect(context.prompt).toContain("review-body");
  919. expect(context.prompt).not.toContain("plan-body");
  920. expect(context.activatedSkillNames).toEqual(["code-review"]);
  921. expect(existsSync(join(home, ".zcode", "v2", "skills-audit.log"))).toBe(true);
  922. });
  923. it("被禁用的技能即便被提及也不会注入", async () => {
  924. const home = makeTempHome();
  925. const workspacePath = join(home, "workspace");
  926. writeSkill(
  927. workspaceSkillRoot(workspacePath),
  928. "code-review",
  929. `---
  930. name: code-review
  931. description: Review code
  932. ---
  933. review-body`,
  934. );
  935. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  936. const list = await service.list({ workspacePath, provider: "glm" });
  937. await service.setEnabled({
  938. workspacePath,
  939. provider: "glm",
  940. scope: list.skills[0]!.scope,
  941. skillId: list.skills[0]!.id,
  942. enabled: false,
  943. });
  944. const context = await service.buildPromptContext({
  945. workspacePath,
  946. provider: "glm",
  947. prompt: "请先执行 $code-review",
  948. });
  949. expect(context.prompt).toBe("请先执行 $code-review");
  950. expect(context.activatedSkillNames).toEqual([]);
  951. });
  952. it("缺少 frontmatter 时用目录名加载且不产出诊断", async () => {
  953. const home = makeTempHome();
  954. const workspacePath = join(home, "workspace");
  955. writeSkill(
  956. workspaceSkillRoot(workspacePath),
  957. "no-frontmatter",
  958. "just markdown body without yaml header",
  959. );
  960. writeSkill(
  961. workspaceSkillRoot(workspacePath),
  962. "good-skill",
  963. `---
  964. name: good-skill
  965. description: ok
  966. ---
  967. body`,
  968. );
  969. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  970. const result = await service.list({ workspacePath, provider: "glm" });
  971. expect(result.skills.map((skill) => skill.name)).toEqual(["good-skill", "no-frontmatter"]);
  972. expect(result.skills.find((skill) => skill.name === "no-frontmatter")?.description).toBe("");
  973. expect(result.skills.find((skill) => skill.name === "no-frontmatter")?.body).toBe(
  974. "just markdown body without yaml header",
  975. );
  976. expect(
  977. result.diagnostics.some(
  978. (diagnostic) =>
  979. diagnostic.code === "skill_missing_frontmatter" && diagnostic.severity === "error",
  980. ),
  981. ).toBe(false);
  982. });
  983. it("name 不符合 lowercase-hyphen 规范时仍按原 name 加载且不产出 warning", async () => {
  984. const home = makeTempHome();
  985. const workspacePath = join(home, "workspace");
  986. writeSkill(
  987. workspaceSkillRoot(workspacePath),
  988. "fallback-name",
  989. `---
  990. name: BadName_With_Caps
  991. description: bad name
  992. ---
  993. body`,
  994. );
  995. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  996. const result = await service.list({ workspacePath, provider: "glm" });
  997. expect(result.skills.map((skill) => skill.name)).toEqual(["BadName_With_Caps"]);
  998. expect(
  999. result.diagnostics.some(
  1000. (diagnostic) =>
  1001. diagnostic.code === "skill_invalid_name" && diagnostic.skillName === "BadName_With_Caps",
  1002. ),
  1003. ).toBe(false);
  1004. });
  1005. it("description 超过 1024 字符会产出 error 并跳过", async () => {
  1006. const home = makeTempHome();
  1007. const workspacePath = join(home, "workspace");
  1008. const longDescription = "a".repeat(1100);
  1009. writeSkill(
  1010. workspaceSkillRoot(workspacePath),
  1011. "too-long",
  1012. `---
  1013. name: too-long
  1014. description: "${longDescription}"
  1015. ---
  1016. body`,
  1017. );
  1018. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  1019. const result = await service.list({ workspacePath, provider: "glm" });
  1020. expect(result.skills).toEqual([]);
  1021. expect(
  1022. result.diagnostics.some((diagnostic) => diagnostic.code === "skill_description_too_long"),
  1023. ).toBe(true);
  1024. });
  1025. it("未识别的 frontmatter key 不产出 warning 且 skill 仍可用", async () => {
  1026. const home = makeTempHome();
  1027. const workspacePath = join(home, "workspace");
  1028. writeSkill(
  1029. workspaceSkillRoot(workspacePath),
  1030. "extra-key",
  1031. `---
  1032. name: extra-key
  1033. description: ok
  1034. unknown-field: hello
  1035. ---
  1036. body`,
  1037. );
  1038. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  1039. const result = await service.list({ workspacePath, provider: "glm" });
  1040. expect(result.skills.map((skill) => skill.name)).toEqual(["extra-key"]);
  1041. expect(
  1042. result.diagnostics.some(
  1043. (diagnostic) =>
  1044. diagnostic.code === "skill_unknown_frontmatter" &&
  1045. diagnostic.message.includes("unknown-field"),
  1046. ),
  1047. ).toBe(false);
  1048. });
  1049. it("同名 skill 出现两次时按路径同时保留且不产出 duplicate warning", async () => {
  1050. const home = makeTempHome();
  1051. const workspacePath = join(home, "workspace");
  1052. writeSkill(
  1053. workspaceSkillRoot(workspacePath),
  1054. "dup-first",
  1055. `---
  1056. name: shared-name
  1057. description: first
  1058. ---
  1059. first-body`,
  1060. );
  1061. writeSkill(
  1062. workspaceSkillRoot(workspacePath),
  1063. "dup-second",
  1064. `---
  1065. name: shared-name
  1066. description: second
  1067. ---
  1068. second-body`,
  1069. );
  1070. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  1071. const result = await service.list({ workspacePath, provider: "glm" });
  1072. expect(result.skills.map((skill) => skill.name)).toEqual(["shared-name", "shared-name"]);
  1073. expect(result.skills.map((skill) => skill.path.replaceAll("\\", "/").split("/skills/").at(-1))).toEqual([
  1074. "dup-first/SKILL.md",
  1075. "dup-second/SKILL.md",
  1076. ]);
  1077. expect(
  1078. result.diagnostics.some((diagnostic) => diagnostic.code === "skill_duplicate_name"),
  1079. ).toBe(false);
  1080. });
  1081. it("worktree 内子目录扫描会向上汇集祖先 .zcode/skills", async () => {
  1082. const home = makeTempHome();
  1083. const repoRoot = join(home, "repo");
  1084. mkdirSync(join(repoRoot, ".git"), { recursive: true });
  1085. const childWorkspace = join(repoRoot, "packages", "child");
  1086. writeSkill(
  1087. workspaceSkillRoot(childWorkspace),
  1088. "child-skill",
  1089. `---
  1090. name: child-skill
  1091. description: scoped to child
  1092. ---
  1093. body`,
  1094. );
  1095. writeSkill(
  1096. workspaceSkillRoot(repoRoot),
  1097. "root-skill",
  1098. `---
  1099. name: root-skill
  1100. description: scoped to repo root
  1101. ---
  1102. body`,
  1103. );
  1104. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  1105. const result = await service.list({ workspacePath: childWorkspace, provider: "glm" });
  1106. expect(result.skills.map((skill) => skill.name).sort()).toEqual([
  1107. "child-skill",
  1108. "root-skill",
  1109. ]);
  1110. });
  1111. it("deleteSkill 删除 .zcode/skills 工作区技能目录", async () => {
  1112. const home = makeTempHome();
  1113. const workspacePath = join(home, "workspace");
  1114. const skillDir = join(workspaceSkillRoot(workspacePath), "repo-review");
  1115. writeSkill(
  1116. workspaceSkillRoot(workspacePath),
  1117. "repo-review",
  1118. `---
  1119. name: repo-review
  1120. description: review
  1121. ---
  1122. body`,
  1123. );
  1124. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  1125. const before = await service.list({ workspacePath, provider: "glm" });
  1126. const skill = before.skills.find((s) => s.name === "repo-review")!;
  1127. expect(existsSync(skillDir)).toBe(true);
  1128. await service.deleteSkill({ workspacePath, skillId: skill.id });
  1129. expect(existsSync(skillDir)).toBe(false);
  1130. const after = await service.list({ workspacePath, provider: "glm" });
  1131. expect(after.skills.find((s) => s.name === "repo-review")).toBeUndefined();
  1132. });
  1133. it.skipIf(process.platform === "win32")(
  1134. "deleteSkill 删除用户级 ~/.zcode/skills 软链技能:只删链接、保留外部目标",
  1135. async () => {
  1136. const home = makeTempHome();
  1137. const workspacePath = join(home, "workspace");
  1138. const sourceParent = join(home, "external-agent", "skills");
  1139. const sourceSkillDir = join(sourceParent, "cua-driver");
  1140. const linkedSkillDir = join(userSkillRoot(home), "cua-driver");
  1141. writeSkill(
  1142. sourceParent,
  1143. "cua-driver",
  1144. `---
  1145. name: cua-driver
  1146. description: linked
  1147. ---
  1148. body`,
  1149. );
  1150. mkdirSync(userSkillRoot(home), { recursive: true });
  1151. symlinkSync(sourceSkillDir, linkedSkillDir, "dir");
  1152. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  1153. const before = await service.list({ workspacePath, provider: "glm" });
  1154. const skill = before.skills.find((s) => s.name === "cua-driver")!;
  1155. expect(skill.scope).toBe("user");
  1156. await service.deleteSkill({ workspacePath, skillId: skill.id });
  1157. // 软链本体被删除
  1158. expect(existsSync(linkedSkillDir)).toBe(false);
  1159. // 外部目标目录及其 SKILL.md 完好无损
  1160. expect(existsSync(sourceSkillDir)).toBe(true);
  1161. expect(existsSync(join(sourceSkillDir, "SKILL.md"))).toBe(true);
  1162. const after = await service.list({ workspacePath, provider: "glm" });
  1163. expect(after.skills.find((s) => s.name === "cua-driver")).toBeUndefined();
  1164. },
  1165. );
  1166. it.skipIf(process.platform === "win32")(
  1167. "deleteSkill 删除工作区 .zcode/skills 软链技能:只删链接、保留外部目标",
  1168. async () => {
  1169. const home = makeTempHome();
  1170. const workspacePath = join(home, "workspace");
  1171. const sourceParent = join(home, "external-agent", "skills");
  1172. const sourceSkillDir = join(sourceParent, "imported-skill");
  1173. const linkedSkillDir = join(workspaceSkillRoot(workspacePath), "imported-skill");
  1174. writeSkill(
  1175. sourceParent,
  1176. "imported-skill",
  1177. `---
  1178. name: imported-skill
  1179. description: linked
  1180. ---
  1181. body`,
  1182. );
  1183. mkdirSync(workspaceSkillRoot(workspacePath), { recursive: true });
  1184. symlinkSync(sourceSkillDir, linkedSkillDir, "dir");
  1185. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  1186. const before = await service.list({ workspacePath, provider: "glm" });
  1187. const skill = before.skills.find((s) => s.name === "imported-skill")!;
  1188. await service.deleteSkill({ workspacePath, skillId: skill.id });
  1189. expect(existsSync(linkedSkillDir)).toBe(false);
  1190. expect(existsSync(join(sourceSkillDir, "SKILL.md"))).toBe(true);
  1191. const after = await service.list({ workspacePath, provider: "glm" });
  1192. expect(after.skills.find((s) => s.name === "imported-skill")).toBeUndefined();
  1193. },
  1194. );
  1195. it.skipIf(process.platform === "win32")(
  1196. "deleteSkill 拒绝经由“软链祖先目录”删除,保护外部目标数据",
  1197. async () => {
  1198. // 用户把一整个外部目录软链进 ~/.zcode/skills(分组软链),
  1199. // 其下的技能扫描时会被发现。删除时若顺着分组软链 rm,会删掉外部真实目录里的子技能——数据丢失。
  1200. // 本用例断言这种情况被安全拦截:抛错、且外部目标完好。
  1201. const home = makeTempHome();
  1202. const workspacePath = join(home, "workspace");
  1203. const externalGroup = join(home, "external-agent", "group");
  1204. writeSkill(
  1205. externalGroup,
  1206. "nested-skill",
  1207. `---
  1208. name: nested-skill
  1209. description: nested under a symlinked group dir
  1210. ---
  1211. body`,
  1212. );
  1213. mkdirSync(userSkillRoot(home), { recursive: true });
  1214. // ~/.zcode/skills/mygroup -> /external-agent/group
  1215. symlinkSync(externalGroup, join(userSkillRoot(home), "mygroup"), "dir");
  1216. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  1217. const before = await service.list({ workspacePath, provider: "glm" });
  1218. const skill = before.skills.find((s) => s.name === "nested-skill")!;
  1219. await expect(
  1220. service.deleteSkill({ workspacePath, skillId: skill.id }),
  1221. ).rejects.toThrow();
  1222. // 外部目标目录及其 SKILL.md 必须完好无损
  1223. expect(existsSync(join(externalGroup, "nested-skill", "SKILL.md"))).toBe(true);
  1224. },
  1225. );
  1226. it("deleteSkill 删除用户级 ~/.agents/skills 技能(removeFromCommon 会拒绝的场景)", async () => {
  1227. const home = makeTempHome();
  1228. const workspacePath = join(home, "workspace");
  1229. const skillDir = join(userAgentsSkillRoot(home), "feishu-drive");
  1230. writeSkill(
  1231. userAgentsSkillRoot(home),
  1232. "feishu-drive",
  1233. `---
  1234. name: feishu-drive
  1235. description: drive
  1236. ---
  1237. body`,
  1238. );
  1239. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  1240. const before = await service.list({ workspacePath, provider: "glm" });
  1241. const skill = before.skills.find((s) => s.name === "feishu-drive")!;
  1242. expect(skill.scope).toBe("user");
  1243. expect(existsSync(skillDir)).toBe(true);
  1244. await service.deleteSkill({ workspacePath, skillId: skill.id });
  1245. expect(existsSync(skillDir)).toBe(false);
  1246. const after = await service.list({ workspacePath, provider: "glm" });
  1247. expect(after.skills.find((s) => s.name === "feishu-drive")).toBeUndefined();
  1248. });
  1249. it("deleteSkill 拒绝删除 plugin 作用域技能", async () => {
  1250. const home = makeTempHome();
  1251. const workspacePath = join(home, "workspace");
  1252. const pluginRoot = marketplacePluginRoot(
  1253. home,
  1254. "claude-plugins-official",
  1255. "amazon-location-service",
  1256. );
  1257. writeClaudePluginManifest(pluginRoot, {
  1258. name: "amazon-location-service",
  1259. skills: "skills",
  1260. version: "1.0.0",
  1261. });
  1262. const pluginSkillDir = join(pluginRoot, "skills", "amazon-location-service");
  1263. writeSkill(
  1264. join(pluginRoot, "skills"),
  1265. "amazon-location-service",
  1266. `---
  1267. name: amazon-location-service
  1268. description: amazon location skill
  1269. ---
  1270. amazon-body`,
  1271. );
  1272. writeInstalledPluginRecord(home, {
  1273. installPath: pluginRoot,
  1274. marketplace: "claude-plugins-official",
  1275. name: "amazon-location-service",
  1276. });
  1277. writeCliConfig(home, {
  1278. plugins: {
  1279. enabledPlugins: {
  1280. "amazon-location-service@claude-plugins-official": true,
  1281. },
  1282. },
  1283. });
  1284. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  1285. const before = await service.list({ workspacePath, provider: "glm" });
  1286. const skill = before.skills.find((s) => s.name === "amazon-location-service")!;
  1287. expect(skill.scope).toBe("plugin");
  1288. await expect(service.deleteSkill({ workspacePath, skillId: skill.id })).rejects.toThrow();
  1289. // 插件技能目录不应被删除。
  1290. expect(existsSync(pluginSkillDir)).toBe(true);
  1291. });
  1292. it("deleteSkill 对未知 skillId 抛错", async () => {
  1293. const home = makeTempHome();
  1294. const workspacePath = join(home, "workspace");
  1295. writeSkill(
  1296. workspaceSkillRoot(workspacePath),
  1297. "repo-review",
  1298. `---
  1299. name: repo-review
  1300. description: review
  1301. ---
  1302. body`,
  1303. );
  1304. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  1305. await expect(
  1306. service.deleteSkill({ workspacePath, skillId: "does-not-exist" }),
  1307. ).rejects.toThrow();
  1308. });
  1309. it("扫描技能根时跳过 node_modules 等内容目录", async () => {
  1310. const home = makeTempHome();
  1311. const workspacePath = join(home, "workspace");
  1312. const root = workspaceSkillRoot(workspacePath);
  1313. // 真实技能。
  1314. writeSkill(
  1315. root,
  1316. "real-skill",
  1317. `---
  1318. name: real-skill
  1319. description: real
  1320. ---
  1321. real-body`,
  1322. );
  1323. // Bugfix(ZCT-2070384010482601984):node_modules 里携带的 SKILL.md 不应被发现,
  1324. // 旧实现会整棵递归 node_modules,在 Windows 上把扫描放大到数十秒。
  1325. writeSkill(
  1326. join(root, "real-skill", "node_modules", "some-dep"),
  1327. "skill",
  1328. `---
  1329. name: dep-skill
  1330. description: should be ignored
  1331. ---
  1332. dep-body`,
  1333. );
  1334. // dist 同理排除。
  1335. writeSkill(
  1336. join(root, "dist"),
  1337. "built",
  1338. `---
  1339. name: dist-skill
  1340. description: should be ignored
  1341. ---
  1342. dist-body`,
  1343. );
  1344. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  1345. const result = await service.list({ workspacePath, provider: "glm" });
  1346. expect(result.skills.map((skill) => skill.name)).toEqual(["real-skill"]);
  1347. });
  1348. it("分组目录(depth 2)内的技能仍可被发现", async () => {
  1349. const home = makeTempHome();
  1350. const workspacePath = join(home, "workspace");
  1351. const root = workspaceSkillRoot(workspacePath);
  1352. writeSkill(
  1353. join(root, "group-a"),
  1354. "grouped-skill",
  1355. `---
  1356. name: grouped-skill
  1357. description: grouped
  1358. ---
  1359. grouped-body`,
  1360. );
  1361. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  1362. const result = await service.list({ workspacePath, provider: "glm" });
  1363. expect(result.skills.map((skill) => skill.name)).toEqual(["grouped-skill"]);
  1364. });
  1365. it("超过最大扫描深度的技能不会被发现", async () => {
  1366. const home = makeTempHome();
  1367. const workspacePath = join(home, "workspace");
  1368. const root = workspaceSkillRoot(workspacePath);
  1369. // 构造一条超过 MAX_SKILL_SCAN_DEPTH 的目录链,末端放一个 SKILL.md。
  1370. const segments = Array.from({ length: MAX_SKILL_SCAN_DEPTH + 2 }, (_, index) => `lvl-${index}`);
  1371. writeSkill(join(root, ...segments), "too-deep", `---
  1372. name: too-deep
  1373. description: beyond depth cap
  1374. ---
  1375. deep-body`);
  1376. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  1377. const result = await service.list({ workspacePath, provider: "glm" });
  1378. expect(result.skills.map((skill) => skill.name)).not.toContain("too-deep");
  1379. });
  1380. it.skipIf(process.platform === "win32")(
  1381. "目录软链构成环时扫描可终止且不重复",
  1382. async () => {
  1383. const home = makeTempHome();
  1384. const workspacePath = join(home, "workspace");
  1385. const root = workspaceSkillRoot(workspacePath);
  1386. writeSkill(
  1387. root,
  1388. "cyclic-skill",
  1389. `---
  1390. name: cyclic-skill
  1391. description: cyclic
  1392. ---
  1393. cyclic-body`,
  1394. );
  1395. // 在技能根内放一个指回根的软链,制造潜在环路。
  1396. mkdirSync(root, { recursive: true });
  1397. symlinkSync(root, join(root, "loop-back"), "dir");
  1398. const service = await createSkillsServiceInEnv(home, { isDesktopRuntime: true });
  1399. const result = await service.list({ workspacePath, provider: "glm" });
  1400. // 不应卡死、不应重复列出同一技能。
  1401. expect(result.skills.map((skill) => skill.name)).toEqual(["cyclic-skill"]);
  1402. },
  1403. );
  1404. });