Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2import type { DesktopUpdateActionResult, DesktopUpdateState } from "@t3tools/contracts";
3
4import {
5 canCheckForUpdate,
6 getArm64IntelBuildWarningDescription,
7 getDesktopUpdateActionError,
8 getDesktopUpdateButtonTooltip,
9 getDesktopUpdateInstallConfirmationMessage,
10 isDesktopUpdateButtonDisabled,
11 resolveDesktopUpdateButtonAction,
12 shouldShowArm64IntelBuildWarning,
13 shouldShowDesktopUpdateButton,
14 shouldToastDesktopUpdateActionResult,
15} from "./desktopUpdate.logic";
16
17const baseState: DesktopUpdateState = {
18 enabled: true,
19 status: "idle",
20 channel: "latest",
21 currentVersion: "1.0.0",
22 hostArch: "x64",
23 appArch: "x64",
24 runningUnderArm64Translation: false,
25 availableVersion: null,
26 downloadedVersion: null,
27 releaseNotes: [],
28 downloadPercent: null,
29 checkedAt: null,
30 message: null,
31 errorContext: null,
32 canRetry: false,
33};
34
35describe("desktop update button state", () => {
36 it("shows a download action when an update is available", () => {
37 const state: DesktopUpdateState = {
38 ...baseState,
39 status: "available",
40 availableVersion: "1.1.0",
41 };
42 expect(shouldShowDesktopUpdateButton(state)).toBe(true);
43 expect(resolveDesktopUpdateButtonAction(state)).toBe("download");
44 });
45
46 it("keeps retry action available after a download error", () => {
47 const state: DesktopUpdateState = {
48 ...baseState,
49 status: "error",
50 availableVersion: "1.1.0",
51 message: "network timeout",
52 errorContext: "download",
53 canRetry: true,
54 };
55 expect(shouldShowDesktopUpdateButton(state)).toBe(true);
56 expect(resolveDesktopUpdateButtonAction(state)).toBe("download");
57 expect(getDesktopUpdateButtonTooltip(state)).toContain("Click to retry");
58 });
59
60 it("keeps install action available after an install error", () => {
61 const state: DesktopUpdateState = {
62 ...baseState,
63 status: "error",
64 downloadedVersion: "1.1.0",
65 availableVersion: "1.1.0",
66 message: "shutdown timeout",
67 errorContext: "install",
68 canRetry: true,
69 };
70 expect(shouldShowDesktopUpdateButton(state)).toBe(true);
71 expect(resolveDesktopUpdateButtonAction(state)).toBe("install");
72 expect(getDesktopUpdateButtonTooltip(state)).toContain("Click to retry");
73 });
74
75 it("prefers install when a downloaded version already exists", () => {
76 const state: DesktopUpdateState = {
77 ...baseState,
78 status: "available",
79 availableVersion: "1.1.0",
80 downloadedVersion: "1.1.0",
81 };
82 expect(resolveDesktopUpdateButtonAction(state)).toBe("install");
83 });
84
85 it("hides the button for non-actionable check errors", () => {
86 const state: DesktopUpdateState = {
87 ...baseState,
88 status: "error",
89 message: "network unavailable",
90 errorContext: "check",
91 canRetry: true,
92 };
93 expect(shouldShowDesktopUpdateButton(state)).toBe(false);
94 expect(resolveDesktopUpdateButtonAction(state)).toBe("none");
95 });
96
97 it("disables the button while downloading", () => {
98 const state: DesktopUpdateState = {
99 ...baseState,
100 status: "downloading",
101 availableVersion: "1.1.0",
102 downloadPercent: 42.5,
103 };
104 expect(shouldShowDesktopUpdateButton(state)).toBe(true);
105 expect(isDesktopUpdateButtonDisabled(state)).toBe(true);
106 expect(getDesktopUpdateButtonTooltip(state)).toContain("42%");
107 });
108});
109
110describe("getDesktopUpdateActionError", () => {
111 it("returns user-visible message for accepted failed attempts", () => {
112 const result: DesktopUpdateActionResult = {
113 accepted: true,
114 completed: false,
115 state: {
116 ...baseState,
117 status: "available",
118 availableVersion: "1.1.0",
119 message: "checksum mismatch",
120 errorContext: "download",
121 canRetry: true,
122 },
123 };
124 expect(getDesktopUpdateActionError(result)).toBe("checksum mismatch");
125 });
126
127 it("ignores messages for non-accepted attempts", () => {
128 const result: DesktopUpdateActionResult = {
129 accepted: false,
130 completed: false,
131 state: {
132 ...baseState,
133 status: "error",
134 message: "background failure",
135 errorContext: "check",
136 canRetry: false,
137 },
138 };
139 expect(getDesktopUpdateActionError(result)).toBeNull();
140 });
141
142 it("ignores messages for successful attempts", () => {
143 const result: DesktopUpdateActionResult = {
144 accepted: true,
145 completed: true,
146 state: {
147 ...baseState,
148 status: "downloaded",
149 downloadedVersion: "1.1.0",
150 availableVersion: "1.1.0",
151 message: null,
152 errorContext: null,
153 canRetry: true,
154 },
155 };
156 expect(getDesktopUpdateActionError(result)).toBeNull();
157 });
158});
159
160describe("desktop update UI helpers", () => {
161 it("toasts only for actionable updater errors", () => {
162 expect(
163 shouldToastDesktopUpdateActionResult({
164 accepted: true,
165 completed: false,
166 state: { ...baseState, message: "checksum mismatch" },
167 }),
168 ).toBe(true);
169 expect(
170 shouldToastDesktopUpdateActionResult({
171 accepted: true,
172 completed: false,
173 state: { ...baseState, message: null },
174 }),
175 ).toBe(false);
176 expect(
177 shouldToastDesktopUpdateActionResult({
178 accepted: true,
179 completed: true,
180 state: { ...baseState, message: "checksum mismatch" },
181 }),
182 ).toBe(false);
183 });
184
185 it("shows an Apple Silicon warning for Intel builds under Rosetta", () => {
186 const state: DesktopUpdateState = {
187 ...baseState,
188 hostArch: "arm64",
189 appArch: "x64",
190 runningUnderArm64Translation: true,
191 };
192
193 expect(shouldShowArm64IntelBuildWarning(state)).toBe(true);
194 expect(getArm64IntelBuildWarningDescription(state)).toContain("Apple Silicon");
195 expect(getArm64IntelBuildWarningDescription(state)).toContain("Intel build");
196 });
197
198 it("changes the warning copy when a native build update is ready to download", () => {
199 const state: DesktopUpdateState = {
200 ...baseState,
201 hostArch: "arm64",
202 appArch: "x64",
203 runningUnderArm64Translation: true,
204 status: "available",
205 availableVersion: "1.1.0",
206 };
207
208 expect(getArm64IntelBuildWarningDescription(state)).toContain("Download the available update");
209 });
210
211 it("includes the downloaded version in the install confirmation copy", () => {
212 expect(
213 getDesktopUpdateInstallConfirmationMessage({
214 availableVersion: "1.1.0",
215 downloadedVersion: "1.1.1",
216 }),
217 ).toContain("Install update 1.1.1 and restart T3 Code?");
218 });
219
220 it("falls back to generic install confirmation copy when no version is available", () => {
221 expect(
222 getDesktopUpdateInstallConfirmationMessage({
223 availableVersion: null,
224 downloadedVersion: null,
225 }),
226 ).toContain("Install update and restart T3 Code?");
227 });
228
229 it("warns Windows users that a silent installation can take several minutes", () => {
230 const message = getDesktopUpdateInstallConfirmationMessage(
231 {
232 availableVersion: "1.1.0",
233 downloadedVersion: "1.1.0",
234 },
235 "Win32",
236 );
237
238 expect(message).toContain("may remain closed for several minutes");
239 expect(message).toContain("no installer window may appear");
240 expect(message).toContain("will reopen automatically");
241 });
242
243 it("keeps the additional silent installation warning Windows-specific", () => {
244 const message = getDesktopUpdateInstallConfirmationMessage(
245 {
246 availableVersion: "1.1.0",
247 downloadedVersion: "1.1.0",
248 },
249 "MacIntel",
250 );
251
252 expect(message).not.toContain("may remain closed for several minutes");
253 });
254});
255
256describe("canCheckForUpdate", () => {
257 it("returns false for null state", () => {
258 expect(canCheckForUpdate(null)).toBe(false);
259 });
260
261 it("returns false when updates are disabled", () => {
262 expect(canCheckForUpdate({ ...baseState, enabled: false, status: "disabled" })).toBe(false);
263 });
264
265 it("returns false while checking", () => {
266 expect(canCheckForUpdate({ ...baseState, status: "checking" })).toBe(false);
267 });
268
269 it("returns false while downloading", () => {
270 expect(canCheckForUpdate({ ...baseState, status: "downloading", downloadPercent: 50 })).toBe(
271 false,
272 );
273 });
274
275 it("returns false once an update has been downloaded", () => {
276 expect(
277 canCheckForUpdate({
278 ...baseState,
279 status: "downloaded",
280 availableVersion: "1.1.0",
281 downloadedVersion: "1.1.0",
282 }),
283 ).toBe(false);
284 });
285
286 it("returns true when idle", () => {
287 expect(canCheckForUpdate({ ...baseState, status: "idle" })).toBe(true);
288 });
289
290 it("returns true when up-to-date", () => {
291 expect(canCheckForUpdate({ ...baseState, status: "up-to-date" })).toBe(true);
292 });
293
294 it("returns true when an update is available", () => {
295 expect(
296 canCheckForUpdate({ ...baseState, status: "available", availableVersion: "1.1.0" }),
297 ).toBe(true);
298 });
299
300 it("returns true on error so the user can retry", () => {
301 expect(
302 canCheckForUpdate({
303 ...baseState,
304 status: "error",
305 errorContext: "check",
306 message: "network",
307 }),
308 ).toBe(true);
309 });
310});
311
312describe("getDesktopUpdateButtonTooltip", () => {
313 it("returns 'Up to date' for non-actionable states", () => {
314 expect(getDesktopUpdateButtonTooltip({ ...baseState, status: "idle" })).toBe("Up to date");
315 expect(getDesktopUpdateButtonTooltip({ ...baseState, status: "up-to-date" })).toBe(
316 "Up to date",
317 );
318 });
319});