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

serverRuntimeStartup.test.ts

270 lines11.3 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 }),
100 Effect.provideService(AnalyticsService.AnalyticsService, {
101 record: () => Effect.void,
102 flush: Effect.void,
103 }),
104 );
105 }),
106 ),
107);
108​
109it.effect("resolveWelcomeBase derives cwd and project name from server config", () =>
110 Effect.gen(function* () {
111 const welcome = yield* ServerRuntimeStartup.resolveWelcomeBase.pipe(
112 Effect.provideService(ServerConfig.ServerConfig, {
113 cwd: "/tmp/startup-project",
114 } as never),
115 );
116​
117 assert.deepStrictEqual(welcome, {
118 cwd: "/tmp/startup-project",
119 projectName: "startup-project",
120 });
121 }),
122);
123​
124it.effect("resolveAutoBootstrapWelcomeTargets returns existing project and thread ids", () => {
125 const bootstrapProjectId = ProjectId.make("project-startup-bootstrap");
126 const bootstrapThreadId = ThreadId.make("thread-startup-bootstrap");
127​
128 return Effect.gen(function* () {
129 const dispatchCalls = yield* Ref.make<ReadonlyArray<string>>([]);
130 const targets = yield* ServerRuntimeStartup.resolveAutoBootstrapWelcomeTargets.pipe(
131 Effect.provideService(ServerConfig.ServerConfig, {
132 cwd: "/tmp/startup-project",
133 autoBootstrapProjectFromCwd: true,
134 } as never),
135 Effect.provideService(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
136 getCommandReadModel: () => Effect.die("unused"),
137 getSnapshot: () => Effect.die("unused"),
138 getShellSnapshot: () => Effect.die("unused"),
139 getArchivedShellSnapshot: () => Effect.die("unused"),
140 getSnapshotSequence: () => Effect.die("unused"),
141 getCounts: () => Effect.die("unused"),
142 getActiveProjectByWorkspaceRoot: () =>
143 Effect.succeed(
144 Option.some({
145 id: bootstrapProjectId,
146 title: "Startup Project",
147 workspaceRoot: "/tmp/startup-project",
148 defaultModelSelection: ServerRuntimeStartup.getAutoBootstrapDefaultModelSelection(),
149 scripts: [],
150 createdAt: "2026-01-01T00:00:00.000Z",
151 updatedAt: "2026-01-01T00:00:00.000Z",
152 deletedAt: null,
153 }),
154 ),
155 getProjectShellById: () => Effect.die("unused"),
156 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.some(bootstrapThreadId)),
157 getThreadCheckpointContext: () => Effect.succeed(Option.none()),
158 getFullThreadDiffContext: () => Effect.succeed(Option.none()),
159 getThreadShellById: () => Effect.die("unused"),
160 getThreadDetailById: () => Effect.die("unused"),
161 }),
162 Effect.provideService(OrchestrationEngine.OrchestrationEngineService, {
163 readEvents: () => Stream.empty,
164 dispatch: (command) =>
165 Ref.update(dispatchCalls, (calls) => [...calls, command.type]).pipe(
166 Effect.as({ sequence: 1 }),
167 ),
168 streamDomainEvents: Stream.empty,
169 } satisfies OrchestrationEngine.OrchestrationEngineService["Service"]),
170 Effect.provide(NodeServices.layer),
171 );
172​
173 assert.deepStrictEqual(targets, {
174 bootstrapProjectId,
175 bootstrapThreadId,
176 });
177 assert.deepStrictEqual(yield* Ref.get(dispatchCalls), []);
178 });
179});
180​
181it.effect("resolveAutoBootstrapWelcomeTargets creates a project and thread when missing", () =>
182 Effect.gen(function* () {
183 const dispatchCalls = yield* Ref.make<ReadonlyArray<string>>([]);
184 const targets = yield* ServerRuntimeStartup.resolveAutoBootstrapWelcomeTargets.pipe(
185 Effect.provideService(ServerConfig.ServerConfig, {
186 cwd: "/tmp/startup-project",
187 autoBootstrapProjectFromCwd: true,
188 } as never),
189 Effect.provideService(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
190 getCommandReadModel: () => Effect.die("unused"),
191 getSnapshot: () => Effect.die("unused"),
192 getShellSnapshot: () => Effect.die("unused"),
193 getArchivedShellSnapshot: () => Effect.die("unused"),
194 getSnapshotSequence: () => Effect.die("unused"),
195 getCounts: () => Effect.die("unused"),
196 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
197 getProjectShellById: () => Effect.die("unused"),
198 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
199 getThreadCheckpointContext: () => Effect.succeed(Option.none()),
200 getFullThreadDiffContext: () => Effect.succeed(Option.none()),
201 getThreadShellById: () => Effect.die("unused"),
202 getThreadDetailById: () => Effect.die("unused"),
203 }),
204 Effect.provideService(OrchestrationEngine.OrchestrationEngineService, {
205 readEvents: () => Stream.empty,
206 dispatch: (command) =>
207 Ref.update(dispatchCalls, (calls) => [...calls, command.type]).pipe(
208 Effect.as({ sequence: 1 }),
209 ),
210 streamDomainEvents: Stream.empty,
211 } satisfies OrchestrationEngine.OrchestrationEngineService["Service"]),
212 Effect.provide(NodeServices.layer),
213 );
214​
215 assert.equal(typeof targets.bootstrapProjectId, "string");
216 assert.equal(typeof targets.bootstrapThreadId, "string");
217 assert.deepStrictEqual(yield* Ref.get(dispatchCalls), ["project.create", "thread.create"]);
218 }),
219);
220​
221it.effect("resolveAutoBootstrapWelcomeTargets preserves typed UUID generation failures", () =>
222 Effect.gen(function* () {
223 const crypto = yield* Crypto.Crypto;
224 const uuidError = PlatformError.systemError({
225 _tag: "Unknown",
226 module: "Crypto",
227 method: "randomUUIDv4",
228 description: "UUID generation unavailable",
229 });
230 const dispatchCalls = yield* Ref.make<ReadonlyArray<string>>([]);
231​
232 const error = yield* ServerRuntimeStartup.resolveAutoBootstrapWelcomeTargets.pipe(
233 Effect.provideService(ServerConfig.ServerConfig, {
234 cwd: "/tmp/startup-project",
235 autoBootstrapProjectFromCwd: true,
236 } as never),
237 Effect.provideService(ProjectionSnapshotQuery.ProjectionSnapshotQuery, {
238 getCommandReadModel: () => Effect.die("unused"),
239 getSnapshot: () => Effect.die("unused"),
240 getShellSnapshot: () => Effect.die("unused"),
241 getArchivedShellSnapshot: () => Effect.die("unused"),
242 getSnapshotSequence: () => Effect.die("unused"),
243 getCounts: () => Effect.die("unused"),
244 getActiveProjectByWorkspaceRoot: () => Effect.succeed(Option.none()),
245 getProjectShellById: () => Effect.die("unused"),
246 getFirstActiveThreadIdByProjectId: () => Effect.succeed(Option.none()),
247 getThreadCheckpointContext: () => Effect.succeed(Option.none()),
248 getFullThreadDiffContext: () => Effect.succeed(Option.none()),
249 getThreadShellById: () => Effect.die("unused"),
250 getThreadDetailById: () => Effect.die("unused"),
251 }),
252 Effect.provideService(OrchestrationEngine.OrchestrationEngineService, {
253 readEvents: () => Stream.empty,
254 dispatch: (command) =>
255 Ref.update(dispatchCalls, (calls) => [...calls, command.type]).pipe(
256 Effect.as({ sequence: 1 }),
257 ),
258 streamDomainEvents: Stream.empty,
259 } satisfies OrchestrationEngine.OrchestrationEngineService["Service"]),
260 Effect.provideService(Crypto.Crypto, {
261 ...crypto,
262 randomUUIDv4: Effect.fail(uuidError),
263 }),
264 Effect.flip,
265 );
266​
267 assert.strictEqual(error, uuidError);
268 assert.deepStrictEqual(yield* Ref.get(dispatchCalls), []);
269 }).pipe(Effect.provide(NodeServices.layer)),
270);