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