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

ServerUpdateAction.test.tsx

409 lines12.6 KB

1import { act, type ReactElement } from "react";
2import { create, type ReactTestRenderer } from "react-test-renderer";
3import { renderToStaticMarkup } from "react-dom/server";
4import type { EnvironmentId } from "@t3tools/contracts";
5import * as Cause from "effect/Cause";
6import { AsyncResult } from "effect/unstable/reactivity";
7import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
8​
9const testState = vi.hoisted(() => ({
10 updateServer: vi.fn(),
11 toast: vi.fn(),
12 continueThreadsAfterServerUpdate: false,
13}));
14​
15vi.mock("~/hooks/useCopyToClipboard", () => ({
16 useCopyToClipboard: () => ({ copyToClipboard: vi.fn() }),
17}));
18vi.mock("~/hooks/useSettings", () => ({
19 useEnvironmentSettings: (
20 _environmentId: EnvironmentId,
21 selector: (settings: { continueThreadsAfterServerUpdate: boolean }) => unknown,
22 ) => selector({ continueThreadsAfterServerUpdate: testState.continueThreadsAfterServerUpdate }),
23}));
24vi.mock("~/state/server", () => ({
25 serverEnvironment: { updateServer: Symbol("updateServer") },
26}));
27vi.mock("~/state/use-atom-command", () => ({
28 useAtomCommand: () => testState.updateServer,
29}));
30vi.mock("./ui/toast", () => ({
31 toastManager: { add: testState.toast },
32}));
33​
34import {
35 readConfirmDialogState,
36 registerConfirmDialogHost,
37 resetConfirmDialogForTests,
38 respondToConfirmDialog,
39} from "~/confirmDialog";
40import {
41 ServerUpdateAction,
42 ServerUpdateProgress,
43 ServerUpdatesAction,
44 type ServerUpdateTarget,
45} from "./ServerUpdateAction";
46​
47type ActionElement = ReactElement<{
48 readonly onClick?: () => void;
49}>;
50​
51function renderAction(): ActionElement {
52 return ServerUpdateAction({
53 environmentId: "env-test" as EnvironmentId,
54 serverLabel: "Test server",
55 selfUpdate: "boot-service",
56 targetVersion: "0.0.31",
57 }) as ActionElement;
59​
60async function flushPromises(): Promise<void> {
61 await Promise.resolve();
62 await Promise.resolve();
64​
65describe("ServerUpdateAction", () => {
66 beforeEach(() => {
67 testState.updateServer.mockReset();
68 testState.toast.mockReset();
69 testState.continueThreadsAfterServerUpdate = false;
70 });
71​
72 it("reports success only after the shared update flow reconnects", async () => {
73 testState.updateServer.mockResolvedValue(
74 AsyncResult.success({ targetVersion: "0.0.31", method: "boot-service" as const }),
75 );
76​
77 renderAction().props.onClick?.();
78 await flushPromises();
79​
80 expect(testState.updateServer).toHaveBeenCalledWith({
81 environmentId: "env-test",
82 input: { targetVersion: "0.0.31" },
83 });
84 expect(testState.toast).toHaveBeenCalledWith({
85 type: "success",
86 title: "Test server updated",
87 description: "Reconnected on t3@0.0.31.",
88 });
89 });
90​
91 it("reports one result when the update action is double-clicked", async () => {
92 let finishUpdate: (() => void) | undefined;
93 testState.updateServer.mockImplementation(
94 () =>
95 new Promise((resolve) => {
96 finishUpdate = () =>
97 resolve(
98 AsyncResult.success({ targetVersion: "0.0.31", method: "boot-service" as const }),
99 );
100 }),
101 );
102​
103 const action = renderAction();
104 action.props.onClick?.();
105 action.props.onClick?.();
106​
107 expect(testState.updateServer).toHaveBeenCalledTimes(1);
108 finishUpdate?.();
109 await flushPromises();
110 expect(testState.toast).toHaveBeenCalledTimes(1);
111 });
112​
113 it("quietly releases the action when the operation is interrupted", async () => {
114 testState.updateServer.mockResolvedValue(AsyncResult.failure(Cause.interrupt()));
115​
116 renderAction().props.onClick?.();
117 await flushPromises();
118​
119 expect(testState.toast).not.toHaveBeenCalled();
120 });
121​
122 it("keeps the manual instruction for desktop servers without remote update support", () => {
123 const markup = renderToStaticMarkup(
124 <ServerUpdateAction
125 environmentId={"env-test" as EnvironmentId}
126 serverLabel="Test server"
127 selfUpdate="desktop-managed"
128 targetVersion="0.0.31"
129 />,
130 );
131​
132 expect(markup).toContain("Update the desktop app on that machine to update this server.");
133 expect(markup).not.toContain("<button");
134 });
135​
136 it("updates remote desktop apps through the shared update flow", async () => {
137 testState.updateServer.mockResolvedValue(
138 AsyncResult.success({ targetVersion: "0.0.34", method: "desktop-app" as const }),
139 );
140​
141 const action = ServerUpdateAction({
142 environmentId: "env-test" as EnvironmentId,
143 serverLabel: "Test server",
144 selfUpdate: "desktop-managed",
145 desktopAppUpdate: true,
146 targetVersion: "0.0.31",
147 }) as ActionElement;
148​
149 // No confirm-dialog host is mounted in this test, which the component
150 // treats as consent: the click itself was the request.
151 action.props.onClick?.();
152 await flushPromises();
153​
154 expect(testState.updateServer).toHaveBeenCalledWith({
155 environmentId: "env-test",
156 input: { targetVersion: "0.0.31" },
157 });
158 expect(testState.toast).toHaveBeenCalledWith({
159 type: "success",
160 title: "Test server updated",
161 description: "Desktop app relaunched on 0.0.34.",
162 });
163 });
164​
165 it("leaves thread continuation off by default", async () => {
166 testState.updateServer.mockResolvedValue(
167 AsyncResult.success({ targetVersion: "0.0.31", method: "boot-service" as const }),
168 );
169 const action = ServerUpdateAction({
170 environmentId: "env-test" as EnvironmentId,
171 serverLabel: "Test server",
172 selfUpdate: "boot-service",
173 threadContinuation: true,
174 targetVersion: "0.0.31",
175 }) as ActionElement;
176​
177 action.props.onClick?.();
178 await flushPromises();
179​
180 expect(testState.updateServer).toHaveBeenCalledWith({
181 environmentId: "env-test",
182 input: { targetVersion: "0.0.31" },
183 });
184 });
185​
186 it("applies the saved thread continuation preference automatically", async () => {
187 testState.updateServer.mockResolvedValue(
188 AsyncResult.success({ targetVersion: "0.0.31", method: "boot-service" as const }),
189 );
190 testState.continueThreadsAfterServerUpdate = true;
191 const action = ServerUpdateAction({
192 environmentId: "env-test" as EnvironmentId,
193 serverLabel: "Test server",
194 selfUpdate: "boot-service",
195 threadContinuation: true,
196 targetVersion: "0.0.31",
197 }) as ActionElement;
198​
199 action.props.onClick?.();
200 await flushPromises();
201​
202 expect(testState.updateServer).toHaveBeenCalledWith({
203 environmentId: "env-test",
204 input: { targetVersion: "0.0.31", continueRunningThreads: true },
205 });
206 });
207});
208​
209describe("ServerUpdatesAction", () => {
210 let renderer: ReactTestRenderer | undefined;
211 const targets: ReadonlyArray<ServerUpdateTarget> = [
212 {
213 environmentId: "batch-a" as EnvironmentId,
214 serverLabel: "Laptop",
215 selfUpdate: "boot-service",
216 targetVersion: "0.0.31",
217 threadContinuation: true,
218 continueThreadsAfterServerUpdate: true,
219 },
220 {
221 environmentId: "batch-b" as EnvironmentId,
222 serverLabel: "Office",
223 selfUpdate: "respawn",
224 targetVersion: "0.0.31",
225 threadContinuation: true,
226 continueThreadsAfterServerUpdate: false,
227 },
228 {
229 environmentId: "batch-c" as EnvironmentId,
230 serverLabel: "Manual",
231 selfUpdate: null,
232 targetVersion: "0.0.31",
233 },
234 ];
235 const success = AsyncResult.success({ targetVersion: "0.0.31", method: "boot-service" as const });
236​
237 async function mount(batch = targets) {
238 await act(async () => {
239 renderer = create(<ServerUpdatesAction targets={batch} />);
240 });
241 return renderer!.root.findByType("button");
242 }
243​
244 beforeEach(() => {
245 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
246 testState.updateServer.mockReset();
247 testState.toast.mockReset();
248 resetConfirmDialogForTests();
249 });
250 afterEach(async () => {
251 await act(async () => {
252 renderer?.unmount();
253 });
254 renderer = undefined;
255 resetConfirmDialogForTests();
256 vi.unstubAllGlobals();
257 });
258​
259 it("updates both supported machines with their own continuation preference and skips the manual machine", async () => {
260 testState.updateServer.mockResolvedValue(success);
261 const button = await mount();
262 await act(async () => {
263 button.props.onClick();
264 });
265​
266 expect(testState.updateServer.mock.calls.map(([target]) => target)).toEqual([
267 {
268 environmentId: "batch-a",
269 input: { targetVersion: "0.0.31", continueRunningThreads: true },
270 },
271 { environmentId: "batch-b", input: { targetVersion: "0.0.31" } },
272 ]);
273 expect(testState.toast.mock.calls.map(([toast]) => toast.title)).toEqual([
274 "Laptop updated",
275 "Office updated",
276 ]);
277 });
278​
279 it("names a failed machine while letting the other machine complete", async () => {
280 testState.updateServer
281 .mockResolvedValueOnce(AsyncResult.failure(Cause.fail(new Error("Download failed"))))
282 .mockResolvedValueOnce(success);
283 const button = await mount();
284 await act(async () => {
285 button.props.onClick();
286 });
287​
288 expect(testState.updateServer).toHaveBeenCalledTimes(2);
289 expect(testState.toast).toHaveBeenCalledWith({
290 type: "error",
291 title: "Laptop update failed",
292 description: "Download failed",
293 });
294 expect(testState.toast).toHaveBeenCalledWith(
295 expect.objectContaining({ type: "success", title: "Office updated" }),
296 );
297 expect(button.props.disabled).toBe(false);
298 });
299​
300 it("starts each machine once when double-clicked and disables the action until both finish", async () => {
301 const completions: Array<() => void> = [];
302 testState.updateServer.mockImplementation(
303 () =>
304 new Promise((resolve) => {
305 completions.push(() => resolve(success));
306 }),
307 );
308 const button = await mount();
309 await act(async () => {
310 button.props.onClick();
311 button.props.onClick();
312 });
313 expect(testState.updateServer).toHaveBeenCalledTimes(2);
314 expect(button.props.disabled).toBe(true);
315 await act(async () => {
316 completions[0]!();
317 });
318 expect(button.props.disabled).toBe(true);
319 await act(async () => {
320 completions[1]!();
321 });
322 expect(button.props.disabled).toBe(false);
323 expect(testState.toast).toHaveBeenCalledTimes(2);
324 });
325​
326 it("asks once for desktop machines and cancels the entire batch", async () => {
327 registerConfirmDialogHost();
328 const button = await mount(
329 targets.map((target, index) =>
330 index < 2 ? { ...target, selfUpdate: "desktop-managed", desktopAppUpdate: true } : target,
331 ),
332 );
333 await act(async () => {
334 button.props.onClick();
335 });
336 const confirmation = readConfirmDialogState();
337 expect(confirmation).toEqual(
338 expect.objectContaining({
339 status: "confirming",
340 message: expect.stringContaining("Laptop, Office"),
341 }),
342 );
343 expect(testState.updateServer).not.toHaveBeenCalled();
344 await act(async () => {
345 respondToConfirmDialog(false);
346 });
347 expect(testState.updateServer).not.toHaveBeenCalled();
348 expect(button.props.disabled).toBe(false);
349 });
350});
351​
352describe("ServerUpdateProgress", () => {
353 it("shows one calm status row for the restart wait", () => {
354 const markup = renderToStaticMarkup(
355 <ServerUpdateProgress
356 state={{
357 status: "running",
358 stage: "resuming",
359 fromVersion: "0.0.30",
360 targetVersion: "0.0.31",
361 }}
362 />,
363 );
364​
365 expect(markup).toContain("Restarting…");
366 // The wait state is monochrome and calm: no versions, no step rail, no
367 // success/warning colors, one duty-cycled pulse on the dot.
368 expect(markup).not.toContain("0.0.30");
369 expect(markup).not.toContain("Resum");
370 expect(markup).not.toContain("text-success");
371 expect(markup).not.toContain("text-primary");
372 expect(markup).toContain("animate-status-pulse");
373 expect(markup).not.toContain("animate-spin");
374 });
375​
376 it("folds the sub-second installing handoff into the download phase", () => {
377 const markup = renderToStaticMarkup(
378 <ServerUpdateProgress
379 state={{
380 status: "running",
381 stage: "installing",
382 fromVersion: "0.0.30",
383 targetVersion: "0.0.31",
384 }}
385 />,
386 );
387​
388 expect(markup).toContain("Downloading…");
389 expect(markup).not.toContain("Install");
390 });
391​
392 it("keeps the failure visible with its retryable error", () => {
393 const markup = renderToStaticMarkup(
394 <ServerUpdateProgress
395 state={{
396 status: "failed",
397 stage: "installing",
398 fromVersion: "0.0.30",
399 targetVersion: "0.0.31",
400 message: "The package could not be verified.",
401 }}
402 />,
403 );
404​
405 expect(markup).toContain('role="alert"');
406 expect(markup).toContain("The package could not be verified.");
407 expect(markup).not.toContain("animate-status-pulse");
408 });
409});