Open History to see the last commit
1import { describe, expect, it } from "vitest";
2import type { DesktopUpdateState } from "@t3tools/contracts";
3
4import {
5 getCanRetryAfterDownloadFailure,
6 getAutoUpdateDisabledReason,
7 nextStatusAfterDownloadFailure,
8 shouldBroadcastDownloadProgress,
9} from "./updateState";
10
11const baseState: DesktopUpdateState = {
12 enabled: true,
13 status: "idle",
14 currentVersion: "1.0.0",
15 hostArch: "x64",
16 appArch: "x64",
17 runningUnderArm64Translation: false,
18 availableVersion: null,
19 downloadedVersion: null,
20 downloadPercent: null,
21 checkedAt: null,
22 message: null,
23 errorContext: null,
24 canRetry: false,
25};
26
27describe("shouldBroadcastDownloadProgress", () => {
28 it("broadcasts the first downloading progress update", () => {
29 expect(
30 shouldBroadcastDownloadProgress(
31 { ...baseState, status: "downloading", downloadPercent: null },
32 1,
33 ),
34 ).toBe(true);
35 });
36
37 it("skips progress updates within the same 10% bucket", () => {
38 expect(
39 shouldBroadcastDownloadProgress(
40 { ...baseState, status: "downloading", downloadPercent: 11.2 },
41 18.7,
42 ),
43 ).toBe(false);
44 });
45
46 it("broadcasts progress updates when a new 10% bucket is reached", () => {
47 expect(
48 shouldBroadcastDownloadProgress(
49 { ...baseState, status: "downloading", downloadPercent: 19.9 },
50 20.1,
51 ),
52 ).toBe(true);
53 });
54
55 it("broadcasts progress updates when a retry resets the download percentage", () => {
56 expect(
57 shouldBroadcastDownloadProgress(
58 { ...baseState, status: "downloading", downloadPercent: 50.4 },
59 0.2,
60 ),
61 ).toBe(true);
62 });
63});
64
65describe("getAutoUpdateDisabledReason", () => {
66 it("reports development builds as disabled", () => {
67 expect(
68 getAutoUpdateDisabledReason({
69 isDevelopment: true,
70 isPackaged: false,
71 platform: "darwin",
72 appImage: undefined,
73 disabledByEnv: false,
74 hasUpdateFeedConfig: true,
75 }),
76 ).toContain("packaged production builds");
77 });
78
79 it("reports packaged local builds without an update feed as disabled", () => {
80 expect(
81 getAutoUpdateDisabledReason({
82 isDevelopment: false,
83 isPackaged: true,
84 platform: "darwin",
85 appImage: undefined,
86 disabledByEnv: false,
87 hasUpdateFeedConfig: false,
88 }),
89 ).toContain("no update feed");
90 });
91
92 it("allows packaged builds with an update feed", () => {
93 expect(
94 getAutoUpdateDisabledReason({
95 isDevelopment: false,
96 isPackaged: true,
97 platform: "darwin",
98 appImage: undefined,
99 disabledByEnv: false,
100 hasUpdateFeedConfig: true,
101 }),
102 ).toBeNull();
103 });
104
105 it("reports env-disabled auto updates", () => {
106 expect(
107 getAutoUpdateDisabledReason({
108 isDevelopment: false,
109 isPackaged: true,
110 platform: "darwin",
111 appImage: undefined,
112 disabledByEnv: true,
113 hasUpdateFeedConfig: true,
114 }),
115 ).toContain("T3CODE_DISABLE_AUTO_UPDATE");
116 });
117
118 it("reports linux non-AppImage builds as disabled", () => {
119 expect(
120 getAutoUpdateDisabledReason({
121 isDevelopment: false,
122 isPackaged: true,
123 platform: "linux",
124 appImage: undefined,
125 disabledByEnv: false,
126 hasUpdateFeedConfig: true,
127 }),
128 ).toContain("AppImage");
129 });
130});
131
132describe("nextStatusAfterDownloadFailure", () => {
133 it("returns available when an update version is still known", () => {
134 expect(
135 nextStatusAfterDownloadFailure({
136 ...baseState,
137 status: "downloading",
138 availableVersion: "1.1.0",
139 }),
140 ).toBe("available");
141 });
142
143 it("returns error when no update version can be retried", () => {
144 expect(
145 nextStatusAfterDownloadFailure({
146 ...baseState,
147 status: "downloading",
148 availableVersion: null,
149 }),
150 ).toBe("error");
151 });
152});
153
154describe("getCanRetryAfterDownloadFailure", () => {
155 it("returns true when an available version is still present", () => {
156 expect(
157 getCanRetryAfterDownloadFailure({
158 ...baseState,
159 status: "downloading",
160 availableVersion: "1.1.0",
161 }),
162 ).toBe(true);
163 });
164
165 it("returns false when no version is available to retry", () => {
166 expect(
167 getCanRetryAfterDownloadFailure({
168 ...baseState,
169 status: "downloading",
170 availableVersion: null,
171 }),
172 ).toBe(false);
173 });
174});