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