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

ServerUpdateAction.test.tsx

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