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