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

updateState.test.ts

175 lines4.4 KB

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