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

remoteUpdateFlow.test.ts

228 lines6.8 KB

1import { assert, describe, it } from "@effect/vitest";
2import type { DesktopUpdateState } from "@t3tools/contracts";
3​
4import {
5 MAX_REMOTE_UPDATE_CHECKS,
6 MAX_REMOTE_UPDATE_DOWNLOADS,
7 nextRemoteDesktopUpdateStep,
8 normalizeRemoteUpdateReason,
9 type RemoteDesktopUpdateAttempts,
10} from "./remoteUpdateFlow.ts";
11​
12const NO_ATTEMPTS: RemoteDesktopUpdateAttempts = { checks: 0, downloads: 0 };
13​
14function makeState(overrides: Partial<DesktopUpdateState> = {}): DesktopUpdateState {
15 return {
16 enabled: true,
17 status: "idle",
18 channel: "latest",
19 currentVersion: "1.2.3",
20 hostArch: "arm64",
21 appArch: "arm64",
22 runningUnderArm64Translation: false,
23 availableVersion: null,
24 downloadedVersion: null,
25 releaseNotes: [],
26 downloadPercent: null,
27 checkedAt: null,
28 message: null,
29 errorContext: null,
30 canRetry: false,
31 omittedReleaseCount: 0,
32 ...overrides,
33 };
35​
36describe("nextRemoteDesktopUpdateStep", () => {
37 it("does not install again a version that did not apply on this machine", () => {
38 const step = nextRemoteDesktopUpdateStep(
39 makeState({ status: "available", availableVersion: "1.2.4", notAppliedVersion: "1.2.4" }),
40 NO_ATTEMPTS,
41 null,
42 );
43 assert.equal(step.action, "done");
44 assert.include(step.action === "done" ? step.reason : "", "1.2.4 did not apply");
45 assert.deepEqual(
46 nextRemoteDesktopUpdateStep(
47 makeState({ status: "available", availableVersion: "1.2.5", notAppliedVersion: "1.2.4" }),
48 NO_ATTEMPTS,
49 null,
50 ),
51 { action: "download" },
52 );
53 });
54​
55 it("fails immediately when updates are disabled, preferring the known reason", () => {
56 const state = makeState({ enabled: false, status: "disabled" });
57 assert.deepEqual(nextRemoteDesktopUpdateStep(state, NO_ATTEMPTS, "dev build"), {
58 action: "done",
59 outcome: "failed",
60 reason: "dev build",
61 });
62 assert.deepEqual(nextRemoteDesktopUpdateStep(state, NO_ATTEMPTS, null), {
63 action: "done",
64 outcome: "failed",
65 reason: "Automatic updates are disabled on this machine.",
66 });
67 });
68​
69 it("installs only from the downloaded status the updater will accept", () => {
70 assert.deepEqual(
71 nextRemoteDesktopUpdateStep(
72 makeState({ status: "downloaded", downloadedVersion: "1.2.4" }),
73 NO_ATTEMPTS,
74 null,
75 ),
76 { action: "install" },
77 );
78 assert.deepEqual(
79 nextRemoteDesktopUpdateStep(
80 makeState({
81 status: "downloaded",
82 downloadedVersion: "1.2.4",
83 errorContext: "install",
84 message: "quitAndInstall failed",
85 }),
86 NO_ATTEMPTS,
87 null,
88 ),
89 { action: "install" },
90 );
91 assert.deepEqual(
92 nextRemoteDesktopUpdateStep(
93 makeState({
94 status: "error",
95 downloadedVersion: "1.2.4",
96 errorContext: null,
97 message: "background updater error",
98 }),
99 NO_ATTEMPTS,
100 null,
101 ),
102 { action: "install" },
103 );
104 const staleError = makeState({
105 status: "error",
106 downloadedVersion: "1.2.4",
107 errorContext: "check",
108 message: "feed unreachable",
109 });
110 assert.deepEqual(nextRemoteDesktopUpdateStep(staleError, NO_ATTEMPTS, null), {
111 action: "check",
112 });
113 assert.deepEqual(nextRemoteDesktopUpdateStep(staleError, { checks: 1, downloads: 0 }, null), {
114 action: "done",
115 outcome: "failed",
116 reason: "feed unreachable",
117 });
118 });
119​
120 it("rides along while a check or download is already in flight", () => {
121 assert.deepEqual(
122 nextRemoteDesktopUpdateStep(makeState({ status: "checking" }), NO_ATTEMPTS, null),
123 {
124 action: "wait",
125 },
126 );
127 assert.deepEqual(
128 nextRemoteDesktopUpdateStep(
129 makeState({ status: "downloading", availableVersion: "1.2.4", downloadPercent: 40 }),
130 NO_ATTEMPTS,
131 null,
132 ),
133 { action: "wait" },
134 );
135 });
136​
137 it("downloads an available update until the attempt cap, then fails", () => {
138 const available = makeState({ status: "available", availableVersion: "1.2.4" });
139 assert.deepEqual(nextRemoteDesktopUpdateStep(available, NO_ATTEMPTS, null), {
140 action: "download",
141 });
142 assert.deepEqual(
143 nextRemoteDesktopUpdateStep(
144 available,
145 { checks: 1, downloads: MAX_REMOTE_UPDATE_DOWNLOADS },
146 null,
147 ),
148 {
149 action: "done",
150 outcome: "failed",
151 reason: "The desktop app failed to download the update.",
152 },
153 );
154 assert.deepEqual(
155 nextRemoteDesktopUpdateStep(
156 makeState({
157 status: "available",
158 availableVersion: "1.2.4",
159 message: "network blipped",
160 }),
161 { checks: 1, downloads: MAX_REMOTE_UPDATE_DOWNLOADS },
162 null,
163 ),
164 { action: "done", outcome: "failed", reason: "network blipped" },
165 );
166 });
167​
168 it("re-checks stale up-to-date and error states before trusting them", () => {
169 assert.deepEqual(
170 nextRemoteDesktopUpdateStep(makeState({ status: "up-to-date" }), NO_ATTEMPTS, null),
171 { action: "check" },
172 );
173 assert.deepEqual(
174 nextRemoteDesktopUpdateStep(
175 makeState({ status: "error", message: "feed unreachable" }),
176 NO_ATTEMPTS,
177 null,
178 ),
179 { action: "check" },
180 );
181 });
182​
183 it("reports up-to-date and error states as terminal after this run's check", () => {
184 const checked = { checks: 1, downloads: 0 };
185 assert.deepEqual(
186 nextRemoteDesktopUpdateStep(makeState({ status: "up-to-date" }), checked, null),
187 { action: "done", outcome: "up-to-date" },
188 );
189 assert.deepEqual(
190 nextRemoteDesktopUpdateStep(
191 makeState({ status: "error", message: "feed unreachable" }),
192 checked,
193 null,
194 ),
195 { action: "done", outcome: "failed", reason: "feed unreachable" },
196 );
197 assert.deepEqual(nextRemoteDesktopUpdateStep(makeState({ status: "error" }), checked, null), {
198 action: "done",
199 outcome: "failed",
200 reason: "The desktop app update failed.",
201 });
202 });
203​
204 it("checks from idle until the attempt cap, then fails", () => {
205 assert.deepEqual(nextRemoteDesktopUpdateStep(makeState(), NO_ATTEMPTS, null), {
206 action: "check",
207 });
208 assert.deepEqual(
209 nextRemoteDesktopUpdateStep(
210 makeState(),
211 { checks: MAX_REMOTE_UPDATE_CHECKS, downloads: 0 },
212 null,
213 ),
214 {
215 action: "done",
216 outcome: "failed",
217 reason: "The desktop app did not report an update result.",
218 },
219 );
220 });
221​
222 it("drops blank reasons so the wire report still encodes", () => {
223 assert.equal(normalizeRemoteUpdateReason(undefined), undefined);
224 assert.equal(normalizeRemoteUpdateReason(""), undefined);
225 assert.equal(normalizeRemoteUpdateReason(" "), undefined);
226 assert.equal(normalizeRemoteUpdateReason(" feed unreachable "), "feed unreachable");
227 });
228});