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