summaryrefslogtreecommitdiffhomepage
path: root/packages/skills/src/skills.test.ts
blob: 57e5d637249a75743942f1e435b12e7e1ce7209a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { createLogger, type ToolExecuteContext } from "@dispatch/kernel";
import type { ToolAssembly } from "@dispatch/session-orchestrator";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { createLoadSkillTool, scanSkillsDir } from "./load-skill.js";
import { makeSkillsToolFilter } from "./tools-filter.js";

function stubCtx(overrides?: Partial<ToolExecuteContext>): ToolExecuteContext {
  return {
    toolCallId: "test-call-1",
    onOutput: () => {},
    signal: AbortSignal.timeout(5000),
    log: createLogger(
      { extensionId: "test" },
      { emit: () => {} },
      { now: () => 0, newId: () => "id" },
    ),
    ...overrides,
  };
}

let homeDir: string;
let workdir: string;

beforeEach(async () => {
  homeDir = await mkdtemp(join(tmpdir(), "skills-home-test-"));
  workdir = await mkdtemp(join(tmpdir(), "skills-workdir-test-"));
});

afterEach(async () => {
  await rm(homeDir, { recursive: true, force: true });
  await rm(workdir, { recursive: true, force: true });
});

describe("load_skill tool", () => {
  it("loads a skill body (strips first two lines) from cwd .skills", async () => {
    const skillsDir = join(workdir, ".skills");
    await mkdir(skillsDir);
    await writeFile(
      join(skillsDir, "web-search.md"),
      "Use for web searches\n---\n# Web Search Skill\nDo a web search.",
      "utf8",
    );

    const tool = createLoadSkillTool({ homeDir, workdir });
    const result = await tool.execute({ name: "web-search" }, stubCtx());

    expect(result.isError).toBeUndefined();
    expect(result.content).toBe("# Web Search Skill\nDo a web search.");
  });

  it("falls back to home .skills when not in cwd", async () => {
    const homeSkillsDir = join(homeDir, ".skills");
    await mkdir(homeSkillsDir);
    await writeFile(
      join(homeSkillsDir, "global-skill.md"),
      "Global skill summary\n---\nGlobal body content",
      "utf8",
    );

    const tool = createLoadSkillTool({ homeDir, workdir });
    const result = await tool.execute({ name: "global-skill" }, stubCtx());

    expect(result.isError).toBeUndefined();
    expect(result.content).toBe("Global body content");
  });

  it("returns isError for an unknown skill", async () => {
    const tool = createLoadSkillTool({ homeDir, workdir });
    const result = await tool.execute({ name: "nonexistent" }, stubCtx());

    expect(result.isError).toBe(true);
    expect(result.content).toContain("unknown skill");
  });

  it("rejects a name containing a path separator", async () => {
    const tool = createLoadSkillTool({ homeDir, workdir });
    const result = await tool.execute({ name: "../escape" }, stubCtx());

    expect(result.isError).toBe(true);
    expect(result.content).toContain("Invalid skill name");
  });

  it("rejects a name containing ..", async () => {
    const tool = createLoadSkillTool({ homeDir, workdir });
    const result = await tool.execute({ name: "skill..evil" }, stubCtx());

    expect(result.isError).toBe(true);
    expect(result.content).toContain("Invalid skill name");
  });

  it("rejects a name containing backslash", async () => {
    const tool = createLoadSkillTool({ homeDir, workdir });
    const result = await tool.execute({ name: "path\\name" }, stubCtx());

    expect(result.isError).toBe(true);
    expect(result.content).toContain("Invalid skill name");
  });

  it("returns the whole file when malformed (no --- on line 2)", async () => {
    const skillsDir = join(workdir, ".skills");
    await mkdir(skillsDir);
    await writeFile(
      join(skillsDir, "malformed.md"),
      "Just some content\nNo separator here\nMore content",
      "utf8",
    );

    const tool = createLoadSkillTool({ homeDir, workdir });
    const result = await tool.execute({ name: "malformed" }, stubCtx());

    expect(result.isError).toBeUndefined();
    expect(result.content).toBe("Just some content\nNo separator here\nMore content");
  });

  it("cwd skill shadows home skill of the same name", async () => {
    const homeSkillsDir = join(homeDir, ".skills");
    await mkdir(homeSkillsDir);
    await writeFile(join(homeSkillsDir, "shared.md"), "Home summary\n---\nHome body", "utf8");

    const cwdSkillsDir = join(workdir, ".skills");
    await mkdir(cwdSkillsDir);
    await writeFile(join(cwdSkillsDir, "shared.md"), "Cwd summary\n---\nCwd body", "utf8");

    const tool = createLoadSkillTool({ homeDir, workdir });
    const result = await tool.execute({ name: "shared" }, stubCtx());

    expect(result.isError).toBeUndefined();
    expect(result.content).toBe("Cwd body");
  });

  it("reads from ctx.cwd when set", async () => {
    const ctxDir = await mkdtemp(join(tmpdir(), "skills-ctx-test-"));
    try {
      const ctxSkillsDir = join(ctxDir, ".skills");
      await mkdir(ctxSkillsDir);
      await writeFile(join(ctxSkillsDir, "ctx-skill.md"), "Ctx summary\n---\nFrom ctx cwd", "utf8");

      const tool = createLoadSkillTool({ homeDir, workdir });
      const result = await tool.execute({ name: "ctx-skill" }, stubCtx({ cwd: ctxDir }));

      expect(result.isError).toBeUndefined();
      expect(result.content).toBe("From ctx cwd");
    } finally {
      await rm(ctxDir, { recursive: true, force: true });
    }
  });

  it("concurrencySafe is true", () => {
    const tool = createLoadSkillTool({ homeDir, workdir });
    expect(tool.concurrencySafe).toBe(true);
  });
});

describe("scanSkillsDir", () => {
  it("scans .md files and parses metadata", async () => {
    const skillsDir = join(workdir, ".skills");
    await mkdir(skillsDir);
    await writeFile(join(skillsDir, "valid.md"), "Summary\n---\nBody", "utf8");
    await writeFile(join(skillsDir, "malformed.md"), "No separator\nBody", "utf8");
    await writeFile(join(skillsDir, "other.txt"), "Not a skill", "utf8");

    const result = await scanSkillsDir(skillsDir);

    expect(result).toHaveLength(2);
    const valid = result.find((e) => e.name === "valid");
    expect(valid?.summary).toBe("Summary");
    const malformed = result.find((e) => e.name === "malformed");
    expect(malformed?.summary).toBeUndefined();
  });

  it("returns empty array for nonexistent directory", async () => {
    const result = await scanSkillsDir(join(workdir, "nonexistent"));
    expect(result).toEqual([]);
  });
});

describe("tools filter", () => {
  it("rewrites load_skill description with the current catalog (cwd-aware)", async () => {
    const homeSkillsDir = join(homeDir, ".skills");
    await mkdir(homeSkillsDir);
    await writeFile(join(homeSkillsDir, "global.md"), "Global summary\n---\nBody", "utf8");

    const cwdSkillsDir = join(workdir, ".skills");
    await mkdir(cwdSkillsDir);
    await writeFile(join(cwdSkillsDir, "local.md"), "Local summary\n---\nBody", "utf8");

    const filter = makeSkillsToolFilter({ homeDir, workdir });

    const tool = createLoadSkillTool({ homeDir, workdir });
    const asm: ToolAssembly = {
      tools: [tool],
      cwd: workdir,
      conversationId: "test-conv",
    };

    const result = await filter(asm);
    const loadSkill = result.tools.find(
      (t: import("@dispatch/kernel").ToolContract) => t.name === "load_skill",
    );
    expect(loadSkill).toBeDefined();
    expect(loadSkill?.description).toContain("global");
    expect(loadSkill?.description).toContain("Global summary");
    expect(loadSkill?.description).toContain("local");
    expect(loadSkill?.description).toContain("Local summary");
  });

  it("updates the name parameter enum with available skills", async () => {
    const cwdSkillsDir = join(workdir, ".skills");
    await mkdir(cwdSkillsDir);
    await writeFile(join(cwdSkillsDir, "alpha.md"), "Alpha\n---\nBody", "utf8");
    await writeFile(join(cwdSkillsDir, "beta.md"), "Beta\n---\nBody", "utf8");

    const filter = makeSkillsToolFilter({ homeDir, workdir });

    const tool = createLoadSkillTool({ homeDir, workdir });
    const asm: ToolAssembly = {
      tools: [tool],
      cwd: workdir,
      conversationId: "test-conv",
    };

    const result = await filter(asm);
    const loadSkill = result.tools.find(
      (t: import("@dispatch/kernel").ToolContract) => t.name === "load_skill",
    );
    expect(loadSkill?.parameters.properties?.name?.enum).toEqual(["alpha", "beta"]);
  });

  it("handles empty skill directories gracefully", async () => {
    const filter = makeSkillsToolFilter({ homeDir, workdir });

    const tool = createLoadSkillTool({ homeDir, workdir });
    const asm: ToolAssembly = {
      tools: [tool],
      cwd: workdir,
      conversationId: "test-conv",
    };

    const result = await filter(asm);
    const loadSkill = result.tools.find(
      (t: import("@dispatch/kernel").ToolContract) => t.name === "load_skill",
    );
    expect(loadSkill?.description).toContain("No skills are currently available");
  });

  it("passes through non-load_skill tools unchanged", async () => {
    const filter = makeSkillsToolFilter({ homeDir, workdir });

    const otherTool = {
      name: "other_tool",
      description: "Some other tool",
      parameters: { type: "object" as const },
      execute: async () => ({ content: "ok" }),
    };
    const asm: ToolAssembly = {
      tools: [otherTool],
      cwd: workdir,
      conversationId: "test-conv",
    };

    const result = await filter(asm);
    expect(result.tools[0]?.name).toBe("other_tool");
    expect(result.tools[0]?.description).toBe("Some other tool");
  });
});