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

firstRun.logic.test.ts

514 lines13.8 KB

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("keeps recovery visible while evidence remains pending", () => {
260 const state = { decision: "pending", stalled: true } as const;
261 expect(transitionFirstRunGateState(state, { type: "evidence", decision: "pending" })).toBe(
262 state,
263 );
264 });
265​
266 it("allows authoritative wizard evidence to replace an app decision", () => {
267 expect(
268 transitionFirstRunGateState(
269 { decision: "app", stalled: false },
270 { type: "evidence", decision: "wizard" },
271 ),
272 ).toEqual({ decision: "wizard", stalled: false });
273 });
274});
275​
276describe("resolveHostedFirstRunDecision", () => {
277 it("keeps the shell hidden until client settings are hydrated", () => {
278 expect(
279 resolveHostedFirstRunDecision({
280 hydrated: false,
281 completed: false,
282 catalogReady: true,
283 environmentCount: 0,
284 }),
285 ).toEqual({
286 decision: "pending",
287 persistCompletion: false,
288 });
289 });
290​
291 it("waits for the saved environment catalog before judging a hosted install", () => {
292 expect(
293 resolveHostedFirstRunDecision({
294 hydrated: true,
295 completed: false,
296 catalogReady: false,
297 environmentCount: 0,
298 }),
299 ).toEqual({
300 decision: "pending",
301 persistCompletion: false,
302 });
303 });
304​
305 it("opens onboarding when a hosted install has no saved environments", () => {
306 expect(
307 resolveHostedFirstRunDecision({
308 hydrated: true,
309 completed: false,
310 catalogReady: true,
311 environmentCount: 0,
312 }),
313 ).toEqual({
314 decision: "wizard",
315 persistCompletion: false,
316 });
317 });
318​
319 it("backfills onboarding for a hosted install with saved environments", () => {
320 expect(
321 resolveHostedFirstRunDecision({
322 hydrated: true,
323 completed: false,
324 catalogReady: true,
325 environmentCount: 1,
326 }),
327 ).toEqual({
328 decision: "app",
329 persistCompletion: true,
330 });
331 });
332​
333 it("opens the app immediately after hosted onboarding is complete", () => {
334 expect(
335 resolveHostedFirstRunDecision({
336 hydrated: true,
337 completed: true,
338 catalogReady: false,
339 environmentCount: 0,
340 }),
341 ).toEqual({
342 decision: "app",
343 persistCompletion: false,
344 });
345 });
346});
347​
348const primaryEnvironmentId = "primary-environment";
349const bootstrapProject = {
350 id: "bootstrap-project",
351 environmentId: primaryEnvironmentId,
352 workspaceRoot: "/projects/current",
353};
354const bootstrapThread = {
355 id: "bootstrap-thread",
356 projectId: bootstrapProject.id,
357 environmentId: primaryEnvironmentId,
358 latestRun: null,
359 latestUserMessageAt: null,
360 runtime: null,
361};
362​
363describe("isFreshFirstRunWorkspace", () => {
364 it("accepts an empty workspace", () => {
365 expect(
366 isFreshFirstRunWorkspace({
367 primaryEnvironmentId,
368 serverCwd: "/projects/current",
369 projects: [],
370 threads: [],
371 }),
372 ).toBe(true);
373 });
374​
375 it("accepts only the unused project and thread created from the server cwd", () => {
376 expect(
377 isFreshFirstRunWorkspace({
378 primaryEnvironmentId,
379 serverCwd: "/projects/current/",
380 bootstrapProjectId: bootstrapProject.id,
381 bootstrapThreadId: bootstrapThread.id,
382 bootstrapProjectCreated: true,
383 bootstrapThreadCreated: true,
384 projects: [bootstrapProject],
385 threads: [bootstrapThread],
386 }),
387 ).toBe(true);
388 });
389​
390 it("rejects an existing unused cwd project and thread reused by startup", () => {
391 expect(
392 isFreshFirstRunWorkspace({
393 primaryEnvironmentId,
394 serverCwd: "/projects/current",
395 bootstrapProjectId: bootstrapProject.id,
396 bootstrapThreadId: bootstrapThread.id,
397 bootstrapProjectCreated: false,
398 bootstrapThreadCreated: false,
399 projects: [bootstrapProject],
400 threads: [bootstrapThread],
401 }),
402 ).toBe(false);
403 });
404​
405 it("rejects a nonempty workspace when an older server omits creation provenance", () => {
406 expect(
407 isFreshFirstRunWorkspace({
408 primaryEnvironmentId,
409 serverCwd: "/projects/current",
410 bootstrapProjectId: bootstrapProject.id,
411 bootstrapThreadId: bootstrapThread.id,
412 projects: [bootstrapProject],
413 threads: [bootstrapThread],
414 }),
415 ).toBe(false);
416 });
417​
418 it("normalizes Windows project paths before checking the bootstrap workspace", () => {
419 expect(
420 isFreshFirstRunWorkspace({
421 primaryEnvironmentId,
422 serverCwd: "C:\\Projects\\Current\\",
423 bootstrapProjectId: bootstrapProject.id,
424 bootstrapThreadId: bootstrapThread.id,
425 bootstrapProjectCreated: true,
426 bootstrapThreadCreated: true,
427 projects: [{ ...bootstrapProject, workspaceRoot: "c:/projects/current" }],
428 threads: [bootstrapThread],
429 }),
430 ).toBe(true);
431 });
432​
433 it("rejects projects from another environment even when their paths match", () => {
434 expect(
435 isFreshFirstRunWorkspace({
436 primaryEnvironmentId,
437 serverCwd: "/projects/current",
438 projects: [{ ...bootstrapProject, environmentId: "remote-environment" }],
439 threads: [],
440 }),
441 ).toBe(false);
442 });
443​
444 it("rejects threads from another environment", () => {
445 expect(
446 isFreshFirstRunWorkspace({
447 primaryEnvironmentId,
448 serverCwd: "/projects/current",
449 projects: [bootstrapProject],
450 threads: [{ ...bootstrapThread, environmentId: "remote-environment" }],
451 }),
452 ).toBe(false);
453 });
454​
455 it("rejects a thread that does not belong to the bootstrap project", () => {
456 expect(
457 isFreshFirstRunWorkspace({
458 primaryEnvironmentId,
459 serverCwd: "/projects/current",
460 projects: [bootstrapProject],
461 threads: [{ ...bootstrapThread, projectId: "another-project" }],
462 }),
463 ).toBe(false);
464 });
465​
466 it("rejects a thread when there is no bootstrap project", () => {
467 expect(
468 isFreshFirstRunWorkspace({
469 primaryEnvironmentId,
470 serverCwd: "/projects/current",
471 projects: [],
472 threads: [bootstrapThread],
473 }),
474 ).toBe(false);
475 });
476​
477 it("rejects a bootstrap thread that already has a user message", () => {
478 expect(
479 isFreshFirstRunWorkspace({
480 primaryEnvironmentId,
481 serverCwd: "/projects/current",
482 projects: [bootstrapProject],
483 threads: [
484 {
485 ...bootstrapThread,
486 latestUserMessageAt: "2026-08-23T12:00:00.000Z",
487 },
488 ],
489 }),
490 ).toBe(false);
491 });
492​
493 it("rejects a bootstrap thread that has started a turn", () => {
494 expect(
495 isFreshFirstRunWorkspace({
496 primaryEnvironmentId,
497 serverCwd: "/projects/current",
498 projects: [bootstrapProject],
499 threads: [{ ...bootstrapThread, latestRun: { id: "first-run" } }],
500 }),
501 ).toBe(false);
502 });
503​
504 it("rejects a bootstrap thread that has a provider session", () => {
505 expect(
506 isFreshFirstRunWorkspace({
507 primaryEnvironmentId,
508 serverCwd: "/projects/current",
509 projects: [bootstrapProject],
510 threads: [{ ...bootstrapThread, runtime: { status: "ready" } }],
511 }),
512 ).toBe(false);
513 });
514});