skills.test.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { describe, expect, it } from "vitest";
  5. import { inspectZCodeSkill, listZCodeSkills } from "../src/skills.js";
  6. describe("inspectZCodeSkill", () => {
  7. it("loads one discovered skill through the configured skill adapter", async () => {
  8. const dir = await mkdtemp(join(tmpdir(), "zcode-bootstrap-skill-"));
  9. const skillDir = join(dir, ".zcode", "skills", "demo");
  10. try {
  11. await mkdir(skillDir, { recursive: true });
  12. await writeFile(
  13. join(skillDir, "SKILL.md"),
  14. [
  15. "---",
  16. "name: demo",
  17. "description: Use for demo tasks.",
  18. "when_to_use: When inspecting skill commands.",
  19. "---",
  20. "",
  21. "# Demo Skill",
  22. "Follow this workflow.",
  23. ].join("\n"),
  24. );
  25. const inspection = await inspectZCodeSkill({
  26. env: {},
  27. name: "demo",
  28. skipUserConfig: true,
  29. workingDirectory: dir,
  30. });
  31. expect(inspection.diagnostics).toEqual([]);
  32. expect(inspection.skill.metadata.name).toBe("demo");
  33. expect(inspection.skill.metadata.whenToUse).toBe("When inspecting skill commands.");
  34. expect(inspection.skill.content).toContain("# Demo Skill");
  35. expect(inspection.skill.content).not.toContain("description:");
  36. } finally {
  37. await rm(dir, { force: true, recursive: true });
  38. }
  39. });
  40. it("returns a stable missing-skill error", async () => {
  41. const dir = await mkdtemp(join(tmpdir(), "zcode-bootstrap-skill-missing-"));
  42. try {
  43. await expect(
  44. inspectZCodeSkill({
  45. env: {},
  46. name: "missing",
  47. skipUserConfig: true,
  48. workingDirectory: dir,
  49. }),
  50. ).rejects.toThrow("Skill not found: missing");
  51. } finally {
  52. await rm(dir, { force: true, recursive: true });
  53. }
  54. });
  55. it("discovers the built-in dynamic-workflows skill as a bundled system skill, outside any plugin", async () => {
  56. // 2026-09-19 事故:技能住在可卸载的 zcode-guide 插件里,插件一停用技能就没了。现在它来自
  57. // packages/bundled-skills(bootstrap/src/app/bundled-skills.ts),与插件启停无关。
  58. const dir = await mkdtemp(join(tmpdir(), "zcode-bootstrap-bundled-skill-"));
  59. try {
  60. const outcome = await listZCodeSkills({
  61. env: { ZCODE_STORAGE_DIR: join(dir, "storage") },
  62. skipUserConfig: true,
  63. workingDirectory: dir,
  64. });
  65. const skill = outcome.skills.find((entry) => entry.name === "dynamic-workflows");
  66. expect(skill).toMatchObject({ scope: "system", source: "bundled" });
  67. expect(skill?.pluginName).toBeUndefined();
  68. expect(skill?.qualifiedName).toBeUndefined();
  69. expect(
  70. skill?.path.endsWith(join("bundled-skills", "skills", "dynamic-workflows", "SKILL.md")),
  71. ).toBe(true);
  72. } finally {
  73. await rm(dir, { force: true, recursive: true });
  74. }
  75. });
  76. it("discovers bundled content skills through default-enabled official plugins", async () => {
  77. const dir = await mkdtemp(join(tmpdir(), "zcode-bootstrap-document-skills-"));
  78. try {
  79. const env = { ZCODE_STORAGE_DIR: join(dir, "storage") };
  80. const outcome = await listZCodeSkills({
  81. env,
  82. skipUserConfig: true,
  83. workingDirectory: dir,
  84. });
  85. const bundledSkills = outcome.skills.filter((skill) =>
  86. ["control-browser", "docx", "pdf", "pptx", "web-gui-tester", "xlsx"].includes(skill.name),
  87. );
  88. expect(bundledSkills.map((skill) => skill.name).toSorted()).toEqual([
  89. "control-browser",
  90. "docx",
  91. "pdf",
  92. "pptx",
  93. "web-gui-tester",
  94. "xlsx",
  95. ]);
  96. expect(bundledSkills.map((skill) => skill.scope)).toEqual([
  97. "system",
  98. "system",
  99. "system",
  100. "system",
  101. "system",
  102. "system",
  103. ]);
  104. expect(bundledSkills.map((skill) => skill.source)).toEqual([
  105. "plugin",
  106. "plugin",
  107. "plugin",
  108. "plugin",
  109. "plugin",
  110. "plugin",
  111. ]);
  112. expect(outcome.diagnostics).toEqual([]);
  113. const pptxInspection = await inspectZCodeSkill({
  114. env,
  115. name: "pptx",
  116. skipUserConfig: true,
  117. workingDirectory: dir,
  118. });
  119. expect(pptxInspection.skill.content).toContain("pptxgenjs");
  120. } finally {
  121. await rm(dir, { force: true, recursive: true });
  122. }
  123. });
  124. });