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

serverRuntimeStartup.test.ts

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