This commit is contained in:
Dax Raad 2026-03-10 01:00:28 -04:00
commit 5ea92ea6cb

View file

@ -5,118 +5,48 @@ import { tmpdir } from "../fixture/fixture"
import { Instance } from "../../src/project/instance" import { Instance } from "../../src/project/instance"
import { ToolRegistry } from "../../src/tool/registry" import { ToolRegistry } from "../../src/tool/registry"
describe("tool.registry", () => { test("loads tools with external dependencies without crashing", async () => {
test("loads tools from .opencode/tool (singular)", async () => { await using tmp = await tmpdir({
await using tmp = await tmpdir({ init: async (dir) => {
init: async (dir) => { const opencodeDir = path.join(dir, ".opencode")
const opencodeDir = path.join(dir, ".opencode") await fs.mkdir(opencodeDir, { recursive: true })
await fs.mkdir(opencodeDir, { recursive: true })
const toolDir = path.join(opencodeDir, "tool") const toolsDir = path.join(opencodeDir, "tools")
await fs.mkdir(toolDir, { recursive: true }) await fs.mkdir(toolsDir, { recursive: true })
await Bun.write( await Bun.write(
path.join(toolDir, "hello.ts"), path.join(opencodeDir, "package.json"),
[ JSON.stringify({
"export default {", name: "custom-tools",
" description: 'hello tool',", dependencies: {
" args: {},", "@opencode-ai/plugin": "^0.0.0",
" execute: async () => {", cowsay: "^1.6.0",
" return 'hello world'", },
" },", }),
"}", )
"",
].join("\n"),
)
},
})
await Instance.provide({ await Bun.write(
directory: tmp.path, path.join(toolsDir, "cowsay.ts"),
fn: async () => { [
const ids = await ToolRegistry.ids() "import { say } from 'cowsay'",
expect(ids).toContain("hello") "export default {",
}, " description: 'tool that imports cowsay at top level',",
}) " args: { text: { type: 'string' } },",
" execute: async ({ text }: { text: string }) => {",
" return say({ text })",
" },",
"}",
"",
].join("\n"),
)
},
}) })
test("loads tools from .opencode/tools (plural)", async () => { await Instance.provide({
await using tmp = await tmpdir({ directory: tmp.path,
init: async (dir) => { fn: async () => {
const opencodeDir = path.join(dir, ".opencode") const ids = await ToolRegistry.ids()
await fs.mkdir(opencodeDir, { recursive: true }) expect(ids).toContain("cowsay")
},
const toolsDir = path.join(opencodeDir, "tools")
await fs.mkdir(toolsDir, { recursive: true })
await Bun.write(
path.join(toolsDir, "hello.ts"),
[
"export default {",
" description: 'hello tool',",
" args: {},",
" execute: async () => {",
" return 'hello world'",
" },",
"}",
"",
].join("\n"),
)
},
})
await Instance.provide({
directory: tmp.path,
fn: async () => {
const ids = await ToolRegistry.ids()
expect(ids).toContain("hello")
},
})
})
test.only("loads tools with external dependencies without crashing", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
const opencodeDir = path.join(dir, ".opencode")
await fs.mkdir(opencodeDir, { recursive: true })
const toolsDir = path.join(opencodeDir, "tools")
await fs.mkdir(toolsDir, { recursive: true })
await Bun.write(
path.join(opencodeDir, "package.json"),
JSON.stringify({
name: "custom-tools",
dependencies: {
"@opencode-ai/plugin": "^0.0.0",
cowsay: "^1.6.0",
},
}),
)
await Bun.write(
path.join(toolsDir, "cowsay.ts"),
[
"import { say } from 'cowsay'",
"export default {",
" description: 'tool that imports cowsay at top level',",
" args: { text: { type: 'string' } },",
" execute: async ({ text }: { text: string }) => {",
" return say({ text })",
" },",
"}",
"",
].join("\n"),
)
},
})
await Instance.provide({
directory: tmp.path,
fn: async () => {
const ids = await ToolRegistry.ids()
expect(ids).toContain("cowsay")
},
})
}) })
}) })