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

serverRuntimeStartup.test.ts

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