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

ServerUpdateAction.test.tsx

253 lines7.9 KB

1import type { ReactElement } from "react";
2import { renderToStaticMarkup } from "react-dom/server";
3import type { EnvironmentId } from "@t3tools/contracts";
4import * as Cause from "effect/Cause";
5import { AsyncResult } from "effect/unstable/reactivity";
6import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
7​
8const testState = vi.hoisted(() => ({
9 updateServer: vi.fn(),
10 toast: vi.fn(),
11 continueThreadsAfterServerUpdate: false,
12}));
13​
14vi.mock("~/hooks/useCopyToClipboard", () => ({
15 useCopyToClipboard: () => ({ copyToClipboard: vi.fn() }),
16}));
17vi.mock("~/hooks/useSettings", () => ({
18 useClientSettings: (
19 selector: (settings: { continueThreadsAfterServerUpdate: boolean }) => unknown,
20 ) => selector({ continueThreadsAfterServerUpdate: testState.continueThreadsAfterServerUpdate }),
21}));
22vi.mock("~/state/server", () => ({
23 serverEnvironment: { updateServer: Symbol("updateServer") },
24}));
25vi.mock("~/state/use-atom-command", () => ({
26 useAtomCommand: () => testState.updateServer,
27}));
28vi.mock("./ui/toast", () => ({
29 toastManager: { add: testState.toast },
30}));
31​
32import { ServerUpdateAction, ServerUpdateProgress } from "./ServerUpdateAction";
33​
34type ActionElement = ReactElement<{
35 readonly onClick?: () => void;
36}>;
37​
38function renderAction(): ActionElement {
39 return ServerUpdateAction({
40 environmentId: "env-test" as EnvironmentId,
41 serverLabel: "Test server",
42 selfUpdate: "boot-service",
43 targetVersion: "0.0.31",
44 }) as ActionElement;
46​
47async function flushPromises(): Promise<void> {
48 await Promise.resolve();
49 await Promise.resolve();
51​
52describe("ServerUpdateAction", () => {
53 beforeEach(() => {
54 testState.updateServer.mockReset();
55 testState.toast.mockReset();
56 testState.continueThreadsAfterServerUpdate = false;
57 });
58​
59 it("reports success only after the shared update flow reconnects", async () => {
60 testState.updateServer.mockResolvedValue(
61 AsyncResult.success({ targetVersion: "0.0.31", method: "boot-service" as const }),
62 );
63​
64 renderAction().props.onClick?.();
65 await flushPromises();
66​
67 expect(testState.updateServer).toHaveBeenCalledWith({
68 environmentId: "env-test",
69 input: { targetVersion: "0.0.31" },
70 });
71 expect(testState.toast).toHaveBeenCalledWith({
72 type: "success",
73 title: "Test server updated",
74 description: "Reconnected on t3@0.0.31.",
75 });
76 });
77​
78 it("reports one result when the update action is double-clicked", async () => {
79 let finishUpdate: (() => void) | undefined;
80 testState.updateServer.mockImplementation(
81 () =>
82 new Promise((resolve) => {
83 finishUpdate = () =>
84 resolve(
85 AsyncResult.success({ targetVersion: "0.0.31", method: "boot-service" as const }),
86 );
87 }),
88 );
89​
90 const action = renderAction();
91 action.props.onClick?.();
92 action.props.onClick?.();
93​
94 expect(testState.updateServer).toHaveBeenCalledTimes(1);
95 finishUpdate?.();
96 await flushPromises();
97 expect(testState.toast).toHaveBeenCalledTimes(1);
98 });
99​
100 it("quietly releases the action when the operation is interrupted", async () => {
101 testState.updateServer.mockResolvedValue(AsyncResult.failure(Cause.interrupt()));
102​
103 renderAction().props.onClick?.();
104 await flushPromises();
105​
106 expect(testState.toast).not.toHaveBeenCalled();
107 });
108​
109 it("keeps the manual instruction for desktop servers without remote update support", () => {
110 const markup = renderToStaticMarkup(
111 <ServerUpdateAction
112 environmentId={"env-test" as EnvironmentId}
113 serverLabel="Test server"
114 selfUpdate="desktop-managed"
115 targetVersion="0.0.31"
116 />,
117 );
118​
119 expect(markup).toContain("Update the desktop app on that machine to update this server.");
120 expect(markup).not.toContain("<button");
121 });
122​
123 it("updates remote desktop apps through the shared update flow", async () => {
124 testState.updateServer.mockResolvedValue(
125 AsyncResult.success({ targetVersion: "0.0.34", method: "desktop-app" as const }),
126 );
127​
128 const action = ServerUpdateAction({
129 environmentId: "env-test" as EnvironmentId,
130 serverLabel: "Test server",
131 selfUpdate: "desktop-managed",
132 desktopAppUpdate: true,
133 targetVersion: "0.0.31",
134 }) as ActionElement;
135​
136 // No confirm-dialog host is mounted in this test, which the component
137 // treats as consent: the click itself was the request.
138 action.props.onClick?.();
139 await flushPromises();
140​
141 expect(testState.updateServer).toHaveBeenCalledWith({
142 environmentId: "env-test",
143 input: { targetVersion: "0.0.31" },
144 });
145 expect(testState.toast).toHaveBeenCalledWith({
146 type: "success",
147 title: "Test server updated",
148 description: "Desktop app relaunched on 0.0.34.",
149 });
150 });
151​
152 it("leaves thread continuation off by default", async () => {
153 testState.updateServer.mockResolvedValue(
154 AsyncResult.success({ targetVersion: "0.0.31", method: "boot-service" as const }),
155 );
156 const action = ServerUpdateAction({
157 environmentId: "env-test" as EnvironmentId,
158 serverLabel: "Test server",
159 selfUpdate: "boot-service",
160 threadContinuation: true,
161 targetVersion: "0.0.31",
162 }) as ActionElement;
163​
164 action.props.onClick?.();
165 await flushPromises();
166​
167 expect(testState.updateServer).toHaveBeenCalledWith({
168 environmentId: "env-test",
169 input: { targetVersion: "0.0.31" },
170 });
171 });
172​
173 it("applies the saved thread continuation preference automatically", async () => {
174 testState.updateServer.mockResolvedValue(
175 AsyncResult.success({ targetVersion: "0.0.31", method: "boot-service" as const }),
176 );
177 testState.continueThreadsAfterServerUpdate = true;
178 const action = ServerUpdateAction({
179 environmentId: "env-test" as EnvironmentId,
180 serverLabel: "Test server",
181 selfUpdate: "boot-service",
182 threadContinuation: true,
183 targetVersion: "0.0.31",
184 }) as ActionElement;
185​
186 action.props.onClick?.();
187 await flushPromises();
188​
189 expect(testState.updateServer).toHaveBeenCalledWith({
190 environmentId: "env-test",
191 input: { targetVersion: "0.0.31", continueRunningThreads: true },
192 });
193 });
194});
195​
196describe("ServerUpdateProgress", () => {
197 it("shows one calm status row for the restart wait", () => {
198 const markup = renderToStaticMarkup(
199 <ServerUpdateProgress
200 state={{
201 status: "running",
202 stage: "resuming",
203 fromVersion: "0.0.30",
204 targetVersion: "0.0.31",
205 }}
206 />,
207 );
208​
209 expect(markup).toContain("Restarting…");
210 // The wait state is monochrome and calm: no versions, no step rail, no
211 // success/warning colors, one duty-cycled pulse on the dot.
212 expect(markup).not.toContain("0.0.30");
213 expect(markup).not.toContain("Resum");
214 expect(markup).not.toContain("text-success");
215 expect(markup).not.toContain("text-primary");
216 expect(markup).toContain("animate-status-pulse");
217 expect(markup).not.toContain("animate-spin");
218 });
219​
220 it("folds the sub-second installing handoff into the download phase", () => {
221 const markup = renderToStaticMarkup(
222 <ServerUpdateProgress
223 state={{
224 status: "running",
225 stage: "installing",
226 fromVersion: "0.0.30",
227 targetVersion: "0.0.31",
228 }}
229 />,
230 );
231​
232 expect(markup).toContain("Downloading…");
233 expect(markup).not.toContain("Install");
234 });
235​
236 it("keeps the failure visible with its retryable error", () => {
237 const markup = renderToStaticMarkup(
238 <ServerUpdateProgress
239 state={{
240 status: "failed",
241 stage: "installing",
242 fromVersion: "0.0.30",
243 targetVersion: "0.0.31",
244 message: "The package could not be verified.",
245 }}
246 />,
247 );
248​
249 expect(markup).toContain('role="alert"');
250 expect(markup).toContain("The package could not be verified.");
251 expect(markup).not.toContain("animate-status-pulse");
252 });
253});