Skip to content

Commit 6b19f41

Browse files
committed
fix(server): cover slow project snapshots and simplify deadlines
1 parent b5e10b2 commit 6b19f41

2 files changed

Lines changed: 59 additions & 51 deletions

File tree

‎apps/server/src/cli/project.test.ts‎

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -128,50 +128,64 @@ it.effect("adds, renames, and removes projects through the V2 project CLI domain
128128
}).pipe(Effect.provide(NodeServices.layer)),
129129
);
130130

131-
it.effect("waits for a busy live server to apply a project change", () =>
132-
Effect.gen(function* () {
133-
const baseDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-live-project-cli-"));
134-
const workspaceRoot = NodeFS.mkdtempSync(
135-
NodePath.join(NodeOS.tmpdir(), "t3-live-project-workspace-"),
136-
);
137-
yield* persistServerRuntimeState({
138-
path: NodePath.join(baseDir, "userdata", "server-runtime.json"),
139-
state: yield* makePersistedServerRuntimeState({
140-
config: { host: "127.0.0.1", devUrl: undefined },
141-
port: 3_773,
142-
}),
143-
});
144-
const now = "2026-01-01T00:00:00.000Z";
145-
const mutateReceived = Promise.withResolvers<void>();
146-
const mutateResponse = Promise.withResolvers<Response>();
147-
const fetch: typeof globalThis.fetch = async (_input, init) => {
148-
if (init?.method !== "POST") return Response.json({ projects: [], updatedAt: now });
149-
mutateReceived.resolve();
150-
return mutateResponse.promise;
151-
};
131+
it.effect.each(["snapshot", "mutation"] as const)(
132+
"waits for a busy live server's %s response",
133+
(slowRequest) =>
134+
Effect.gen(function* () {
135+
const fs = yield* FileSystem.FileSystem;
136+
const root = yield* fs.makeTempDirectoryScoped({ prefix: "t3-live-project-cli-" });
137+
const baseDir = NodePath.join(root, "state");
138+
const workspaceRoot = NodePath.join(root, "workspace");
139+
yield* fs.makeDirectory(workspaceRoot);
140+
yield* persistServerRuntimeState({
141+
path: NodePath.join(baseDir, "userdata", "server-runtime.json"),
142+
state: yield* makePersistedServerRuntimeState({
143+
config: { host: "127.0.0.1", devUrl: undefined },
144+
port: 3_773,
145+
}),
146+
});
147+
const now = "2026-01-01T00:00:00.000Z";
148+
const requestReceived = Promise.withResolvers<void>();
149+
const releaseResponse = Promise.withResolvers<void>();
150+
let snapshotRequests = 0;
151+
let mutations = 0;
152+
const fetch: typeof globalThis.fetch = async (_input, init) => {
153+
const isMutation = init?.method === "POST";
154+
if (isMutation) mutations++;
155+
else snapshotRequests++;
156+
if (slowRequest === "mutation" ? isMutation : !isMutation && snapshotRequests === 2) {
157+
requestReceived.resolve();
158+
await releaseResponse.promise;
159+
}
160+
return Response.json(
161+
isMutation
162+
? {
163+
id: "live-project",
164+
title: NodePath.basename(workspaceRoot),
165+
workspaceRoot,
166+
defaultModelSelection: null,
167+
scripts: [],
168+
createdAt: now,
169+
updatedAt: now,
170+
deletedAt: null,
171+
}
172+
: { projects: [], updatedAt: now },
173+
);
174+
};
152175

153-
const add = yield* runCli(["project", "add", workspaceRoot, "--base-dir", baseDir]).pipe(
154-
Effect.provideService(FetchHttpClient.Fetch, fetch),
155-
Effect.forkChild,
156-
);
157-
yield* Effect.promise(() => mutateReceived.promise);
158-
yield* TestClock.adjust("1 second");
159-
mutateResponse.resolve(
160-
Response.json({
161-
id: "live-project",
162-
title: NodePath.basename(workspaceRoot),
163-
workspaceRoot,
164-
defaultModelSelection: null,
165-
scripts: [],
166-
createdAt: now,
167-
updatedAt: now,
168-
deletedAt: null,
169-
}),
170-
);
171-
yield* Fiber.join(add);
176+
const add = yield* runCli(["project", "add", workspaceRoot, "--base-dir", baseDir]).pipe(
177+
Effect.provideService(FetchHttpClient.Fetch, fetch),
178+
Effect.forkChild,
179+
);
180+
yield* Effect.promise(() => requestReceived.promise);
181+
yield* TestClock.adjust("2 seconds");
182+
releaseResponse.resolve();
183+
yield* Fiber.join(add);
172184

173-
assert.deepEqual((yield* readProjects(baseDir)).projects, []);
174-
}).pipe(Effect.provide(NodeServices.layer)),
185+
assert.equal(snapshotRequests, 2);
186+
assert.equal(mutations, 1);
187+
assert.deepEqual((yield* readProjects(baseDir)).projects, []);
188+
}).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
175189
);
176190

177191
const makeProjectLookupFixture = Effect.fn("ProjectCliTest.makeProjectLookupFixture")(function* () {

‎apps/server/src/cli/project.ts‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,6 @@ const layerProjectCliRuntime = RuntimeLayer.layerProjectService.pipe(
214214
Layer.provideMerge(SqlitePersistence.layerConfig),
215215
);
216216

217-
// The probe only picks live or offline mode. A server that answered it is
218-
// live, so the requests after it get longer to reply.
219217
const PROJECT_CLI_LIVE_SERVER_PROBE_TIMEOUT = Duration.seconds(1);
220218
const PROJECT_CLI_LIVE_SERVER_REQUEST_TIMEOUT = Duration.seconds(10);
221219
const withProjectCliSessionToken = <A, E, R>(
@@ -317,13 +315,13 @@ const findActiveProjectTarget = Effect.fn("findActiveProjectTarget")(function* (
317315
});
318316

319317
const fetchLiveOrchestrationSnapshot = Effect.fn("project.fetchLiveOrchestrationSnapshot")(
320-
function* (origin: string, bearerToken: string, _timeout: Duration.Duration) {
318+
function* (origin: string, bearerToken: string, _timeout?: Duration.Duration) {
321319
const client = yield* makeLiveServerClient(origin);
322320
return yield* client.projects.snapshot({
323321
headers: { authorization: `Bearer ${bearerToken}` },
324322
});
325323
},
326-
(effect, _origin, _bearerToken, timeout) =>
324+
(effect, _origin, _bearerToken, timeout = PROJECT_CLI_LIVE_SERVER_REQUEST_TIMEOUT) =>
327325
effect.pipe(Effect.timeout(timeout), Effect.mapError(projectCommandErrorFromLiveServerRequest)),
328326
);
329327

@@ -411,11 +409,7 @@ const runProjectMutation = Effect.fn("runProjectMutation")(function* (
411409
if (Option.isSome(liveMode)) {
412410
return yield* withProjectCliSessionToken(environmentAuth, (token) =>
413411
Effect.gen(function* () {
414-
const snapshot = yield* fetchLiveOrchestrationSnapshot(
415-
liveMode.value.origin,
416-
token,
417-
PROJECT_CLI_LIVE_SERVER_REQUEST_TIMEOUT,
418-
);
412+
const snapshot = yield* fetchLiveOrchestrationSnapshot(liveMode.value.origin, token);
419413
const output = yield* run({
420414
snapshot,
421415
dispatch: (command) =>

0 commit comments

Comments
 (0)