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
103describe("ServerUpdateProgress", () => {
104 it("renders the monochrome step rail with only the active step highlighted", () => {
105 const markup = renderToStaticMarkup(
106 <ServerUpdateProgress
107 fromVersion="0.0.30"
108 state={{
109 status: "running",
110 stage: "resuming",
111 fromVersion: "0.0.30",
112 targetVersion: "0.0.31",
113 }}
114 />,
115 );
116
117 expect(markup).toContain("0.0.30");
118 expect(markup).toContain("0.0.31");
119 expect(markup).toContain("Download");
120 expect(markup).toContain("Install");
121 expect(markup).toContain("Resuming");
122 // The wait state is monochrome and calm: no success/warning colors, no
123 // status sentence, one duty-cycled pulse on the active step.
124 expect(markup).not.toContain("text-success");
125 expect(markup).not.toContain("text-primary");
126 expect(markup).toContain("animate-status-pulse");
127 expect(markup).not.toContain("animate-spin");
128 });
129
130 it("keeps the failed stage visible with its retryable error", () => {
131 const markup = renderToStaticMarkup(
132 <ServerUpdateProgress
133 fromVersion="0.0.30"
134 state={{
135 status: "failed",
136 stage: "installing",
137 fromVersion: "0.0.30",
138 targetVersion: "0.0.31",
139 message: "The package could not be verified.",
140 }}
141 />,
142 );
143
144 expect(markup).toContain('role="alert"');
145 expect(markup).toContain("The package could not be verified.");
146 expect(markup).not.toContain("animate-status-pulse");
147 });
148});