Skip to content

Commit 857b8a4

Browse files
authored
test: add test for new skill improvements (#11969)
1 parent 3975329 commit 857b8a4

1 file changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { describe, expect, test } from "bun:test"
2+
import path from "path"
3+
import { pathToFileURL } from "url"
4+
import type { PermissionNext } from "../../src/permission/next"
5+
import type { Tool } from "../../src/tool/tool"
6+
import { Instance } from "../../src/project/instance"
7+
import { SkillTool } from "../../src/tool/skill"
8+
import { tmpdir } from "../fixture/fixture"
9+
10+
const baseCtx: Omit<Tool.Context, "ask"> = {
11+
sessionID: "test",
12+
messageID: "",
13+
callID: "",
14+
agent: "build",
15+
abort: AbortSignal.any([]),
16+
messages: [],
17+
metadata: () => {},
18+
}
19+
20+
describe("tool.skill", () => {
21+
test("description lists skill location URL", async () => {
22+
await using tmp = await tmpdir({
23+
git: true,
24+
init: async (dir) => {
25+
const skillDir = path.join(dir, ".opencode", "skill", "tool-skill")
26+
await Bun.write(
27+
path.join(skillDir, "SKILL.md"),
28+
`---
29+
name: tool-skill
30+
description: Skill for tool tests.
31+
---
32+
33+
# Tool Skill
34+
`,
35+
)
36+
},
37+
})
38+
39+
const home = process.env.OPENCODE_TEST_HOME
40+
process.env.OPENCODE_TEST_HOME = tmp.path
41+
42+
try {
43+
await Instance.provide({
44+
directory: tmp.path,
45+
fn: async () => {
46+
const tool = await SkillTool.init()
47+
const skillPath = path.join(tmp.path, ".opencode", "skill", "tool-skill", "SKILL.md")
48+
expect(tool.description).toContain(`<location>${pathToFileURL(skillPath).href}</location>`)
49+
},
50+
})
51+
} finally {
52+
process.env.OPENCODE_TEST_HOME = home
53+
}
54+
})
55+
56+
test("execute returns skill content block with files", async () => {
57+
await using tmp = await tmpdir({
58+
git: true,
59+
init: async (dir) => {
60+
const skillDir = path.join(dir, ".opencode", "skill", "tool-skill")
61+
await Bun.write(
62+
path.join(skillDir, "SKILL.md"),
63+
`---
64+
name: tool-skill
65+
description: Skill for tool tests.
66+
---
67+
68+
# Tool Skill
69+
70+
Use this skill.
71+
`,
72+
)
73+
await Bun.write(path.join(skillDir, "scripts", "demo.txt"), "demo")
74+
},
75+
})
76+
77+
const home = process.env.OPENCODE_TEST_HOME
78+
process.env.OPENCODE_TEST_HOME = tmp.path
79+
80+
try {
81+
await Instance.provide({
82+
directory: tmp.path,
83+
fn: async () => {
84+
const tool = await SkillTool.init()
85+
const requests: Array<Omit<PermissionNext.Request, "id" | "sessionID" | "tool">> = []
86+
const ctx: Tool.Context = {
87+
...baseCtx,
88+
ask: async (req) => {
89+
requests.push(req)
90+
},
91+
}
92+
93+
const result = await tool.execute({ name: "tool-skill" }, ctx)
94+
const dir = path.join(tmp.path, ".opencode", "skill", "tool-skill")
95+
const file = path.resolve(dir, "scripts", "demo.txt")
96+
97+
expect(requests.length).toBe(1)
98+
expect(requests[0].permission).toBe("skill")
99+
expect(requests[0].patterns).toContain("tool-skill")
100+
expect(requests[0].always).toContain("tool-skill")
101+
102+
expect(result.metadata.dir).toBe(dir)
103+
expect(result.output).toContain(`<skill_content name="tool-skill">`)
104+
expect(result.output).toContain(`Base directory for this skill: ${pathToFileURL(dir).href}`)
105+
expect(result.output).toContain(`<file>${file}</file>`)
106+
},
107+
})
108+
} finally {
109+
process.env.OPENCODE_TEST_HOME = home
110+
}
111+
})
112+
})

0 commit comments

Comments
 (0)