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

ServerUpdateAction.test.tsx

160 lines4.7 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}));
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;
40​
41async function flushPromises(): Promise<void> {
42 await Promise.resolve();
43 await Promise.resolve();
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​
103describe("ServerUpdateProgress", () => {
104 it("shows one calm status row for the restart wait", () => {
105 const markup = renderToStaticMarkup(
106 <ServerUpdateProgress
107 state={{
108 status: "running",
109 stage: "resuming",
110 fromVersion: "0.0.30",
111 targetVersion: "0.0.31",
112 }}
113 />,
114 );
115​
116 expect(markup).toContain("Restarting…");
117 // The wait state is monochrome and calm: no versions, no step rail, no
118 // success/warning colors, one duty-cycled pulse on the dot.
119 expect(markup).not.toContain("0.0.30");
120 expect(markup).not.toContain("Resum");
121 expect(markup).not.toContain("text-success");
122 expect(markup).not.toContain("text-primary");
123 expect(markup).toContain("animate-status-pulse");
124 expect(markup).not.toContain("animate-spin");
125 });
126​
127 it("folds the sub-second installing handoff into the download phase", () => {
128 const markup = renderToStaticMarkup(
129 <ServerUpdateProgress
130 state={{
131 status: "running",
132 stage: "installing",
133 fromVersion: "0.0.30",
134 targetVersion: "0.0.31",
135 }}
136 />,
137 );
138​
139 expect(markup).toContain("Downloading…");
140 expect(markup).not.toContain("Install");
141 });
142​
143 it("keeps the failure visible with its retryable error", () => {
144 const markup = renderToStaticMarkup(
145 <ServerUpdateProgress
146 state={{
147 status: "failed",
148 stage: "installing",
149 fromVersion: "0.0.30",
150 targetVersion: "0.0.31",
151 message: "The package could not be verified.",
152 }}
153 />,
154 );
155​
156 expect(markup).toContain('role="alert"');
157 expect(markup).toContain("The package could not be verified.");
158 expect(markup).not.toContain("animate-status-pulse");
159 });
160});