skills.test.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. import { homedir } from "os";
  2. import { join, resolve } from "path";
  3. import { describe, expect, it } from "vitest";
  4. import type { ResourceDiagnostic } from "../src/core/diagnostics.ts";
  5. import { formatSkillsForPrompt, loadSkills, loadSkillsFromDir, type Skill } from "../src/core/skills.ts";
  6. import { createSyntheticSourceInfo } from "../src/core/source-info.ts";
  7. const fixturesDir = resolve(__dirname, "fixtures/skills");
  8. const collisionFixturesDir = resolve(__dirname, "fixtures/skills-collision");
  9. function createTestSkill(options: {
  10. name: string;
  11. description: string;
  12. filePath: string;
  13. baseDir: string;
  14. disableModelInvocation?: boolean;
  15. source?: string;
  16. }): Skill {
  17. return {
  18. name: options.name,
  19. description: options.description,
  20. filePath: options.filePath,
  21. baseDir: options.baseDir,
  22. sourceInfo: createSyntheticSourceInfo(options.filePath, { source: options.source ?? "test" }),
  23. disableModelInvocation: options.disableModelInvocation ?? false,
  24. };
  25. }
  26. describe("skills", () => {
  27. describe("loadSkillsFromDir", () => {
  28. it("should load a valid skill", () => {
  29. const { skills, diagnostics } = loadSkillsFromDir({
  30. dir: join(fixturesDir, "valid-skill"),
  31. source: "test",
  32. });
  33. expect(skills).toHaveLength(1);
  34. expect(skills[0].name).toBe("valid-skill");
  35. expect(skills[0].description).toBe("A valid skill for testing purposes.");
  36. expect(skills[0].sourceInfo.source).toBe("test");
  37. expect(diagnostics).toHaveLength(0);
  38. });
  39. it("should allow names that don't match parent directory", () => {
  40. const { skills, diagnostics } = loadSkillsFromDir({
  41. dir: join(fixturesDir, "name-mismatch"),
  42. source: "test",
  43. });
  44. expect(skills).toHaveLength(1);
  45. expect(skills[0].name).toBe("different-name");
  46. expect(
  47. diagnostics.some((d: ResourceDiagnostic) => d.message.includes("does not match parent directory")),
  48. ).toBe(false);
  49. });
  50. it("should warn when name contains invalid characters", () => {
  51. const { skills, diagnostics } = loadSkillsFromDir({
  52. dir: join(fixturesDir, "invalid-name-chars"),
  53. source: "test",
  54. });
  55. expect(skills).toHaveLength(1);
  56. expect(diagnostics.some((d: ResourceDiagnostic) => d.message.includes("invalid characters"))).toBe(true);
  57. });
  58. it("should warn when name exceeds 64 characters", () => {
  59. const { skills, diagnostics } = loadSkillsFromDir({
  60. dir: join(fixturesDir, "long-name"),
  61. source: "test",
  62. });
  63. expect(skills).toHaveLength(1);
  64. expect(diagnostics.some((d: ResourceDiagnostic) => d.message.includes("exceeds 64 characters"))).toBe(true);
  65. });
  66. it("should warn and skip skill when description is missing", () => {
  67. const { skills, diagnostics } = loadSkillsFromDir({
  68. dir: join(fixturesDir, "missing-description"),
  69. source: "test",
  70. });
  71. expect(skills).toHaveLength(0);
  72. expect(diagnostics.some((d: ResourceDiagnostic) => d.message.includes("description is required"))).toBe(true);
  73. });
  74. it("should ignore unknown frontmatter fields", () => {
  75. const { skills, diagnostics } = loadSkillsFromDir({
  76. dir: join(fixturesDir, "unknown-field"),
  77. source: "test",
  78. });
  79. expect(skills).toHaveLength(1);
  80. expect(diagnostics).toHaveLength(0);
  81. });
  82. it("should load nested skills recursively", () => {
  83. const { skills, diagnostics } = loadSkillsFromDir({
  84. dir: join(fixturesDir, "nested"),
  85. source: "test",
  86. });
  87. expect(skills).toHaveLength(1);
  88. expect(skills[0].name).toBe("child-skill");
  89. expect(diagnostics).toHaveLength(0);
  90. });
  91. it("should prefer a directory's root SKILL.md over nested SKILL.md files", () => {
  92. const { skills, diagnostics } = loadSkillsFromDir({
  93. dir: join(fixturesDir, "root-skill-preferred"),
  94. source: "test",
  95. });
  96. expect(skills).toHaveLength(1);
  97. expect(skills[0].name).toBe("root-skill-preferred");
  98. expect(skills[0].description).toBe("Root skill should win.");
  99. expect(diagnostics).toHaveLength(0);
  100. });
  101. it("should skip files without frontmatter", () => {
  102. const { skills, diagnostics } = loadSkillsFromDir({
  103. dir: join(fixturesDir, "no-frontmatter"),
  104. source: "test",
  105. });
  106. // no-frontmatter has no description, so it should be skipped
  107. expect(skills).toHaveLength(0);
  108. expect(diagnostics.some((d: ResourceDiagnostic) => d.message.includes("description is required"))).toBe(true);
  109. });
  110. it("should warn and skip skill when YAML frontmatter is invalid", () => {
  111. const { skills, diagnostics } = loadSkillsFromDir({
  112. dir: join(fixturesDir, "invalid-yaml"),
  113. source: "test",
  114. });
  115. expect(skills).toHaveLength(0);
  116. expect(diagnostics.some((d: ResourceDiagnostic) => d.message.includes("at line"))).toBe(true);
  117. });
  118. it("should preserve multiline descriptions from YAML", () => {
  119. const { skills, diagnostics } = loadSkillsFromDir({
  120. dir: join(fixturesDir, "multiline-description"),
  121. source: "test",
  122. });
  123. expect(skills).toHaveLength(1);
  124. expect(skills[0].description).toContain("\n");
  125. expect(skills[0].description).toContain("This is a multiline description.");
  126. expect(diagnostics).toHaveLength(0);
  127. });
  128. it("should warn when name contains consecutive hyphens", () => {
  129. const { skills, diagnostics } = loadSkillsFromDir({
  130. dir: join(fixturesDir, "consecutive-hyphens"),
  131. source: "test",
  132. });
  133. expect(skills).toHaveLength(1);
  134. expect(diagnostics.some((d: ResourceDiagnostic) => d.message.includes("consecutive hyphens"))).toBe(true);
  135. });
  136. it("should load all skills from fixture directory", () => {
  137. const { skills } = loadSkillsFromDir({
  138. dir: fixturesDir,
  139. source: "test",
  140. });
  141. // Should load all skills that have descriptions (even with warnings)
  142. // valid-skill, name-mismatch, invalid-name-chars, long-name, unknown-field, nested/child-skill, consecutive-hyphens
  143. // NOT: missing-description, no-frontmatter (both missing descriptions)
  144. expect(skills.length).toBeGreaterThanOrEqual(6);
  145. });
  146. it("should return empty for non-existent directory", () => {
  147. const { skills, diagnostics } = loadSkillsFromDir({
  148. dir: "/non/existent/path",
  149. source: "test",
  150. });
  151. expect(skills).toHaveLength(0);
  152. expect(diagnostics).toHaveLength(0);
  153. });
  154. it("should use parent directory name when name not in frontmatter", () => {
  155. // The no-frontmatter fixture has no name in frontmatter, so it should use "no-frontmatter"
  156. // But it also has no description, so it won't load
  157. // Let's test with a valid skill that relies on directory name
  158. const { skills } = loadSkillsFromDir({
  159. dir: join(fixturesDir, "valid-skill"),
  160. source: "test",
  161. });
  162. expect(skills).toHaveLength(1);
  163. expect(skills[0].name).toBe("valid-skill");
  164. });
  165. it("should parse disable-model-invocation frontmatter field", () => {
  166. const { skills, diagnostics } = loadSkillsFromDir({
  167. dir: join(fixturesDir, "disable-model-invocation"),
  168. source: "test",
  169. });
  170. expect(skills).toHaveLength(1);
  171. expect(skills[0].name).toBe("disable-model-invocation");
  172. expect(skills[0].disableModelInvocation).toBe(true);
  173. // Should not warn about unknown field
  174. expect(diagnostics.some((d: ResourceDiagnostic) => d.message.includes("unknown frontmatter field"))).toBe(
  175. false,
  176. );
  177. });
  178. it("should default disableModelInvocation to false when not specified", () => {
  179. const { skills } = loadSkillsFromDir({
  180. dir: join(fixturesDir, "valid-skill"),
  181. source: "test",
  182. });
  183. expect(skills).toHaveLength(1);
  184. expect(skills[0].disableModelInvocation).toBe(false);
  185. });
  186. });
  187. describe("formatSkillsForPrompt", () => {
  188. it("should return empty string for no skills", () => {
  189. const result = formatSkillsForPrompt([]);
  190. expect(result).toBe("");
  191. });
  192. it("should format skills as XML", () => {
  193. const skills: Skill[] = [
  194. createTestSkill({
  195. name: "test-skill",
  196. description: "A test skill.",
  197. filePath: "/path/to/skill/SKILL.md",
  198. baseDir: "/path/to/skill",
  199. }),
  200. ];
  201. const result = formatSkillsForPrompt(skills);
  202. expect(result).toContain("<available_skills>");
  203. expect(result).toContain("</available_skills>");
  204. expect(result).toContain("<skill>");
  205. expect(result).toContain("<name>test-skill</name>");
  206. expect(result).toContain("<description>A test skill.</description>");
  207. expect(result).toContain("<location>/path/to/skill/SKILL.md</location>");
  208. });
  209. it("should include intro text before XML", () => {
  210. const skills: Skill[] = [
  211. createTestSkill({
  212. name: "test-skill",
  213. description: "A test skill.",
  214. filePath: "/path/to/skill/SKILL.md",
  215. baseDir: "/path/to/skill",
  216. }),
  217. ];
  218. const result = formatSkillsForPrompt(skills);
  219. const xmlStart = result.indexOf("<available_skills>");
  220. const introText = result.substring(0, xmlStart);
  221. expect(introText).toContain("The following skills provide specialized instructions");
  222. expect(introText).toContain("Use the read tool to load a skill's file");
  223. });
  224. it("should escape XML special characters", () => {
  225. const skills: Skill[] = [
  226. createTestSkill({
  227. name: "test-skill",
  228. description: 'A skill with <special> & "characters".',
  229. filePath: "/path/to/skill/SKILL.md",
  230. baseDir: "/path/to/skill",
  231. }),
  232. ];
  233. const result = formatSkillsForPrompt(skills);
  234. expect(result).toContain("&lt;special&gt;");
  235. expect(result).toContain("&amp;");
  236. expect(result).toContain("&quot;characters&quot;");
  237. });
  238. it("should format multiple skills", () => {
  239. const skills: Skill[] = [
  240. createTestSkill({
  241. name: "skill-one",
  242. description: "First skill.",
  243. filePath: "/path/one/SKILL.md",
  244. baseDir: "/path/one",
  245. }),
  246. createTestSkill({
  247. name: "skill-two",
  248. description: "Second skill.",
  249. filePath: "/path/two/SKILL.md",
  250. baseDir: "/path/two",
  251. }),
  252. ];
  253. const result = formatSkillsForPrompt(skills);
  254. expect(result).toContain("<name>skill-one</name>");
  255. expect(result).toContain("<name>skill-two</name>");
  256. expect((result.match(/<skill>/g) || []).length).toBe(2);
  257. });
  258. it("should exclude skills with disableModelInvocation from prompt", () => {
  259. const skills: Skill[] = [
  260. createTestSkill({
  261. name: "visible-skill",
  262. description: "A visible skill.",
  263. filePath: "/path/visible/SKILL.md",
  264. baseDir: "/path/visible",
  265. }),
  266. createTestSkill({
  267. name: "hidden-skill",
  268. description: "A hidden skill.",
  269. filePath: "/path/hidden/SKILL.md",
  270. baseDir: "/path/hidden",
  271. disableModelInvocation: true,
  272. }),
  273. ];
  274. const result = formatSkillsForPrompt(skills);
  275. expect(result).toContain("<name>visible-skill</name>");
  276. expect(result).not.toContain("<name>hidden-skill</name>");
  277. expect((result.match(/<skill>/g) || []).length).toBe(1);
  278. });
  279. it("should return empty string when all skills have disableModelInvocation", () => {
  280. const skills: Skill[] = [
  281. createTestSkill({
  282. name: "hidden-skill",
  283. description: "A hidden skill.",
  284. filePath: "/path/hidden/SKILL.md",
  285. baseDir: "/path/hidden",
  286. disableModelInvocation: true,
  287. }),
  288. ];
  289. const result = formatSkillsForPrompt(skills);
  290. expect(result).toBe("");
  291. });
  292. });
  293. describe("loadSkills with options", () => {
  294. const emptyAgentDir = resolve(__dirname, "fixtures/empty-agent");
  295. const emptyCwd = resolve(__dirname, "fixtures/empty-cwd");
  296. it("should load from explicit skillPaths", () => {
  297. const { skills, diagnostics } = loadSkills({
  298. agentDir: emptyAgentDir,
  299. cwd: emptyCwd,
  300. skillPaths: [join(fixturesDir, "valid-skill")],
  301. includeDefaults: true,
  302. });
  303. expect(skills).toHaveLength(1);
  304. expect(skills[0].sourceInfo.scope).toBe("temporary");
  305. expect(diagnostics).toHaveLength(0);
  306. });
  307. it("should warn when skill path does not exist", () => {
  308. const { skills, diagnostics } = loadSkills({
  309. agentDir: emptyAgentDir,
  310. cwd: emptyCwd,
  311. skillPaths: ["/non/existent/path"],
  312. includeDefaults: true,
  313. });
  314. expect(skills).toHaveLength(0);
  315. expect(diagnostics.some((d: ResourceDiagnostic) => d.message.includes("does not exist"))).toBe(true);
  316. });
  317. it("should expand ~ in skillPaths", () => {
  318. const homeSkillsDir = join(homedir(), ".pi/agent/skills");
  319. const { skills: withTilde } = loadSkills({
  320. agentDir: emptyAgentDir,
  321. cwd: emptyCwd,
  322. skillPaths: ["~/.pi/agent/skills"],
  323. includeDefaults: true,
  324. });
  325. const { skills: withoutTilde } = loadSkills({
  326. agentDir: emptyAgentDir,
  327. cwd: emptyCwd,
  328. skillPaths: [homeSkillsDir],
  329. includeDefaults: true,
  330. });
  331. expect(withTilde.length).toBe(withoutTilde.length);
  332. });
  333. });
  334. describe("collision handling", () => {
  335. it("should detect name collisions and keep first skill", () => {
  336. // Load from first directory
  337. const first = loadSkillsFromDir({
  338. dir: join(collisionFixturesDir, "first"),
  339. source: "first",
  340. });
  341. const second = loadSkillsFromDir({
  342. dir: join(collisionFixturesDir, "second"),
  343. source: "second",
  344. });
  345. // Simulate the collision behavior from loadSkills()
  346. const skillMap = new Map<string, Skill>();
  347. const collisionWarnings: Array<{ skillPath: string; message: string }> = [];
  348. for (const skill of first.skills) {
  349. skillMap.set(skill.name, skill);
  350. }
  351. for (const skill of second.skills) {
  352. const existing = skillMap.get(skill.name);
  353. if (existing) {
  354. collisionWarnings.push({
  355. skillPath: skill.filePath,
  356. message: `name collision: "${skill.name}" already loaded from ${existing.filePath}`,
  357. });
  358. } else {
  359. skillMap.set(skill.name, skill);
  360. }
  361. }
  362. expect(skillMap.size).toBe(1);
  363. expect(skillMap.get("calendar")?.sourceInfo.source).toBe("first");
  364. expect(collisionWarnings).toHaveLength(1);
  365. expect(collisionWarnings[0].message).toContain("name collision");
  366. });
  367. });
  368. });