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

serverRuntimeStartup.test.ts

166 lines5.7 KB

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