Open History to see the last commit
1import * as NodeAssert from "node:assert/strict";
2
3import * as NodeServices from "@effect/platform-node/NodeServices";
4import { it } from "@effect/vitest";
5import * as Effect from "effect/Effect";
6import * as Layer from "effect/Layer";
7import * as Schema from "effect/Schema";
8import { beforeEach } from "vite-plus/test";
9
10import { OpenCodeSettings } from "@t3tools/contracts";
11import { ServerConfig } from "../../config.ts";
12import {
13 OpenCodeRuntime,
14 OpenCodeRuntimeError,
15 type OpenCodeRuntimeShape,
16} from "../opencodeRuntime.ts";
17import { checkOpenCodeProviderStatus } from "./OpenCodeProvider.ts";
18import type { OpenCodeInventory } from "../opencodeRuntime.ts";
19const decodeOpenCodeSettings = Schema.decodeSync(OpenCodeSettings);
20
21const DEFAULT_VERSION_STDOUT = "opencode 1.14.19\n";
22
23/**
24 * The legacy `OpenCodeProviderLive` Layer + `OpenCodeProvider` service tag
25 * are deleted. The snapshot-producing logic they wrapped now lives in the
26 * standalone `checkOpenCodeProviderStatus(settings, cwd)` Effect, which
27 * drivers call directly when building their per-instance snapshot
28 * `ServerProviderShape`. Tests mirror that shape: build a settings payload,
29 * invoke the check, assert on the returned snapshot.
30 */
31
32const runtimeMock = {
33 state: {
34 runVersionError: null as Error | null,
35 versionStdout: DEFAULT_VERSION_STDOUT,
36 inventoryError: null as Error | null,
37 inventoryCwd: null as string | null,
38 closeCalls: 0,
39 inventory: {
40 providerList: { connected: [] as string[], all: [] as unknown[], default: {} },
41 agents: [] as unknown[],
42 skills: [] as unknown[],
43 } as unknown,
44 },
45 reset() {
46 this.state.runVersionError = null;
47 this.state.versionStdout = DEFAULT_VERSION_STDOUT;
48 this.state.inventoryError = null;
49 this.state.inventoryCwd = null;
50 this.state.closeCalls = 0;
51 this.state.inventory = {
52 providerList: { connected: [], all: [] as unknown[], default: {} },
53 agents: [] as unknown[],
54 skills: [] as unknown[],
55 };
56 },
57};
58
59const OpenCodeRuntimeTestDouble: OpenCodeRuntimeShape = {
60 startOpenCodeServerProcess: () =>
61 Effect.succeed({
62 url: "http://127.0.0.1:4301",
63 exitCode: Effect.never,
64 }),
65 connectToOpenCodeServer: ({ serverUrl }) =>
66 Effect.gen(function* () {
67 if (!serverUrl) {
68 yield* Effect.addFinalizer(() =>
69 Effect.sync(() => {
70 runtimeMock.state.closeCalls += 1;
71 }),
72 );
73 }
74 return {
75 url: serverUrl ?? "http://127.0.0.1:4301",
76 exitCode: null,
77 external: Boolean(serverUrl),
78 };
79 }),
80 runOpenCodeCommand: () =>
81 runtimeMock.state.runVersionError
82 ? Effect.fail(
83 new OpenCodeRuntimeError({
84 operation: "runOpenCodeCommand",
85 detail: runtimeMock.state.runVersionError.message,
86 cause: runtimeMock.state.runVersionError,
87 }),
88 )
89 : Effect.succeed({ stdout: runtimeMock.state.versionStdout, stderr: "", code: 0 }),
90 createOpenCodeSdkClient: () =>
91 ({}) as unknown as ReturnType<OpenCodeRuntimeShape["createOpenCodeSdkClient"]>,
92 loadOpenCodeInventory: () =>
93 runtimeMock.state.inventoryError
94 ? Effect.fail(
95 new OpenCodeRuntimeError({
96 operation: "loadOpenCodeInventory",
97 detail: runtimeMock.state.inventoryError.message,
98 cause: runtimeMock.state.inventoryError,
99 }),
100 )
101 : Effect.succeed(runtimeMock.state.inventory as OpenCodeInventory),
102 loadInventoryFromCli: ({ cwd }) => {
103 runtimeMock.state.inventoryCwd = cwd;
104 return runtimeMock.state.inventoryError
105 ? Effect.fail(
106 new OpenCodeRuntimeError({
107 operation: "loadInventoryFromCli",
108 detail: runtimeMock.state.inventoryError.message,
109 cause: runtimeMock.state.inventoryError,
110 }),
111 )
112 : Effect.succeed(runtimeMock.state.inventory as OpenCodeInventory);
113 },
114};
115
116beforeEach(() => {
117 runtimeMock.reset();
118});
119
120const testLayer = Layer.succeed(OpenCodeRuntime, OpenCodeRuntimeTestDouble).pipe(
121 Layer.provideMerge(ServerConfig.layerTest(process.cwd(), process.cwd())),
122 Layer.provideMerge(NodeServices.layer),
123);
124
125const makeOpenCodeSettings = (overrides?: Partial<OpenCodeSettings>): OpenCodeSettings =>
126 decodeOpenCodeSettings({
127 enabled: true,
128 binaryPath: "opencode",
129 serverUrl: "",
130 serverPassword: "",
131 customModels: [],
132 ...overrides,
133 });
134
135it.layer(testLayer)("checkOpenCodeProviderStatus", (it) => {
136 it.effect("shows a codex-style missing binary message", () =>
137 Effect.gen(function* () {
138 runtimeMock.state.runVersionError = new Error("spawn opencode ENOENT");
139 const snapshot = yield* checkOpenCodeProviderStatus(makeOpenCodeSettings(), process.cwd());
140
141 NodeAssert.equal(snapshot.status, "error");
142 NodeAssert.equal(snapshot.installed, false);
143 NodeAssert.equal(
144 snapshot.message,
145 "OpenCode CLI (`opencode`) is not installed or not on PATH.",
146 );
147 }),
148 );
149
150 it.effect("hides generic Effect.tryPromise text for local CLI probe failures", () =>
151 Effect.gen(function* () {
152 runtimeMock.state.runVersionError = new Error("An error occurred in Effect.tryPromise");
153 const snapshot = yield* checkOpenCodeProviderStatus(makeOpenCodeSettings(), process.cwd());
154
155 NodeAssert.equal(snapshot.status, "error");
156 NodeAssert.equal(snapshot.installed, true);
157 NodeAssert.equal(snapshot.message, "Failed to execute OpenCode CLI health check.");
158 }),
159 );
160
161 it.effect("emits OpenCode variant defaults so trait picker can resolve a visible selection", () =>
162 Effect.gen(function* () {
163 runtimeMock.state.inventory = {
164 providerList: {
165 connected: ["openai"],
166 all: [
167 {
168 id: "openai",
169 name: "OpenAI",
170 models: {
171 "gpt-5.4": {
172 id: "gpt-5.4",
173 name: "GPT-5.4",
174 variants: {
175 none: {},
176 low: {},
177 medium: {},
178 high: {},
179 xhigh: {},
180 },
181 },
182 },
183 },
184 ],
185 default: {},
186 },
187 agents: [
188 { name: "build", hidden: false, mode: "primary" },
189 { name: "plan", hidden: false, mode: "primary" },
190 ],
191 };
192
193 const snapshot = yield* checkOpenCodeProviderStatus(makeOpenCodeSettings(), process.cwd());
194 const model = snapshot.models.find((entry) => entry.slug === "openai/gpt-5.4");
195
196 NodeAssert.ok(model);
197 const variantDescriptor = model.capabilities?.optionDescriptors?.find(
198 (descriptor) => descriptor.id === "variant" && descriptor.type === "select",
199 );
200 NodeAssert.ok(variantDescriptor && variantDescriptor.type === "select");
201 NodeAssert.equal(
202 variantDescriptor.options.find((option) => option.isDefault === true)?.id,
203 "medium",
204 );
205 const agentDescriptor = model.capabilities?.optionDescriptors?.find(
206 (descriptor) => descriptor.id === "agent" && descriptor.type === "select",
207 );
208 NodeAssert.ok(agentDescriptor && agentDescriptor.type === "select");
209 NodeAssert.equal(
210 agentDescriptor.options.find((option) => option.isDefault === true)?.id,
211 "build",
212 );
213 }),
214 );
215
216 it.effect("includes OpenCode skills in the provider snapshot", () =>
217 Effect.gen(function* () {
218 runtimeMock.state.inventory = {
219 providerList: {
220 connected: ["openai"],
221 all: [
222 {
223 id: "openai",
224 name: "OpenAI",
225 models: {
226 "gpt-5.4": {
227 id: "gpt-5.4",
228 name: "GPT-5.4",
229 variants: {},
230 },
231 },
232 },
233 ],
234 default: {},
235 },
236 agents: [],
237 skills: [
238 {
239 name: "openclaw-review",
240 description: "Review OpenClaw workflow changes.",
241 location: "/Users/test/.agents/skills/openclaw-review/SKILL.md",
242 },
243 {
244 name: "openclaw-triage",
245 description: "Triage OpenClaw routing issues.",
246 location: "/Users/test/.agents/skills/openclaw-triage/SKILL.md",
247 },
248 {
249 name: "missing-location",
250 description: "This incomplete SDK row should be skipped.",
251 location: "",
252 },
253 ],
254 };
255
256 const snapshot = yield* checkOpenCodeProviderStatus(makeOpenCodeSettings(), process.cwd());
257
258 NodeAssert.deepEqual(
259 snapshot.skills.map((skill) => ({
260 name: skill.name,
261 path: skill.path,
262 enabled: skill.enabled,
263 shortDescription: skill.shortDescription,
264 })),
265 [
266 {
267 name: "openclaw-review",
268 path: "/Users/test/.agents/skills/openclaw-review/SKILL.md",
269 enabled: true,
270 shortDescription: "Review OpenClaw workflow changes.",
271 },
272 {
273 name: "openclaw-triage",
274 path: "/Users/test/.agents/skills/openclaw-triage/SKILL.md",
275 enabled: true,
276 shortDescription: "Triage OpenClaw routing issues.",
277 },
278 ],
279 );
280 }),
281 );
282
283 it.effect("does not spawn a local server for health check (uses CLI instead)", () =>
284 Effect.gen(function* () {
285 yield* checkOpenCodeProviderStatus(makeOpenCodeSettings(), process.cwd());
286
287 NodeAssert.equal(runtimeMock.state.closeCalls, 0);
288 NodeAssert.equal(runtimeMock.state.inventoryCwd, process.cwd());
289 }),
290 );
291
292 it.effect("reports local model inventory failures without treating them as empty", () =>
293 Effect.gen(function* () {
294 runtimeMock.state.inventoryError = new Error("opencode models failed");
295 const snapshot = yield* checkOpenCodeProviderStatus(makeOpenCodeSettings(), process.cwd());
296
297 NodeAssert.equal(snapshot.status, "error");
298 NodeAssert.equal(snapshot.installed, true);
299 NodeAssert.equal(snapshot.models.length, 0);
300 NodeAssert.equal(
301 snapshot.message,
302 "Failed to execute OpenCode CLI health check: opencode models failed",
303 );
304 }),
305 );
306});
307
308it.layer(testLayer)("checkOpenCodeProviderStatus with configured server URL", (it) => {
309 it.effect("surfaces a friendly auth error for configured servers", () =>
310 Effect.gen(function* () {
311 runtimeMock.state.inventoryError = new Error("401 Unauthorized");
312 const snapshot = yield* checkOpenCodeProviderStatus(
313 makeOpenCodeSettings({
314 serverUrl: "http://127.0.0.1:9999",
315 serverPassword: "secret-password",
316 }),
317 process.cwd(),
318 );
319
320 NodeAssert.equal(snapshot.status, "error");
321 NodeAssert.equal(snapshot.installed, true);
322 NodeAssert.equal(
323 snapshot.message,
324 "OpenCode server rejected authentication. Check the server URL and password.",
325 );
326 }),
327 );
328
329 it.effect("surfaces a friendly connection error for configured servers", () =>
330 Effect.gen(function* () {
331 runtimeMock.state.inventoryError = new Error(
332 "fetch failed: connect ECONNREFUSED 127.0.0.1:9999",
333 );
334 const snapshot = yield* checkOpenCodeProviderStatus(
335 makeOpenCodeSettings({
336 serverUrl: "http://127.0.0.1:9999",
337 serverPassword: "secret-password",
338 }),
339 process.cwd(),
340 );
341
342 NodeAssert.equal(snapshot.status, "error");
343 NodeAssert.equal(snapshot.installed, true);
344 NodeAssert.equal(
345 snapshot.message,
346 "Couldn't reach the configured OpenCode server at http://127.0.0.1:9999. Check that the server is running and the URL is correct.",
347 );
348 }),
349 );
350});