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

AddProjectToMachineDialog.browser.test.tsx

261 lines9.2 KB

1import { EnvironmentId, ProjectId, type ProjectCloneSnapshot } from "@t3tools/contracts";
2import { scopeProjectRef } from "@t3tools/client-runtime/environment";
3import { describe, expect, it, vi } from "vite-plus/test";
4import { page, userEvent } from "vite-plus/test/browser";
5​
6import { act, render } from "../../test/render";
7​
8const clone = vi.hoisted(() => vi.fn());
9const snapshots = vi.hoisted(() => vi.fn<() => ReadonlyArray<ProjectCloneSnapshot>>());
10​
11vi.mock(import("../lib/addProjectToMachine"), async (original) => ({
12 ...(await original()),
13 addProjectToMachine: clone,
14}));
15vi.mock(import("../state/projectClones"), async (original) => ({
16 ...(await original()),
17 // Clones land on the target machine only.
18 useEnvironmentProjectClones: (environmentId) =>
19 environmentId === "environment-2" ? snapshots() : [],
20}));
21​
22const project = vi.hoisted(() => {
23 let value: unknown = null;
24 const listeners = new Set<() => void>();
25 return {
26 // Each test starts from no project and no listeners left by an earlier test.
27 setup: (initial: unknown) => {
28 value = initial;
29 listeners.clear();
30 },
31 get: () => value,
32 set: (next: unknown) => {
33 value = next;
34 for (const listener of listeners) listener();
35 },
36 subscribe: (listener: () => void) => {
37 listeners.add(listener);
38 return () => listeners.delete(listener);
39 },
40 };
41});
42​
43vi.mock(import("../state/entities"), async (original) => {
44 const { useSyncExternalStore } = await import("react");
45 return {
46 ...(await original()),
47 readProject: (() => project.get()) as never,
48 subscribeProject: ((_ref: unknown, listener: (value: unknown) => void) =>
49 project.subscribe(() => listener(project.get()))) as never,
50 useProject: (() => useSyncExternalStore(project.subscribe, project.get)) as never,
51 };
52});
53vi.mock(import("../state/environments"), async (original) => ({
54 ...(await original()),
55 useEnvironments: (() => ({
56 environments: [
57 { environmentId: EnvironmentId.make("environment-2") },
58 {
59 environmentId: EnvironmentId.make("environment-3"),
60 serverConfig: {
61 environment: {
62 platform: { machine: "linux" },
63 capabilities: { ignoredFileListing: true, ignoredFileSearch: true },
64 },
65 },
66 },
67 ],
68 })) as never,
69}));
70vi.mock(import("../state/projects"), async (original) => {
71 const { AsyncResult, Atom } = await import("effect/reactivity");
72 const actual = await original();
73 const listing = Atom.make(
74 AsyncResult.success({ files: [{ path: ".env", byteLength: 12 }], truncated: true }),
75 );
76 return {
77 ...actual,
78 projectEnvironment: { ...actual.projectEnvironment, listIgnoredFiles: () => listing },
79 };
80});
81vi.mock(import("../hooks/useHandleNewThread"), async (original) => ({
82 ...(await original()),
83 useNewThreadHandler: (() => () => undefined) as never,
84}));
85vi.mock(import("../hooks/useAddProjectTargets"), async (original) => ({
86 ...(await original()),
87 useAddProjectTargets: () => [
88 {
89 environmentId: EnvironmentId.make("environment-2"),
90 label: "Remote machine",
91 machine: "linux",
92 },
93 ],
94}));
95vi.mock(import("../hooks/useLogicalProjectEnvironments"), async (original) => ({
96 ...(await original()),
97 useLogicalProjectEnvironments: (() => []) as never,
98}));
99​
100import {
101 AddProjectToMachineDialogHost,
102 openAddProjectToMachine,
103} from "./AddProjectToMachineDialog";
104import { ProjectCloneToastCoordinator } from "./ProjectCloneToastCoordinator";
105import { ToastProvider } from "./ui/toast";
106​
107const environmentId = EnvironmentId.make("environment-1");
108const projectRef = scopeProjectRef(environmentId, ProjectId.make("project-1"));
109const nextProjectRef = scopeProjectRef(environmentId, ProjectId.make("project-2"));
110// Only environment-3 can list ignored files.
111const searchableProjectRef = scopeProjectRef(
112 EnvironmentId.make("environment-3"),
113 ProjectId.make("project-3"),
114);
115const PROJECT = {
116 environmentId,
117 id: projectRef.projectId,
118 title: "Repo",
119 workspaceRoot: "/repo",
120 repositoryIdentity: null,
121};
122​
123function CloneDialogWithToasts() {
124 return (
125 <ToastProvider>
126 <ProjectCloneToastCoordinator />
127 <AddProjectToMachineDialogHost />
128 </ToastProvider>
129 );
131​
132describe("AddProjectToMachineDialog", () => {
133 it("keeps the replacement open after the dismissed clone finishes starting", async () => {
134 project.setup(PROJECT);
135 const reply = Promise.withResolvers<ProjectId>();
136 const requested = Promise.withResolvers<ProjectId>();
137 clone.mockImplementationOnce(({ projectId }: { projectId: ProjectId }) => {
138 requested.resolve(projectId);
139 return reply.promise;
140 });
141 snapshots.mockReturnValue([]);
142 await using screen = await render(<CloneDialogWithToasts />);
143​
144 await act(() => openAddProjectToMachine({ projectRef, targetEnvironmentId: null }));
145 const folder = screen.getByRole("textbox", { name: /^Folder on/ });
146 await folder.fill("/checkout");
147 await userEvent.keyboard("{Enter}");
148 await expect.element(screen.getByRole("button", { name: "Starting…" })).toBeDisabled();
149 const projectId = await requested.promise;
150 snapshots.mockReturnValue([
151 {
152 projectId,
153 remoteUrl: "https://example.com/repo.git",
154 destinationPath: "/checkout",
155 repository: null,
156 phase: "running",
157 stage: "connecting",
158 percent: null,
159 detail: null,
160 error: null,
161 startedAt: "2026-01-01T00:00:00.000Z",
162 endedAt: null,
163 sequence: 1,
164 },
165 ]);
166 await screen.rerender(<CloneDialogWithToasts />);
167 await userEvent.keyboard("{Escape}");
168​
169 await act(() => {
170 project.set({ ...PROJECT, id: nextProjectRef.projectId, title: "Next repo" });
171 openAddProjectToMachine({ projectRef: nextProjectRef, targetEnvironmentId: null });
172 });
173 await expect.element(screen.getByText(/Clone Next repo/)).toBeVisible();
174 const progress = page.getByText("Cloning checkout", { exact: true });
175 await expect.element(progress).not.toBeInTheDocument();
176 reply.resolve(projectId);
177 await expect.element(progress).toBeVisible();
178 await expect.element(screen.getByText(/Clone Next repo/)).toBeVisible();
179 });
180​
181 it("closes when its project goes away", async () => {
182 project.setup(PROJECT);
183 await using screen = await render(<AddProjectToMachineDialogHost />);
184​
185 await act(() => openAddProjectToMachine({ projectRef, targetEnvironmentId: null }));
186 await expect.element(screen.getByText("Add to another machine")).toBeInTheDocument();
187​
188 await act(() => project.set(null));
189 await act(() => project.set(PROJECT));
190 await expect.element(screen.getByText("Add to another machine")).not.toBeInTheDocument();
191 });
192​
193 it("does not reopen for a project that vanished before the dialog showed", async () => {
194 project.setup(PROJECT);
195 await using screen = await render(<AddProjectToMachineDialogHost />);
196​
197 await act(() => {
198 openAddProjectToMachine({ projectRef, targetEnvironmentId: null });
199 project.set(null);
200 });
201 await act(() => project.set(PROJECT));
202​
203 await expect.element(screen.getByText("Add to another machine")).not.toBeInTheDocument();
204 });
205​
206 it("clears the ignored file search on Escape before closing", async () => {
207 project.setup({
208 ...PROJECT,
209 environmentId: searchableProjectRef.environmentId,
210 id: searchableProjectRef.projectId,
211 });
212 await using screen = await render(<AddProjectToMachineDialogHost />);
213​
214 await act(() =>
215 openAddProjectToMachine({ projectRef: searchableProjectRef, targetEnvironmentId: null }),
216 );
217 const file = screen.getByRole("checkbox", { name: /\.env/ });
218 await file.click();
219 await expect.element(file).not.toBeChecked();
220 const dialog = screen.getByRole("dialog", { name: "Add to another machine" });
221 const search = screen.getByRole("searchbox", { name: "Search ignored files" });
222 await search.fill("env");
223 for (const composition of [
224 { key: "Escape", isComposing: true, keyCode: 27 },
225 { key: "Escape", isComposing: false, keyCode: 229 },
226 ]) {
227 // Safari ends the composition just before the Escape keydown.
228 await act(() => {
229 search.element().dispatchEvent(new CompositionEvent("compositionend", { bubbles: true }));
230 search
231 .element()
232 .dispatchEvent(
233 new KeyboardEvent("keydown", { ...composition, bubbles: true, cancelable: true }),
234 );
235 });
236 await expect.element(search).toHaveValue("env");
237 await expect.element(dialog).toHaveAttribute("data-open");
238 }
239​
240 await userEvent.keyboard("{Escape}");
241 await expect.element(search).toHaveValue("");
242 await expect.element(search).toHaveFocus();
243 await expect.element(search).toHaveAttribute("type", "text");
244 await expect.element(file).not.toBeChecked();
245 // Holding Escape repeats the keydown, which must not also close the dialog.
246 await act(() => {
247 search.element().dispatchEvent(
248 new KeyboardEvent("keydown", {
249 key: "Escape",
250 repeat: true,
251 bubbles: true,
252 cancelable: true,
253 }),
254 );
255 });
256 await expect.element(dialog).toHaveAttribute("data-open");
257​
258 await userEvent.keyboard("{Escape}");
259 await expect.element(dialog).not.toBeInTheDocument();
260 });
261});