skillsService.test.ts 46 KB

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