Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

desktopUpdate.logic.test.ts

426 lines13.0 KB

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