Open History to see the last commit
1import { it } from "@effect/vitest";
2import * as Deferred from "effect/Deferred";
3import * as Duration from "effect/Duration";
4import * as Effect from "effect/Effect";
5import * as Fiber from "effect/Fiber";
6import * as Ref from "effect/Ref";
7import * as TestClock from "effect/testing/TestClock";
8import { expect } from "vite-plus/test";
9
10import {
11 OpenCodeRuntime,
12 OpenCodeRuntimeError,
13 type OpenCodeRuntimeShape,
14} from "./opencodeRuntime.ts";
15import * as OpenCodeServerOwner from "./OpenCodeServerOwner.ts";
16
17const unusedRuntimeMethod = () =>
18 Effect.fail(
19 new OpenCodeRuntimeError({
20 operation: "unused",
21 detail: "unused test method",
22 }),
23 );
24
25const makeRuntime = Effect.gen(function* () {
26 const starts = yield* Ref.make(0);
27 const closes = yield* Ref.make(0);
28 const failNextStart = yield* Ref.make(false);
29 const started = yield* Deferred.make<void>();
30 const closed = yield* Deferred.make<void>();
31 const runtime: OpenCodeRuntimeShape = {
32 startOpenCodeServerProcess: () =>
33 Effect.gen(function* () {
34 if (yield* Ref.getAndSet(failNextStart, false)) {
35 return yield* new OpenCodeRuntimeError({
36 operation: "startOpenCodeServerProcess",
37 detail: "start failed",
38 });
39 }
40 const index = yield* Ref.updateAndGet(starts, (count) => count + 1);
41 yield* Deferred.succeed(started, undefined).pipe(Effect.ignore);
42 yield* Effect.addFinalizer(() =>
43 Ref.update(closes, (count) => count + 1).pipe(
44 Effect.andThen(Deferred.succeed(closed, undefined)),
45 Effect.ignore,
46 ),
47 );
48 return {
49 url: `http://127.0.0.1:${index}`,
50 version: "1.14.19",
51 isRunning: Effect.succeed(true),
52 exitCode: Effect.never,
53 };
54 }),
55 connectToOpenCodeServer: unusedRuntimeMethod,
56 runOpenCodeCommand: unusedRuntimeMethod,
57 createOpenCodeSdkClient: () => ({}) as never,
58 loadOpenCodeInventory: unusedRuntimeMethod,
59 loadOpenCodeSkills: unusedRuntimeMethod,
60 };
61 return { runtime, starts, closes, failNextStart, started, closed };
62});
63
64it.effect("shares concurrent borrowers and closes after the idle TTL", () =>
65 Effect.gen(function* () {
66 const testRuntime = yield* makeRuntime;
67 const release = yield* Deferred.make<void>();
68 yield* Effect.scoped(
69 Effect.gen(function* () {
70 const owner = yield* OpenCodeServerOwner.make({
71 binaryPath: "opencode",
72 directory: "/project",
73 });
74 const useServer = owner.withServer((server) =>
75 Deferred.await(release).pipe(Effect.as(server.url)),
76 );
77 const fibers = yield* Effect.all([useServer, useServer], {
78 concurrency: "unbounded",
79 }).pipe(Effect.forkChild);
80 yield* Deferred.await(testRuntime.started);
81 expect(yield* Ref.get(testRuntime.starts)).toBe(1);
82 yield* Deferred.succeed(release, undefined);
83 expect(yield* Fiber.join(fibers)).toEqual(["http://127.0.0.1:1", "http://127.0.0.1:1"]);
84 yield* TestClock.adjust(Duration.seconds(31));
85 yield* Deferred.await(testRuntime.closed);
86 expect(yield* Ref.get(testRuntime.closes)).toBe(1);
87 }),
88 ).pipe(Effect.provideService(OpenCodeRuntime, testRuntime.runtime));
89 }).pipe(Effect.provide(TestClock.layer())),
90);
91
92it.effect("retries a failed start and closes on owner scope shutdown", () =>
93 Effect.gen(function* () {
94 const testRuntime = yield* makeRuntime;
95 yield* Ref.set(testRuntime.failNextStart, true);
96 yield* Effect.scoped(
97 Effect.gen(function* () {
98 const owner = yield* OpenCodeServerOwner.make({
99 binaryPath: "opencode",
100 directory: "/project",
101 });
102 expect(
103 (yield* Effect.exit(owner.withServer((server) => Effect.succeed(server.url))))._tag,
104 ).toBe("Failure");
105 expect(yield* owner.withServer((server) => Effect.succeed(server.url))).toBe(
106 "http://127.0.0.1:1",
107 );
108 }),
109 ).pipe(Effect.provideService(OpenCodeRuntime, testRuntime.runtime));
110 expect(yield* Ref.get(testRuntime.starts)).toBe(1);
111 expect(yield* Ref.get(testRuntime.closes)).toBe(1);
112 }),
113);
114
115it.effect("invalidates an exited process so the next borrower starts a new one", () =>
116 Effect.gen(function* () {
117 const starts = yield* Ref.make(0);
118 const processExits: Array<Deferred.Deferred<number>> = [];
119 const processClosed = yield* Deferred.make<void>();
120 const runtime: OpenCodeRuntimeShape = {
121 startOpenCodeServerProcess: () =>
122 Effect.gen(function* () {
123 const index = yield* Ref.updateAndGet(starts, (count) => count + 1);
124 const exitCode = yield* Deferred.make<number>();
125 processExits.push(exitCode);
126 yield* Effect.addFinalizer(() =>
127 Deferred.succeed(processClosed, undefined).pipe(Effect.ignore),
128 );
129 return {
130 url: `http://127.0.0.1:${index}`,
131 version: "1.14.19",
132 isRunning: Effect.succeed(true),
133 exitCode: Deferred.await(exitCode),
134 };
135 }),
136 connectToOpenCodeServer: unusedRuntimeMethod,
137 runOpenCodeCommand: unusedRuntimeMethod,
138 createOpenCodeSdkClient: () => ({}) as never,
139 loadOpenCodeInventory: unusedRuntimeMethod,
140 loadOpenCodeSkills: unusedRuntimeMethod,
141 };
142
143 yield* Effect.scoped(
144 Effect.gen(function* () {
145 const owner = yield* OpenCodeServerOwner.make({
146 binaryPath: "opencode",
147 directory: "/project",
148 });
149 expect(yield* owner.withServer((server) => Effect.succeed(server.url))).toBe(
150 "http://127.0.0.1:1",
151 );
152 yield* Deferred.succeed(processExits[0]!, 1);
153 yield* Deferred.await(processClosed);
154 expect(yield* owner.withServer((server) => Effect.succeed(server.url))).toBe(
155 "http://127.0.0.1:2",
156 );
157 }),
158 ).pipe(Effect.provideService(OpenCodeRuntime, runtime));
159 expect(yield* Ref.get(starts)).toBe(2);
160 }),
161);
162
163it.effect("replaces a dead cached process before its exit watcher runs", () =>
164 Effect.gen(function* () {
165 const starts = yield* Ref.make(0);
166 const closes = yield* Ref.make(0);
167 const processRunning: Array<Ref.Ref<boolean>> = [];
168 const runtime: OpenCodeRuntimeShape = {
169 startOpenCodeServerProcess: () =>
170 Effect.gen(function* () {
171 const index = yield* Ref.updateAndGet(starts, (count) => count + 1);
172 const isRunning = yield* Ref.make(true);
173 processRunning.push(isRunning);
174 yield* Effect.addFinalizer(() => Ref.update(closes, (count) => count + 1));
175 return {
176 url: `http://127.0.0.1:${index}`,
177 version: "1.14.19",
178 isRunning: Ref.get(isRunning),
179 exitCode: Effect.never,
180 };
181 }),
182 connectToOpenCodeServer: unusedRuntimeMethod,
183 runOpenCodeCommand: unusedRuntimeMethod,
184 createOpenCodeSdkClient: () => ({}) as never,
185 loadOpenCodeInventory: unusedRuntimeMethod,
186 loadOpenCodeSkills: unusedRuntimeMethod,
187 };
188
189 yield* Effect.scoped(
190 Effect.gen(function* () {
191 const owner = yield* OpenCodeServerOwner.make({
192 binaryPath: "opencode",
193 directory: "/project",
194 });
195 expect(yield* owner.withServer((server) => Effect.succeed(server.url))).toBe(
196 "http://127.0.0.1:1",
197 );
198 yield* Ref.set(processRunning[0]!, false);
199
200 expect(yield* owner.withServer((server) => Effect.succeed(server.url))).toBe(
201 "http://127.0.0.1:2",
202 );
203 expect(yield* Ref.get(starts)).toBe(2);
204 expect(yield* Ref.get(closes)).toBe(1);
205 }),
206 ).pipe(Effect.provideService(OpenCodeRuntime, runtime));
207 }),
208);
209
210it.effect("cleans up an interrupted startup and allows a retry", () =>
211 Effect.gen(function* () {
212 const starts = yield* Ref.make(0);
213 const firstStartEntered = yield* Deferred.make<void>();
214 const firstStartClosed = yield* Deferred.make<void>();
215 const runtime: OpenCodeRuntimeShape = {
216 startOpenCodeServerProcess: () =>
217 Effect.gen(function* () {
218 const index = yield* Ref.updateAndGet(starts, (count) => count + 1);
219 yield* Effect.addFinalizer(() =>
220 index === 1
221 ? Deferred.succeed(firstStartClosed, undefined).pipe(Effect.ignore)
222 : Effect.void,
223 );
224 if (index === 1) {
225 yield* Deferred.succeed(firstStartEntered, undefined);
226 return yield* Effect.never;
227 }
228 return {
229 url: `http://127.0.0.1:${index}`,
230 version: "1.14.19",
231 isRunning: Effect.succeed(true),
232 exitCode: Effect.never,
233 };
234 }),
235 connectToOpenCodeServer: unusedRuntimeMethod,
236 runOpenCodeCommand: unusedRuntimeMethod,
237 createOpenCodeSdkClient: () => ({}) as never,
238 loadOpenCodeInventory: unusedRuntimeMethod,
239 loadOpenCodeSkills: unusedRuntimeMethod,
240 };
241
242 yield* Effect.scoped(
243 Effect.gen(function* () {
244 const owner = yield* OpenCodeServerOwner.make({
245 binaryPath: "opencode",
246 directory: "/project",
247 });
248 const firstBorrower = yield* owner
249 .withServer((server) => Effect.succeed(server.url))
250 .pipe(Effect.forkChild);
251 yield* Deferred.await(firstStartEntered);
252 yield* Fiber.interrupt(firstBorrower);
253 yield* Deferred.await(firstStartClosed);
254 expect(yield* owner.withServer((server) => Effect.succeed(server.url))).toBe(
255 "http://127.0.0.1:2",
256 );
257 }),
258 ).pipe(Effect.provideService(OpenCodeRuntime, runtime));
259 }),
260);
261
262it.effect("releases an interrupted borrower and closes after the idle TTL", () =>
263 Effect.gen(function* () {
264 const testRuntime = yield* makeRuntime;
265 const borrowerEntered = yield* Deferred.make<void>();
266 yield* Effect.scoped(
267 Effect.gen(function* () {
268 const owner = yield* OpenCodeServerOwner.make({
269 binaryPath: "opencode",
270 directory: "/project",
271 });
272 const borrower = yield* owner
273 .withServer(() =>
274 Deferred.succeed(borrowerEntered, undefined).pipe(Effect.andThen(Effect.never)),
275 )
276 .pipe(Effect.forkChild);
277 yield* Deferred.await(borrowerEntered);
278 yield* Fiber.interrupt(borrower);
279 yield* TestClock.adjust(Duration.seconds(31));
280 yield* Deferred.await(testRuntime.closed);
281 expect(yield* Ref.get(testRuntime.closes)).toBe(1);
282 }),
283 ).pipe(Effect.provideService(OpenCodeRuntime, testRuntime.runtime));
284 }).pipe(Effect.provide(TestClock.layer())),
285);