Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

OpenCodeDriver.test.ts

95 lines4.0 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, it } from "@effect/vitest";
3import { ProviderInstanceId, type OpenCodeSettings } from "@t3tools/contracts";
4import * as Effect from "effect/Effect";
5import * as Fiber from "effect/Fiber";
6import * as Layer from "effect/Layer";
7import * as TestClock from "effect/testing/TestClock";
8import { HttpClient } from "effect/unstable/http";
9​
10import * as BackgroundPolicy from "../../background/BackgroundPolicy.ts";
11import * as ServerConfig from "../../config.ts";
12import * as IdAllocator from "../../orchestration-v2/IdAllocator.ts";
13import * as ServerSettings from "../../serverSettings.ts";
14import * as ProviderEventLoggers from "../Layers/ProviderEventLoggers.ts";
15import * as OpenCodeRuntime from "../opencodeRuntime.ts";
16import { OPENCODE_2_TEXT_GENERATION_UNSUPPORTED, OpenCodeDriver } from "./OpenCodeDriver.ts";
17​
18const serverStarts: Array<string> = [];
19const reachedServer = (operation: string) =>
20 Effect.sync(() => serverStarts.push(operation)).pipe(
21 Effect.andThen(
22 Effect.fail(
23 new OpenCodeRuntime.OpenCodeRuntimeError({
24 operation,
25 detail: "reached a 1.x server path",
26 }),
27 ),
28 ),
29 );
30// Reports OpenCode 2 from `--version`; any attempt to reach a server is recorded and refused.
31const openCode2Runtime = {
32 runOpenCodeCommand: () => Effect.succeed({ stdout: "opencode v2.0.18\n", stderr: "", code: 0 }),
33 startOpenCodeServerProcess: () => reachedServer("start"),
34 connectToOpenCodeServer: () => reachedServer("connect"),
35} as unknown as OpenCodeRuntime.OpenCodeRuntimeShape;
36​
37const layer = Layer.mergeAll(
38 ServerConfig.layerTest(process.cwd(), { prefix: "t3-opencode-driver-" }),
39 IdAllocator.layer,
40 ServerSettings.layerTest(),
41 Layer.mock(BackgroundPolicy.BackgroundPolicy)({}),
42 Layer.succeed(
43 ProviderEventLoggers.ProviderEventLoggers,
44 ProviderEventLoggers.NoOpProviderEventLoggers,
45 ),
46 Layer.succeed(OpenCodeRuntime.OpenCodeRuntime, openCode2Runtime),
47).pipe(Layer.provideMerge(NodeServices.layer));
48​
49const create = (config: Partial<OpenCodeSettings>, http: HttpClient.HttpClient) =>
50 OpenCodeDriver.create({
51 instanceId: ProviderInstanceId.make("opencode-test"),
52 displayName: undefined,
53 environment: [],
54 enabled: true,
55 config: { ...OpenCodeDriver.defaultConfig(), ...config },
56 }).pipe(Effect.provideService(HttpClient.HttpClient, http));
57​
58const noHttp = HttpClient.make(() => Effect.die("A local binary must not be probed over HTTP"));
59​
60it.layer(layer)("OpenCodeDriver runtime selection", (it) => {
61 it.effect("never starts a 1.x server for an OpenCode 2 instance", () =>
62 Effect.gen(function* () {
63 serverStarts.length = 0;
64 const instance = yield* create({}, noHttp);
65​
66 // The workspace snapshot falls back to the machine snapshot.
67 const workspace = yield* instance.snapshotForCwd!(process.cwd());
68 assert.strictEqual(workspace.instanceId, instance.instanceId);
69 const title = yield* Effect.flip(
70 instance.textGeneration.generateThreadTitle({
71 cwd: process.cwd(),
72 message: "hello",
73 modelSelection: { instanceId: instance.instanceId, model: "opencode/big-pickle" },
74 }),
75 );
76 assert.strictEqual(title.detail, OPENCODE_2_TEXT_GENERATION_UNSUPPORTED);
77 assert.deepStrictEqual(serverStarts, []);
78 }).pipe(Effect.scoped),
79 );
80​
81 it.effect("answers capability reads without waiting on an unreachable server", () =>
82 Effect.gen(function* () {
83 const hang = HttpClient.make(() => Effect.never);
84 const instance = yield* create({ serverUrl: "http://127.0.0.1:9" }, hang);
85​
86 // No probe has succeeded yet, and the server never answers: the 1.x default applies.
87 const capabilities = yield* instance.orchestrationAdapter
88 .getCapabilities()
89 .pipe(Effect.forkChild);
90 yield* Effect.yieldNow;
91 assert.isDefined(capabilities.pollUnsafe());
92 yield* Fiber.join(capabilities);
93 }).pipe(Effect.scoped, Effect.provide(TestClock.layer())),
94 );
95});