Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 isFirstRunWorkspaceProvenanceAuthoritative,
5 isFreshFirstRunWorkspace,
6 resolveFirstRunDecision,
7 resolveHostedFirstRunDecision,
8 transitionFirstRunGateState,
9} from "./firstRun.logic";
10
11const freshWorkspace = {
12 enabled: true,
13 hydrated: true,
14 completed: false,
15 bootstrapped: true,
16 authoritative: true,
17 workspaceAuthoritative: true,
18 workspaceProvenanceAuthoritative: true,
19 catalogReady: true,
20 serverConfigAvailable: true,
21 workspaceFresh: true,
22 projectCount: 1,
23 threadCount: 1,
24} as const;
25
26describe("resolveFirstRunDecision", () => {
27 it("opens the wizard for an authoritative fresh workspace", () => {
28 expect(resolveFirstRunDecision(freshWorkspace)).toEqual({
29 decision: "wizard",
30 persistCompletion: false,
31 });
32 });
33
34 it("does not permanently complete onboarding from cached project counts", () => {
35 expect(
36 resolveFirstRunDecision({
37 ...freshWorkspace,
38 authoritative: false,
39 projectCount: 3,
40 workspaceFresh: false,
41 }),
42 ).toEqual({
43 decision: "app",
44 persistCompletion: false,
45 });
46 });
47
48 it("backfills completion once existing projects are confirmed by the server", () => {
49 expect(
50 resolveFirstRunDecision({
51 ...freshWorkspace,
52 projectCount: 3,
53 workspaceFresh: false,
54 }),
55 ).toEqual({
56 decision: "app",
57 persistCompletion: true,
58 });
59 });
60
61 it("does not complete onboarding while another environment is still bootstrapping", () => {
62 expect(
63 resolveFirstRunDecision({
64 ...freshWorkspace,
65 bootstrapped: false,
66 projectCount: 3,
67 workspaceFresh: false,
68 }),
69 ).toEqual({
70 decision: "app",
71 persistCompletion: false,
72 });
73 });
74
75 it("waits for managed environments before treating a workspace as new", () => {
76 expect(resolveFirstRunDecision({ ...freshWorkspace, catalogReady: false })).toEqual({
77 decision: "pending",
78 persistCompletion: false,
79 });
80 });
81
82 it("does not complete onboarding before the environment catalog is ready", () => {
83 expect(
84 resolveFirstRunDecision({
85 ...freshWorkspace,
86 catalogReady: false,
87 projectCount: 3,
88 workspaceFresh: false,
89 }),
90 ).toEqual({
91 decision: "app",
92 persistCompletion: false,
93 });
94 });
95
96 it("does not complete onboarding before the server configuration is available", () => {
97 expect(
98 resolveFirstRunDecision({
99 ...freshWorkspace,
100 projectCount: 3,
101 serverConfigAvailable: false,
102 workspaceFresh: false,
103 }),
104 ).toEqual({
105 decision: "app",
106 persistCompletion: false,
107 });
108 });
109
110 it("does not complete onboarding from cached remote projects", () => {
111 expect(
112 resolveFirstRunDecision({
113 ...freshWorkspace,
114 projectCount: 3,
115 workspaceAuthoritative: false,
116 workspaceFresh: false,
117 }),
118 ).toEqual({
119 decision: "app",
120 persistCompletion: false,
121 });
122 });
123
124 it("does not complete onboarding from a single cached remote project", () => {
125 expect(
126 resolveFirstRunDecision({
127 ...freshWorkspace,
128 workspaceAuthoritative: false,
129 workspaceFresh: false,
130 }),
131 ).toEqual({
132 decision: "app",
133 persistCompletion: false,
134 });
135 });
136
137 it("waits for live data before judging a single cached project", () => {
138 expect(
139 resolveFirstRunDecision({
140 ...freshWorkspace,
141 authoritative: false,
142 workspaceFresh: false,
143 }),
144 ).toEqual({
145 decision: "pending",
146 persistCompletion: false,
147 });
148 });
149
150 it("waits for the completed bootstrap welcome before judging a nonempty workspace", () => {
151 expect(
152 resolveFirstRunDecision({
153 ...freshWorkspace,
154 workspaceProvenanceAuthoritative: false,
155 }),
156 ).toEqual({
157 decision: "pending",
158 persistCompletion: false,
159 });
160 });
161
162 it("waits when the initial welcome is pending and opens the wizard after completion", () => {
163 const pendingProvenance = isFirstRunWorkspaceProvenanceAuthoritative({
164 welcomeReceived: true,
165 bootstrapStatus: "pending",
166 });
167 expect(
168 resolveFirstRunDecision({
169 ...freshWorkspace,
170 workspaceProvenanceAuthoritative: pendingProvenance,
171 }),
172 ).toEqual({ decision: "pending", persistCompletion: false });
173
174 const completedProvenance = isFirstRunWorkspaceProvenanceAuthoritative({
175 welcomeReceived: true,
176 bootstrapStatus: "complete",
177 });
178 expect(
179 resolveFirstRunDecision({
180 ...freshWorkspace,
181 workspaceProvenanceAuthoritative: completedProvenance,
182 }),
183 ).toEqual({ decision: "wizard", persistCompletion: false });
184 });
185
186 it("does not wait for server data after onboarding is already complete", () => {
187 expect(
188 resolveFirstRunDecision({
189 ...freshWorkspace,
190 authoritative: false,
191 bootstrapped: false,
192 completed: true,
193 serverConfigAvailable: false,
194 }),
195 ).toEqual({
196 decision: "app",
197 persistCompletion: false,
198 });
199 });
200});
201
202describe("isFirstRunWorkspaceProvenanceAuthoritative", () => {
203 it("waits for cwd bootstrap when the initial catalog is empty", () => {
204 expect(
205 isFirstRunWorkspaceProvenanceAuthoritative({
206 welcomeReceived: true,
207 bootstrapStatus: "pending",
208 }),
209 ).toBe(false);
210 });
211
212 it("accepts an empty catalog after cwd bootstrap completes", () => {
213 expect(
214 isFirstRunWorkspaceProvenanceAuthoritative({
215 welcomeReceived: true,
216 bootstrapStatus: "complete",
217 }),
218 ).toBe(true);
219 });
220
221 it("waits for a welcome before treating an empty catalog as final", () => {
222 expect(
223 isFirstRunWorkspaceProvenanceAuthoritative({
224 welcomeReceived: false,
225 bootstrapStatus: null,
226 }),
227 ).toBe(false);
228 });
229
230 it("accepts a legacy welcome without bootstrap status", () => {
231 expect(
232 isFirstRunWorkspaceProvenanceAuthoritative({
233 welcomeReceived: true,
234 bootstrapStatus: null,
235 }),
236 ).toBe(true);
237 });
238});
239
240describe("transitionFirstRunGateState", () => {
241 it("shows recovery without mounting the app when evidence stalls", () => {
242 expect(
243 transitionFirstRunGateState({ decision: "pending", stalled: false }, { type: "timeout" }),
244 ).toEqual({ decision: "pending", stalled: true });
245 });
246
247 it.each(["app", "wizard"] as const)(
248 "resolves stalled recovery to %s only after authoritative evidence",
249 (decision) => {
250 expect(
251 transitionFirstRunGateState(
252 { decision: "pending", stalled: true },
253 { type: "evidence", decision },
254 ),
255 ).toEqual({ decision, stalled: false });
256 },
257 );
258
259 it.each([
260 { decision: "pending", stalled: true },
261 { decision: "wizard", stalled: false },
262 ] as const)("retains $decision while evidence remains pending", (state) => {
263 expect(transitionFirstRunGateState(state, { type: "evidence", decision: "pending" })).toBe(
264 state,
265 );
266 });
267
268 it.each([
269 { previous: "app", workspaceFresh: true, decision: "wizard" },
270 { previous: "wizard", workspaceFresh: false, decision: "app" },
271 ] as const)(
272 "updates $previous to $decision when authoritative workspace evidence changes",
273 ({ previous, workspaceFresh, decision }) => {
274 const evidence = resolveFirstRunDecision({ ...freshWorkspace, workspaceFresh });
275 expect(
276 transitionFirstRunGateState(
277 { decision: previous, stalled: false },
278 { type: "evidence", decision: evidence.decision },
279 ),
280 ).toEqual({ decision, stalled: false });
281 },
282 );
283
284 it.each(["pending", "app", "wizard"] as const)(
285 "preserves state identity for unchanged %s evidence",
286 (decision) => {
287 const state = { decision, stalled: false };
288 expect(transitionFirstRunGateState(state, { type: "evidence", decision })).toBe(state);
289 },
290 );
291});
292
293describe("resolveHostedFirstRunDecision", () => {
294 it("keeps the shell hidden until client settings are hydrated", () => {
295 expect(resolveHostedFirstRunDecision(false)).toEqual({
296 decision: "pending",
297 persistCompletion: false,
298 });
299 });
300
301 it("opens the app without setup, even with no saved computers", () => {
302 expect(resolveHostedFirstRunDecision(true)).toEqual({
303 decision: "app",
304 persistCompletion: false,
305 });
306 });
307});
308
309const primaryEnvironmentId = "primary-environment";
310const bootstrapProject = {
311 id: "bootstrap-project",
312 environmentId: primaryEnvironmentId,
313 workspaceRoot: "/projects/current",
314};
315const bootstrapThread = {
316 id: "bootstrap-thread",
317 projectId: bootstrapProject.id,
318 environmentId: primaryEnvironmentId,
319 latestRun: null,
320 latestUserMessageAt: null,
321 runtime: null,
322};
323
324describe("isFreshFirstRunWorkspace", () => {
325 it("accepts an empty workspace", () => {
326 expect(
327 isFreshFirstRunWorkspace({
328 primaryEnvironmentId,
329 serverCwd: "/projects/current",
330 projects: [],
331 threads: [],
332 }),
333 ).toBe(true);
334 });
335
336 it("accepts only the unused project and thread created from the server cwd", () => {
337 expect(
338 isFreshFirstRunWorkspace({
339 primaryEnvironmentId,
340 serverCwd: "/projects/current/",
341 bootstrapProjectId: bootstrapProject.id,
342 bootstrapThreadId: bootstrapThread.id,
343 bootstrapProjectCreated: true,
344 bootstrapThreadCreated: true,
345 projects: [bootstrapProject],
346 threads: [bootstrapThread],
347 }),
348 ).toBe(true);
349 });
350
351 it("rejects an existing unused cwd project and thread reused by startup", () => {
352 expect(
353 isFreshFirstRunWorkspace({
354 primaryEnvironmentId,
355 serverCwd: "/projects/current",
356 bootstrapProjectId: bootstrapProject.id,
357 bootstrapThreadId: bootstrapThread.id,
358 bootstrapProjectCreated: false,
359 bootstrapThreadCreated: false,
360 projects: [bootstrapProject],
361 threads: [bootstrapThread],
362 }),
363 ).toBe(false);
364 });
365
366 it("rejects a nonempty workspace when an older server omits creation provenance", () => {
367 expect(
368 isFreshFirstRunWorkspace({
369 primaryEnvironmentId,
370 serverCwd: "/projects/current",
371 bootstrapProjectId: bootstrapProject.id,
372 bootstrapThreadId: bootstrapThread.id,
373 projects: [bootstrapProject],
374 threads: [bootstrapThread],
375 }),
376 ).toBe(false);
377 });
378
379 it("normalizes Windows project paths before checking the bootstrap workspace", () => {
380 expect(
381 isFreshFirstRunWorkspace({
382 primaryEnvironmentId,
383 serverCwd: "C:\\Projects\\Current\\",
384 bootstrapProjectId: bootstrapProject.id,
385 bootstrapThreadId: bootstrapThread.id,
386 bootstrapProjectCreated: true,
387 bootstrapThreadCreated: true,
388 projects: [{ ...bootstrapProject, workspaceRoot: "c:/projects/current" }],
389 threads: [bootstrapThread],
390 }),
391 ).toBe(true);
392 });
393
394 it("rejects projects from another environment even when their paths match", () => {
395 expect(
396 isFreshFirstRunWorkspace({
397 primaryEnvironmentId,
398 serverCwd: "/projects/current",
399 projects: [{ ...bootstrapProject, environmentId: "remote-environment" }],
400 threads: [],
401 }),
402 ).toBe(false);
403 });
404
405 it("rejects threads from another environment", () => {
406 expect(
407 isFreshFirstRunWorkspace({
408 primaryEnvironmentId,
409 serverCwd: "/projects/current",
410 projects: [bootstrapProject],
411 threads: [{ ...bootstrapThread, environmentId: "remote-environment" }],
412 }),
413 ).toBe(false);
414 });
415
416 it("rejects a thread that does not belong to the bootstrap project", () => {
417 expect(
418 isFreshFirstRunWorkspace({
419 primaryEnvironmentId,
420 serverCwd: "/projects/current",
421 projects: [bootstrapProject],
422 threads: [{ ...bootstrapThread, projectId: "another-project" }],
423 }),
424 ).toBe(false);
425 });
426
427 it("rejects a thread when there is no bootstrap project", () => {
428 expect(
429 isFreshFirstRunWorkspace({
430 primaryEnvironmentId,
431 serverCwd: "/projects/current",
432 projects: [],
433 threads: [bootstrapThread],
434 }),
435 ).toBe(false);
436 });
437
438 it("rejects a bootstrap thread that already has a user message", () => {
439 expect(
440 isFreshFirstRunWorkspace({
441 primaryEnvironmentId,
442 serverCwd: "/projects/current",
443 projects: [bootstrapProject],
444 threads: [
445 {
446 ...bootstrapThread,
447 latestUserMessageAt: "2026-08-23T12:00:00.000Z",
448 },
449 ],
450 }),
451 ).toBe(false);
452 });
453
454 it("rejects a bootstrap thread that has started a turn", () => {
455 expect(
456 isFreshFirstRunWorkspace({
457 primaryEnvironmentId,
458 serverCwd: "/projects/current",
459 projects: [bootstrapProject],
460 threads: [{ ...bootstrapThread, latestRun: { id: "first-run" } }],
461 }),
462 ).toBe(false);
463 });
464
465 it("rejects a bootstrap thread that has a provider session", () => {
466 expect(
467 isFreshFirstRunWorkspace({
468 primaryEnvironmentId,
469 serverCwd: "/projects/current",
470 projects: [bootstrapProject],
471 threads: [{ ...bootstrapThread, runtime: { status: "ready" } }],
472 }),
473 ).toBe(false);
474 });
475});