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

desktopUpdate.logic.test.ts

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