|
| 1 | +import fs from "node:fs"; |
| 2 | +import path from "node:path"; |
| 3 | +import { afterAll, beforeEach, describe, expect, it } from "vitest"; |
| 4 | +import { PROJECTS_ROOT } from "../src/config.ts"; |
| 5 | +import { ensureProjectExists, resolvePaths } from "../src/projects.ts"; |
| 6 | +import { skillLabelForRead } from "../src/agent/skill-label.ts"; |
| 7 | +import { skillFieldFor } from "../src/agent/events.ts"; |
| 8 | + |
| 9 | +function reset(): void { |
| 10 | + fs.rmSync(PROJECTS_ROOT, { recursive: true, force: true }); |
| 11 | + fs.mkdirSync(PROJECTS_ROOT, { recursive: true }); |
| 12 | +} |
| 13 | +function makeSkill(dir: string, dirName: string, fmName: string): string { |
| 14 | + const d = path.join(dir, dirName); |
| 15 | + fs.mkdirSync(d, { recursive: true }); |
| 16 | + const file = path.join(d, "SKILL.md"); |
| 17 | + fs.writeFileSync( |
| 18 | + file, |
| 19 | + `---\nname: ${fmName}\ndescription: test skill\n---\n\nBody.\n`, |
| 20 | + "utf-8", |
| 21 | + ); |
| 22 | + return file; |
| 23 | +} |
| 24 | +beforeEach(reset); |
| 25 | +afterAll(() => fs.rmSync(PROJECTS_ROOT, { recursive: true, force: true })); |
| 26 | + |
| 27 | +describe("skillLabelForRead", () => { |
| 28 | + it("returns the frontmatter name, not the directory name", () => { |
| 29 | + ensureProjectExists("p1"); |
| 30 | + const paths = resolvePaths("p1"); |
| 31 | + makeSkill(paths.skillsDir, "scrnaseq-qc-dir", "single-cell-qc"); |
| 32 | + expect(skillLabelForRead(".pi/skills/scrnaseq-qc-dir/SKILL.md", paths.sandbox)).toBe( |
| 33 | + "single-cell-qc", |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + it("resolves absolute paths too", () => { |
| 38 | + ensureProjectExists("p2"); |
| 39 | + const paths = resolvePaths("p2"); |
| 40 | + const file = makeSkill(paths.skillsDir, "foo", "fancy-foo"); |
| 41 | + expect(skillLabelForRead(file, paths.sandbox)).toBe("fancy-foo"); |
| 42 | + }); |
| 43 | + |
| 44 | + it("falls back to the directory name when the file is gone", () => { |
| 45 | + ensureProjectExists("p3"); |
| 46 | + const paths = resolvePaths("p3"); |
| 47 | + expect(skillLabelForRead(".pi/skills/vanished/SKILL.md", paths.sandbox)).toBe( |
| 48 | + "vanished", |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + it("ignores non-skill reads", () => { |
| 53 | + ensureProjectExists("p4"); |
| 54 | + const paths = resolvePaths("p4"); |
| 55 | + expect(skillLabelForRead("analysis/results.md", paths.sandbox)).toBeNull(); |
| 56 | + expect(skillLabelForRead(".pi/skills/foo/references/api.md", paths.sandbox)).toBeNull(); |
| 57 | + expect(skillLabelForRead(undefined, paths.sandbox)).toBeNull(); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +describe("skillFieldFor", () => { |
| 62 | + it("attaches the skill field only for read calls on skill files", () => { |
| 63 | + ensureProjectExists("p5"); |
| 64 | + const paths = resolvePaths("p5"); |
| 65 | + makeSkill(paths.skillsDir, "bar", "better-bar"); |
| 66 | + expect( |
| 67 | + skillFieldFor("read", { path: ".pi/skills/bar/SKILL.md" }, paths.sandbox), |
| 68 | + ).toEqual({ skill: "better-bar" }); |
| 69 | + expect(skillFieldFor("read", { path: "notes.md" }, paths.sandbox)).toBeUndefined(); |
| 70 | + expect( |
| 71 | + skillFieldFor("bash", { command: "cat .pi/skills/bar/SKILL.md" }, paths.sandbox), |
| 72 | + ).toBeUndefined(); |
| 73 | + }); |
| 74 | +}); |
0 commit comments