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 getDesktopUpdateReleaseHistoryUrl,
11 getDesktopUpdateReleaseUrl,
12 isDesktopUpdateButtonDisabled,
13 resolveDesktopUpdateButtonAction,
14 shouldShowArm64IntelBuildWarning,
15 shouldShowDesktopUpdateButton,
16 shouldToastDesktopUpdateActionResult,
17} from "./desktopUpdate.logic";
18
19const baseState: DesktopUpdateState = {
20 enabled: true,
21 status: "idle",
22 channel: "latest",
23 currentVersion: "1.0.0",
24 hostArch: "x64",
25 appArch: "x64",
26 runningUnderArm64Translation: false,
27 availableVersion: null,
28 downloadedVersion: null,
29 releaseNotes: [],
30 omittedReleaseCount: 0,
31 downloadPercent: null,
32 checkedAt: null,
33 message: null,
34 errorContext: null,
35 canRetry: false,
36};
37
38describe("desktop update button state", () => {
39 it("shows a download action when an update is available", () => {
40 const state: DesktopUpdateState = {
41 ...baseState,
42 status: "available",
43 availableVersion: "1.1.0",
44 };
45 expect(shouldShowDesktopUpdateButton(state)).toBe(true);
46 expect(resolveDesktopUpdateButtonAction(state)).toBe("download");
47 });
48
49 it("keeps retry action available after a download error", () => {
50 const state: DesktopUpdateState = {
51 ...baseState,
52 status: "error",
53 availableVersion: "1.1.0",
54 message: "network timeout",
55 errorContext: "download",
56 canRetry: true,
57 };
58 expect(shouldShowDesktopUpdateButton(state)).toBe(true);
59 expect(resolveDesktopUpdateButtonAction(state)).toBe("download");
60 expect(getDesktopUpdateButtonTooltip(state)).toContain("Click to retry");
61 });
62
63 it("keeps install action available after an install error", () => {
64 const state: DesktopUpdateState = {
65 ...baseState,
66 status: "error",
67 downloadedVersion: "1.1.0",
68 availableVersion: "1.1.0",
69 message: "shutdown timeout",
70 errorContext: "install",
71 canRetry: true,
72 };
73 expect(shouldShowDesktopUpdateButton(state)).toBe(true);
74 expect(resolveDesktopUpdateButtonAction(state)).toBe("install");
75 expect(getDesktopUpdateButtonTooltip(state)).toContain("Click to retry");
76 });
77
78 it("keeps install action available after a background updater error", () => {
79 const state: DesktopUpdateState = {
80 ...baseState,
81 status: "error",
82 downloadedVersion: "1.1.0",
83 availableVersion: "1.1.0",
84 message: "background updater error",
85 errorContext: null,
86 canRetry: true,
87 };
88 expect(shouldShowDesktopUpdateButton(state)).toBe(true);
89 expect(resolveDesktopUpdateButtonAction(state)).toBe("install");
90 expect(getDesktopUpdateButtonTooltip(state)).toContain("Click to restart and install");
91 });
92
93 it("prefers a newly available release over a stale downloaded version", () => {
94 const state: DesktopUpdateState = {
95 ...baseState,
96 status: "available",
97 availableVersion: "1.2.0",
98 downloadedVersion: "1.1.0",
99 };
100 expect(resolveDesktopUpdateButtonAction(state)).toBe("download");
101 });
102
103 it("hides the install action while checking for a newer release", () => {
104 const state: DesktopUpdateState = {
105 ...baseState,
106 status: "checking",
107 availableVersion: "1.1.0",
108 downloadedVersion: "1.1.0",
109 downloadPercent: 100,
110 };
111 expect(resolveDesktopUpdateButtonAction(state)).toBe("none");
112 });
113
114 it("hides the button for non-actionable check errors", () => {
115 const state: DesktopUpdateState = {
116 ...baseState,
117 status: "error",
118 message: "network unavailable",
119 errorContext: "check",
120 canRetry: true,
121 };
122 expect(shouldShowDesktopUpdateButton(state)).toBe(false);
123 expect(resolveDesktopUpdateButtonAction(state)).toBe("none");
124 });
125
126 it("disables the button while downloading", () => {
127 const state: DesktopUpdateState = {
128 ...baseState,
129 status: "downloading",
130 availableVersion: "1.1.0",
131 downloadPercent: 42.5,
132 };
133 expect(shouldShowDesktopUpdateButton(state)).toBe(true);
134 expect(isDesktopUpdateButtonDisabled(state)).toBe(true);
135 expect(getDesktopUpdateButtonTooltip(state)).toContain("42%");
136 });
137});
138
139describe("getDesktopUpdateActionError", () => {
140 it("returns user-visible message for accepted failed attempts", () => {
141 const result: DesktopUpdateActionResult = {
142 accepted: true,
143 completed: false,
144 state: {
145 ...baseState,
146 status: "available",
147 availableVersion: "1.1.0",
148 message: "checksum mismatch",
149 errorContext: "download",
150 canRetry: true,
151 },
152 };
153 expect(getDesktopUpdateActionError(result)).toBe("checksum mismatch");
154 });
155
156 it("ignores messages for non-accepted attempts", () => {
157 const result: DesktopUpdateActionResult = {
158 accepted: false,
159 completed: false,
160 state: {
161 ...baseState,
162 status: "error",
163 message: "background failure",
164 errorContext: "check",
165 canRetry: false,
166 },
167 };
168 expect(getDesktopUpdateActionError(result)).toBeNull();
169 });
170
171 it("ignores messages for successful attempts", () => {
172 const result: DesktopUpdateActionResult = {
173 accepted: true,
174 completed: true,
175 state: {
176 ...baseState,
177 status: "downloaded",
178 downloadedVersion: "1.1.0",
179 availableVersion: "1.1.0",
180 message: null,
181 errorContext: null,
182 canRetry: true,
183 },
184 };
185 expect(getDesktopUpdateActionError(result)).toBeNull();
186 });
187});
188
189describe("desktop update UI helpers", () => {
190 it("builds the stable release URL for a downloaded version", () => {
191 expect(getDesktopUpdateReleaseUrl("0.0.30")).toBe(
192 "https://github.com/pingdotgg/t3code/releases/tag/v0.0.30",
193 );
194 });
195
196 it("builds the nightly release URL without dropping its version suffix", () => {
197 expect(getDesktopUpdateReleaseUrl("0.0.30-nightly.20260728.931")).toBe(
198 "https://github.com/pingdotgg/t3code/releases/tag/v0.0.30-nightly.20260728.931",
199 );
200 });
201
202 it("omits the release URL when the updater does not report a version", () => {
203 expect(getDesktopUpdateReleaseUrl(null)).toBeNull();
204 expect(getDesktopUpdateReleaseUrl(" ")).toBeNull();
205 });
206
207 it("builds the release history URL", () => {
208 expect(getDesktopUpdateReleaseHistoryUrl()).toBe(
209 "https://github.com/pingdotgg/t3code/releases",
210 );
211 });
212
213 it("toasts only for actionable updater errors", () => {
214 expect(
215 shouldToastDesktopUpdateActionResult({
216 accepted: true,
217 completed: false,
218 state: { ...baseState, message: "checksum mismatch" },
219 }),
220 ).toBe(true);
221 expect(
222 shouldToastDesktopUpdateActionResult({
223 accepted: true,
224 completed: false,
225 state: { ...baseState, message: null },
226 }),
227 ).toBe(false);
228 expect(
229 shouldToastDesktopUpdateActionResult({
230 accepted: true,
231 completed: true,
232 state: { ...baseState, message: "checksum mismatch" },
233 }),
234 ).toBe(false);
235 });
236
237 it("shows an Apple Silicon warning for Intel builds under Rosetta", () => {
238 const state: DesktopUpdateState = {
239 ...baseState,
240 hostArch: "arm64",
241 appArch: "x64",
242 runningUnderArm64Translation: true,
243 };
244
245 expect(shouldShowArm64IntelBuildWarning(state)).toBe(true);
246 expect(getArm64IntelBuildWarningDescription(state)).toContain("Apple Silicon");
247 expect(getArm64IntelBuildWarningDescription(state)).toContain("Intel build");
248 });
249
250 it("changes the warning copy when a native build update is ready to download", () => {
251 const state: DesktopUpdateState = {
252 ...baseState,
253 hostArch: "arm64",
254 appArch: "x64",
255 runningUnderArm64Translation: true,
256 status: "available",
257 availableVersion: "1.1.0",
258 };
259
260 expect(getArm64IntelBuildWarningDescription(state)).toContain("Download the available update");
261 });
262
263 it("includes the downloaded version in the install confirmation copy", () => {
264 expect(
265 getDesktopUpdateInstallConfirmationMessage({
266 availableVersion: "1.1.0",
267 downloadedVersion: "1.1.1",
268 }),
269 ).toContain("Install update 1.1.1 and restart T3 Code?");
270 });
271
272 it("falls back to generic install confirmation copy when no version is available", () => {
273 expect(
274 getDesktopUpdateInstallConfirmationMessage({
275 availableVersion: null,
276 downloadedVersion: null,
277 }),
278 ).toContain("Install update and restart T3 Code?");
279 });
280
281 it("keeps the same install confirmation copy across desktop platforms", () => {
282 expect(
283 getDesktopUpdateInstallConfirmationMessage({
284 availableVersion: "1.1.0",
285 downloadedVersion: "1.1.0",
286 }),
287 ).toBe(
288 "Install update 1.1.0 and restart T3 Code?\n\nAny running tasks will be interrupted. Make sure you're ready before continuing.",
289 );
290 });
291});
292
293describe("canCheckForUpdate", () => {
294 it("returns false for null state", () => {
295 expect(canCheckForUpdate(null)).toBe(false);
296 });
297
298 it("returns false when updates are disabled", () => {
299 expect(canCheckForUpdate({ ...baseState, enabled: false, status: "disabled" })).toBe(false);
300 });
301
302 it("returns false while checking", () => {
303 expect(canCheckForUpdate({ ...baseState, status: "checking" })).toBe(false);
304 });
305
306 it("returns false while downloading", () => {
307 expect(canCheckForUpdate({ ...baseState, status: "downloading", downloadPercent: 50 })).toBe(
308 false,
309 );
310 });
311
312 it("returns true once an update has been downloaded so newer releases can be found", () => {
313 expect(
314 canCheckForUpdate({
315 ...baseState,
316 status: "downloaded",
317 availableVersion: "1.1.0",
318 downloadedVersion: "1.1.0",
319 }),
320 ).toBe(true);
321 });
322
323 it("returns true when idle", () => {
324 expect(canCheckForUpdate({ ...baseState, status: "idle" })).toBe(true);
325 });
326
327 it("returns true when up-to-date", () => {
328 expect(canCheckForUpdate({ ...baseState, status: "up-to-date" })).toBe(true);
329 });
330
331 it("returns true when an update is available", () => {
332 expect(
333 canCheckForUpdate({ ...baseState, status: "available", availableVersion: "1.1.0" }),
334 ).toBe(true);
335 });
336
337 it("returns true on error so the user can retry", () => {
338 expect(
339 canCheckForUpdate({
340 ...baseState,
341 status: "error",
342 errorContext: "check",
343 message: "network",
344 }),
345 ).toBe(true);
346 });
347});
348
349describe("getDesktopUpdateButtonTooltip", () => {
350 it("returns 'Up to date' for non-actionable states", () => {
351 expect(getDesktopUpdateButtonTooltip({ ...baseState, status: "idle" })).toBe("Up to date");
352 expect(getDesktopUpdateButtonTooltip({ ...baseState, status: "up-to-date" })).toBe(
353 "Up to date",
354 );
355 });
356});