Open History to see the last commit
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 DesktopAppSettings from "../settings/DesktopAppSettings.ts";
18import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
19import * as DesktopState from "../app/DesktopState.ts";
20import * as DesktopUpdates from "./DesktopUpdates.ts";
21import { flushCallbacks, makeHarness } from "./updatesTestHarness.ts";
22
23describe("DesktopUpdates", () => {
24 it("preserves complete causes for update poller and event failures", () => {
25 const cause = Cause.combine(
26 Cause.fail(new Error("updater failed")),
27 Cause.die(new Error("updater defect")),
28 );
29 const pollerError = new DesktopUpdates.DesktopUpdatePollerError({
30 poller: "startup",
31 cause,
32 });
33 const eventError = new DesktopUpdates.DesktopUpdateEventHandlingError({
34 event: "download-progress",
35 cause,
36 });
37 const reportedError = new DesktopUpdates.DesktopUpdaterReportedError({
38 operation: "download",
39 cause,
40 });
41 const unexpectedActionError = new DesktopUpdates.DesktopUpdateUnexpectedActionError({
42 action: "install",
43 cause,
44 });
45
46 assert.strictEqual(pollerError.cause, cause);
47 assert.equal(pollerError.poller, "startup");
48 assert.equal(pollerError.message, "Desktop update startup poller failed.");
49 assert.strictEqual(eventError.cause, cause);
50 assert.equal(eventError.event, "download-progress");
51 assert.equal(eventError.message, "Failed to handle desktop update download-progress event.");
52 assert.strictEqual(reportedError.cause, cause);
53 assert.equal(reportedError.operation, "download");
54 assert.equal(reportedError.message, "Desktop updater download operation reported an error.");
55 assert.strictEqual(unexpectedActionError.cause, cause);
56 assert.equal(unexpectedActionError.action, "install");
57 assert.equal(
58 unexpectedActionError.message,
59 "Desktop update install action failed unexpectedly.",
60 );
61 });
62
63 it.effect("configures the updater and runs startup checks on the test clock", () => {
64 const harness = makeHarness();
65
66 return Effect.gen(function* () {
67 yield* Effect.scoped(
68 Effect.gen(function* () {
69 const updates = yield* DesktopUpdates.DesktopUpdates;
70 yield* updates.configure;
71
72 const state = yield* updates.getState;
73 assert.equal(state.enabled, true);
74 assert.equal(state.status, "idle");
75 assert.deepEqual(harness.feedUrls(), [
76 { provider: "generic", url: "http://localhost:4141" },
77 ]);
78 assert.equal(harness.listenerCount(), 6);
79 assert.equal(harness.checkCount(), 0);
80
81 yield* TestClock.adjust(Duration.millis(15_000));
82 assert.equal(harness.checkCount(), 1);
83 }),
84 );
85
86 assert.equal(harness.listenerCount(), 0);
87 }).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
88 });
89
90 it.effect("updates Linux .deb installs and leaves other non-AppImage installs off", () =>
91 Effect.gen(function* () {
92 const linuxState = (packageType: string | undefined) =>
93 Effect.scoped(
94 Effect.gen(function* () {
95 const updates = yield* DesktopUpdates.DesktopUpdates;
96 yield* updates.configure;
97 return yield* updates.getState;
98 }),
99 ).pipe(Effect.provide(makeHarness({ platform: "linux", packageType }).layer));
100
101 const deb = yield* linuxState("deb\n");
102 assert.equal(deb.status, "idle");
103
104 const unmarked = yield* linuxState(undefined);
105 assert.equal(unmarked.status, "disabled");
106 }),
107 );
108
109 it.effect("subscribe delivers the latest state plus subsequent changes", () => {
110 const harness = makeHarness();
111
112 return Effect.scoped(
113 Effect.gen(function* () {
114 const updates = yield* DesktopUpdates.DesktopUpdates;
115 yield* updates.configure;
116
117 const { latest, changes } = yield* updates.subscribe;
118 assert.equal(latest.status, "idle");
119
120 const nextState = yield* Stream.runHead(changes).pipe(Effect.forkChild);
121 yield* flushCallbacks;
122 harness.emit("update-available", { version: "1.2.4" });
123 yield* flushCallbacks;
124
125 const observed = yield* Fiber.join(nextState);
126 assert.equal(Option.getOrThrow(observed).status, "available");
127 assert.equal(Option.getOrThrow(observed).availableVersion, "1.2.4");
128 }),
129 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
130 });
131
132 it.effect("updates and broadcasts state from updater events", () => {
133 const harness = makeHarness();
134
135 return Effect.scoped(
136 Effect.gen(function* () {
137 const updates = yield* DesktopUpdates.DesktopUpdates;
138 yield* updates.configure;
139
140 harness.emit("update-available", { version: "1.2.4" });
141 yield* flushCallbacks;
142
143 const state = yield* updates.getState;
144 assert.equal(state.status, "available");
145 assert.equal(state.availableVersion, "1.2.4");
146 assert.isNotNull(state.checkedAt);
147 assert.equal(harness.sentStates.at(-1)?.status, "available");
148 }),
149 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
150 });
151
152 it.effect("enables nightly full changelog release notes and broadcasts summaries", () => {
153 const harness = makeHarness();
154
155 return Effect.scoped(
156 Effect.gen(function* () {
157 const updates = yield* DesktopUpdates.DesktopUpdates;
158 yield* updates.configure;
159
160 yield* updates.setChannel("nightly");
161 assert.equal(harness.fullChangelog(), true);
162
163 harness.emit("update-available", {
164 version: "1.2.4-nightly.20260709.766",
165 releaseNotes: [
166 {
167 version: "1.2.4-nightly.20260709.766",
168 note: `<h2>What's Changed</h2><ul><li>feat(client): persist offline environment data by <a>@juliusmarminge</a> in <a>#3795</a></li></ul><h2>Full Changelog</h2>`,
169 },
170 {
171 version: "1.2.4-nightly.20260709.765",
172 note: "- [codex] Upgrade Clerk stack by @juliusmarminge in #3821",
173 },
174 { version: "1.2.4-nightly.20260709.764", note: "- Change 764" },
175 { version: "1.2.4-nightly.20260709.763", note: "- Change 763" },
176 { version: "1.2.4-nightly.20260709.762", note: "- Change 762" },
177 { version: "1.2.4-nightly.20260709.761", note: "- Change 761" },
178 { version: "1.2.4-nightly.20260709.760", note: "- Change 760" },
179 ],
180 });
181 yield* flushCallbacks;
182
183 const state = yield* updates.getState;
184 assert.equal(state.status, "available");
185 assert.deepEqual(state.releaseNotes, [
186 {
187 version: "1.2.4-nightly.20260709.766",
188 items: ["feat(client): persist offline environment data by @juliusmarminge in #3795"],
189 totalItems: 1,
190 },
191 {
192 version: "1.2.4-nightly.20260709.765",
193 items: ["[codex] Upgrade Clerk stack by @juliusmarminge in #3821"],
194 totalItems: 1,
195 },
196 { version: "1.2.4-nightly.20260709.764", items: ["Change 764"], totalItems: 1 },
197 { version: "1.2.4-nightly.20260709.763", items: ["Change 763"], totalItems: 1 },
198 { version: "1.2.4-nightly.20260709.762", items: ["Change 762"], totalItems: 1 },
199 { version: "1.2.4-nightly.20260709.761", items: ["Change 761"], totalItems: 1 },
200 ]);
201 assert.equal(state.omittedReleaseCount, 1);
202 assert.deepEqual(harness.sentStates.at(-1)?.releaseNotes, state.releaseNotes);
203 assert.equal(harness.sentStates.at(-1)?.omittedReleaseCount, 1);
204 }),
205 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
206 });
207
208 it.effect("checks for newer releases after an update has been downloaded", () => {
209 const harness = makeHarness();
210
211 return Effect.scoped(
212 Effect.gen(function* () {
213 const updates = yield* DesktopUpdates.DesktopUpdates;
214 yield* updates.configure;
215
216 harness.emit("update-available", {
217 version: "1.2.4",
218 releaseNotes: "## What's changed\n- fix: queued update",
219 });
220 yield* flushCallbacks;
221 harness.emit("update-downloaded", { version: "1.2.4" });
222 yield* flushCallbacks;
223
224 const result = yield* updates.check("poll");
225 assert.isTrue(result.checked);
226
227 harness.emit("update-available", { version: "1.2.4" });
228 yield* flushCallbacks;
229
230 const unchangedState = yield* updates.getState;
231 assert.equal(unchangedState.status, "downloaded");
232 assert.equal(unchangedState.downloadedVersion, "1.2.4");
233 assert.deepEqual(unchangedState.releaseNotes, [
234 { version: "1.2.4", items: ["fix: queued update"], totalItems: 1 },
235 ]);
236 assert.equal(unchangedState.omittedReleaseCount, 0);
237
238 const nextResult = yield* updates.check("poll");
239 assert.isTrue(nextResult.checked);
240
241 harness.emit("update-available", { version: "1.2.5" });
242 yield* flushCallbacks;
243
244 const state = yield* updates.getState;
245 assert.equal(state.status, "available");
246 assert.equal(state.availableVersion, "1.2.5");
247 assert.isNull(state.downloadedVersion);
248 }),
249 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
250 });
251
252 it.effect("preserves a queued installer when the feed has no update", () => {
253 const harness = makeHarness();
254
255 return Effect.scoped(
256 Effect.gen(function* () {
257 const updates = yield* DesktopUpdates.DesktopUpdates;
258 yield* updates.configure;
259
260 harness.emit("update-available", {
261 version: "1.2.4",
262 releaseNotes: "## What's changed\n- fix: queued update",
263 });
264 yield* flushCallbacks;
265 harness.emit("update-downloaded", { version: "1.2.4" });
266 yield* flushCallbacks;
267
268 yield* updates.check("poll");
269 harness.emit("update-not-available");
270 yield* flushCallbacks;
271
272 const state = yield* updates.getState;
273 assert.equal(state.status, "downloaded");
274 assert.equal(state.availableVersion, "1.2.4");
275 assert.equal(state.downloadedVersion, "1.2.4");
276 assert.deepEqual(state.releaseNotes, [
277 { version: "1.2.4", items: ["fix: queued update"], totalItems: 1 },
278 ]);
279 assert.equal(state.omittedReleaseCount, 0);
280 assert.equal(state.downloadPercent, 100);
281 }),
282 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
283 });
284
285 it.effect("preserves a queued installer when the feed offers another channel", () => {
286 const harness = makeHarness();
287
288 return Effect.scoped(
289 Effect.gen(function* () {
290 const updates = yield* DesktopUpdates.DesktopUpdates;
291 yield* updates.configure;
292
293 harness.emit("update-available", {
294 version: "1.2.4",
295 releaseNotes: "## What's changed\n- fix: queued update",
296 });
297 yield* flushCallbacks;
298 harness.emit("update-downloaded", { version: "1.2.4" });
299 yield* flushCallbacks;
300
301 yield* updates.check("poll");
302 harness.emit("update-available", { version: "1.2.5-nightly.20260710.1" });
303 yield* flushCallbacks;
304
305 const state = yield* updates.getState;
306 assert.equal(state.status, "downloaded");
307 assert.equal(state.availableVersion, "1.2.4");
308 assert.equal(state.downloadedVersion, "1.2.4");
309 assert.deepEqual(state.releaseNotes, [
310 { version: "1.2.4", items: ["fix: queued update"], totalItems: 1 },
311 ]);
312 assert.equal(state.omittedReleaseCount, 0);
313 assert.equal(state.downloadPercent, 100);
314 }),
315 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
316 });
317
318 it.effect(
319 "rejects install while a refresh check is in progress and releases the reservation",
320 () =>
321 Effect.gen(function* () {
322 const checkStarted = yield* Deferred.make<void>();
323 const releaseCheck = yield* Deferred.make<void>();
324 const harness = makeHarness({
325 checkForUpdates: Deferred.succeed(checkStarted, undefined).pipe(
326 Effect.andThen(Deferred.await(releaseCheck)),
327 ),
328 });
329
330 yield* Effect.scoped(
331 Effect.gen(function* () {
332 const updates = yield* DesktopUpdates.DesktopUpdates;
333 yield* updates.configure;
334 harness.emit("update-downloaded", { version: "1.2.4" });
335 yield* flushCallbacks;
336
337 const checkFiber = yield* updates.check("manual").pipe(Effect.forkScoped);
338 yield* Deferred.await(checkStarted);
339
340 const installResult = yield* updates.install;
341 assert.isFalse(installResult.accepted);
342
343 yield* Deferred.succeed(releaseCheck, undefined);
344 const checkResult = yield* Fiber.join(checkFiber);
345 assert.isTrue(checkResult.checked);
346
347 const followUpCheck = yield* updates.check("manual");
348 assert.isTrue(followUpCheck.checked);
349 assert.equal(harness.checkCount(), 2);
350 }),
351 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
352 }),
353 );
354
355 it.effect("rejects refresh checks while install is in progress", () =>
356 Effect.gen(function* () {
357 const installStarted = yield* Deferred.make<void>();
358 const releaseInstall = yield* Deferred.make<void>();
359 const harness = makeHarness({
360 stopBackend: Deferred.succeed(installStarted, undefined).pipe(
361 Effect.andThen(Deferred.await(releaseInstall)),
362 ),
363 });
364
365 yield* Effect.scoped(
366 Effect.gen(function* () {
367 const updates = yield* DesktopUpdates.DesktopUpdates;
368 yield* updates.configure;
369 harness.emit("update-downloaded", { version: "1.2.4" });
370 yield* flushCallbacks;
371
372 const installFiber = yield* updates.install.pipe(Effect.forkScoped);
373 yield* Deferred.await(installStarted);
374
375 const checkResult = yield* updates.check("manual");
376 assert.isFalse(checkResult.checked);
377 assert.equal(harness.checkCount(), 0);
378
379 yield* Deferred.succeed(releaseInstall, undefined);
380 const installResult = yield* Fiber.join(installFiber);
381 assert.isTrue(installResult.accepted);
382 }),
383 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
384 }),
385 );
386
387 it.effect("keeps raw updater event failures out of update state", () => {
388 const harness = makeHarness();
389 const cause = new Error(
390 "request failed for https://user:secret@example.com/update?token=secret",
391 );
392
393 return Effect.scoped(
394 Effect.gen(function* () {
395 const updates = yield* DesktopUpdates.DesktopUpdates;
396 yield* updates.configure;
397
398 harness.emit("error", cause);
399 yield* flushCallbacks;
400
401 const state = yield* updates.getState;
402 assert.equal(state.status, "error");
403 assert.equal(state.message, "Desktop updater background operation reported an error.");
404 assert.notInclude(state.message ?? "", "secret");
405 }),
406 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
407 });
408
409 it.effect("preserves a queued installer after a background updater error", () => {
410 const harness = makeHarness();
411
412 return Effect.scoped(
413 Effect.gen(function* () {
414 const updates = yield* DesktopUpdates.DesktopUpdates;
415 yield* updates.configure;
416 harness.emit("update-downloaded", { version: "1.2.4" });
417 yield* flushCallbacks;
418
419 harness.emit("error", new Error("background updater failure"));
420 yield* flushCallbacks;
421
422 const state = yield* updates.getState;
423 assert.equal(state.status, "error");
424 assert.equal(state.downloadedVersion, "1.2.4");
425 assert.isNull(state.errorContext);
426
427 const result = yield* updates.install;
428 assert.isTrue(result.accepted);
429 }),
430 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
431 });
432
433 it.effect("logs bounded updater failure context without exposing the cause", () => {
434 const cause = new Error(
435 "request failed for https://user:secret@example.com/update?token=secret",
436 );
437 const updaterError = new ElectronUpdater.ElectronUpdaterCheckForUpdatesError({
438 channel: null,
439 cause,
440 });
441 const harness = makeHarness({ checkForUpdates: Effect.fail(updaterError) });
442 const loggedAnnotations: Array<Record<string, unknown>> = [];
443 const logger = Logger.make(({ fiber }) => {
444 const annotations = fiber.getRef(References.CurrentLogAnnotations);
445 if (annotations.errorTag === "ElectronUpdaterCheckForUpdatesError") {
446 loggedAnnotations.push(annotations);
447 }
448 });
449
450 return Effect.scoped(
451 Effect.gen(function* () {
452 const updates = yield* DesktopUpdates.DesktopUpdates;
453 yield* updates.configure;
454
455 yield* updates.check("manual");
456
457 const state = yield* updates.getState;
458 const loggedAnnotation = loggedAnnotations.at(-1);
459 assert.isDefined(loggedAnnotation);
460 assert.equal(loggedAnnotation.errorTag, "ElectronUpdaterCheckForUpdatesError");
461 assert.isNull(loggedAnnotation.channel);
462 assert.notProperty(loggedAnnotation, "error");
463 assert.notInclude(Object.values(loggedAnnotation).map(String).join(" "), "secret");
464 assert.equal(
465 state.message,
466 "Electron updater failed to check for updates on channel default.",
467 );
468 assert.notInclude(state.message ?? "", "secret");
469 }),
470 ).pipe(
471 Effect.provide(
472 Layer.mergeAll(
473 TestClock.layer(),
474 harness.layer,
475 Logger.layer([logger], { mergeWithExisting: false }),
476 ),
477 ),
478 );
479 });
480
481 it.effect("recovers download state after an unexpected setup failure", () => {
482 let disableDifferentialCalls = 0;
483 const harness = makeHarness({
484 setDisableDifferentialDownload: Effect.suspend(() => {
485 disableDifferentialCalls += 1;
486 return disableDifferentialCalls === 1
487 ? Effect.void
488 : Effect.die(new Error("download setup failed"));
489 }),
490 });
491
492 return Effect.scoped(
493 Effect.gen(function* () {
494 const updates = yield* DesktopUpdates.DesktopUpdates;
495 yield* updates.configure;
496 harness.emit("update-available", { version: "1.2.4" });
497 yield* flushCallbacks;
498
499 const result = yield* updates.download;
500 assert.isTrue(result.accepted);
501 assert.isFalse(result.completed);
502
503 const failedState = yield* updates.getState;
504 assert.equal(failedState.status, "available");
505 assert.equal(failedState.errorContext, "download");
506 assert.equal(failedState.message, "Desktop update download action failed unexpectedly.");
507
508 const changedState = yield* updates.setChannel("nightly");
509 assert.equal(changedState.channel, "nightly");
510 }),
511 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
512 });
513
514 it.effect("restores download state and permits retry after interruption", () =>
515 Effect.gen(function* () {
516 const actionStarted = yield* Deferred.make<void>();
517 let disableDifferentialCalls = 0;
518 const harness = makeHarness({
519 setDisableDifferentialDownload: Effect.suspend(() => {
520 disableDifferentialCalls += 1;
521 if (disableDifferentialCalls === 1) {
522 return Effect.void;
523 }
524 if (disableDifferentialCalls === 2) {
525 return Deferred.succeed(actionStarted, undefined).pipe(Effect.andThen(Effect.never));
526 }
527 return Effect.void;
528 }),
529 });
530
531 yield* Effect.scoped(
532 Effect.gen(function* () {
533 const updates = yield* DesktopUpdates.DesktopUpdates;
534 yield* updates.configure;
535 harness.emit("update-available", { version: "1.2.4" });
536 yield* flushCallbacks;
537
538 const downloadFiber = yield* updates.download.pipe(Effect.forkScoped);
539 yield* Deferred.await(actionStarted);
540 yield* Fiber.interrupt(downloadFiber);
541
542 const interruptedState = yield* updates.getState;
543 assert.equal(interruptedState.status, "available");
544 assert.isNull(interruptedState.message);
545
546 const retry = yield* updates.download;
547 assert.isTrue(retry.accepted);
548 assert.isTrue(retry.completed);
549 }),
550 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
551 }),
552 );
553
554 it.effect("clears quitting state after an unexpected install setup failure", () => {
555 const harness = makeHarness({
556 stopBackend: Effect.die(new Error("backend stop failed")),
557 });
558
559 return Effect.scoped(
560 Effect.gen(function* () {
561 const desktopState = yield* DesktopState.DesktopState;
562 const updates = yield* DesktopUpdates.DesktopUpdates;
563 yield* updates.configure;
564 harness.emit("update-downloaded", { version: "1.2.4" });
565 yield* flushCallbacks;
566
567 const result = yield* updates.install;
568 assert.isTrue(result.accepted);
569 assert.isFalse(result.completed);
570 assert.isFalse(yield* Ref.get(desktopState.quitting));
571
572 const failedState = yield* updates.getState;
573 assert.equal(failedState.status, "downloaded");
574 assert.equal(failedState.errorContext, "install");
575 assert.equal(failedState.message, "Desktop update install action failed unexpectedly.");
576
577 const changedState = yield* updates.setChannel("nightly");
578 assert.equal(changedState.channel, "nightly");
579 }),
580 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
581 });
582
583 it.effect("marks the backend stop for an install as an update restart", () => {
584 let markersAtStop: ReadonlyArray<string> = [];
585 const harness = makeHarness({
586 stopBackend: Effect.sync(() => {
587 markersAtStop = [...harness.updateRestartMarkers];
588 }),
589 });
590
591 return Effect.scoped(
592 Effect.gen(function* () {
593 const environment = yield* DesktopEnvironment.DesktopEnvironment;
594 const updates = yield* DesktopUpdates.DesktopUpdates;
595 yield* updates.configure;
596 harness.emit("update-downloaded", { version: "1.2.4" });
597 yield* flushCallbacks;
598
599 assert.isTrue((yield* updates.install).accepted);
600 assert.deepEqual(markersAtStop, [
601 environment.path.join(environment.baseDir, "runtime", DESKTOP_UPDATE_RESTART_MARKER_FILE),
602 ]);
603 }),
604 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
605 });
606
607 it.effect("drops the update restart marker when an install is interrupted", () =>
608 Effect.gen(function* () {
609 const stopping = yield* Deferred.make<void>();
610 const harness = makeHarness({
611 stopBackend: Deferred.succeed(stopping, undefined).pipe(Effect.andThen(Effect.never)),
612 });
613
614 yield* Effect.scoped(
615 Effect.gen(function* () {
616 const updates = yield* DesktopUpdates.DesktopUpdates;
617 yield* updates.configure;
618 harness.emit("update-downloaded", { version: "1.2.4" });
619 yield* flushCallbacks;
620
621 const installFiber = yield* updates.install.pipe(Effect.forkScoped);
622 yield* Deferred.await(stopping);
623 assert.equal(harness.updateRestartMarkers.size, 1);
624
625 yield* Fiber.interrupt(installFiber);
626 assert.equal(harness.updateRestartMarkers.size, 0);
627 }),
628 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
629 }),
630 );
631
632 it.effect("keeps windows and restarts backends when quitAndInstall fails", () => {
633 const harness = makeHarness({
634 quitAndInstall: Effect.fail(
635 new ElectronUpdater.ElectronUpdaterQuitAndInstallError({
636 channel: "latest",
637 isSilent: true,
638 isForceRunAfter: true,
639 cause: new Error("installer refused"),
640 }),
641 ),
642 });
643
644 return Effect.scoped(
645 Effect.gen(function* () {
646 const desktopState = yield* DesktopState.DesktopState;
647 const updates = yield* DesktopUpdates.DesktopUpdates;
648 yield* updates.configure;
649 harness.emit("update-downloaded", { version: "1.2.4" });
650 yield* flushCallbacks;
651
652 const result = yield* updates.install;
653 assert.isTrue(result.accepted);
654 assert.isFalse(yield* Ref.get(desktopState.quitting));
655 assert.deepEqual(harness.installSteps, ["quitAndInstall", "startBackend"]);
656 // The restarted old backend must release its tunnel on a later quit.
657 assert.equal(harness.updateRestartMarkers.size, 0);
658 }),
659 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
660 });
661
662 it.effect("holds the install reservation until failed-install recovery finishes", () => {
663 const recoveryStarted = Deferred.makeUnsafe<void>();
664 const releaseRecovery = Deferred.makeUnsafe<void>();
665 const harness = makeHarness({
666 quitAndInstall: Effect.fail(
667 new ElectronUpdater.ElectronUpdaterQuitAndInstallError({
668 channel: "latest",
669 isSilent: true,
670 isForceRunAfter: true,
671 cause: new Error("installer refused"),
672 }),
673 ),
674 startBackend: Deferred.succeed(recoveryStarted, undefined).pipe(
675 Effect.andThen(Deferred.await(releaseRecovery)),
676 ),
677 });
678
679 return Effect.scoped(
680 Effect.gen(function* () {
681 const updates = yield* DesktopUpdates.DesktopUpdates;
682 yield* updates.configure;
683 harness.emit("update-downloaded", { version: "1.2.4" });
684 yield* flushCallbacks;
685
686 const failedInstall = yield* updates.install.pipe(Effect.forkChild);
687 yield* Deferred.await(recoveryStarted);
688 assert.isFalse(yield* updates.isInstallActive);
689
690 const overlappingInstall = yield* updates.install;
691 assert.isFalse(overlappingInstall.accepted);
692 assert.equal(harness.quitAndInstalls(), 1);
693 harness.emit("error", new Error("duplicate native installer error"));
694 yield* flushCallbacks;
695 assert.deepEqual(harness.installSteps, ["quitAndInstall", "startBackend"]);
696
697 yield* Deferred.succeed(releaseRecovery, undefined);
698 const failedResult = yield* Fiber.join(failedInstall);
699 assert.equal(failedResult.state.errorContext, "install");
700
701 const retry = yield* updates.install;
702 assert.isTrue(retry.accepted);
703 assert.equal(harness.quitAndInstalls(), 2);
704 }),
705 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
706 });
707
708 it.effect("recovers when quitAndInstall reports failure through an updater event", () => {
709 const harness = makeHarness();
710
711 return Effect.scoped(
712 Effect.gen(function* () {
713 const desktopState = yield* DesktopState.DesktopState;
714 const updates = yield* DesktopUpdates.DesktopUpdates;
715 yield* updates.configure;
716 harness.emit("update-downloaded", { version: "1.2.4" });
717 yield* flushCallbacks;
718
719 yield* updates.install;
720 assert.deepEqual(harness.installSteps, ["quitAndInstall"]);
721 harness.emit("error", new Error("native installer refused"));
722 yield* flushCallbacks;
723
724 assert.isFalse(yield* Ref.get(desktopState.quitting));
725 assert.deepEqual(harness.installSteps, ["quitAndInstall", "startBackend"]);
726 assert.equal((yield* updates.getState).errorContext, "install");
727 }),
728 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
729 });
730
731 it.effect("rejects a prepared install when the downloaded version changed", () => {
732 const harness = makeHarness();
733
734 return Effect.scoped(
735 Effect.gen(function* () {
736 const updates = yield* DesktopUpdates.DesktopUpdates;
737 yield* updates.configure;
738 harness.emit("update-downloaded", { version: "1.2.5" });
739 yield* flushCallbacks;
740
741 const result = yield* updates.installPrepared("1.2.4");
742 assert.isFalse(result.accepted);
743 assert.equal(harness.quitAndInstalls(), 0);
744 }),
745 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
746 });
747
748 it.effect("persists channel changes through the settings service", () => {
749 const harness = makeHarness();
750
751 return Effect.scoped(
752 Effect.gen(function* () {
753 const settings = yield* DesktopAppSettings.DesktopAppSettings;
754 const updates = yield* DesktopUpdates.DesktopUpdates;
755 yield* updates.configure;
756
757 const state = yield* updates.setChannel("nightly");
758 const persistedSettings = yield* settings.get;
759
760 assert.equal(state.channel, "nightly");
761 assert.equal(persistedSettings.updateChannel, "nightly");
762 assert.equal(persistedSettings.updateChannelConfiguredByUser, true);
763 }),
764 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
765 });
766
767 it.effect("does not persist an unchanged update channel as a user preference", () => {
768 const harness = makeHarness();
769
770 return Effect.scoped(
771 Effect.gen(function* () {
772 const settings = yield* DesktopAppSettings.DesktopAppSettings;
773 const updates = yield* DesktopUpdates.DesktopUpdates;
774 yield* updates.configure;
775
776 const state = yield* updates.setChannel("latest");
777 const persistedSettings = yield* settings.get;
778
779 assert.equal(state.channel, "latest");
780 assert.equal(persistedSettings.updateChannel, "latest");
781 assert.equal(persistedSettings.updateChannelConfiguredByUser, false);
782 }),
783 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
784 });
785
786 it.effect("fails channel changes with a typed error while a check is in progress", () =>
787 Effect.gen(function* () {
788 const checkStarted = yield* Deferred.make<void>();
789 const releaseCheck = yield* Deferred.make<void>();
790 const harness = makeHarness({
791 checkForUpdates: Deferred.succeed(checkStarted, undefined).pipe(
792 Effect.andThen(Deferred.await(releaseCheck)),
793 ),
794 });
795
796 yield* Effect.scoped(
797 Effect.gen(function* () {
798 const updates = yield* DesktopUpdates.DesktopUpdates;
799 yield* updates.configure;
800
801 const checkFiber = yield* updates.check("manual").pipe(Effect.forkScoped);
802 yield* Deferred.await(checkStarted);
803
804 const exit = yield* Effect.exit(updates.setChannel("nightly"));
805 assert.equal(exit._tag, "Failure");
806 if (exit._tag === "Failure") {
807 const error = Cause.squash(exit.cause);
808 assert.instanceOf(error, DesktopUpdates.DesktopUpdateActionInProgressError);
809 assert.equal(error.action, "check");
810 assert.equal(error.requestedChannel, "nightly");
811 }
812
813 yield* Deferred.succeed(releaseCheck, undefined);
814 yield* Fiber.join(checkFiber);
815 }),
816 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
817 }),
818 );
819
820 it.effect("rejects checks while an update channel change is being persisted", () =>
821 Effect.gen(function* () {
822 const channelChangeStarted = yield* Deferred.make<void>();
823 const releaseChannelChange = yield* Deferred.make<void>();
824 const harness = makeHarness({
825 beforeSetUpdateChannel: Deferred.succeed(channelChangeStarted, undefined).pipe(
826 Effect.andThen(Deferred.await(releaseChannelChange)),
827 ),
828 });
829
830 yield* Effect.scoped(
831 Effect.gen(function* () {
832 const updates = yield* DesktopUpdates.DesktopUpdates;
833 yield* updates.configure;
834
835 const channelFiber = yield* updates.setChannel("nightly").pipe(Effect.forkScoped);
836 yield* Deferred.await(channelChangeStarted);
837
838 const checkResult = yield* updates.check("manual");
839 assert.isFalse(checkResult.checked);
840 assert.equal(harness.checkCount(), 0);
841
842 yield* Deferred.succeed(releaseChannelChange, undefined);
843 const state = yield* Fiber.join(channelFiber);
844
845 assert.equal(state.channel, "nightly");
846 assert.equal(harness.checkCount(), 1);
847 }),
848 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
849 }),
850 );
851
852 it.effect("preserves settings failure context when an update channel cannot be persisted", () => {
853 const diskFailure = new Error("disk exploded");
854 const settingsFailure = new DesktopAppSettings.DesktopSettingsWriteError({
855 operation: "replace-settings-file",
856 path: "/tmp/settings.json",
857 cause: diskFailure,
858 });
859 const harness = makeHarness({ setUpdateChannelError: settingsFailure });
860
861 return Effect.scoped(
862 Effect.gen(function* () {
863 const updates = yield* DesktopUpdates.DesktopUpdates;
864 yield* updates.configure;
865
866 const error = yield* updates.setChannel("nightly").pipe(Effect.flip);
867
868 assert.instanceOf(error, DesktopUpdates.DesktopUpdateChannelPersistenceError);
869 assert.equal(error.channel, "nightly");
870 assert.strictEqual(error.cause, settingsFailure);
871 assert.strictEqual(error.cause.cause, diskFailure);
872 assert.equal(error.message, "Failed to persist the nightly desktop update channel.");
873 assert.notInclude(error.message, diskFailure.message);
874
875 const checkResult = yield* updates.check("manual");
876 assert.isTrue(checkResult.checked);
877 assert.equal(harness.checkCount(), 1);
878 }),
879 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
880 });
881});