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