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