Open History to see the last commit
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 recoverDelegatedTasks: record("delegated"),
35 startEffectWorker: record("worker"),
36 autoBootstrap: record("bootstrap").pipe(Effect.as({ projectId: "project-1" })),
37 });
38
39 assert.deepEqual(yield* Ref.get(calls), [
40 "import",
41 "recover",
42 "delegated",
43 "worker",
44 "bootstrap",
45 ]);
46 assert.deepEqual(result, {
47 recovery: { closedRequests: 2 },
48 bootstrap: { projectId: "project-1" },
49 });
50 }),
51);
52
53it.effect("interrupts the effect worker when awareness relay startup fails", () =>
54 Effect.scoped(
55 Effect.gen(function* () {
56 const workerInterrupted = yield* Ref.make(false);
57 const workerFiberRef = yield* Ref.make<Fiber.Fiber<void, never> | null>(null);
58
59 const exit = yield* ServerRuntimeStartup.startEffectWorkerWithRelay({
60 runWorker: Effect.never.pipe(Effect.ensuring(Ref.set(workerInterrupted, true))),
61 startRelay: Effect.yieldNow.pipe(
62 Effect.andThen(Effect.die("awareness relay startup failed")),
63 ),
64 workerFiberRef,
65 }).pipe(Effect.exit);
66
67 assert.isTrue(Exit.isFailure(exit));
68 assert.isTrue(yield* Ref.get(workerInterrupted));
69 assert.isNull(yield* Ref.get(workerFiberRef));
70 }),
71 ),
72);
73
74it.effect("queues commands until startup signals readiness", () =>
75 Effect.scoped(
76 Effect.gen(function* () {
77 const gate = yield* ServerRuntimeStartup.makeCommandGate;
78 const count = yield* Ref.make(0);
79 const queued = yield* gate
80 .enqueueCommand(Ref.updateAndGet(count, (value) => value + 1))
81 .pipe(Effect.forkScoped);
82
83 yield* Effect.yieldNow;
84 assert.equal(yield* Ref.get(count), 0);
85 yield* gate.signalCommandReady;
86 assert.equal(yield* Fiber.join(queued), 1);
87 }),
88 ),
89);
90
91it.effect("enqueueCommand fails queued work when readiness fails", () =>
92 Effect.scoped(
93 Effect.gen(function* () {
94 const commandGate = yield* ServerRuntimeStartup.makeCommandGate;
95 const failure = yield* Deferred.make<void, never>();
96
97 const queuedCommandFiber = yield* commandGate
98 .enqueueCommand(Deferred.await(failure).pipe(Effect.as("should-not-run")))
99 .pipe(Effect.forkScoped);
100
101 yield* commandGate.failCommandReady(
102 new ServerRuntimeStartup.ServerRuntimeStartupError({
103 mode: "web",
104 host: "127.0.0.1",
105 port: 3773,
106 cause: new Error("test startup failure"),
107 }),
108 );
109
110 const error = yield* Effect.flip(Fiber.join(queuedCommandFiber));
111 assert.equal(error.message, "Server runtime startup failed before command readiness.");
112 }),
113 ),
114);
115
116it.effect("resolveWelcomeBase derives cwd and project name from server config", () =>
117 Effect.gen(function* () {
118 const welcome = yield* ServerRuntimeStartup.resolveWelcomeBase.pipe(
119 Effect.provideService(ServerConfig.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("automatic pull only updates enabled, behind, clean default-branch checkouts", () =>
132 Effect.gen(function* () {
133 const pulled: string[] = [];
134 const git = {
135 statusDetails: (cwd: string) =>
136 Effect.succeed({
137 isRepo: true,
138 isDefaultBranch: cwd !== "/feature",
139 hasUpstream: true,
140 hasWorkingTreeChanges: cwd === "/dirty",
141 aheadCount: cwd === "/ahead" ? 1 : 0,
142 behindCount: cwd === "/current" ? 0 : 1,
143 } as never),
144 pullCurrentBranch: (cwd: string) =>
145 Effect.sync(() => {
146 pulled.push(cwd);
147 return {
148 status: "pulled" as const,
149 refName: "main",
150 upstreamRef: "origin/main",
151 };
152 }),
153 } as unknown as GitVcsDriver.GitVcsDriver["Service"];
154 const project = (workspaceRoot: string) =>
155 ({ id: ProjectId.make(workspaceRoot), workspaceRoot }) as never;
156 const overrides = (entries: Record<string, boolean>) => ({
157 ...DEFAULT_SERVER_SETTINGS,
158 projectSettingsOverrides: Object.fromEntries(
159 Object.entries(entries).map(([root, defaultAutoPull]) => [
160 ProjectId.make(root),
161 { defaultAutoPull },
162 ]),
163 ),
164 });
165
166 yield* ServerRuntimeStartup.autoPullProjects(
167 [
168 project("/clean"),
169 project("/current"),
170 project("/dirty"),
171 project("/ahead"),
172 project("/feature"),
173 project("/disabled"),
174 ],
175 overrides({
176 "/clean": true,
177 "/current": true,
178 "/dirty": true,
179 "/ahead": true,
180 "/feature": true,
181 "/disabled": false,
182 }),
183 ).pipe(Effect.provideService(GitVcsDriver.GitVcsDriver, git));
184
185 assert.deepStrictEqual(pulled, ["/clean"]);
186
187 pulled.length = 0;
188 yield* ServerRuntimeStartup.autoPullProjects(
189 [project("/inherited"), project("/opted-out"), project("/dirty")],
190 { ...overrides({ "/opted-out": false }), defaultAutoPull: true },
191 ).pipe(Effect.provideService(GitVcsDriver.GitVcsDriver, git));
192 assert.deepStrictEqual(pulled, ["/inherited"]);
193 }),
194);