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