Open History to see the last commit
1import { describe, expect, it } from "vitest";
2import type { DesktopUpdateActionResult, DesktopUpdateState } from "@t3tools/contracts";
3
4import {
5 getArm64IntelBuildWarningDescription,
6 getDesktopUpdateActionError,
7 getDesktopUpdateButtonTooltip,
8 isDesktopUpdateButtonDisabled,
9 resolveDesktopUpdateButtonAction,
10 shouldHighlightDesktopUpdateError,
11 shouldShowArm64IntelBuildWarning,
12 shouldShowDesktopUpdateButton,
13 shouldToastDesktopUpdateActionResult,
14} from "./desktopUpdate.logic";
15
16const baseState: DesktopUpdateState = {
17 enabled: true,
18 status: "idle",
19 currentVersion: "1.0.0",
20 hostArch: "x64",
21 appArch: "x64",
22 runningUnderArm64Translation: false,
23 availableVersion: null,
24 downloadedVersion: null,
25 downloadPercent: null,
26 checkedAt: null,
27 message: null,
28 errorContext: null,
29 canRetry: false,
30};
31
32describe("desktop update button state", () => {
33 it("shows a download action when an update is available", () => {
34 const state: DesktopUpdateState = {
35 ...baseState,
36 status: "available",
37 availableVersion: "1.1.0",
38 };
39 expect(shouldShowDesktopUpdateButton(state)).toBe(true);
40 expect(resolveDesktopUpdateButtonAction(state)).toBe("download");
41 });
42
43 it("keeps retry action available after a download error", () => {
44 const state: DesktopUpdateState = {
45 ...baseState,
46 status: "error",
47 availableVersion: "1.1.0",
48 message: "network timeout",
49 errorContext: "download",
50 canRetry: true,
51 };
52 expect(shouldShowDesktopUpdateButton(state)).toBe(true);
53 expect(resolveDesktopUpdateButtonAction(state)).toBe("download");
54 expect(getDesktopUpdateButtonTooltip(state)).toContain("Click to retry");
55 });
56
57 it("keeps install action available after an install error", () => {
58 const state: DesktopUpdateState = {
59 ...baseState,
60 status: "error",
61 downloadedVersion: "1.1.0",
62 availableVersion: "1.1.0",
63 message: "shutdown timeout",
64 errorContext: "install",
65 canRetry: true,
66 };
67 expect(shouldShowDesktopUpdateButton(state)).toBe(true);
68 expect(resolveDesktopUpdateButtonAction(state)).toBe("install");
69 expect(getDesktopUpdateButtonTooltip(state)).toContain("Click to retry");
70 });
71
72 it("hides the button for non-actionable check errors", () => {
73 const state: DesktopUpdateState = {
74 ...baseState,
75 status: "error",
76 message: "network unavailable",
77 errorContext: "check",
78 canRetry: true,
79 };
80 expect(shouldShowDesktopUpdateButton(state)).toBe(false);
81 expect(resolveDesktopUpdateButtonAction(state)).toBe("none");
82 });
83
84 it("disables the button while downloading", () => {
85 const state: DesktopUpdateState = {
86 ...baseState,
87 status: "downloading",
88 availableVersion: "1.1.0",
89 downloadPercent: 42.5,
90 };
91 expect(shouldShowDesktopUpdateButton(state)).toBe(true);
92 expect(isDesktopUpdateButtonDisabled(state)).toBe(true);
93 expect(getDesktopUpdateButtonTooltip(state)).toContain("42%");
94 });
95});
96
97describe("getDesktopUpdateActionError", () => {
98 it("returns user-visible message for accepted failed attempts", () => {
99 const result: DesktopUpdateActionResult = {
100 accepted: true,
101 completed: false,
102 state: {
103 ...baseState,
104 status: "available",
105 availableVersion: "1.1.0",
106 message: "checksum mismatch",
107 errorContext: "download",
108 canRetry: true,
109 },
110 };
111 expect(getDesktopUpdateActionError(result)).toBe("checksum mismatch");
112 });
113
114 it("ignores messages for non-accepted attempts", () => {
115 const result: DesktopUpdateActionResult = {
116 accepted: false,
117 completed: false,
118 state: {
119 ...baseState,
120 status: "error",
121 message: "background failure",
122 errorContext: "check",
123 canRetry: false,
124 },
125 };
126 expect(getDesktopUpdateActionError(result)).toBeNull();
127 });
128
129 it("ignores messages for successful attempts", () => {
130 const result: DesktopUpdateActionResult = {
131 accepted: true,
132 completed: true,
133 state: {
134 ...baseState,
135 status: "downloaded",
136 downloadedVersion: "1.1.0",
137 availableVersion: "1.1.0",
138 message: null,
139 errorContext: null,
140 canRetry: true,
141 },
142 };
143 expect(getDesktopUpdateActionError(result)).toBeNull();
144 });
145});
146
147describe("desktop update UI helpers", () => {
148 it("toasts only for accepted incomplete actions", () => {
149 expect(
150 shouldToastDesktopUpdateActionResult({
151 accepted: true,
152 completed: false,
153 state: baseState,
154 }),
155 ).toBe(true);
156 expect(
157 shouldToastDesktopUpdateActionResult({
158 accepted: true,
159 completed: true,
160 state: baseState,
161 }),
162 ).toBe(false);
163 });
164
165 it("highlights only actionable updater errors", () => {
166 expect(
167 shouldHighlightDesktopUpdateError({
168 ...baseState,
169 status: "error",
170 errorContext: "download",
171 canRetry: true,
172 }),
173 ).toBe(true);
174 expect(
175 shouldHighlightDesktopUpdateError({
176 ...baseState,
177 status: "error",
178 errorContext: "check",
179 canRetry: true,
180 }),
181 ).toBe(false);
182 });
183
184 it("shows an Apple Silicon warning for Intel builds under Rosetta", () => {
185 const state: DesktopUpdateState = {
186 ...baseState,
187 hostArch: "arm64",
188 appArch: "x64",
189 runningUnderArm64Translation: true,
190 };
191
192 expect(shouldShowArm64IntelBuildWarning(state)).toBe(true);
193 expect(getArm64IntelBuildWarningDescription(state)).toContain("Apple Silicon");
194 expect(getArm64IntelBuildWarningDescription(state)).toContain("Intel build");
195 });
196
197 it("changes the warning copy when a native build update is ready to download", () => {
198 const state: DesktopUpdateState = {
199 ...baseState,
200 hostArch: "arm64",
201 appArch: "x64",
202 runningUnderArm64Translation: true,
203 status: "available",
204 availableVersion: "1.1.0",
205 };
206
207 expect(getArm64IntelBuildWarningDescription(state)).toContain("Download the available update");
208 });
209});