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

serverRuntimeStartup.test.ts

277 lines11.6 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { DEFAULT_MODEL, ProjectId, ProviderInstanceId, ThreadId } from "@t3tools/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 latestSequence: Effect.succeed(0),
172 } satisfies OrchestrationEngine.OrchestrationEngineService["Service"]),
173 Effect.provide(NodeServices.layer),
174 );
175​
176 assert.deepStrictEqual(targets, {
177 bootstrapProjectId,
178 bootstrapThreadId,
179 });
180 assert.deepStrictEqual(yield* Ref.get(dispatchCalls), []);
181 });
182});
183​
184it.effect("resolveAutoBootstrapWelcomeTargets creates a project and thread when missing", () =>
185 Effect.gen(function* () {
186 const dispatchCalls = yield* Ref.make<ReadonlyArray<string>>([]);
187 const targets = yield* ServerRuntimeStartup.resolveAutoBootstrapWelcomeTargets.pipe(
188 Effect.provideService(ServerConfig.ServerConfig, {
189 cwd: "/tmp/startup-project",
190 autoBootstrapProjectFromCwd: true,
191 } as never),
192 Effect.provideService(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
193 getCommandReadModel: () => Effect.die("unused"),
194 getSnapshot: () => Effect.die("unused"),
195 getShellSnapshot: () => Effect.die("unused"),
196 getArchivedShellSnapshot: () => Effect.die("unused"),
197 getSnapshotSequence: () => Effect.die("unused"),
198 getCounts: () => Effect.die("unused"),
199 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
200 getProjectShellById: () => Effect.die("unused"),
201 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
202 getThreadCheckpointContext: () => Effect.succeed(Option.none()),
203 getFullThreadDiffContext: () => Effect.succeed(Option.none()),
204 getThreadShellById: () => Effect.die("unused"),
205 getThreadDetailById: () => Effect.die("unused"),
206 getThreadDetailSnapshot: () => Effect.die("unused"),
207 }),
208 Effect.provideService(OrchestrationEngine.OrchestrationEngineService, {
209 readEvents: () => Stream.empty,
210 dispatch: (command) =>
211 Ref.update(dispatchCalls, (calls) => [...calls, command.type]).pipe(
212 Effect.as({ sequence: 1 }),
213 ),
214 streamDomainEvents: Stream.empty,
215 latestSequence: Effect.succeed(0),
216 } satisfies OrchestrationEngine.OrchestrationEngineService["Service"]),
217 Effect.provide(NodeServices.layer),
218 );
219​
220 assert.equal(typeof targets.bootstrapProjectId, "string");
221 assert.equal(typeof targets.bootstrapThreadId, "string");
222 assert.deepStrictEqual(yield* Ref.get(dispatchCalls), ["project.create", "thread.create"]);
223 }),
224);
225​
226it.effect("resolveAutoBootstrapWelcomeTargets preserves typed UUID generation failures", () =>
227 Effect.gen(function* () {
228 const crypto = yield* Crypto.Crypto;
229 const uuidError = PlatformError.systemError({
230 _tag: "Unknown",
231 module: "Crypto",
232 method: "randomUUIDv4",
233 description: "UUID generation unavailable",
234 });
235 const dispatchCalls = yield* Ref.make<ReadonlyArray<string>>([]);
236​
237 const error = yield* ServerRuntimeStartup.resolveAutoBootstrapWelcomeTargets.pipe(
238 Effect.provideService(ServerConfig.ServerConfig, {
239 cwd: "/tmp/startup-project",
240 autoBootstrapProjectFromCwd: true,
241 } as never),
242 Effect.provideService(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
243 getCommandReadModel: () => Effect.die("unused"),
244 getSnapshot: () => Effect.die("unused"),
245 getShellSnapshot: () => Effect.die("unused"),
246 getArchivedShellSnapshot: () => Effect.die("unused"),
247 getSnapshotSequence: () => Effect.die("unused"),
248 getCounts: () => Effect.die("unused"),
249 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
250 getProjectShellById: () => Effect.die("unused"),
251 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
252 getThreadCheckpointContext: () => Effect.succeed(Option.none()),
253 getFullThreadDiffContext: () => Effect.succeed(Option.none()),
254 getThreadShellById: () => Effect.die("unused"),
255 getThreadDetailById: () => Effect.die("unused"),
256 getThreadDetailSnapshot: () => Effect.die("unused"),
257 }),
258 Effect.provideService(OrchestrationEngine.OrchestrationEngineService, {
259 readEvents: () => Stream.empty,
260 dispatch: (command) =>
261 Ref.update(dispatchCalls, (calls) => [...calls, command.type]).pipe(
262 Effect.as({ sequence: 1 }),
263 ),
264 streamDomainEvents: Stream.empty,
265 latestSequence: Effect.succeed(0),
266 } satisfies OrchestrationEngine.OrchestrationEngineService["Service"]),
267 Effect.provideService(Crypto.Crypto, {
268 ...crypto,
269 randomUUIDv4: Effect.fail(uuidError),
270 }),
271 Effect.flip,
272 );
273​
274 assert.strictEqual(error, uuidError);
275 assert.deepStrictEqual(yield* Ref.get(dispatchCalls), []);
276 }).pipe(Effect.provide(NodeServices.layer)),
277);