skills.test.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { symlink } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import { describe, expect, it } from "vitest";
  4. import { NodeExecutionEnv } from "../../src/harness/env/nodejs.ts";
  5. import { loadSkills, loadSourcedSkills } from "../../src/harness/skills.ts";
  6. import { createTempDir } from "./session-test-utils.ts";
  7. describe("loadSkills", () => {
  8. it("loads SKILL.md files through the execution environment", async () => {
  9. const root = createTempDir();
  10. const env = new NodeExecutionEnv({ cwd: root });
  11. await env.createDir(".agents/skills/example", { recursive: true });
  12. await env.writeFile(
  13. ".agents/skills/example/SKILL.md",
  14. `---
  15. name: example
  16. description: Example skill
  17. disable-model-invocation: true
  18. ---
  19. Use this skill.
  20. `,
  21. );
  22. const { skills, diagnostics } = await loadSkills(env, ".agents/skills");
  23. expect(diagnostics).toEqual([]);
  24. expect(skills).toEqual([
  25. {
  26. name: "example",
  27. description: "Example skill",
  28. content: "Use this skill.",
  29. filePath: join(root, ".agents/skills/example/SKILL.md"),
  30. disableModelInvocation: true,
  31. },
  32. ]);
  33. });
  34. it("loads skills through symlinked directories", async () => {
  35. const root = createTempDir();
  36. const env = new NodeExecutionEnv({ cwd: root });
  37. await env.createDir("actual/example", { recursive: true });
  38. await env.writeFile(
  39. "actual/example/SKILL.md",
  40. "---\nname: example\ndescription: Example skill\n---\nUse this skill.",
  41. );
  42. await symlink(join(root, "actual"), join(root, "skills-link"));
  43. const { skills } = await loadSkills(env, "skills-link");
  44. expect(skills.map((skill) => skill.name)).toEqual(["example"]);
  45. expect(skills[0]?.filePath).toBe(join(root, "skills-link/example/SKILL.md"));
  46. });
  47. it("preserves source info for sourced skills", async () => {
  48. const root = createTempDir();
  49. const env = new NodeExecutionEnv({ cwd: root });
  50. await env.createDir("user/example", { recursive: true });
  51. await env.writeFile(
  52. "user/example/SKILL.md",
  53. "---\nname: example\ndescription: Example skill\n---\nUse this skill.",
  54. );
  55. const { skills, diagnostics } = await loadSourcedSkills(env, [
  56. { path: "user", source: { type: "user" as const } },
  57. ]);
  58. expect(diagnostics).toEqual([]);
  59. expect(skills).toEqual([
  60. {
  61. skill: {
  62. name: "example",
  63. description: "Example skill",
  64. content: "Use this skill.",
  65. filePath: join(root, "user/example/SKILL.md"),
  66. disableModelInvocation: false,
  67. },
  68. source: { type: "user" },
  69. },
  70. ]);
  71. });
  72. it("attaches source info to diagnostics", async () => {
  73. const root = createTempDir();
  74. const env = new NodeExecutionEnv({ cwd: root });
  75. await env.createDir("user/broken", { recursive: true });
  76. await env.writeFile("user/broken/SKILL.md", "---\nname: broken\n---\nMissing description.");
  77. const { skills, diagnostics } = await loadSourcedSkills(env, [
  78. { path: "user", source: { type: "user" as const } },
  79. ]);
  80. expect(skills).toEqual([]);
  81. expect(diagnostics).toEqual([
  82. {
  83. type: "warning",
  84. code: "invalid_metadata",
  85. message: "description is required",
  86. path: join(root, "user/broken/SKILL.md"),
  87. source: { type: "user" },
  88. },
  89. ]);
  90. });
  91. it("loads direct markdown children only from the root directory", async () => {
  92. const root = createTempDir();
  93. const env = new NodeExecutionEnv({ cwd: root });
  94. await env.createDir("skills/nested", { recursive: true });
  95. await env.writeFile("skills/root.md", "---\ndescription: Root skill\n---\nRoot content");
  96. await env.writeFile("skills/nested/ignored.md", "---\ndescription: Ignored\n---\nIgnored content");
  97. const { skills } = await loadSkills(env, "skills");
  98. expect(skills.map((skill) => skill.name)).toEqual(["skills"]);
  99. expect(skills[0]?.content).toBe("Root content");
  100. });
  101. });