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 step = nextRemoteDesktopUpdateStep(
37 makeState({ status: "available", availableVersion: "1.2.4", notAppliedVersion: "1.2.4" }),
38 NO_ATTEMPTS,
39 null,
40 );
41 assert.equal(step.action, "done");
42 assert.include(step.action === "done" ? step.reason : "", "1.2.4 did not apply");
43 assert.deepEqual(
44 nextRemoteDesktopUpdateStep(
45 makeState({ status: "available", availableVersion: "1.2.5", notAppliedVersion: "1.2.4" }),
46 NO_ATTEMPTS,
47 null,
48 ),
49 { action: "download" },
50 );
51 });
52
53 it("fails immediately when updates are disabled, preferring the known reason", () => {
54 const state = makeState({ enabled: false, status: "disabled" });
55 assert.deepEqual(nextRemoteDesktopUpdateStep(state, NO_ATTEMPTS, "dev build"), {
56 action: "done",
57 outcome: "failed",
58 reason: "dev build",
59 });
60 assert.deepEqual(nextRemoteDesktopUpdateStep(state, NO_ATTEMPTS, null), {
61 action: "done",
62 outcome: "failed",
63 reason: "Automatic updates are disabled on this machine.",
64 });
65 });
66
67 it("installs only from the downloaded status the updater will accept", () => {
68 assert.deepEqual(
69 nextRemoteDesktopUpdateStep(
70 makeState({ status: "downloaded", downloadedVersion: "1.2.4" }),
71 NO_ATTEMPTS,
72 null,
73 ),
74 { action: "install" },
75 );
76 assert.deepEqual(
77 nextRemoteDesktopUpdateStep(
78 makeState({
79 status: "downloaded",
80 downloadedVersion: "1.2.4",
81 errorContext: "install",
82 message: "quitAndInstall failed",
83 }),
84 NO_ATTEMPTS,
85 null,
86 ),
87 { action: "install" },
88 );
89 assert.deepEqual(
90 nextRemoteDesktopUpdateStep(
91 makeState({
92 status: "error",
93 downloadedVersion: "1.2.4",
94 errorContext: null,
95 message: "background updater error",
96 }),
97 NO_ATTEMPTS,
98 null,
99 ),
100 { action: "install" },
101 );
102 const staleError = makeState({
103 status: "error",
104 downloadedVersion: "1.2.4",
105 errorContext: "check",
106 message: "feed unreachable",
107 });
108 assert.deepEqual(nextRemoteDesktopUpdateStep(staleError, NO_ATTEMPTS, null), {
109 action: "check",
110 });
111 assert.deepEqual(nextRemoteDesktopUpdateStep(staleError, { checks: 1, downloads: 0 }, null), {
112 action: "done",
113 outcome: "failed",
114 reason: "feed unreachable",
115 });
116 });
117
118 it("downloads an available update until the attempt cap, then fails", () => {
119 const available = makeState({ status: "available", availableVersion: "1.2.4" });
120 assert.deepEqual(nextRemoteDesktopUpdateStep(available, NO_ATTEMPTS, null), {
121 action: "download",
122 });
123 assert.deepEqual(
124 nextRemoteDesktopUpdateStep(
125 available,
126 { checks: 1, downloads: MAX_REMOTE_UPDATE_DOWNLOADS },
127 null,
128 ),
129 {
130 action: "done",
131 outcome: "failed",
132 reason: "The desktop app failed to download the update.",
133 },
134 );
135 assert.deepEqual(
136 nextRemoteDesktopUpdateStep(
137 makeState({
138 status: "available",
139 availableVersion: "1.2.4",
140 message: "network blipped",
141 }),
142 { checks: 1, downloads: MAX_REMOTE_UPDATE_DOWNLOADS },
143 null,
144 ),
145 { action: "done", outcome: "failed", reason: "network blipped" },
146 );
147 });
148
149 it("re-checks stale up-to-date and error states before trusting them", () => {
150 assert.deepEqual(
151 nextRemoteDesktopUpdateStep(makeState({ status: "up-to-date" }), NO_ATTEMPTS, null),
152 { action: "check" },
153 );
154 assert.deepEqual(
155 nextRemoteDesktopUpdateStep(
156 makeState({ status: "error", message: "feed unreachable" }),
157 NO_ATTEMPTS,
158 null,
159 ),
160 { action: "check" },
161 );
162 });
163
164 it("reports up-to-date and error states as terminal after this run's check", () => {
165 const checked = { checks: 1, downloads: 0 };
166 assert.deepEqual(
167 nextRemoteDesktopUpdateStep(makeState({ status: "up-to-date" }), checked, null),
168 { action: "done", outcome: "up-to-date" },
169 );
170 assert.deepEqual(
171 nextRemoteDesktopUpdateStep(
172 makeState({ status: "error", message: "feed unreachable" }),
173 checked,
174 null,
175 ),
176 { action: "done", outcome: "failed", reason: "feed unreachable" },
177 );
178 assert.deepEqual(nextRemoteDesktopUpdateStep(makeState({ status: "error" }), checked, null), {
179 action: "done",
180 outcome: "failed",
181 reason: "The desktop app update failed.",
182 });
183 });
184});