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

OpenCodeServerOwner.test.ts

214 lines8.1 KB

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 * as OpenCodeRuntime from "./OpenCodeRuntime.ts";
11import * as OpenCodeServerOwner from "./OpenCodeServerOwner.ts";
12​
13const unusedRuntimeMethod = () =>
14 Effect.fail(
15 new OpenCodeRuntime.OpenCodeRuntimeError({
16 operation: "unused",
17 detail: "unused test method",
18 }),
19 );
20​
21const makeRuntime = Effect.gen(function* () {
22 const starts = yield* Ref.make(0);
23 const closes = yield* Ref.make(0);
24 const failNextStart = yield* Ref.make(false);
25 const started = yield* Deferred.make<void>();
26 const closed = yield* Deferred.make<void>();
27 const runtime: OpenCodeRuntime.OpenCodeRuntime["Service"] = {
28 startOpenCodeServerProcess: () =>
29 Effect.gen(function* () {
30 if (yield* Ref.getAndSet(failNextStart, false)) {
31 return yield* new OpenCodeRuntime.OpenCodeRuntimeError({
32 operation: "startOpenCodeServerProcess",
33 detail: "start failed",
34 });
35 }
36 const index = yield* Ref.updateAndGet(starts, (count) => count + 1);
37 yield* Deferred.succeed(started, undefined).pipe(Effect.ignore);
38 yield* Effect.addFinalizer(() =>
39 Ref.update(closes, (count) => count + 1).pipe(
40 Effect.andThen(Deferred.succeed(closed, undefined)),
41 Effect.ignore,
42 ),
43 );
44 return {
45 url: `http://127.0.0.1:${index}`,
46 version: "1.14.19",
47 isRunning: Effect.succeed(true),
48 exitCode: Effect.never,
49 };
50 }),
51 connectToOpenCodeServer: unusedRuntimeMethod,
52 runOpenCodeCommand: unusedRuntimeMethod,
53 createOpenCodeSdkClient: () => ({}) as never,
54 loadOpenCodeInventory: unusedRuntimeMethod,
55 loadOpenCodeSkills: unusedRuntimeMethod,
56 loadInventoryFromCli: unusedRuntimeMethod,
57 loadSkillsFromCli: unusedRuntimeMethod,
58 };
59 return { runtime, starts, closes, failNextStart, started, closed };
60});
61​
62it.effect("shares concurrent borrowers and closes after the idle TTL", () =>
63 Effect.gen(function* () {
64 const testRuntime = yield* makeRuntime;
65 const release = yield* Deferred.make<void>();
66 yield* Effect.scoped(
67 Effect.gen(function* () {
68 const owner = yield* OpenCodeServerOwner.make({
69 binaryPath: "opencode",
70 directory: "/project",
71 });
72 const useServer = owner.withServer((server) =>
73 Deferred.await(release).pipe(Effect.as(server.url)),
74 );
75 const fibers = yield* Effect.all([useServer, useServer], {
76 concurrency: "unbounded",
77 }).pipe(Effect.forkChild);
78 yield* Deferred.await(testRuntime.started);
79 expect(yield* Ref.get(testRuntime.starts)).toBe(1);
80 yield* Deferred.succeed(release, undefined);
81 expect(yield* Fiber.join(fibers)).toEqual(["http://127.0.0.1:1", "http://127.0.0.1:1"]);
82 yield* TestClock.adjust(Duration.seconds(31));
83 yield* Deferred.await(testRuntime.closed);
84 expect(yield* Ref.get(testRuntime.closes)).toBe(1);
85 }),
86 ).pipe(Effect.provideService(OpenCodeRuntime.OpenCodeRuntime, testRuntime.runtime));
87 }).pipe(Effect.provide(TestClock.layer())),
88);
89​
90it.effect("retries a failed start and closes on owner scope shutdown", () =>
91 Effect.gen(function* () {
92 const testRuntime = yield* makeRuntime;
93 yield* Ref.set(testRuntime.failNextStart, true);
94 yield* Effect.scoped(
95 Effect.gen(function* () {
96 const owner = yield* OpenCodeServerOwner.make({
97 binaryPath: "opencode",
98 directory: "/project",
99 });
100 expect(
101 (yield* Effect.exit(owner.withServer((server) => Effect.succeed(server.url))))._tag,
102 ).toBe("Failure");
103 expect(yield* owner.withServer((server) => Effect.succeed(server.url))).toBe(
104 "http://127.0.0.1:1",
105 );
106 }),
107 ).pipe(Effect.provideService(OpenCodeRuntime.OpenCodeRuntime, testRuntime.runtime));
108 expect(yield* Ref.get(testRuntime.starts)).toBe(1);
109 expect(yield* Ref.get(testRuntime.closes)).toBe(1);
110 }),
111);
112​
113it.effect("replaces a dead cached process before its exit watcher runs", () =>
114 Effect.gen(function* () {
115 const starts = yield* Ref.make(0);
116 const closes = yield* Ref.make(0);
117 const processRunning: Array<Ref.Ref<boolean>> = [];
118 const runtime: OpenCodeRuntime.OpenCodeRuntime["Service"] = {
119 startOpenCodeServerProcess: () =>
120 Effect.gen(function* () {
121 const index = yield* Ref.updateAndGet(starts, (count) => count + 1);
122 const isRunning = yield* Ref.make(true);
123 processRunning.push(isRunning);
124 yield* Effect.addFinalizer(() => Ref.update(closes, (count) => count + 1));
125 return {
126 url: `http://127.0.0.1:${index}`,
127 version: "1.14.19",
128 isRunning: Ref.get(isRunning),
129 exitCode: Effect.never,
130 };
131 }),
132 connectToOpenCodeServer: unusedRuntimeMethod,
133 runOpenCodeCommand: unusedRuntimeMethod,
134 createOpenCodeSdkClient: () => ({}) as never,
135 loadOpenCodeInventory: unusedRuntimeMethod,
136 loadOpenCodeSkills: unusedRuntimeMethod,
137 loadInventoryFromCli: unusedRuntimeMethod,
138 loadSkillsFromCli: unusedRuntimeMethod,
139 };
140​
141 yield* Effect.scoped(
142 Effect.gen(function* () {
143 const owner = yield* OpenCodeServerOwner.make({
144 binaryPath: "opencode",
145 directory: "/project",
146 });
147 expect(yield* owner.withServer((server) => Effect.succeed(server.url))).toBe(
148 "http://127.0.0.1:1",
149 );
150 yield* Ref.set(processRunning[0]!, false);
151​
152 expect(yield* owner.withServer((server) => Effect.succeed(server.url))).toBe(
153 "http://127.0.0.1:2",
154 );
155 expect(yield* Ref.get(starts)).toBe(2);
156 expect(yield* Ref.get(closes)).toBe(1);
157 }),
158 ).pipe(Effect.provideService(OpenCodeRuntime.OpenCodeRuntime, runtime));
159 }),
160);
161​
162it.effect("cleans up an interrupted startup and allows a retry", () =>
163 Effect.gen(function* () {
164 const starts = yield* Ref.make(0);
165 const firstStartEntered = yield* Deferred.make<void>();
166 const firstStartClosed = yield* Deferred.make<void>();
167 const runtime: OpenCodeRuntime.OpenCodeRuntime["Service"] = {
168 startOpenCodeServerProcess: () =>
169 Effect.gen(function* () {
170 const index = yield* Ref.updateAndGet(starts, (count) => count + 1);
171 yield* Effect.addFinalizer(() =>
172 index === 1
173 ? Deferred.succeed(firstStartClosed, undefined).pipe(Effect.ignore)
174 : Effect.void,
175 );
176 if (index === 1) {
177 yield* Deferred.succeed(firstStartEntered, undefined);
178 return yield* Effect.never;
179 }
180 return {
181 url: `http://127.0.0.1:${index}`,
182 version: "1.14.19",
183 isRunning: Effect.succeed(true),
184 exitCode: Effect.never,
185 };
186 }),
187 connectToOpenCodeServer: unusedRuntimeMethod,
188 runOpenCodeCommand: unusedRuntimeMethod,
189 createOpenCodeSdkClient: () => ({}) as never,
190 loadOpenCodeInventory: unusedRuntimeMethod,
191 loadOpenCodeSkills: unusedRuntimeMethod,
192 loadInventoryFromCli: unusedRuntimeMethod,
193 loadSkillsFromCli: unusedRuntimeMethod,
194 };
195​
196 yield* Effect.scoped(
197 Effect.gen(function* () {
198 const owner = yield* OpenCodeServerOwner.make({
199 binaryPath: "opencode",
200 directory: "/project",
201 });
202 const firstBorrower = yield* owner
203 .withServer((server) => Effect.succeed(server.url))
204 .pipe(Effect.forkChild);
205 yield* Deferred.await(firstStartEntered);
206 yield* Fiber.interrupt(firstBorrower);
207 yield* Deferred.await(firstStartClosed);
208 expect(yield* owner.withServer((server) => Effect.succeed(server.url))).toBe(
209 "http://127.0.0.1:2",
210 );
211 }),
212 ).pipe(Effect.provideService(OpenCodeRuntime.OpenCodeRuntime, runtime));
213 }),
214);