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

desktopUpdate.logic.test.ts

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