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