Skip to content

Commit 4538f00

Browse files
committed
test(opencode): handle disabled providers and all loopback hosts
1 parent 442c885 commit 4538f00

2 files changed

Lines changed: 39 additions & 9 deletions

File tree

‎packages/provider-opencode/src/server/driver.test.ts‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ import * as TestProviderHost from "@t3tools/provider-testing/TestProviderHost";
2727

2828
it("only probes a locally configured OpenCode server's installed CLI", () => {
2929
assert.isTrue(isLocalOpenCodeServerUrl("http://127.0.0.1:35768"));
30+
assert.isTrue(isLocalOpenCodeServerUrl("http://127.0.0.2:4096"));
31+
assert.isTrue(isLocalOpenCodeServerUrl("http://127.255.255.254:4096"));
32+
assert.isFalse(isLocalOpenCodeServerUrl("http://127.example.com:4096"));
33+
assert.isFalse(isLocalOpenCodeServerUrl("http://128.0.0.1:4096"));
3034
assert.isTrue(isLocalOpenCodeServerUrl("http://localhost:4096"));
3135
assert.isTrue(isLocalOpenCodeServerUrl("http://[::1]:4096"));
3236
assert.isFalse(isLocalOpenCodeServerUrl("https://remote.example.com:4096"));
@@ -64,12 +68,12 @@ const layer = Layer.mergeAll(
6468
Layer.succeed(OpenCodeRuntime.OpenCodeRuntime, openCode2Runtime),
6569
).pipe(Layer.provideMerge(NodeServices.layer));
6670

67-
const create = (config: Partial<OpenCodeSettings>, http: HttpClient.HttpClient) =>
71+
const create = (config: Partial<OpenCodeSettings>, http: HttpClient.HttpClient, enabled = true) =>
6872
OpenCodeDriver.create({
6973
instanceId: ProviderInstanceId.make("opencode-test"),
7074
displayName: undefined,
7175
environment: [],
72-
enabled: true,
76+
enabled,
7377
config: { ...OpenCodeDriver.defaultConfig(), ...config },
7478
}).pipe(Effect.provideService(HttpClient.HttpClient, http));
7579

@@ -109,6 +113,27 @@ it.layer(layer)("OpenCodeDriver runtime selection", (it) => {
109113
}).pipe(Effect.scoped),
110114
);
111115

116+
it.effect("skips the installed CLI probe when a localhost provider is disabled", () =>
117+
Effect.gen(function* () {
118+
const probed: string[] = [];
119+
const runtime = {
120+
...openCode2Runtime,
121+
runOpenCodeCommand: () =>
122+
Effect.sync(() => {
123+
probed.push("--version");
124+
return { stdout: "opencode v2.1.2\n", stderr: "", code: 0 };
125+
}),
126+
} as OpenCodeRuntime.OpenCodeRuntime["Service"];
127+
const instance = yield* create({ serverUrl: "http://127.0.0.1:4096" }, noHttp, false).pipe(
128+
Effect.provideService(OpenCodeRuntime.OpenCodeRuntime, runtime),
129+
);
130+
131+
const snapshot = yield* instance.snapshot.refresh;
132+
assert.isFalse(snapshot.enabled);
133+
assert.deepStrictEqual(probed, []);
134+
}).pipe(Effect.scoped),
135+
);
136+
112137
it.effect("answers capability reads without waiting on an unreachable server", () =>
113138
Effect.gen(function* () {
114139
const hang = HttpClient.make(() => Effect.never);

‎packages/provider-opencode/src/server/driver.ts‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ const DRIVER_KIND = ProviderDriverKind.make("opencode");
7373
export function isLocalOpenCodeServerUrl(serverUrl: string): boolean {
7474
const url = URL.parse(serverUrl.trim());
7575
if (url?.protocol !== "http:" && url?.protocol !== "https:") return false;
76-
return ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname);
76+
return (
77+
url.hostname === "localhost" ||
78+
url.hostname === "[::1]" ||
79+
/^127\.(?:\d{1,3}\.){2}\d{1,3}$/.test(url.hostname)
80+
);
7781
}
7882

7983
function isOpenCodeNativeCommandPath(commandPath: string): boolean {
@@ -357,12 +361,13 @@ export const OpenCodeDriver: ProviderDriver<
357361
// A configured loopback server reports its running version. Probe the local
358362
// CLI separately so a completed npm update is not mistaken for a failure
359363
// while that independently managed server still runs the previous binary.
360-
installedVersion: isLocalOpenCodeServerUrl(effectiveConfig.serverUrl)
361-
? probeOpenCodeBinary(effectiveConfig.binaryPath, processEnv).pipe(
362-
Effect.map((probed) => probed.version),
363-
Effect.orElseSucceed(() => undefined),
364-
)
365-
: Effect.succeed(undefined),
364+
installedVersion:
365+
effectiveConfig.enabled && isLocalOpenCodeServerUrl(effectiveConfig.serverUrl)
366+
? probeOpenCodeBinary(effectiveConfig.binaryPath, processEnv).pipe(
367+
Effect.map((probed) => probed.version),
368+
Effect.orElseSucceed(() => undefined),
369+
)
370+
: Effect.succeed(undefined),
366371
provider: checkOpenCodeProviderStatus(
367372
effectiveConfig,
368373
host.paths.cwd,

0 commit comments

Comments
 (0)