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