Open History to see the last commit
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 };
34}
35
36describe("nextRemoteDesktopUpdateStep", () => {
37 it("fails immediately when updates are disabled, preferring the known reason", () => {
38 const state = makeState({ enabled: false, status: "disabled" });
39 assert.deepEqual(nextRemoteDesktopUpdateStep(state, NO_ATTEMPTS, "dev build"), {
40 action: "done",
41 outcome: "failed",
42 reason: "dev build",
43 });
44 assert.deepEqual(nextRemoteDesktopUpdateStep(state, NO_ATTEMPTS, null), {
45 action: "done",
46 outcome: "failed",
47 reason: "Automatic updates are disabled on this machine.",
48 });
49 });
50
51 it("installs only from the downloaded status the updater will accept", () => {
52 assert.deepEqual(
53 nextRemoteDesktopUpdateStep(
54 makeState({ status: "downloaded", downloadedVersion: "1.2.4" }),
55 NO_ATTEMPTS,
56 null,
57 ),
58 { action: "install" },
59 );
60 // A previous install failure keeps status "downloaded", so a remote run
61 // retries the install.
62 assert.deepEqual(
63 nextRemoteDesktopUpdateStep(
64 makeState({
65 status: "downloaded",
66 downloadedVersion: "1.2.4",
67 errorContext: "install",
68 message: "quitAndInstall failed",
69 }),
70 NO_ATTEMPTS,
71 null,
72 ),
73 { action: "install" },
74 );
75 // A download survives an unrelated background updater error, so a run
76 // installs it instead of replaying that error.
77 assert.deepEqual(
78 nextRemoteDesktopUpdateStep(
79 makeState({
80 status: "error",
81 downloadedVersion: "1.2.4",
82 errorContext: null,
83 message: "background updater error",
84 }),
85 NO_ATTEMPTS,
86 null,
87 ),
88 { action: "install" },
89 );
90 // A leftover download behind a check error is not installable: fresh
91 // runs re-check, and post-check the error is terminal.
92 const staleError = makeState({
93 status: "error",
94 downloadedVersion: "1.2.4",
95 errorContext: "check",
96 message: "feed unreachable",
97 });
98 assert.deepEqual(nextRemoteDesktopUpdateStep(staleError, NO_ATTEMPTS, null), {
99 action: "check",
100 });
101 assert.deepEqual(nextRemoteDesktopUpdateStep(staleError, { checks: 1, downloads: 0 }, null), {
102 action: "done",
103 outcome: "failed",
104 reason: "feed unreachable",
105 });
106 });
107
108 it("rides along while a check or download is already in flight", () => {
109 assert.deepEqual(
110 nextRemoteDesktopUpdateStep(makeState({ status: "checking" }), NO_ATTEMPTS, null),
111 {
112 action: "wait",
113 },
114 );
115 assert.deepEqual(
116 nextRemoteDesktopUpdateStep(
117 makeState({ status: "downloading", availableVersion: "1.2.4", downloadPercent: 40 }),
118 NO_ATTEMPTS,
119 null,
120 ),
121 { action: "wait" },
122 );
123 });
124
125 it("downloads an available update until the attempt cap, then fails", () => {
126 const available = makeState({ status: "available", availableVersion: "1.2.4" });
127 assert.deepEqual(nextRemoteDesktopUpdateStep(available, NO_ATTEMPTS, null), {
128 action: "download",
129 });
130 assert.deepEqual(
131 nextRemoteDesktopUpdateStep(
132 available,
133 { checks: 1, downloads: MAX_REMOTE_UPDATE_DOWNLOADS },
134 null,
135 ),
136 {
137 action: "done",
138 outcome: "failed",
139 reason: "The desktop app failed to download the update.",
140 },
141 );
142 assert.deepEqual(
143 nextRemoteDesktopUpdateStep(
144 makeState({
145 status: "available",
146 availableVersion: "1.2.4",
147 message: "network blipped",
148 }),
149 { checks: 1, downloads: MAX_REMOTE_UPDATE_DOWNLOADS },
150 null,
151 ),
152 { action: "done", outcome: "failed", reason: "network blipped" },
153 );
154 });
155
156 it("re-checks stale up-to-date and error states before trusting them", () => {
157 // These states are retained from earlier/background checks; a remote
158 // request must look again instead of replaying them.
159 assert.deepEqual(
160 nextRemoteDesktopUpdateStep(makeState({ status: "up-to-date" }), NO_ATTEMPTS, null),
161 { action: "check" },
162 );
163 assert.deepEqual(
164 nextRemoteDesktopUpdateStep(
165 makeState({ status: "error", message: "feed unreachable" }),
166 NO_ATTEMPTS,
167 null,
168 ),
169 { action: "check" },
170 );
171 });
172
173 it("reports up-to-date and error states as terminal after this run's check", () => {
174 const checked = { checks: 1, downloads: 0 };
175 assert.deepEqual(
176 nextRemoteDesktopUpdateStep(makeState({ status: "up-to-date" }), checked, null),
177 { action: "done", outcome: "up-to-date" },
178 );
179 assert.deepEqual(
180 nextRemoteDesktopUpdateStep(
181 makeState({ status: "error", message: "feed unreachable" }),
182 checked,
183 null,
184 ),
185 { action: "done", outcome: "failed", reason: "feed unreachable" },
186 );
187 assert.deepEqual(nextRemoteDesktopUpdateStep(makeState({ status: "error" }), checked, null), {
188 action: "done",
189 outcome: "failed",
190 reason: "The desktop app update failed.",
191 });
192 });
193
194 it("checks from idle until the attempt cap, then fails", () => {
195 assert.deepEqual(nextRemoteDesktopUpdateStep(makeState(), NO_ATTEMPTS, null), {
196 action: "check",
197 });
198 assert.deepEqual(
199 nextRemoteDesktopUpdateStep(
200 makeState(),
201 { checks: MAX_REMOTE_UPDATE_CHECKS, downloads: 0 },
202 null,
203 ),
204 {
205 action: "done",
206 outcome: "failed",
207 reason: "The desktop app did not report an update result.",
208 },
209 );
210 });
211
212 it("drops blank reasons so the wire report still encodes", () => {
213 assert.equal(normalizeRemoteUpdateReason(undefined), undefined);
214 assert.equal(normalizeRemoteUpdateReason(""), undefined);
215 assert.equal(normalizeRemoteUpdateReason(" "), undefined);
216 assert.equal(normalizeRemoteUpdateReason(" feed unreachable "), "feed unreachable");
217 });
218});