Skip to content

Commit baa6976

Browse files
authored
Use instance test helper in tool registry tests (#25461)
1 parent 73406e7 commit baa6976

1 file changed

Lines changed: 122 additions & 126 deletions

File tree

packages/opencode/test/tool/registry.test.ts

Lines changed: 122 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import { afterEach, describe, expect } from "bun:test"
22
import path from "path"
33
import fs from "fs/promises"
44
import { Effect, Layer } from "effect"
5-
import { Instance } from "../../src/project/instance"
65
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
76
import { ToolRegistry } from "@/tool/registry"
8-
import { disposeAllInstances, provideTmpdirInstance } from "../fixture/fixture"
7+
import { disposeAllInstances, TestInstance } from "../fixture/fixture"
98
import { testEffect } from "../lib/effect"
109

1110
const node = CrossSpawnSpawner.defaultLayer
@@ -17,136 +16,133 @@ afterEach(async () => {
1716
})
1817

1918
describe("tool.registry", () => {
20-
it.live("loads tools from .opencode/tool (singular)", () =>
21-
provideTmpdirInstance((dir) =>
22-
Effect.gen(function* () {
23-
const opencode = path.join(dir, ".opencode")
24-
const tool = path.join(opencode, "tool")
25-
yield* Effect.promise(() => fs.mkdir(tool, { recursive: true }))
26-
yield* Effect.promise(() =>
27-
Bun.write(
28-
path.join(tool, "hello.ts"),
29-
[
30-
"export default {",
31-
" description: 'hello tool',",
32-
" args: {},",
33-
" execute: async () => {",
34-
" return 'hello world'",
35-
" },",
36-
"}",
37-
"",
38-
].join("\n"),
39-
),
40-
)
41-
const registry = yield* ToolRegistry.Service
42-
const ids = yield* registry.ids()
43-
expect(ids).toContain("hello")
44-
}),
45-
),
19+
it.instance("loads tools from .opencode/tool (singular)", () =>
20+
Effect.gen(function* () {
21+
const test = yield* TestInstance
22+
const opencode = path.join(test.directory, ".opencode")
23+
const tool = path.join(opencode, "tool")
24+
yield* Effect.promise(() => fs.mkdir(tool, { recursive: true }))
25+
yield* Effect.promise(() =>
26+
Bun.write(
27+
path.join(tool, "hello.ts"),
28+
[
29+
"export default {",
30+
" description: 'hello tool',",
31+
" args: {},",
32+
" execute: async () => {",
33+
" return 'hello world'",
34+
" },",
35+
"}",
36+
"",
37+
].join("\n"),
38+
),
39+
)
40+
const registry = yield* ToolRegistry.Service
41+
const ids = yield* registry.ids()
42+
expect(ids).toContain("hello")
43+
}),
4644
)
4745

48-
it.live("loads tools from .opencode/tools (plural)", () =>
49-
provideTmpdirInstance((dir) =>
50-
Effect.gen(function* () {
51-
const opencode = path.join(dir, ".opencode")
52-
const tools = path.join(opencode, "tools")
53-
yield* Effect.promise(() => fs.mkdir(tools, { recursive: true }))
54-
yield* Effect.promise(() =>
55-
Bun.write(
56-
path.join(tools, "hello.ts"),
57-
[
58-
"export default {",
59-
" description: 'hello tool',",
60-
" args: {},",
61-
" execute: async () => {",
62-
" return 'hello world'",
63-
" },",
64-
"}",
65-
"",
66-
].join("\n"),
67-
),
68-
)
69-
const registry = yield* ToolRegistry.Service
70-
const ids = yield* registry.ids()
71-
expect(ids).toContain("hello")
72-
}),
73-
),
46+
it.instance("loads tools from .opencode/tools (plural)", () =>
47+
Effect.gen(function* () {
48+
const test = yield* TestInstance
49+
const opencode = path.join(test.directory, ".opencode")
50+
const tools = path.join(opencode, "tools")
51+
yield* Effect.promise(() => fs.mkdir(tools, { recursive: true }))
52+
yield* Effect.promise(() =>
53+
Bun.write(
54+
path.join(tools, "hello.ts"),
55+
[
56+
"export default {",
57+
" description: 'hello tool',",
58+
" args: {},",
59+
" execute: async () => {",
60+
" return 'hello world'",
61+
" },",
62+
"}",
63+
"",
64+
].join("\n"),
65+
),
66+
)
67+
const registry = yield* ToolRegistry.Service
68+
const ids = yield* registry.ids()
69+
expect(ids).toContain("hello")
70+
}),
7471
)
7572

76-
it.live("loads tools with external dependencies without crashing", () =>
77-
provideTmpdirInstance((dir) =>
78-
Effect.gen(function* () {
79-
const opencode = path.join(dir, ".opencode")
80-
const tools = path.join(opencode, "tools")
81-
yield* Effect.promise(() => fs.mkdir(tools, { recursive: true }))
82-
yield* Effect.promise(() =>
83-
Bun.write(
84-
path.join(opencode, "package.json"),
85-
JSON.stringify({
86-
name: "custom-tools",
87-
dependencies: {
88-
"@opencode-ai/plugin": "^0.0.0",
89-
cowsay: "^1.6.0",
90-
},
91-
}),
92-
),
93-
)
94-
yield* Effect.promise(() =>
95-
Bun.write(
96-
path.join(opencode, "package-lock.json"),
97-
JSON.stringify({
98-
name: "custom-tools",
99-
lockfileVersion: 3,
100-
packages: {
101-
"": {
102-
dependencies: {
103-
"@opencode-ai/plugin": "^0.0.0",
104-
cowsay: "^1.6.0",
105-
},
73+
it.instance("loads tools with external dependencies without crashing", () =>
74+
Effect.gen(function* () {
75+
const test = yield* TestInstance
76+
const opencode = path.join(test.directory, ".opencode")
77+
const tools = path.join(opencode, "tools")
78+
yield* Effect.promise(() => fs.mkdir(tools, { recursive: true }))
79+
yield* Effect.promise(() =>
80+
Bun.write(
81+
path.join(opencode, "package.json"),
82+
JSON.stringify({
83+
name: "custom-tools",
84+
dependencies: {
85+
"@opencode-ai/plugin": "^0.0.0",
86+
cowsay: "^1.6.0",
87+
},
88+
}),
89+
),
90+
)
91+
yield* Effect.promise(() =>
92+
Bun.write(
93+
path.join(opencode, "package-lock.json"),
94+
JSON.stringify({
95+
name: "custom-tools",
96+
lockfileVersion: 3,
97+
packages: {
98+
"": {
99+
dependencies: {
100+
"@opencode-ai/plugin": "^0.0.0",
101+
cowsay: "^1.6.0",
106102
},
107103
},
108-
}),
109-
),
110-
)
104+
},
105+
}),
106+
),
107+
)
111108

112-
const cowsay = path.join(opencode, "node_modules", "cowsay")
113-
yield* Effect.promise(() => fs.mkdir(cowsay, { recursive: true }))
114-
yield* Effect.promise(() =>
115-
Bun.write(
116-
path.join(cowsay, "package.json"),
117-
JSON.stringify({
118-
name: "cowsay",
119-
type: "module",
120-
exports: "./index.js",
121-
}),
122-
),
123-
)
124-
yield* Effect.promise(() =>
125-
Bun.write(
126-
path.join(cowsay, "index.js"),
127-
["export function say({ text }) {", " return `moo ${text}`", "}", ""].join("\n"),
128-
),
129-
)
130-
yield* Effect.promise(() =>
131-
Bun.write(
132-
path.join(tools, "cowsay.ts"),
133-
[
134-
"import { say } from 'cowsay'",
135-
"export default {",
136-
" description: 'tool that imports cowsay at top level',",
137-
" args: { text: { type: 'string' } },",
138-
" execute: async ({ text }: { text: string }) => {",
139-
" return say({ text })",
140-
" },",
141-
"}",
142-
"",
143-
].join("\n"),
144-
),
145-
)
146-
const registry = yield* ToolRegistry.Service
147-
const ids = yield* registry.ids()
148-
expect(ids).toContain("cowsay")
149-
}),
150-
),
109+
const cowsay = path.join(opencode, "node_modules", "cowsay")
110+
yield* Effect.promise(() => fs.mkdir(cowsay, { recursive: true }))
111+
yield* Effect.promise(() =>
112+
Bun.write(
113+
path.join(cowsay, "package.json"),
114+
JSON.stringify({
115+
name: "cowsay",
116+
type: "module",
117+
exports: "./index.js",
118+
}),
119+
),
120+
)
121+
yield* Effect.promise(() =>
122+
Bun.write(
123+
path.join(cowsay, "index.js"),
124+
["export function say({ text }) {", " return `moo ${text}`", "}", ""].join("\n"),
125+
),
126+
)
127+
yield* Effect.promise(() =>
128+
Bun.write(
129+
path.join(tools, "cowsay.ts"),
130+
[
131+
"import { say } from 'cowsay'",
132+
"export default {",
133+
" description: 'tool that imports cowsay at top level',",
134+
" args: { text: { type: 'string' } },",
135+
" execute: async ({ text }: { text: string }) => {",
136+
" return say({ text })",
137+
" },",
138+
"}",
139+
"",
140+
].join("\n"),
141+
),
142+
)
143+
const registry = yield* ToolRegistry.Service
144+
const ids = yield* registry.ids()
145+
expect(ids).toContain("cowsay")
146+
}),
151147
)
152148
})

0 commit comments

Comments
 (0)