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

WelcomeWizard.test.tsx

214 lines7.1 KB

1// @vitest-environment jsdom
2import { act } from "react";
3import { createRoot, type Root } from "react-dom/client";
4import { EnvironmentId, ProjectId } from "@t3tools/contracts";
5import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
6​
7const mocks = vi.hoisted(() => ({
8 importThreads: vi.fn(),
9 createProject: vi.fn(),
10 complete: vi.fn(),
11 refresh: vi.fn(),
12 toast: vi.fn(),
13 projects: [] as Array<{ id: string; environmentId: string; workspaceRoot: string }>,
14}));
15vi.mock("../../state/agentSessions", () => ({ agentSessionImport: "import" }));
16vi.mock("../../state/projects", () => ({ projectEnvironment: { create: "create" } }));
17vi.mock("../../state/use-atom-command", () => ({
18 useAtomCommand: (command: string) =>
19 command === "import"
20 ? mocks.importThreads
21 : command === "create"
22 ? mocks.createProject
23 : mocks.refresh,
24}));
25vi.mock("../../onboarding/firstRun", () => ({ useCompleteOnboarding: () => mocks.complete }));
26vi.mock("../../state/entities", () => ({
27 useProjects: () => mocks.projects,
28 readProjects: () => mocks.projects,
29}));
30vi.mock("../../state/environments", () => {
31 const environment = {
32 environmentId: "test-env",
33 label: "Computer",
34 connection: { phase: "connected" },
35 };
36 return {
37 useEnvironments: () => ({ environments: [environment] }),
38 usePrimaryEnvironment: () => environment,
39 };
40});
41vi.mock("../../state/server", () => ({
42 serverEnvironment: {
43 providersValueAtom: () => [],
44 configValueAtom: () => null,
45 refreshProviders: "refresh",
46 },
47}));
48vi.mock("@effect/atom-react", () => ({ useAtomValue: (value: unknown) => value }));
49vi.mock("../../onboarding/useProjectScans", () => ({
50 useProjectScans: () => [
51 {
52 environmentId: "test-env",
53 isPending: false,
54 error: null,
55 refresh: mocks.refresh,
56 data: {
57 truncated: false,
58 candidates: [
59 {
60 path: "/project",
61 title: "project",
62 projectId: "test-project",
63 threadCount: 29,
64 lastActiveAt: new Date().toISOString(),
65 sources: ["codex"],
66 },
67 ],
68 },
69 },
70 ],
71}));
72vi.mock("../../connection/onboarding", () => ({ connectPairing: vi.fn() }));
73vi.mock("../../state/terminal", () => ({ terminalEnvironment: {} }));
74vi.mock("../clerk/useT3ConnectAuthPrompt", () => ({ useT3ConnectAuthPrompt: vi.fn() }));
75vi.mock("../../cloud/publicConfig", () => ({ hasCloudPublicConfig: () => false }));
76vi.mock("../ThreadTerminalDrawer", () => ({ TerminalViewport: () => null }));
77vi.mock("../settings/ChatGptWelcomeCoordinator", () => ({ ChatGptWelcomeCoordinator: () => null }));
78vi.mock("../settings/CodexSetupSection", () => ({
79 CodexSetupSection: () => null,
80 AddManagedCodexAccountDialog: () => null,
81}));
82vi.mock("../cloud/CloudEnvironmentConnectList", () => ({
83 CloudEnvironmentConnectRows: () => null,
84}));
85vi.mock("../ui/toast", () => ({
86 toastManager: { add: mocks.toast, close: vi.fn(), update: vi.fn() },
87}));
88​
89import { WelcomeWizard } from "./WelcomeWizard";
90​
91let root: Root;
92let container: HTMLDivElement;
93​
94beforeEach(() => {
95 vi.clearAllMocks();
96 vi.stubGlobal(
97 "ResizeObserver",
98 class {
99 observe() {}
100 unobserve() {}
101 disconnect() {}
102 },
103 );
104 Object.defineProperty(Element.prototype, "getAnimations", {
105 configurable: true,
106 value: () => [],
107 });
108 mocks.projects = [{ id: "test-project", environmentId: "test-env", workspaceRoot: "/project" }];
109 mocks.complete.mockResolvedValue(undefined);
110 mocks.refresh.mockResolvedValue(undefined);
111 mocks.importThreads.mockResolvedValue({
112 _tag: "Success",
113 value: { importedCount: 28, skippedCount: 1 },
114 });
115 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
116 container = document.createElement("div");
117 document.body.append(container);
118 root = createRoot(container);
119});
120afterEach(async () => {
121 await act(async () => root.unmount());
122 container.remove();
123});
124​
125async function click(label: string) {
126 const button = [...document.querySelectorAll("button")].find(
127 (element) => element.textContent?.trim() === label,
128 );
129 expect(button, `button ${label}`).toBeDefined();
130 await act(async () => button!.click());
132​
133it("enters the workspace after a partial import and warns after navigation finishes", async () => {
134 let finishNavigation = () => {};
135 const navigation = new Promise<void>((resolve) => {
136 finishNavigation = resolve;
137 });
138 const onDone = vi.fn(() => navigation);
139 await act(async () => root.render(<WelcomeWizard localAvailable onDone={onDone} />));
140 await click("Continue");
141 await click("Continue");
142 await click("Import 1 project");
143 expect(onDone).toHaveBeenCalledWith({
144 environmentId: EnvironmentId.make("test-env"),
145 projectId: ProjectId.make("test-project"),
146 });
147 expect(mocks.toast).not.toHaveBeenCalled();
148 await act(async () => finishNavigation());
149 expect(mocks.toast).toHaveBeenCalledWith(
150 expect.objectContaining({
151 type: "warning",
152 description: "Imported 28 threads. 1 thread could not be imported.",
153 }),
154 );
155 expect(mocks.toast.mock.invocationCallOrder[0]).toBeGreaterThan(
156 onDone.mock.invocationCallOrder[0]!,
157 );
158});
159​
160it.each([
161 [0, 0, null],
162 [29, 0, null],
163 [1, 0, null],
164 [0, 1, "1 thread could not be imported."],
165 [0, 2, "2 threads could not be imported."],
166] as const)(
167 "finishes setup with %i imported and %i skipped threads",
168 async (importedCount, skippedCount, warning) => {
169 mocks.importThreads.mockResolvedValue({
170 _tag: "Success",
171 value: { importedCount, skippedCount },
172 });
173 const onDone = vi.fn();
174 await act(async () => root.render(<WelcomeWizard localAvailable onDone={onDone} />));
175 await click("Continue");
176 await click("Continue");
177 await click("Import 1 project");
178 expect(onDone).toHaveBeenCalledOnce();
179 if (warning === null && importedCount > 0) {
180 expect(mocks.toast).toHaveBeenCalledWith({
181 type: "success",
182 title: `Imported ${importedCount} ${importedCount === 1 ? "thread" : "threads"}`,
183 });
184 } else if (warning === null) {
185 expect(mocks.toast).not.toHaveBeenCalled();
186 } else {
187 expect(mocks.toast).toHaveBeenCalledWith(
188 expect.objectContaining({ type: "warning", description: warning }),
189 );
190 }
191 },
192);
193​
194it("keeps setup open when saving completion fails and preserves the import warning on retry", async () => {
195 mocks.complete.mockRejectedValueOnce(new Error("settings unavailable"));
196 const onDone = vi.fn();
197 await act(async () => root.render(<WelcomeWizard localAvailable onDone={onDone} />));
198 await click("Continue");
199 await click("Continue");
200 await click("Import 1 project");
201 expect(onDone).not.toHaveBeenCalled();
202 expect(mocks.toast).toHaveBeenCalledWith(
203 expect.objectContaining({ type: "error", title: "Could not finish setup" }),
204 );
205 await click("Do not import projects");
206 expect(onDone).toHaveBeenCalledOnce();
207 expect(mocks.importThreads).toHaveBeenCalledOnce();
208 expect(mocks.toast).toHaveBeenLastCalledWith(
209 expect.objectContaining({
210 type: "warning",
211 description: "Imported 28 threads. 1 thread could not be imported.",
212 }),
213 );
214});