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
324const freshBootstrap = {
325 primaryEnvironmentId,
326 serverCwd: "/projects/current",
327 bootstrapProjectId: bootstrapProject.id,
328 bootstrapThreadId: bootstrapThread.id,
329 bootstrapProjectCreated: true,
330 bootstrapThreadCreated: true,
331 projects: [bootstrapProject],
332 threads: [bootstrapThread],
333};
334
335describe("isFreshFirstRunWorkspace", () => {
336 it("accepts an empty workspace", () => {
337 expect(
338 isFreshFirstRunWorkspace({
339 primaryEnvironmentId,
340 serverCwd: "/projects/current",
341 projects: [],
342 threads: [],
343 }),
344 ).toBe(true);
345 });
346
347 it("accepts only the unused project and thread created from the server cwd", () => {
348 expect(
349 isFreshFirstRunWorkspace({
350 primaryEnvironmentId,
351 serverCwd: "/projects/current/",
352 bootstrapProjectId: bootstrapProject.id,
353 bootstrapThreadId: bootstrapThread.id,
354 bootstrapProjectCreated: true,
355 bootstrapThreadCreated: true,
356 projects: [bootstrapProject],
357 threads: [bootstrapThread],
358 }),
359 ).toBe(true);
360 });
361
362 it("rejects an existing unused cwd project and thread reused by startup", () => {
363 expect(
364 isFreshFirstRunWorkspace({
365 primaryEnvironmentId,
366 serverCwd: "/projects/current",
367 bootstrapProjectId: bootstrapProject.id,
368 bootstrapThreadId: bootstrapThread.id,
369 bootstrapProjectCreated: false,
370 bootstrapThreadCreated: false,
371 projects: [bootstrapProject],
372 threads: [bootstrapThread],
373 }),
374 ).toBe(false);
375 });
376
377 it("rejects a nonempty workspace when an older server omits creation provenance", () => {
378 expect(
379 isFreshFirstRunWorkspace({
380 primaryEnvironmentId,
381 serverCwd: "/projects/current",
382 bootstrapProjectId: bootstrapProject.id,
383 bootstrapThreadId: bootstrapThread.id,
384 projects: [bootstrapProject],
385 threads: [bootstrapThread],
386 }),
387 ).toBe(false);
388 });
389
390 it("normalizes Windows project paths before checking the bootstrap workspace", () => {
391 expect(
392 isFreshFirstRunWorkspace({
393 primaryEnvironmentId,
394 serverCwd: "C:\\Projects\\Current\\",
395 bootstrapProjectId: bootstrapProject.id,
396 bootstrapThreadId: bootstrapThread.id,
397 bootstrapProjectCreated: true,
398 bootstrapThreadCreated: true,
399 projects: [{ ...bootstrapProject, workspaceRoot: "c:/projects/current" }],
400 threads: [bootstrapThread],
401 }),
402 ).toBe(true);
403 });
404
405 it("rejects projects from another environment even when their paths match", () => {
406 expect(
407 isFreshFirstRunWorkspace({
408 ...freshBootstrap,
409 projects: [{ ...bootstrapProject, environmentId: "remote-environment" }],
410 threads: [],
411 }),
412 ).toBe(false);
413 });
414
415 it("rejects threads from another environment", () => {
416 expect(
417 isFreshFirstRunWorkspace({
418 ...freshBootstrap,
419 projects: [bootstrapProject],
420 threads: [{ ...bootstrapThread, environmentId: "remote-environment" }],
421 }),
422 ).toBe(false);
423 });
424
425 it("rejects a thread that does not belong to the bootstrap project", () => {
426 expect(
427 isFreshFirstRunWorkspace({
428 ...freshBootstrap,
429 projects: [bootstrapProject],
430 threads: [{ ...bootstrapThread, projectId: "another-project" }],
431 }),
432 ).toBe(false);
433 });
434
435 it("rejects a thread when there is no bootstrap project", () => {
436 expect(
437 isFreshFirstRunWorkspace({
438 ...freshBootstrap,
439 projects: [],
440 threads: [bootstrapThread],
441 }),
442 ).toBe(false);
443 });
444
445 it("rejects a bootstrap thread that already has a user message", () => {
446 expect(
447 isFreshFirstRunWorkspace({
448 ...freshBootstrap,
449 projects: [bootstrapProject],
450 threads: [
451 {
452 ...bootstrapThread,
453 latestUserMessageAt: "2026-08-23T12:00:00.000Z",
454 },
455 ],
456 }),
457 ).toBe(false);
458 });
459
460 it("rejects a bootstrap thread that has started a turn", () => {
461 expect(
462 isFreshFirstRunWorkspace({
463 ...freshBootstrap,
464 projects: [bootstrapProject],
465 threads: [{ ...bootstrapThread, latestRun: { id: "first-run" } }],
466 }),
467 ).toBe(false);
468 });
469
470 it("rejects a bootstrap thread that has a provider session", () => {
471 expect(
472 isFreshFirstRunWorkspace({
473 ...freshBootstrap,
474 projects: [bootstrapProject],
475 threads: [{ ...bootstrapThread, runtime: { status: "ready" } }],
476 }),
477 ).toBe(false);
478 });
479});