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

serverRuntimeStartup.test.ts

274 lines11.5 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { DEFAULT_MODEL, ProjectId, ProviderInstanceId, ThreadId } from "@v12/contracts";
3import { assert, it } from "@effect/vitest";
4import * as Crypto from "effect/Crypto";
5import * as Deferred from "effect/Deferred";
6import * as Effect from "effect/Effect";
7import * as Fiber from "effect/Fiber";
8import * as Option from "effect/Option";
9import * as PlatformError from "effect/PlatformError";
10import * as Ref from "effect/Ref";
11import * as Stream from "effect/Stream";
12​
13import * as ServerConfig from "./config.ts";
14import * as OrchestrationEngine from "./orchestration/Services/OrchestrationEngine.ts";
15import * as ProjectionSnapshotQuery from "./orchestration/Services/ProjectionSnapshotQuery.ts";
16import * as AnalyticsService from "./telemetry/AnalyticsService.ts";
17import * as ServerRuntimeStartup from "./serverRuntimeStartup.ts";
18​
19it("uses the canonical Codex default for auto-bootstrapped model selection", () => {
20 assert.deepStrictEqual(ServerRuntimeStartup.getAutoBootstrapDefaultModelSelection(), {
21 instanceId: ProviderInstanceId.make("codex"),
22 model: DEFAULT_MODEL,
23 });
24});
25​
26it.effect("enqueueCommand waits for readiness and then drains queued work", () =>
27 Effect.scoped(
28 Effect.gen(function* () {
29 const executionCount = yield* Ref.make(0);
30 const commandGate = yield* ServerRuntimeStartup.makeCommandGate;
31​
32 const queuedCommandFiber = yield* commandGate
33 .enqueueCommand(Ref.updateAndGet(executionCount, (count) => count + 1))
34 .pipe(Effect.forkScoped);
35​
36 yield* Effect.yieldNow;
37 assert.equal(yield* Ref.get(executionCount), 0);
38​
39 yield* commandGate.signalCommandReady;
40​
41 const result = yield* Fiber.join(queuedCommandFiber);
42 assert.equal(result, 1);
43 assert.equal(yield* Ref.get(executionCount), 1);
44 }),
45 ),
46);
47​
48it.effect("enqueueCommand fails queued work when readiness fails", () =>
49 Effect.scoped(
50 Effect.gen(function* () {
51 const commandGate = yield* ServerRuntimeStartup.makeCommandGate;
52 const failure = yield* Deferred.make<void, never>();
53​
54 const queuedCommandFiber = yield* commandGate
55 .enqueueCommand(Deferred.await(failure).pipe(Effect.as("should-not-run")))
56 .pipe(Effect.forkScoped);
57​
58 yield* commandGate.failCommandReady(
59 new ServerRuntimeStartup.ServerRuntimeStartupError({
60 mode: "web",
61 host: "127.0.0.1",
62 port: 3773,
63 cause: new Error("test startup failure"),
64 }),
65 );
66​
67 const error = yield* Effect.flip(Fiber.join(queuedCommandFiber));
68 assert.equal(error.message, "Server runtime startup failed before command readiness.");
69 }),
70 ),
71);
72​
73it.effect("launchStartupHeartbeat does not block the caller while counts are loading", () =>
74 Effect.scoped(
75 Effect.gen(function* () {
76 const releaseCounts = yield* Deferred.make<void, never>();
77​
78 yield* ServerRuntimeStartup.launchStartupHeartbeat.pipe(
79 Effect.provideService(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
80 getCommandReadModel: () => Effect.die("unused"),
81 getSnapshot: () => Effect.die("unused"),
82 getShellSnapshot: () => Effect.die("unused"),
83 getArchivedShellSnapshot: () => Effect.die("unused"),
84 getSnapshotSequence: () => Effect.die("unused"),
85 getCounts: () =>
86 Deferred.await(releaseCounts).pipe(
87 Effect.as({
88 projectCount: 2,
89 threadCount: 3,
90 }),
91 ),
92 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
93 getProjectShellById: () => Effect.succeed(Option.none()),
94 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
95 getThreadCheckpointContext: () => Effect.succeed(Option.none()),
96 getFullThreadDiffContext: () => Effect.succeed(Option.none()),
97 getThreadShellById: () => Effect.succeed(Option.none()),
98 getThreadDetailById: () => Effect.succeed(Option.none()),
99 getThreadDetailSnapshot: () => Effect.succeed(Option.none()),
100 }),
101 Effect.provideService(AnalyticsService.AnalyticsService, {
102 record: () => Effect.void,
103 flush: Effect.void,
104 }),
105 );
106 }),
107 ),
108);
109​
110it.effect("resolveWelcomeBase derives cwd and project name from server config", () =>
111 Effect.gen(function* () {
112 const welcome = yield* ServerRuntimeStartup.resolveWelcomeBase.pipe(
113 Effect.provideService(ServerConfig.ServerConfig, {
114 cwd: "/tmp/startup-project",
115 } as never),
116 );
117​
118 assert.deepStrictEqual(welcome, {
119 cwd: "/tmp/startup-project",
120 projectName: "startup-project",
121 });
122 }),
123);
124​
125it.effect("resolveAutoBootstrapWelcomeTargets returns existing project and thread ids", () => {
126 const bootstrapProjectId = ProjectId.make("project-startup-bootstrap");
127 const bootstrapThreadId = ThreadId.make("thread-startup-bootstrap");
128​
129 return Effect.gen(function* () {
130 const dispatchCalls = yield* Ref.make<ReadonlyArray<string>>([]);
131 const targets = yield* ServerRuntimeStartup.resolveAutoBootstrapWelcomeTargets.pipe(
132 Effect.provideService(ServerConfig.ServerConfig, {
133 cwd: "/tmp/startup-project",
134 autoBootstrapProjectFromCwd: true,
135 } as never),
136 Effect.provideService(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
137 getCommandReadModel: () => Effect.die("unused"),
138 getSnapshot: () => Effect.die("unused"),
139 getShellSnapshot: () => Effect.die("unused"),
140 getArchivedShellSnapshot: () => Effect.die("unused"),
141 getSnapshotSequence: () => Effect.die("unused"),
142 getCounts: () => Effect.die("unused"),
143 getActiveProjectByWorkspaceRoot: () =>
144 Effect.succeed(
145 Option.some({
146 id: bootstrapProjectId,
147 title: "Startup Project",
148 workspaceRoot: "/tmp/startup-project",
149 defaultModelSelection: ServerRuntimeStartup.getAutoBootstrapDefaultModelSelection(),
150 scripts: [],
151 createdAt: "2026-01-01T00:00:00.000Z",
152 updatedAt: "2026-01-01T00:00:00.000Z",
153 deletedAt: null,
154 }),
155 ),
156 getProjectShellById: () => Effect.die("unused"),
157 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.some(bootstrapThreadId)),
158 getThreadCheckpointContext: () => Effect.succeed(Option.none()),
159 getFullThreadDiffContext: () => Effect.succeed(Option.none()),
160 getThreadShellById: () => Effect.die("unused"),
161 getThreadDetailById: () => Effect.die("unused"),
162 getThreadDetailSnapshot: () => Effect.die("unused"),
163 }),
164 Effect.provideService(OrchestrationEngine.OrchestrationEngineService, {
165 readEvents: () => Stream.empty,
166 dispatch: (command) =>
167 Ref.update(dispatchCalls, (calls) => [...calls, command.type]).pipe(
168 Effect.as({ sequence: 1 }),
169 ),
170 streamDomainEvents: Stream.empty,
171 } satisfies OrchestrationEngine.OrchestrationEngineService["Service"]),
172 Effect.provide(NodeServices.layer),
173 );
174​
175 assert.deepStrictEqual(targets, {
176 bootstrapProjectId,
177 bootstrapThreadId,
178 });
179 assert.deepStrictEqual(yield* Ref.get(dispatchCalls), []);
180 });
181});
182​
183it.effect("resolveAutoBootstrapWelcomeTargets creates a project and thread when missing", () =>
184 Effect.gen(function* () {
185 const dispatchCalls = yield* Ref.make<ReadonlyArray<string>>([]);
186 const targets = yield* ServerRuntimeStartup.resolveAutoBootstrapWelcomeTargets.pipe(
187 Effect.provideService(ServerConfig.ServerConfig, {
188 cwd: "/tmp/startup-project",
189 autoBootstrapProjectFromCwd: true,
190 } as never),
191 Effect.provideService(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
192 getCommandReadModel: () => Effect.die("unused"),
193 getSnapshot: () => Effect.die("unused"),
194 getShellSnapshot: () => Effect.die("unused"),
195 getArchivedShellSnapshot: () => Effect.die("unused"),
196 getSnapshotSequence: () => Effect.die("unused"),
197 getCounts: () => Effect.die("unused"),
198 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
199 getProjectShellById: () => Effect.die("unused"),
200 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
201 getThreadCheckpointContext: () => Effect.succeed(Option.none()),
202 getFullThreadDiffContext: () => Effect.succeed(Option.none()),
203 getThreadShellById: () => Effect.die("unused"),
204 getThreadDetailById: () => Effect.die("unused"),
205 getThreadDetailSnapshot: () => Effect.die("unused"),
206 }),
207 Effect.provideService(OrchestrationEngine.OrchestrationEngineService, {
208 readEvents: () => Stream.empty,
209 dispatch: (command) =>
210 Ref.update(dispatchCalls, (calls) => [...calls, command.type]).pipe(
211 Effect.as({ sequence: 1 }),
212 ),
213 streamDomainEvents: Stream.empty,
214 } satisfies OrchestrationEngine.OrchestrationEngineService["Service"]),
215 Effect.provide(NodeServices.layer),
216 );
217​
218 assert.equal(typeof targets.bootstrapProjectId, "string");
219 assert.equal(typeof targets.bootstrapThreadId, "string");
220 assert.deepStrictEqual(yield* Ref.get(dispatchCalls), ["project.create", "thread.create"]);
221 }),
222);
223​
224it.effect("resolveAutoBootstrapWelcomeTargets preserves typed UUID generation failures", () =>
225 Effect.gen(function* () {
226 const crypto = yield* Crypto.Crypto;
227 const uuidError = PlatformError.systemError({
228 _tag: "Unknown",
229 module: "Crypto",
230 method: "randomUUIDv4",
231 description: "UUID generation unavailable",
232 });
233 const dispatchCalls = yield* Ref.make<ReadonlyArray<string>>([]);
234​
235 const error = yield* ServerRuntimeStartup.resolveAutoBootstrapWelcomeTargets.pipe(
236 Effect.provideService(ServerConfig.ServerConfig, {
237 cwd: "/tmp/startup-project",
238 autoBootstrapProjectFromCwd: true,
239 } as never),
240 Effect.provideService(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
241 getCommandReadModel: () => Effect.die("unused"),
242 getSnapshot: () => Effect.die("unused"),
243 getShellSnapshot: () => Effect.die("unused"),
244 getArchivedShellSnapshot: () => Effect.die("unused"),
245 getSnapshotSequence: () => Effect.die("unused"),
246 getCounts: () => Effect.die("unused"),
247 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
248 getProjectShellById: () => Effect.die("unused"),
249 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
250 getThreadCheckpointContext: () => Effect.succeed(Option.none()),
251 getFullThreadDiffContext: () => Effect.succeed(Option.none()),
252 getThreadShellById: () => Effect.die("unused"),
253 getThreadDetailById: () => Effect.die("unused"),
254 getThreadDetailSnapshot: () => Effect.die("unused"),
255 }),
256 Effect.provideService(OrchestrationEngine.OrchestrationEngineService, {
257 readEvents: () => Stream.empty,
258 dispatch: (command) =>
259 Ref.update(dispatchCalls, (calls) => [...calls, command.type]).pipe(
260 Effect.as({ sequence: 1 }),
261 ),
262 streamDomainEvents: Stream.empty,
263 } satisfies OrchestrationEngine.OrchestrationEngineService["Service"]),
264 Effect.provideService(Crypto.Crypto, {
265 ...crypto,
266 randomUUIDv4: Effect.fail(uuidError),
267 }),
268 Effect.flip,
269 );
270​
271 assert.strictEqual(error, uuidError);
272 assert.deepStrictEqual(yield* Ref.get(dispatchCalls), []);
273 }).pipe(Effect.provide(NodeServices.layer)),
274);