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

serverRuntimeStartup.test.ts

187 lines6.2 KB

1import { assert, it } from "@effect/vitest";
2import {
3 DEFAULT_SERVER_SETTINGS,
4 DEFAULT_MODEL,
5 ProjectId,
6 ProviderInstanceId,
7} from "@t3tools/contracts";
8import * as Deferred from "effect/Deferred";
9import * as Effect from "effect/Effect";
10import * as Exit from "effect/Exit";
11import * as Fiber from "effect/Fiber";
12import * as Ref from "effect/Ref";
13​
14import * as GitVcsDriver from "./vcs/GitVcsDriver.ts";
15​
16import * as ServerConfig from "./config.ts";
17import * as ServerRuntimeStartup from "./serverRuntimeStartup.ts";
18​
19it("uses the canonical Codex model for auto-bootstrap", () => {
20 assert.deepEqual(ServerRuntimeStartup.getAutoBootstrapThreadModelSelection(), {
21 instanceId: ProviderInstanceId.make("codex"),
22 model: DEFAULT_MODEL,
23 });
24});
25​
26it.effect("starts without scanning or rebuilding projection history", () =>
27 Effect.gen(function* () {
28 const calls = yield* Ref.make<ReadonlyArray<string>>([]);
29 const record = (label: string) => Ref.update(calls, (current) => [...current, label]);
30​
31 const result = yield* ServerRuntimeStartup.runOrderedV2StartupPhases({
32 importLegacyShells: record("import"),
33 recover: record("recover").pipe(Effect.as({ closedRequests: 2 })),
34 startEffectWorker: record("worker"),
35 autoBootstrap: record("bootstrap").pipe(Effect.as({ projectId: "project-1" })),
36 });
37​
38 assert.deepEqual(yield* Ref.get(calls), ["import", "recover", "worker", "bootstrap"]);
39 assert.deepEqual(result, {
40 recovery: { closedRequests: 2 },
41 bootstrap: { projectId: "project-1" },
42 });
43 }),
44);
45​
46it.effect("interrupts the effect worker when awareness relay startup fails", () =>
47 Effect.scoped(
48 Effect.gen(function* () {
49 const workerInterrupted = yield* Ref.make(false);
50 const workerFiberRef = yield* Ref.make<Fiber.Fiber<void, never> | null>(null);
51​
52 const exit = yield* ServerRuntimeStartup.startEffectWorkerWithRelay({
53 runWorker: Effect.never.pipe(Effect.ensuring(Ref.set(workerInterrupted, true))),
54 startRelay: Effect.yieldNow.pipe(
55 Effect.andThen(Effect.die("awareness relay startup failed")),
56 ),
57 workerFiberRef,
58 }).pipe(Effect.exit);
59​
60 assert.isTrue(Exit.isFailure(exit));
61 assert.isTrue(yield* Ref.get(workerInterrupted));
62 assert.isNull(yield* Ref.get(workerFiberRef));
63 }),
64 ),
65);
66​
67it.effect("queues commands until startup signals readiness", () =>
68 Effect.scoped(
69 Effect.gen(function* () {
70 const gate = yield* ServerRuntimeStartup.makeCommandGate;
71 const count = yield* Ref.make(0);
72 const queued = yield* gate
73 .enqueueCommand(Ref.updateAndGet(count, (value) => value + 1))
74 .pipe(Effect.forkScoped);
75​
76 yield* Effect.yieldNow;
77 assert.equal(yield* Ref.get(count), 0);
78 yield* gate.signalCommandReady;
79 assert.equal(yield* Fiber.join(queued), 1);
80 }),
81 ),
82);
83​
84it.effect("enqueueCommand fails queued work when readiness fails", () =>
85 Effect.scoped(
86 Effect.gen(function* () {
87 const commandGate = yield* ServerRuntimeStartup.makeCommandGate;
88 const failure = yield* Deferred.make<void, never>();
89​
90 const queuedCommandFiber = yield* commandGate
91 .enqueueCommand(Deferred.await(failure).pipe(Effect.as("should-not-run")))
92 .pipe(Effect.forkScoped);
93​
94 yield* commandGate.failCommandReady(
95 new ServerRuntimeStartup.ServerRuntimeStartupError({
96 mode: "web",
97 host: "127.0.0.1",
98 port: 3773,
99 cause: new Error("test startup failure"),
100 }),
101 );
102​
103 const error = yield* Effect.flip(Fiber.join(queuedCommandFiber));
104 assert.equal(error.message, "Server runtime startup failed before command readiness.");
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("automatic pull only updates enabled, behind, clean default-branch checkouts", () =>
125 Effect.gen(function* () {
126 const pulled: string[] = [];
127 const git = {
128 statusDetails: (cwd: string) =>
129 Effect.succeed({
130 isRepo: true,
131 isDefaultBranch: cwd !== "/feature",
132 hasUpstream: true,
133 hasWorkingTreeChanges: cwd === "/dirty",
134 aheadCount: cwd === "/ahead" ? 1 : 0,
135 behindCount: cwd === "/current" ? 0 : 1,
136 } as never),
137 pullCurrentBranch: (cwd: string) =>
138 Effect.sync(() => {
139 pulled.push(cwd);
140 return {
141 status: "pulled" as const,
142 refName: "main",
143 upstreamRef: "origin/main",
144 };
145 }),
146 } as unknown as GitVcsDriver.GitVcsDriver["Service"];
147 const project = (workspaceRoot: string) =>
148 ({ id: ProjectId.make(workspaceRoot), workspaceRoot }) as never;
149 const overrides = (entries: Record<string, boolean>) => ({
150 ...DEFAULT_SERVER_SETTINGS,
151 projectSettingsOverrides: Object.fromEntries(
152 Object.entries(entries).map(([root, defaultAutoPull]) => [
153 ProjectId.make(root),
154 { defaultAutoPull },
155 ]),
156 ),
157 });
158​
159 yield* ServerRuntimeStartup.autoPullProjects(
160 [
161 project("/clean"),
162 project("/current"),
163 project("/dirty"),
164 project("/ahead"),
165 project("/feature"),
166 project("/disabled"),
167 ],
168 overrides({
169 "/clean": true,
170 "/current": true,
171 "/dirty": true,
172 "/ahead": true,
173 "/feature": true,
174 "/disabled": false,
175 }),
176 ).pipe(Effect.provideService(GitVcsDriver.GitVcsDriver, git));
177​
178 assert.deepStrictEqual(pulled, ["/clean"]);
179​
180 pulled.length = 0;
181 yield* ServerRuntimeStartup.autoPullProjects(
182 [project("/inherited"), project("/opted-out"), project("/dirty")],
183 { ...overrides({ "/opted-out": false }), defaultAutoPull: true },
184 ).pipe(Effect.provideService(GitVcsDriver.GitVcsDriver, git));
185 assert.deepStrictEqual(pulled, ["/inherited"]);
186 }),
187);