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

DesktopUpdates.test.ts

965 lines37.2 KB

1import { assert, describe, it } from "@effect/vitest";
2import { DESKTOP_UPDATE_RESTART_MARKER_FILE } from "@t3tools/contracts";
3import * as Cause from "effect/Cause";
4import * as Deferred from "effect/Deferred";
5import * as Duration from "effect/Duration";
6import * as Effect from "effect/Effect";
7import * as Fiber from "effect/Fiber";
8import * as Layer from "effect/Layer";
9import * as Logger from "effect/Logger";
10import * as Option from "effect/Option";
11import * as References from "effect/References";
12import * as Ref from "effect/Ref";
13import * as Stream from "effect/Stream";
14import * as TestClock from "effect/testing/TestClock";
15​
16import * as ElectronUpdater from "../electron/ElectronUpdater.ts";
17import * as DesktopBackendPool from "../backend/DesktopBackendPool.ts";
18import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
19import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
20import * as DesktopState from "../app/DesktopState.ts";
21import * as DesktopUpdates from "./DesktopUpdates.ts";
22import { flushCallbacks, makeHarness } from "./updatesTestHarness.ts";
23​
24describe("DesktopUpdates", () => {
25 it.effect("updates Linux .deb installs and leaves other non-AppImage installs off", () =>
26 Effect.gen(function* () {
27 const linuxState = (packageType: string | undefined) =>
28 Effect.scoped(
29 Effect.gen(function* () {
30 const updates = yield* DesktopUpdates.DesktopUpdates;
31 yield* updates.configure;
32 return yield* updates.getState;
33 }),
34 ).pipe(Effect.provide(makeHarness({ platform: "linux", packageType }).layer));
35​
36 const deb = yield* linuxState("deb\n");
37 assert.equal(deb.status, "idle");
38​
39 const unmarked = yield* linuxState(undefined);
40 assert.equal(unmarked.status, "disabled");
41 }),
42 );
43​
44 it.effect("keeps the disabled reason available after configuration and channel changes", () => {
45 const harness = makeHarness({ env: { T3CODE_DISABLE_AUTO_UPDATE: "true" } });
46 const reason = "Automatic updates are disabled by the T3CODE_DISABLE_AUTO_UPDATE setting.";
47​
48 return Effect.scoped(
49 Effect.gen(function* () {
50 const updates = yield* DesktopUpdates.DesktopUpdates;
51 yield* updates.configure;
52​
53 const configured = yield* updates.getState;
54 assert.equal(configured.message, reason);
55​
56 const changed = yield* updates.setChannel("nightly");
57 assert.equal(changed.channel, "nightly");
58 assert.equal(changed.enabled, false);
59 assert.equal(changed.status, "disabled");
60 assert.equal(changed.message, reason);
61 assert.equal(harness.sentStates.at(-1)?.message, reason);
62 assert.equal(harness.checkCount(), 0);
63 }),
64 ).pipe(Effect.provide(harness.layer));
65 });
66​
67 it.effect("checks for newer releases after an update has been downloaded", () => {
68 const harness = makeHarness();
69​
70 return Effect.scoped(
71 Effect.gen(function* () {
72 const updates = yield* DesktopUpdates.DesktopUpdates;
73 yield* updates.configure;
74​
75 harness.emit("update-available", {
76 version: "1.2.4",
77 releaseNotes: "## What's changed\n- fix: queued update",
78 });
79 yield* flushCallbacks;
80 harness.emit("update-downloaded", { version: "1.2.4" });
81 yield* flushCallbacks;
82​
83 const result = yield* updates.check("poll");
84 assert.isTrue(result.checked);
85​
86 harness.emit("update-available", { version: "1.2.4" });
87 yield* flushCallbacks;
88​
89 const unchangedState = yield* updates.getState;
90 assert.equal(unchangedState.status, "downloaded");
91 assert.equal(unchangedState.downloadedVersion, "1.2.4");
92 assert.deepEqual(unchangedState.releaseNotes, [
93 { version: "1.2.4", items: ["fix: queued update"], totalItems: 1 },
94 ]);
95 assert.equal(unchangedState.omittedReleaseCount, 0);
96​
97 const nextResult = yield* updates.check("poll");
98 assert.isTrue(nextResult.checked);
99​
100 harness.emit("update-available", { version: "1.2.5" });
101 yield* flushCallbacks;
102​
103 const state = yield* updates.getState;
104 assert.equal(state.status, "available");
105 assert.equal(state.availableVersion, "1.2.5");
106 assert.isNull(state.downloadedVersion);
107 }),
108 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
109 });
110​
111 it.effect("preserves a queued installer when the feed has no update", () => {
112 const harness = makeHarness();
113​
114 return Effect.scoped(
115 Effect.gen(function* () {
116 const updates = yield* DesktopUpdates.DesktopUpdates;
117 yield* updates.configure;
118​
119 harness.emit("update-available", {
120 version: "1.2.4",
121 releaseNotes: "## What's changed\n- fix: queued update",
122 });
123 yield* flushCallbacks;
124 harness.emit("update-downloaded", { version: "1.2.4" });
125 yield* flushCallbacks;
126​
127 yield* updates.check("poll");
128 harness.emit("update-not-available");
129 yield* flushCallbacks;
130​
131 const state = yield* updates.getState;
132 assert.equal(state.status, "downloaded");
133 assert.equal(state.availableVersion, "1.2.4");
134 assert.equal(state.downloadedVersion, "1.2.4");
135 assert.deepEqual(state.releaseNotes, [
136 { version: "1.2.4", items: ["fix: queued update"], totalItems: 1 },
137 ]);
138 assert.equal(state.omittedReleaseCount, 0);
139 assert.equal(state.downloadPercent, 100);
140 }),
141 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
142 });
143​
144 it.effect(
145 "rejects install while a refresh check is in progress and releases the reservation",
146 () =>
147 Effect.gen(function* () {
148 const checkStarted = yield* Deferred.make<void>();
149 const releaseCheck = yield* Deferred.make<void>();
150 const harness = makeHarness({
151 checkForUpdates: Deferred.succeed(checkStarted, undefined).pipe(
152 Effect.andThen(Deferred.await(releaseCheck)),
153 ),
154 });
155​
156 yield* Effect.scoped(
157 Effect.gen(function* () {
158 const updates = yield* DesktopUpdates.DesktopUpdates;
159 yield* updates.configure;
160 harness.emit("update-downloaded", { version: "1.2.4" });
161 yield* flushCallbacks;
162​
163 const checkFiber = yield* updates.check("manual").pipe(Effect.forkScoped);
164 yield* Deferred.await(checkStarted);
165​
166 const installResult = yield* updates.install;
167 assert.isFalse(installResult.accepted);
168​
169 yield* Deferred.succeed(releaseCheck, undefined);
170 const checkResult = yield* Fiber.join(checkFiber);
171 assert.isTrue(checkResult.checked);
172​
173 const followUpCheck = yield* updates.check("manual");
174 assert.isTrue(followUpCheck.checked);
175 assert.equal(harness.checkCount(), 2);
176 }),
177 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
178 }),
179 );
180​
181 it.effect("rejects refresh checks while install is in progress", () =>
182 Effect.gen(function* () {
183 const installStarted = yield* Deferred.make<void>();
184 const releaseInstall = yield* Deferred.make<void>();
185 const harness = makeHarness({
186 stopBackend: Deferred.succeed(installStarted, undefined).pipe(
187 Effect.andThen(Deferred.await(releaseInstall)),
188 ),
189 });
190​
191 yield* Effect.scoped(
192 Effect.gen(function* () {
193 const updates = yield* DesktopUpdates.DesktopUpdates;
194 yield* updates.configure;
195 harness.emit("update-downloaded", { version: "1.2.4" });
196 yield* flushCallbacks;
197​
198 const installFiber = yield* updates.install.pipe(Effect.forkScoped);
199 yield* Deferred.await(installStarted);
200​
201 const checkResult = yield* updates.check("manual");
202 assert.isFalse(checkResult.checked);
203 assert.equal(harness.checkCount(), 0);
204​
205 yield* Deferred.succeed(releaseInstall, undefined);
206 const installResult = yield* Fiber.join(installFiber);
207 assert.isTrue(installResult.accepted);
208 }),
209 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
210 }),
211 );
212​
213 it.effect("preserves a queued installer after a background updater error", () => {
214 const harness = makeHarness();
215​
216 return Effect.scoped(
217 Effect.gen(function* () {
218 const updates = yield* DesktopUpdates.DesktopUpdates;
219 yield* updates.configure;
220 harness.emit("update-downloaded", { version: "1.2.4" });
221 yield* flushCallbacks;
222​
223 harness.emit("error", new Error("background updater failure"));
224 yield* flushCallbacks;
225​
226 const state = yield* updates.getState;
227 assert.equal(state.status, "error");
228 assert.equal(state.downloadedVersion, "1.2.4");
229 assert.isNull(state.errorContext);
230​
231 const result = yield* updates.install;
232 assert.isTrue(result.accepted);
233 }),
234 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
235 });
236​
237 it.effect("logs the redacted cause of a rejected update check", () => {
238 const cause = Object.assign(
239 new Error("request failed for https://user:secret@example.com/update?token=secret"),
240 { code: "ERR_UPDATER_LATEST_VERSION_NOT_FOUND" },
241 );
242 const updaterError = new ElectronUpdater.ElectronUpdaterCheckForUpdatesError({
243 channel: null,
244 cause,
245 });
246 const harness = makeHarness({ checkForUpdates: Effect.fail(updaterError) });
247 const loggedAnnotations: Array<Record<string, unknown>> = [];
248 const logger = Logger.make(({ fiber }) => {
249 const annotations = fiber.getRef(References.CurrentLogAnnotations);
250 if (annotations.errorTag === "ElectronUpdaterCheckForUpdatesError") {
251 loggedAnnotations.push(annotations);
252 }
253 });
254​
255 return Effect.scoped(
256 Effect.gen(function* () {
257 const updates = yield* DesktopUpdates.DesktopUpdates;
258 yield* updates.configure;
259​
260 yield* updates.check("manual");
261​
262 const state = yield* updates.getState;
263 const loggedAnnotation = loggedAnnotations.at(-1);
264 assert.isDefined(loggedAnnotation);
265 assert.equal(loggedAnnotation.errorTag, "ElectronUpdaterCheckForUpdatesError");
266 assert.isNull(loggedAnnotation.channel);
267 assert.equal(
268 loggedAnnotation.cause,
269 "Error [ERR_UPDATER_LATEST_VERSION_NOT_FOUND]: request failed for [redacted URL]",
270 );
271 assert.notProperty(loggedAnnotation, "error");
272 assert.notInclude(Object.values(loggedAnnotation).map(String).join(" "), "secret");
273 assert.equal(
274 state.message,
275 "Electron updater failed to check for updates on channel default.",
276 );
277 assert.notInclude(state.message ?? "", "secret");
278 }),
279 ).pipe(
280 Effect.provide(
281 Layer.mergeAll(
282 TestClock.layer(),
283 harness.layer,
284 Logger.layer([logger], { mergeWithExisting: false }),
285 ),
286 ),
287 );
288 });
289​
290 it.effect("recovers download state after an unexpected setup failure", () => {
291 let disableDifferentialCalls = 0;
292 const harness = makeHarness({
293 setDisableDifferentialDownload: Effect.suspend(() => {
294 disableDifferentialCalls += 1;
295 return disableDifferentialCalls === 1
296 ? Effect.void
297 : Effect.die(new Error("download setup failed"));
298 }),
299 });
300​
301 return Effect.scoped(
302 Effect.gen(function* () {
303 const updates = yield* DesktopUpdates.DesktopUpdates;
304 yield* updates.configure;
305 harness.emit("update-available", { version: "1.2.4" });
306 yield* flushCallbacks;
307​
308 const result = yield* updates.download;
309 assert.isTrue(result.accepted);
310 assert.isFalse(result.completed);
311​
312 const failedState = yield* updates.getState;
313 assert.equal(failedState.status, "available");
314 assert.equal(failedState.errorContext, "download");
315 assert.equal(failedState.message, "Desktop update download action failed unexpectedly.");
316​
317 const changedState = yield* updates.setChannel("nightly");
318 assert.equal(changedState.channel, "nightly");
319 }),
320 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
321 });
322​
323 it.effect("restores download state and permits retry after interruption", () =>
324 Effect.gen(function* () {
325 const actionStarted = yield* Deferred.make<void>();
326 let disableDifferentialCalls = 0;
327 const harness = makeHarness({
328 setDisableDifferentialDownload: Effect.suspend(() => {
329 disableDifferentialCalls += 1;
330 if (disableDifferentialCalls === 1) {
331 return Effect.void;
332 }
333 if (disableDifferentialCalls === 2) {
334 return Deferred.succeed(actionStarted, undefined).pipe(Effect.andThen(Effect.never));
335 }
336 return Effect.void;
337 }),
338 });
339​
340 yield* Effect.scoped(
341 Effect.gen(function* () {
342 const updates = yield* DesktopUpdates.DesktopUpdates;
343 yield* updates.configure;
344 harness.emit("update-available", { version: "1.2.4" });
345 yield* flushCallbacks;
346​
347 const downloadFiber = yield* updates.download.pipe(Effect.forkScoped);
348 yield* Deferred.await(actionStarted);
349 yield* Fiber.interrupt(downloadFiber);
350​
351 const interruptedState = yield* updates.getState;
352 assert.equal(interruptedState.status, "available");
353 assert.isNull(interruptedState.message);
354​
355 const retry = yield* updates.download;
356 assert.isTrue(retry.accepted);
357 assert.isTrue(retry.completed);
358 }),
359 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
360 }),
361 );
362​
363 it.effect("clears quitting state after an unexpected install setup failure", () => {
364 const harness = makeHarness({
365 stopBackend: Effect.die(new Error("backend stop failed")),
366 });
367​
368 return Effect.scoped(
369 Effect.gen(function* () {
370 const desktopState = yield* DesktopState.DesktopState;
371 const updates = yield* DesktopUpdates.DesktopUpdates;
372 yield* updates.configure;
373 harness.emit("update-downloaded", { version: "1.2.4" });
374 yield* flushCallbacks;
375​
376 const result = yield* updates.install;
377 assert.isTrue(result.accepted);
378 assert.isFalse(result.completed);
379 assert.isFalse(yield* Ref.get(desktopState.quitting));
380​
381 const failedState = yield* updates.getState;
382 assert.equal(failedState.status, "downloaded");
383 assert.equal(failedState.errorContext, "install");
384 assert.equal(failedState.message, "Desktop update install action failed unexpectedly.");
385​
386 const changedState = yield* updates.setChannel("nightly");
387 assert.equal(changedState.channel, "nightly");
388 }),
389 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
390 });
391​
392 it.effect("marks the backend stop for an install as an update restart", () => {
393 let markersAtStop: ReadonlyArray<string> = [];
394 const harness = makeHarness({
395 stopBackend: Effect.sync(() => {
396 markersAtStop = [...harness.updateRestartMarkers];
397 }),
398 });
399​
400 return Effect.scoped(
401 Effect.gen(function* () {
402 const environment = yield* DesktopEnvironment.DesktopEnvironment;
403 const updates = yield* DesktopUpdates.DesktopUpdates;
404 yield* updates.configure;
405 harness.emit("update-downloaded", { version: "1.2.4" });
406 yield* flushCallbacks;
407​
408 assert.isTrue((yield* updates.install).accepted);
409 assert.deepEqual(markersAtStop, [
410 environment.path.join(environment.baseDir, "runtime", DESKTOP_UPDATE_RESTART_MARKER_FILE),
411 ]);
412 }),
413 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
414 });
415​
416 it.effect("records the version an install hands to the installer", () => {
417 let recordsAtStop: ReadonlyArray<string> = [];
418 const harness = makeHarness({
419 stopBackend: Effect.sync(() => {
420 recordsAtStop = [...harness.installRecords.values()];
421 }),
422 });
423​
424 return Effect.scoped(
425 Effect.gen(function* () {
426 const updates = yield* DesktopUpdates.DesktopUpdates;
427 yield* updates.configure;
428 harness.emit("update-downloaded", { version: "1.2.4" });
429 yield* flushCallbacks;
430​
431 assert.isTrue((yield* updates.install).accepted);
432 assert.deepEqual(
433 recordsAtStop.map((record) => JSON.parse(record)),
434 [{ fromVersion: "1.2.3", targetVersion: "1.2.4" }],
435 );
436 }),
437 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
438 });
439​
440 it.effect("flags an install that did not apply and keeps the flag through checks", () => {
441 const harness = makeHarness({
442 installRecord: JSON.stringify({ fromVersion: "1.2.3", targetVersion: "1.2.4" }),
443 });
444​
445 return Effect.scoped(
446 Effect.gen(function* () {
447 const updates = yield* DesktopUpdates.DesktopUpdates;
448 yield* updates.configure;
449 assert.equal((yield* updates.getState).notAppliedVersion, "1.2.4");
450 assert.isDefined(harness.previousInstallRecord());
451​
452 harness.emit("update-available", { version: "1.2.4" });
453 yield* flushCallbacks;
454 const state = yield* updates.getState;
455 assert.equal(state.status, "available");
456 assert.equal(state.notAppliedVersion, "1.2.4");
457 }),
458 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
459 });
460​
461 // regression: #19291 an install that did apply was still flagged as not applied
462 it.effect("does not flag an install that applied", () => {
463 const harness = makeHarness({
464 installRecord: JSON.stringify({ fromVersion: "1.2.2", targetVersion: "1.2.3" }),
465 });
466​
467 return Effect.scoped(
468 Effect.gen(function* () {
469 const updates = yield* DesktopUpdates.DesktopUpdates;
470 yield* updates.configure;
471 assert.isUndefined((yield* updates.getState).notAppliedVersion);
472 assert.isUndefined(harness.previousInstallRecord());
473 }),
474 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
475 });
476​
477 it.effect("drops the update restart marker when an install is interrupted", () =>
478 Effect.gen(function* () {
479 const stopping = yield* Deferred.make<void>();
480 const harness = makeHarness({
481 stopBackend: Deferred.succeed(stopping, undefined).pipe(Effect.andThen(Effect.never)),
482 });
483​
484 yield* Effect.scoped(
485 Effect.gen(function* () {
486 const updates = yield* DesktopUpdates.DesktopUpdates;
487 yield* updates.configure;
488 harness.emit("update-downloaded", { version: "1.2.4" });
489 yield* flushCallbacks;
490​
491 const installFiber = yield* updates.install.pipe(Effect.forkScoped);
492 yield* Deferred.await(stopping);
493 assert.equal(harness.updateRestartMarkers.size, 1);
494​
495 yield* Fiber.interrupt(installFiber);
496 assert.equal(harness.updateRestartMarkers.size, 0);
497 }),
498 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
499 }),
500 );
501​
502 it.effect("keeps windows and restarts backends when quitAndInstall fails", () => {
503 const harness = makeHarness({
504 quitAndInstall: Effect.fail(
505 new ElectronUpdater.ElectronUpdaterQuitAndInstallError({
506 channel: "latest",
507 isSilent: true,
508 isForceRunAfter: true,
509 cause: new Error("installer refused"),
510 }),
511 ),
512 });
513​
514 return Effect.scoped(
515 Effect.gen(function* () {
516 const desktopState = yield* DesktopState.DesktopState;
517 const updates = yield* DesktopUpdates.DesktopUpdates;
518 yield* updates.configure;
519 harness.emit("update-downloaded", { version: "1.2.4" });
520 yield* flushCallbacks;
521​
522 const result = yield* updates.install;
523 assert.isTrue(result.accepted);
524 assert.isFalse(yield* Ref.get(desktopState.quitting));
525 assert.deepEqual(harness.installSteps, ["quitAndInstall", "startBackend"]);
526 assert.equal(harness.updateRestartMarkers.size, 0);
527 assert.equal(harness.installRecords.size, 0);
528 }),
529 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
530 });
531​
532 it.effect("keeps the disabled primary stopped when quitAndInstall fails", () => {
533 const harness = makeHarness({
534 backendDesiredRunning: false,
535 quitAndInstall: Effect.fail(
536 new ElectronUpdater.ElectronUpdaterQuitAndInstallError({
537 channel: "latest",
538 isSilent: true,
539 isForceRunAfter: true,
540 cause: new Error("installer refused"),
541 }),
542 ),
543 });
544​
545 return Effect.scoped(
546 Effect.gen(function* () {
547 const updates = yield* DesktopUpdates.DesktopUpdates;
548 const pool = yield* DesktopBackendPool.DesktopBackendPool;
549 yield* updates.configure;
550 harness.emit("update-downloaded", { version: "1.2.4" });
551 yield* flushCallbacks;
552​
553 const result = yield* updates.install;
554 assert.equal(result.state.errorContext, "install");
555 assert.deepEqual(harness.installSteps, ["quitAndInstall"]);
556 assert.isFalse((yield* (yield* pool.primary).snapshot).desiredRunning);
557 }),
558 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
559 });
560​
561 it.effect("restores only desired-running instances after an installer error event", () => {
562 const starts: string[] = [];
563 const stops: string[] = [];
564 const makeInstance = (id: DesktopBackendPool.BackendInstanceId, initiallyRunning: boolean) => {
565 let desiredRunning = initiallyRunning;
566 return {
567 id,
568 label: Effect.succeed(id),
569 currentConfig: Effect.succeedNone,
570 start: Effect.sync(() => {
571 starts.push(id);
572 desiredRunning = true;
573 }),
574 stop: () =>
575 Effect.sync(() => {
576 stops.push(id);
577 desiredRunning = false;
578 }),
579 snapshot: Effect.sync(() => ({
580 desiredRunning,
581 ready: false,
582 activePid: Option.none(),
583 restartAttempt: 0,
584 restartScheduled: false,
585 })),
586 waitForReady: () => Effect.succeed(true),
587 } satisfies DesktopBackendPool.DesktopBackendInstance;
588 };
589 const primary = makeInstance(DesktopBackendPool.PRIMARY_INSTANCE_ID, false);
590 const wsl = makeInstance(DesktopBackendPool.BackendInstanceId("wsl:default"), true);
591 const harness = makeHarness({ backendInstances: [primary, wsl] });
592​
593 return Effect.scoped(
594 Effect.gen(function* () {
595 const updates = yield* DesktopUpdates.DesktopUpdates;
596 yield* updates.configure;
597 harness.emit("update-downloaded", { version: "1.2.4" });
598 yield* flushCallbacks;
599​
600 yield* updates.install;
601 assert.sameMembers(stops, [primary.id, wsl.id]);
602 assert.isFalse((yield* wsl.snapshot).desiredRunning);
603 const { changes } = yield* updates.subscribe;
604 const recovered = yield* Stream.runHead(changes).pipe(Effect.forkChild);
605 yield* flushCallbacks;
606 harness.emit("error", new Error("native installer refused"));
607 assert.equal(Option.getOrThrow(yield* Fiber.join(recovered)).errorContext, "install");
608​
609 assert.deepEqual(starts, [wsl.id]);
610 assert.isFalse((yield* primary.snapshot).desiredRunning);
611 assert.isTrue((yield* wsl.snapshot).desiredRunning);
612 }),
613 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
614 });
615​
616 it.effect("holds the install reservation until failed-install recovery finishes", () => {
617 const recoveryStarted = Deferred.makeUnsafe<void>();
618 const releaseRecovery = Deferred.makeUnsafe<void>();
619 const harness = makeHarness({
620 quitAndInstall: Effect.fail(
621 new ElectronUpdater.ElectronUpdaterQuitAndInstallError({
622 channel: "latest",
623 isSilent: true,
624 isForceRunAfter: true,
625 cause: new Error("installer refused"),
626 }),
627 ),
628 startBackend: Deferred.succeed(recoveryStarted, undefined).pipe(
629 Effect.andThen(Deferred.await(releaseRecovery)),
630 ),
631 });
632​
633 return Effect.scoped(
634 Effect.gen(function* () {
635 const updates = yield* DesktopUpdates.DesktopUpdates;
636 yield* updates.configure;
637 harness.emit("update-downloaded", { version: "1.2.4" });
638 yield* flushCallbacks;
639​
640 const failedInstall = yield* updates.install.pipe(Effect.forkChild);
641 yield* Deferred.await(recoveryStarted);
642 assert.isFalse(yield* updates.isInstallActive);
643​
644 const overlappingInstall = yield* updates.install;
645 assert.isFalse(overlappingInstall.accepted);
646 assert.equal(harness.quitAndInstalls(), 1);
647 harness.emit("error", new Error("duplicate native installer error"));
648 yield* flushCallbacks;
649 assert.deepEqual(harness.installSteps, ["quitAndInstall", "startBackend"]);
650​
651 yield* Deferred.succeed(releaseRecovery, undefined);
652 const failedResult = yield* Fiber.join(failedInstall);
653 assert.equal(failedResult.state.errorContext, "install");
654​
655 const retry = yield* updates.install;
656 assert.isTrue(retry.accepted);
657 assert.equal(harness.quitAndInstalls(), 2);
658 }),
659 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
660 });
661​
662 it.effect("rejects a prepared install when the downloaded version changed", () => {
663 const harness = makeHarness();
664​
665 return Effect.scoped(
666 Effect.gen(function* () {
667 const updates = yield* DesktopUpdates.DesktopUpdates;
668 yield* updates.configure;
669 harness.emit("update-downloaded", { version: "1.2.5" });
670 yield* flushCallbacks;
671​
672 const result = yield* updates.installPrepared("1.2.4");
673 assert.isFalse(result.accepted);
674 assert.equal(harness.quitAndInstalls(), 0);
675 }),
676 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
677 });
678​
679 it.effect("persists channel changes through the settings service", () => {
680 const harness = makeHarness();
681​
682 return Effect.scoped(
683 Effect.gen(function* () {
684 const settings = yield* DesktopAppSettings.DesktopAppSettings;
685 const updates = yield* DesktopUpdates.DesktopUpdates;
686 yield* updates.configure;
687​
688 const state = yield* updates.setChannel("nightly");
689 const persistedSettings = yield* settings.get;
690​
691 assert.equal(state.channel, "nightly");
692 assert.equal(persistedSettings.updateChannel, "nightly");
693 assert.equal(persistedSettings.updateChannelConfiguredByUser, true);
694 }),
695 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
696 });
697​
698 it.effect("ignores a finished download from a track it no longer follows", () => {
699 const harness = makeHarness();
700​
701 return Effect.scoped(
702 Effect.gen(function* () {
703 const updates = yield* DesktopUpdates.DesktopUpdates;
704 yield* updates.configure;
705 // A backed-out switch's download keeps reporting progress until it finishes.
706 harness.emit("download-progress", { percent: 40 });
707 yield* flushCallbacks;
708 harness.emit("update-downloaded", { version: "1.2.4-nightly.20261027.1" });
709 yield* flushCallbacks;
710​
711 const state = yield* updates.getState;
712 assert.equal(state.downloadedVersion, null);
713 assert.notEqual(state.status, "downloading");
714 }),
715 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
716 });
717​
718 it.effect("offers older releases only to checks that leave the running build's track", () => {
719 const harness = makeHarness({ beforeSetUpdateChannel: Effect.void });
720​
721 return Effect.scoped(
722 Effect.gen(function* () {
723 const updates = yield* DesktopUpdates.DesktopUpdates;
724 yield* updates.configure;
725 const before = harness.checkAllowedDowngrade.length;
726 yield* updates.setChannel("nightly");
727 yield* updates.setChannel("latest");
728 yield* updates.check("remote-update", { allowDowngrade: true });
729​
730 // Back on its own Stable track the build never needs an older release.
731 assert.deepEqual(harness.checkAllowedDowngrade.slice(before), [true, false, true]);
732 assert.isFalse(harness.allowDowngrade());
733 }),
734 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
735 });
736​
737 it.effect("re-checks a saved track that differs from the running build's track", () => {
738 const harness = makeHarness({
739 appVersion: "1.2.4-nightly.20261027.1",
740 beforeSetUpdateChannel: Effect.void,
741 });
742​
743 return Effect.scoped(
744 Effect.gen(function* () {
745 const updates = yield* DesktopUpdates.DesktopUpdates;
746 yield* updates.configure;
747 yield* updates.setChannel("latest");
748 const checks = harness.checkCount();
749​
750 yield* updates.setChannel("latest");
751​
752 assert.equal(harness.checkCount(), checks + 1);
753 }),
754 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
755 });
756​
757 it.effect("does not persist an unchanged update channel as a user preference", () => {
758 const harness = makeHarness();
759​
760 return Effect.scoped(
761 Effect.gen(function* () {
762 const settings = yield* DesktopAppSettings.DesktopAppSettings;
763 const updates = yield* DesktopUpdates.DesktopUpdates;
764 yield* updates.configure;
765​
766 const state = yield* updates.setChannel("latest");
767 const persistedSettings = yield* settings.get;
768​
769 assert.equal(state.channel, "latest");
770 assert.equal(persistedSettings.updateChannel, "latest");
771 assert.equal(persistedSettings.updateChannelConfiguredByUser, false);
772 }),
773 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
774 });
775​
776 it.effect("offers nothing below a track switch's checked release", () => {
777 const harness = makeHarness({
778 appVersion: "1.2.4-nightly.20261027.1",
779 beforeSetUpdateChannel: Effect.void,
780 });
781​
782 return Effect.scoped(
783 Effect.gen(function* () {
784 const updates = yield* DesktopUpdates.DesktopUpdates;
785 yield* updates.configure;
786 yield* updates.setChannel("latest", { minimumVersion: "1.2.3" });
787​
788 harness.emit("update-available", { version: "1.2.2" });
789 yield* flushCallbacks;
790 assert.isNull((yield* updates.getState).availableVersion);
791 harness.emit("update-downloaded", { version: "1.2.2" });
792 yield* flushCallbacks;
793 assert.isNull((yield* updates.getState).downloadedVersion);
794​
795 harness.emit("update-available", { version: "1.2.3" });
796 yield* flushCallbacks;
797 assert.equal((yield* updates.getState).availableVersion, "1.2.3");
798 }),
799 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
800 });
801​
802 it.effect("keeps a saved Stable track across a restart without offering an older release", () => {
803 // A Nightly build whose switch to Stable did not finish before the app quit.
804 const harness = makeHarness({ appVersion: "1.2.4-nightly.20261027.1", savedChannel: "latest" });
805​
806 return Effect.scoped(
807 Effect.gen(function* () {
808 const updates = yield* DesktopUpdates.DesktopUpdates;
809 yield* updates.configure;
810 yield* updates.check("startup");
811​
812 assert.equal((yield* updates.getState).channel, "latest");
813 assert.deepEqual(harness.checkAllowedDowngrade, [false]);
814 // Semver ranks Stable 1.2.4 above this Nightly, but a Nightly built before the release's
815 // version bump can hold migrations 1.2.4 lacks. Crossing needs a guarded switch.
816 harness.emit("update-available", { version: "1.2.4" });
817 yield* flushCallbacks;
818 assert.isNull((yield* updates.getState).availableVersion);
819 }),
820 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
821 });
822​
823 it.effect("keeps a saved Nightly track across a restart without crossing on its own", () => {
824 // A Stable build whose switch to Nightly was refused before the app quit; its floor is gone.
825 const harness = makeHarness({ savedChannel: "nightly" });
826​
827 return Effect.scoped(
828 Effect.gen(function* () {
829 const updates = yield* DesktopUpdates.DesktopUpdates;
830 yield* updates.configure;
831 yield* updates.check("startup");
832 harness.emit("update-available", { version: "1.2.4-nightly.20261027.1" });
833 yield* flushCallbacks;
834​
835 assert.equal((yield* updates.getState).channel, "nightly");
836 assert.isNull((yield* updates.getState).availableVersion);
837 assert.deepEqual(harness.checkAllowedDowngrade, [false]);
838 }),
839 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
840 });
841​
842 it.effect("never offers an older Nightly to a Nightly build's own checks", () => {
843 const harness = makeHarness({ appVersion: "1.2.4-nightly.20261027.1" });
844​
845 return Effect.scoped(
846 Effect.gen(function* () {
847 const updates = yield* DesktopUpdates.DesktopUpdates;
848 yield* updates.configure;
849 yield* updates.check("poll");
850​
851 assert.equal((yield* updates.getState).channel, "nightly");
852 assert.deepEqual(harness.checkAllowedDowngrade, [false]);
853 }),
854 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
855 });
856​
857 it.effect("waits out a background check instead of refusing a track change", () =>
858 Effect.gen(function* () {
859 const checkStarted = yield* Deferred.make<void>();
860 const releaseCheck = yield* Deferred.make<void>();
861 const harness = makeHarness({
862 beforeSetUpdateChannel: Effect.void,
863 checkForUpdates: Deferred.succeed(checkStarted, undefined).pipe(
864 Effect.andThen(Deferred.await(releaseCheck)),
865 ),
866 });
867​
868 yield* Effect.scoped(
869 Effect.gen(function* () {
870 const updates = yield* DesktopUpdates.DesktopUpdates;
871 yield* updates.configure;
872 yield* updates.check("poll").pipe(Effect.forkScoped);
873 yield* Deferred.await(checkStarted);
874​
875 const channelFiber = yield* updates.setChannel("nightly").pipe(Effect.forkScoped);
876 yield* Deferred.succeed(releaseCheck, undefined);
877 yield* TestClock.adjust(Duration.seconds(2));
878 const state = yield* Fiber.join(channelFiber);
879​
880 assert.equal(state.channel, "nightly");
881 }),
882 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
883 }),
884 );
885​
886 it.effect("rejects checks while an update channel change is being persisted", () =>
887 Effect.gen(function* () {
888 const channelChangeStarted = yield* Deferred.make<void>();
889 const releaseChannelChange = yield* Deferred.make<void>();
890 const harness = makeHarness({
891 beforeSetUpdateChannel: Deferred.succeed(channelChangeStarted, undefined).pipe(
892 Effect.andThen(Deferred.await(releaseChannelChange)),
893 ),
894 });
895​
896 yield* Effect.scoped(
897 Effect.gen(function* () {
898 const updates = yield* DesktopUpdates.DesktopUpdates;
899 yield* updates.configure;
900​
901 const channelFiber = yield* updates.setChannel("nightly").pipe(Effect.forkScoped);
902 yield* Deferred.await(channelChangeStarted);
903​
904 const checkResult = yield* updates.check("manual");
905 assert.isFalse(checkResult.checked);
906 assert.equal(harness.checkCount(), 0);
907​
908 yield* Deferred.succeed(releaseChannelChange, undefined);
909 const state = yield* Fiber.join(channelFiber);
910​
911 assert.equal(state.channel, "nightly");
912 assert.equal(harness.checkCount(), 1);
913 }),
914 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
915 }),
916 );
917});
918​
919describe("describeUpdateErrorCause", () => {
920 it("removes URLs, credentials, response bodies, and headers from summaries", () => {
921 const messages = [
922 "Cannot download https://user:url-secret@example.com/update?token=query-secret, status 403",
923 "Request failed: Bearer bearer-secret",
924 "Request failed: Basic basic-secret",
925 "Request failed: Authorization: Bearer header-secret",
926 "Request failed: Cookie=session-secret",
927 "Request failed: api_key=\"key-secret\" password='password-secret' access_token=token-secret",
928 '404 Not Found\n{"body":"body-secret"}\nHeaders: cookie=header-secret',
929 "Request failed\rHeaders: cookie=header-secret",
930 ];
931​
932 for (const message of messages) {
933 const summary = DesktopUpdates.describeUpdateErrorCause(new Error(message));
934 assert.include(summary, "Error:");
935 assert.notInclude(summary, "secret");
936 assert.notInclude(summary, "https://");
937 assert.notInclude(summary, "Headers:");
938 }
939 assert.equal(
940 DesktopUpdates.describeUpdateErrorCause("404 Not Found\nbody-secret"),
941 "404 Not Found",
942 );
943 });
944​
945 it("bounds long messages, parallel failures, and cyclic causes", () => {
946 const longError = new Error("x".repeat(10_000));
947 const parallel = Cause.combine(Cause.fail(longError), Cause.die(longError));
948 assert.isAtMost(DesktopUpdates.describeUpdateErrorCause(parallel).length, 1024);
949​
950 const cyclic = new Error("retry failed");
951 cyclic.cause = cyclic;
952 assert.include(DesktopUpdates.describeUpdateErrorCause(cyclic), "[truncated cause]");
953 assert.isAtMost(DesktopUpdates.describeUpdateErrorCause(cyclic).length, 1024);
954 });
955​
956 it("does not serialize arbitrary payloads or let unreadable errors break logging", () => {
957 assert.equal(DesktopUpdates.describeUpdateErrorCause({ payload: "secret" }), "object");
958 const unreadable = Object.defineProperty({}, "message", {
959 get() {
960 throw new Error("unreadable error");
961 },
962 });
963 assert.equal(DesktopUpdates.describeUpdateErrorCause(unreadable), "UnknownError");
964 });
965});