Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import type {
3 DesktopTelemetryRequestDesktopUpdate,
4 DesktopTelemetryCommitDesktopUpdate,
5 DesktopTelemetryCancelDesktopUpdate,
6 DesktopUpdateStatusReport,
7} from "@t3tools/contracts";
8import * as Deferred from "effect/Deferred";
9import * as Duration from "effect/Duration";
10import * as Effect from "effect/Effect";
11import * as Fiber from "effect/Fiber";
12import * as Layer from "effect/Layer";
13import * as Queue from "effect/Queue";
14import * as Ref from "effect/Ref";
15import * as Stream from "effect/Stream";
16import * as TestClock from "effect/testing/TestClock";
17
18import * as ElectronUpdater from "../electron/ElectronUpdater.ts";
19import * as DesktopState from "../app/DesktopState.ts";
20import * as DesktopTelemetryPublisher from "../telemetry/DesktopTelemetryPublisher.ts";
21import * as DesktopRemoteUpdates from "./DesktopRemoteUpdates.ts";
22import * as DesktopUpdates from "./DesktopUpdates.ts";
23import { makeHarness } from "./updatesTestHarness.ts";
24
25// Updater runPromise handlers need real microtasks between test-runtime fiber yields.
26const settle = Effect.gen(function* () {
27 for (let i = 0; i < 20; i += 1) {
28 yield* Effect.yieldNow;
29 yield* Effect.promise(() => Promise.resolve());
30 }
31});
32
33const request = (requestId: string): DesktopTelemetryRequestDesktopUpdate => ({
34 version: 1,
35 type: "requestDesktopUpdate",
36 requestId,
37});
38
39function runRemoteUpdatesTest(
40 harness: ReturnType<typeof makeHarness>,
41 body: (context: {
42 readonly reports: DesktopUpdateStatusReport[];
43 readonly requests: Queue.Queue<DesktopTelemetryRequestDesktopUpdate>;
44 readonly commits: Queue.Queue<DesktopTelemetryCommitDesktopUpdate>;
45 readonly cancellations: Queue.Queue<DesktopTelemetryCancelDesktopUpdate>;
46 }) => Effect.Effect<void, never, DesktopUpdates.DesktopUpdates | DesktopState.DesktopState>,
47) {
48 return Effect.scoped(
49 Effect.gen(function* () {
50 const requests = yield* Queue.unbounded<DesktopTelemetryRequestDesktopUpdate>();
51 const commits = yield* Queue.unbounded<DesktopTelemetryCommitDesktopUpdate>();
52 const cancellations = yield* Queue.unbounded<DesktopTelemetryCancelDesktopUpdate>();
53 const reports: DesktopUpdateStatusReport[] = [];
54 const publisher = DesktopTelemetryPublisher.DesktopTelemetryPublisher.of({
55 latest: Effect.succeedNone,
56 changes: Stream.empty,
57 encoded: Stream.empty,
58 handleControlForSource: () => Effect.void,
59 removeControlSource: () => Effect.void,
60 publishUpdateReport: (report) =>
61 Effect.sync(() => {
62 reports.push(report);
63 }),
64 updateRequests: Stream.fromQueue(requests),
65 updateCommits: Stream.fromQueue(commits),
66 updateCancellations: Stream.fromQueue(cancellations),
67 });
68
69 const updates = yield* DesktopUpdates.DesktopUpdates;
70 yield* updates.configure;
71 yield* DesktopRemoteUpdates.listen.pipe(
72 Effect.provideService(DesktopTelemetryPublisher.DesktopTelemetryPublisher, publisher),
73 );
74 yield* settle;
75 yield* body({ reports, requests, commits, cancellations });
76 }),
77 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
78}
79
80function terminalReports(reports: DesktopUpdateStatusReport[]): DesktopUpdateStatusReport[] {
81 return reports.filter((report) => report.outcome !== undefined);
82}
83
84describe("DesktopRemoteUpdates", () => {
85 it.effect("drives check, download, and install with no local confirmation", () => {
86 const harness = makeHarness();
87
88 return runRemoteUpdatesTest(harness, ({ reports, requests, commits }) =>
89 Effect.gen(function* () {
90 yield* Queue.offer(requests, request("req-1"));
91 yield* settle;
92 assert.equal(harness.checkCount(), 1);
93
94 harness.emit("update-available", { version: "1.2.4" });
95 yield* settle;
96 harness.emit("update-downloaded", { version: "1.2.4" });
97 yield* settle;
98
99 const terminals = terminalReports(reports);
100 assert.equal(terminals.length, 1);
101 assert.equal(terminals[0]?.outcome, "ready-to-install");
102 assert.equal(terminals[0]?.requestId, "req-1");
103 assert.equal(harness.quitAndInstalls(), 0);
104
105 yield* Queue.offer(commits, {
106 version: 1,
107 type: "commitDesktopUpdate",
108 requestId: "req-1",
109 });
110 yield* settle;
111 assert.equal(harness.quitAndInstalls(), 1);
112
113 const statuses = reports
114 .filter((report) => report.requestId === "req-1")
115 .map((report) => report.state.status);
116 assert.include(statuses, "available");
117 assert.include(statuses, "downloaded");
118 }),
119 );
120 });
121
122 it.effect("switches to the requested track before preparing its release", () => {
123 // In-memory settings keep the channel write off real disk I/O.
124 const harness = makeHarness({ beforeSetUpdateChannel: Effect.void });
125
126 return runRemoteUpdatesTest(harness, ({ reports, requests }) =>
127 Effect.gen(function* () {
128 const updates = yield* DesktopUpdates.DesktopUpdates;
129 yield* Queue.offer(requests, {
130 ...request("req-switch"),
131 channel: "nightly",
132 targetVersion: "1.2.4-nightly.20261027.1",
133 });
134 yield* settle;
135 assert.equal((yield* updates.getState).channel, "nightly");
136
137 harness.emit("update-available", { version: "1.2.4-nightly.20261027.1" });
138 yield* settle;
139 harness.emit("update-downloaded", { version: "1.2.4-nightly.20261027.1" });
140 yield* settle;
141
142 const terminals = terminalReports(reports);
143 assert.deepEqual(
144 terminals.map((report) => [report.requestId, report.outcome]),
145 [["req-switch", "ready-to-install"]],
146 );
147 assert.equal(terminals[0]?.state.downloadedVersion, "1.2.4-nightly.20261027.1");
148 }),
149 );
150 });
151
152 it.effect(
153 "refuses a crossing whose feed has only an older build, and never offers it later",
154 () => {
155 const harness = makeHarness({ beforeSetUpdateChannel: Effect.void });
156
157 return runRemoteUpdatesTest(harness, ({ reports, requests }) =>
158 Effect.gen(function* () {
159 const updates = yield* DesktopUpdates.DesktopUpdates;
160 const older = "1.2.4-nightly.20261020.1";
161 yield* Queue.offer(requests, {
162 ...request("req-older"),
163 channel: "nightly",
164 targetVersion: "1.2.4-nightly.20261027.1",
165 });
166 yield* settle;
167 // Answers the track change's check, then the request's own check.
168 harness.emit("update-available", { version: older });
169 yield* settle;
170 harness.emit("update-available", { version: older });
171 yield* settle;
172
173 assert.deepEqual(
174 terminalReports(reports).map((report) => [report.outcome, report.reason]),
175 [["failed", "The desktop app found no build of 1.2.4-nightly.20261027.1."]],
176 );
177 // The saved track is the user's intent; a retry picks it up from here.
178 assert.equal((yield* updates.getState).channel, "nightly");
179
180 // A plain update afterwards cannot reach the build the switch refused.
181 yield* Queue.offer(requests, request("req-plain"));
182 yield* settle;
183 harness.emit("update-available", { version: older });
184 yield* settle;
185 assert.isNull((yield* updates.getState).availableVersion);
186 assert.equal(harness.downloadCount(), 0);
187 }),
188 );
189 },
190 );
191
192 it.effect("only saves the track on a host already running the release", () => {
193 const harness = makeHarness({ savedChannel: "nightly" });
194
195 return runRemoteUpdatesTest(harness, ({ reports, requests }) =>
196 Effect.gen(function* () {
197 const updates = yield* DesktopUpdates.DesktopUpdates;
198 yield* Queue.offer(requests, {
199 ...request("req-track-only"),
200 channel: "latest",
201 targetVersion: "1.2.3",
202 });
203 yield* settle;
204 // Even with a newer build on the feed, the host only saves its track.
205 harness.emit("update-available", { version: "1.2.4" });
206 yield* settle;
207
208 assert.deepEqual(
209 terminalReports(reports).map((report) => report.outcome),
210 ["up-to-date"],
211 );
212 assert.equal((yield* updates.getState).channel, "latest");
213 assert.equal(harness.downloadCount(), 0);
214 }),
215 );
216 });
217
218 it.effect("drops a retained download below the switch's checked release", () => {
219 const harness = makeHarness({ beforeSetUpdateChannel: Effect.void });
220
221 return runRemoteUpdatesTest(harness, ({ reports, requests }) =>
222 Effect.gen(function* () {
223 const updates = yield* DesktopUpdates.DesktopUpdates;
224 // Downloaded earlier on this Stable host, before the switch checked 1.2.5.
225 harness.emit("update-downloaded", { version: "1.2.4" });
226 yield* settle;
227 assert.equal((yield* updates.getState).downloadedVersion, "1.2.4");
228
229 yield* Queue.offer(requests, {
230 ...request("req-floor"),
231 channel: "latest",
232 targetVersion: "1.2.5",
233 });
234 yield* settle;
235 harness.emit("update-available", { version: "1.2.5" });
236 yield* settle;
237 harness.emit("update-downloaded", { version: "1.2.5" });
238 yield* settle;
239
240 assert.deepEqual(
241 terminalReports(reports).map((report) => [
242 report.outcome,
243 report.state.downloadedVersion,
244 ]),
245 [["ready-to-install", "1.2.5"]],
246 );
247 }),
248 );
249 });
250
251 it.effect("refuses to cross tracks remotely while WSL backends need their own checks", () => {
252 const harness = makeHarness({ beforeSetUpdateChannel: Effect.void, wslBackend: true });
253
254 return runRemoteUpdatesTest(harness, ({ reports, requests }) =>
255 Effect.gen(function* () {
256 const updates = yield* DesktopUpdates.DesktopUpdates;
257 yield* Queue.offer(requests, {
258 ...request("req-wsl"),
259 channel: "nightly",
260 targetVersion: "1.2.4-nightly.20261027.1",
261 });
262 yield* settle;
263
264 assert.deepEqual(
265 terminalReports(reports).map((report) => report.outcome),
266 ["failed"],
267 );
268 assert.include(terminalReports(reports)[0]?.reason ?? "", "Switch it on that machine");
269 assert.equal((yield* updates.getState).channel, "latest");
270 }),
271 );
272 });
273
274 it.effect("fails a switch that leaves a same-track host below the checked release", () => {
275 const harness = makeHarness({ beforeSetUpdateChannel: Effect.void });
276
277 return runRemoteUpdatesTest(harness, ({ reports, requests }) =>
278 Effect.gen(function* () {
279 yield* Queue.offer(requests, {
280 ...request("req-behind"),
281 channel: "latest",
282 targetVersion: "1.2.5",
283 });
284 yield* settle;
285 // The feed lags npm and has nothing newer than this host's 1.2.3.
286 harness.emit("update-not-available");
287 yield* settle;
288 harness.emit("update-not-available");
289 yield* settle;
290
291 assert.deepEqual(
292 terminalReports(reports).map((report) => [report.outcome, report.reason]),
293 [["failed", "The desktop app found no build of 1.2.5."]],
294 );
295 }),
296 );
297 });
298
299 it.effect("fails a crossing to an older-numbered Stable that the feed does not offer", () => {
300 const harness = makeHarness({
301 appVersion: "1.2.4-nightly.20261027.1",
302 beforeSetUpdateChannel: Effect.void,
303 });
304
305 return runRemoteUpdatesTest(harness, ({ reports, requests }) =>
306 Effect.gen(function* () {
307 yield* Queue.offer(requests, {
308 ...request("req-to-stable"),
309 channel: "latest",
310 targetVersion: "1.2.3",
311 });
312 yield* settle;
313 harness.emit("update-not-available");
314 yield* settle;
315 harness.emit("update-not-available");
316 yield* settle;
317
318 // Still on Nightly, though the target is numbered below this build.
319 assert.deepEqual(
320 terminalReports(reports).map((report) => [report.outcome, report.reason]),
321 [["failed", "The desktop app found no build of 1.2.3."]],
322 );
323 }),
324 );
325 });
326
327 it.effect("accepts a newer build than the checked release on the requested track", () => {
328 const harness = makeHarness({ beforeSetUpdateChannel: Effect.void });
329
330 return runRemoteUpdatesTest(harness, ({ reports, requests }) =>
331 Effect.gen(function* () {
332 const newer = "1.2.4-nightly.20261028.2";
333 yield* Queue.offer(requests, {
334 ...request("req-newer"),
335 channel: "nightly",
336 targetVersion: "1.2.4-nightly.20261027.1",
337 });
338 yield* settle;
339 harness.emit("update-available", { version: newer });
340 yield* settle;
341 harness.emit("update-downloaded", { version: newer });
342 yield* settle;
343
344 assert.deepEqual(
345 terminalReports(reports).map((report) => [
346 report.outcome,
347 report.state.downloadedVersion,
348 ]),
349 [["ready-to-install", newer]],
350 );
351 }),
352 );
353 });
354
355 it.effect("reports a failed outcome when quitAndInstall fails", () => {
356 const harness = makeHarness({
357 quitAndInstall: Effect.fail(
358 new ElectronUpdater.ElectronUpdaterQuitAndInstallError({
359 channel: "latest",
360 isSilent: true,
361 isForceRunAfter: true,
362 cause: new Error("spawn failed"),
363 }),
364 ),
365 });
366
367 return runRemoteUpdatesTest(harness, ({ reports, requests, commits }) =>
368 Effect.gen(function* () {
369 yield* Queue.offer(requests, request("req-4"));
370 yield* settle;
371 harness.emit("update-available", { version: "1.2.4" });
372 yield* settle;
373 harness.emit("update-downloaded", { version: "1.2.4" });
374 yield* settle;
375
376 yield* Queue.offer(commits, {
377 version: 1,
378 type: "commitDesktopUpdate",
379 requestId: "req-4",
380 });
381 yield* settle;
382
383 const terminals = terminalReports(reports);
384 assert.deepEqual(
385 terminals.map((report) => report.outcome),
386 ["ready-to-install", "failed"],
387 );
388 assert.equal(terminals[1]?.state.errorContext, "install");
389 }),
390 );
391 });
392
393 it.effect("joins an in-progress install on commit instead of failing the token", () => {
394 const installStarted = Deferred.makeUnsafe<void>();
395 const releaseInstall = Deferred.makeUnsafe<void>();
396 const harness = makeHarness({
397 stopBackend: Deferred.succeed(installStarted, undefined).pipe(
398 Effect.andThen(Deferred.await(releaseInstall)),
399 ),
400 });
401
402 return runRemoteUpdatesTest(harness, ({ reports, requests, commits }) =>
403 Effect.gen(function* () {
404 const updates = yield* DesktopUpdates.DesktopUpdates;
405 yield* Queue.offer(requests, request("req-join-commit"));
406 yield* settle;
407 harness.emit("update-available", { version: "1.2.4" });
408 yield* settle;
409 harness.emit("update-downloaded", { version: "1.2.4" });
410 yield* settle;
411 assert.deepEqual(
412 terminalReports(reports).map((report) => report.outcome),
413 ["ready-to-install"],
414 );
415
416 const localInstall = yield* updates.install.pipe(Effect.forkChild);
417 yield* Deferred.await(installStarted);
418 yield* Queue.offer(commits, {
419 version: 1,
420 type: "commitDesktopUpdate",
421 requestId: "req-join-commit",
422 });
423 yield* settle;
424
425 assert.deepEqual(
426 terminalReports(reports).map((report) => report.outcome),
427 ["ready-to-install"],
428 );
429 assert.equal(harness.quitAndInstalls(), 0);
430
431 yield* Deferred.succeed(releaseInstall, undefined);
432 yield* Fiber.join(localInstall);
433 }),
434 );
435 });
436
437 it.effect("does not join a normal app quit as an update install", () => {
438 const harness = makeHarness();
439
440 return runRemoteUpdatesTest(harness, ({ reports, requests, commits }) =>
441 Effect.gen(function* () {
442 const desktopState = yield* DesktopState.DesktopState;
443 yield* Queue.offer(requests, request("req-normal-quit"));
444 yield* settle;
445 harness.emit("update-available", { version: "1.2.4" });
446 yield* settle;
447 harness.emit("update-downloaded", { version: "1.2.4" });
448 yield* settle;
449
450 yield* Ref.set(desktopState.quitting, true);
451 yield* Queue.offer(commits, {
452 version: 1,
453 type: "commitDesktopUpdate",
454 requestId: "req-normal-quit",
455 });
456 yield* settle;
457
458 assert.deepEqual(
459 terminalReports(reports).map((report) => report.outcome),
460 ["ready-to-install", "failed"],
461 );
462 assert.equal(harness.quitAndInstalls(), 0);
463 }),
464 );
465 });
466
467 it.effect("does not misread a lingering install error as a failed retry", () => {
468 let installAttempts = 0;
469 const harness = makeHarness({
470 quitAndInstall: Effect.suspend(() => {
471 installAttempts += 1;
472 return installAttempts === 1
473 ? Effect.fail(
474 new ElectronUpdater.ElectronUpdaterQuitAndInstallError({
475 channel: "latest",
476 isSilent: true,
477 isForceRunAfter: true,
478 cause: new Error("first attempt failed"),
479 }),
480 )
481 : Effect.void;
482 }),
483 });
484
485 return runRemoteUpdatesTest(harness, ({ reports, requests, commits }) =>
486 Effect.gen(function* () {
487 yield* Queue.offer(requests, request("req-5"));
488 yield* settle;
489 harness.emit("update-available", { version: "1.2.4" });
490 yield* settle;
491 harness.emit("update-downloaded", { version: "1.2.4" });
492 yield* settle;
493 yield* Queue.offer(commits, {
494 version: 1,
495 type: "commitDesktopUpdate",
496 requestId: "req-5",
497 });
498 yield* settle;
499 yield* Queue.offer(requests, request("req-6"));
500 yield* settle;
501 yield* Queue.offer(commits, {
502 version: 1,
503 type: "commitDesktopUpdate",
504 requestId: "req-6",
505 });
506 yield* settle;
507
508 const retryTerminals = terminalReports(reports).filter(
509 (report) => report.requestId === "req-6",
510 );
511 assert.deepEqual(
512 retryTerminals.map((report) => report.outcome),
513 ["ready-to-install"],
514 );
515 assert.equal(harness.quitAndInstalls(), 2);
516 }),
517 );
518 });
519
520 it.effect("ignores an unrelated updater event while an install is starting", () => {
521 const stopStarted = Deferred.makeUnsafe<void>();
522 const releaseStop = Deferred.makeUnsafe<void>();
523 const harness = makeHarness({
524 stopBackend: Deferred.succeed(stopStarted, undefined).pipe(
525 Effect.andThen(Deferred.await(releaseStop)),
526 ),
527 });
528
529 return runRemoteUpdatesTest(harness, ({ reports, requests, commits }) =>
530 Effect.gen(function* () {
531 yield* Queue.offer(requests, request("req-late-event"));
532 yield* settle;
533 harness.emit("update-available", { version: "1.2.4" });
534 yield* settle;
535 harness.emit("update-downloaded", { version: "1.2.4" });
536 yield* settle;
537
538 yield* Queue.offer(commits, {
539 version: 1,
540 type: "commitDesktopUpdate",
541 requestId: "req-late-event",
542 });
543 yield* Deferred.await(stopStarted);
544 harness.emit("download-progress", { percent: 90 });
545 yield* settle;
546 yield* Deferred.succeed(releaseStop, undefined);
547 yield* settle;
548
549 assert.deepEqual(
550 terminalReports(reports)
551 .filter((report) => report.requestId === "req-late-event")
552 .map((report) => report.outcome),
553 ["ready-to-install"],
554 );
555 }),
556 );
557 });
558
559 it.effect("retries a download refused while the check still holds the reservation", () => {
560 const releaseCheck = Deferred.makeUnsafe<void>();
561 const harness = makeHarness({ checkForUpdates: Deferred.await(releaseCheck) });
562
563 return runRemoteUpdatesTest(harness, ({ reports, requests }) =>
564 Effect.gen(function* () {
565 yield* Queue.offer(requests, request("req-8"));
566 yield* settle;
567 assert.equal(harness.checkCount(), 1);
568
569 harness.emit("update-available", { version: "1.2.4" });
570 yield* settle;
571 assert.equal(harness.downloadCount(), 0);
572
573 yield* Deferred.succeed(releaseCheck, undefined);
574 yield* settle;
575 yield* TestClock.adjust(Duration.millis(300));
576 yield* settle;
577 assert.equal(harness.downloadCount(), 1);
578
579 harness.emit("update-downloaded", { version: "1.2.4" });
580 yield* settle;
581 assert.deepEqual(
582 terminalReports(reports).map((report) => report.outcome),
583 ["ready-to-install"],
584 );
585 assert.equal(harness.quitAndInstalls(), 0);
586 }),
587 );
588 });
589
590 it.effect("waits for the download reservation before reporting prepared", () => {
591 const releaseDownload = Deferred.makeUnsafe<void>();
592 const harness = makeHarness({ downloadUpdate: Deferred.await(releaseDownload) });
593
594 return runRemoteUpdatesTest(harness, ({ reports, requests }) =>
595 Effect.gen(function* () {
596 yield* Queue.offer(requests, request("req-10"));
597 yield* settle;
598 harness.emit("update-available", { version: "1.2.4" });
599 yield* settle;
600 harness.emit("update-downloaded", { version: "1.2.4" });
601 yield* settle;
602 assert.deepEqual(terminalReports(reports), []);
603 assert.equal(harness.quitAndInstalls(), 0);
604
605 yield* Deferred.succeed(releaseDownload, undefined);
606 yield* settle;
607 yield* TestClock.adjust(Duration.millis(300));
608 yield* settle;
609 assert.deepEqual(
610 terminalReports(reports).map((report) => report.outcome),
611 ["ready-to-install"],
612 );
613 assert.equal(harness.quitAndInstalls(), 0);
614 }),
615 );
616 });
617
618 it.effect("joins an install that is already tearing the app down", () => {
619 const harness = makeHarness();
620
621 return runRemoteUpdatesTest(harness, ({ reports, requests }) =>
622 Effect.gen(function* () {
623 const desktopState = yield* DesktopState.DesktopState;
624 harness.emit("update-downloaded", { version: "1.2.4" });
625 yield* settle;
626 yield* Ref.set(desktopState.quitting, true);
627
628 yield* Queue.offer(requests, request("req-9"));
629 yield* settle;
630
631 assert.deepEqual(
632 terminalReports(reports).map((report) => report.outcome),
633 ["ready-to-install"],
634 );
635 assert.equal(harness.quitAndInstalls(), 0);
636 }),
637 );
638 });
639
640 it.effect("does not run the installer twice for a repeated commit", () => {
641 const harness = makeHarness();
642
643 return runRemoteUpdatesTest(harness, ({ requests, commits }) =>
644 Effect.gen(function* () {
645 yield* Queue.offer(requests, request("req-repeat"));
646 yield* settle;
647 harness.emit("update-available", { version: "1.2.4" });
648 yield* settle;
649 harness.emit("update-downloaded", { version: "1.2.4" });
650 yield* settle;
651
652 const commit = {
653 version: 1,
654 type: "commitDesktopUpdate",
655 requestId: "req-repeat",
656 } as const;
657 yield* Queue.offer(commits, commit);
658 yield* settle;
659 yield* Queue.offer(commits, commit);
660 yield* settle;
661
662 assert.equal(harness.quitAndInstalls(), 1);
663 }),
664 );
665 });
666
667 it.effect("waits for a background check before installing a prepared update", () => {
668 const backgroundCheckStarted = Deferred.makeUnsafe<void>();
669 const releaseBackgroundCheck = Deferred.makeUnsafe<void>();
670 let checks = 0;
671 const harness = makeHarness({
672 checkForUpdates: Effect.suspend(() => {
673 checks += 1;
674 return checks === 2
675 ? Deferred.succeed(backgroundCheckStarted, undefined).pipe(
676 Effect.andThen(Deferred.await(releaseBackgroundCheck)),
677 )
678 : Effect.void;
679 }),
680 });
681
682 return runRemoteUpdatesTest(harness, ({ requests, commits }) =>
683 Effect.gen(function* () {
684 const updates = yield* DesktopUpdates.DesktopUpdates;
685 yield* Queue.offer(requests, request("req-background-check"));
686 yield* settle;
687 harness.emit("update-available", { version: "1.2.4" });
688 yield* settle;
689 harness.emit("update-downloaded", { version: "1.2.4" });
690 yield* settle;
691
692 const backgroundCheck = yield* updates.check("poll").pipe(Effect.forkChild);
693 yield* Deferred.await(backgroundCheckStarted);
694 assert.equal((yield* updates.getState).status, "checking");
695
696 yield* Queue.offer(commits, {
697 version: 1,
698 type: "commitDesktopUpdate",
699 requestId: "req-background-check",
700 });
701 yield* settle;
702 assert.equal(harness.quitAndInstalls(), 0);
703
704 harness.emit("update-downloaded", { version: "1.2.4" });
705 yield* Deferred.succeed(releaseBackgroundCheck, undefined);
706 yield* Fiber.join(backgroundCheck);
707 yield* settle;
708 assert.equal(harness.quitAndInstalls(), 1);
709 }),
710 );
711 });
712
713 it.effect("rejects a new preparation while an install commit is active", () => {
714 const installStarted = Deferred.makeUnsafe<void>();
715 const releaseInstall = Deferred.makeUnsafe<void>();
716 const harness = makeHarness({
717 quitAndInstall: Deferred.succeed(installStarted, undefined).pipe(
718 Effect.andThen(Deferred.await(releaseInstall)),
719 Effect.andThen(
720 Effect.fail(
721 new ElectronUpdater.ElectronUpdaterQuitAndInstallError({
722 channel: "latest",
723 isSilent: true,
724 isForceRunAfter: true,
725 cause: new Error("installer refused"),
726 }),
727 ),
728 ),
729 ),
730 });
731
732 return runRemoteUpdatesTest(harness, ({ reports, requests, commits }) =>
733 Effect.gen(function* () {
734 yield* Queue.offer(requests, request("req-active"));
735 yield* settle;
736 harness.emit("update-available", { version: "1.2.4" });
737 yield* settle;
738 harness.emit("update-downloaded", { version: "1.2.4" });
739 yield* settle;
740 yield* Queue.offer(commits, {
741 version: 1,
742 type: "commitDesktopUpdate",
743 requestId: "req-active",
744 });
745 yield* Deferred.await(installStarted);
746
747 yield* Queue.offer(requests, request("req-overlap"));
748 yield* settle;
749 const overlap = terminalReports(reports).find(
750 (report) => report.requestId === "req-overlap",
751 );
752 assert.equal(overlap?.outcome, "failed");
753 assert.equal(overlap?.reason, "A prepared desktop update is already in progress.");
754
755 yield* Deferred.succeed(releaseInstall, undefined);
756 yield* settle;
757 }),
758 );
759 });
760
761 it.effect("cancels a track switch while its own feed check is still running", () => {
762 const releaseCheck = Deferred.makeUnsafe<void>();
763 const harness = makeHarness({
764 beforeSetUpdateChannel: Effect.void,
765 checkForUpdates: Deferred.await(releaseCheck),
766 });
767
768 return runRemoteUpdatesTest(harness, ({ requests, cancellations }) =>
769 Effect.gen(function* () {
770 yield* Queue.offer(requests, {
771 ...request("req-switch-cancel"),
772 channel: "nightly",
773 targetVersion: "1.2.4-nightly.20261027.1",
774 });
775 yield* settle;
776 const updates = yield* DesktopUpdates.DesktopUpdates;
777 yield* Queue.offer(cancellations, {
778 version: 1,
779 type: "cancelDesktopUpdate",
780 requestId: "req-switch-cancel",
781 });
782 yield* settle;
783 // The next request starts without waiting for the cancelled switch's feed check.
784 yield* Queue.offer(requests, {
785 ...request("req-switch-back"),
786 channel: "latest",
787 targetVersion: "1.2.3",
788 });
789 yield* settle;
790
791 assert.equal((yield* updates.getState).channel, "latest");
792 yield* Deferred.succeed(releaseCheck, undefined);
793 }),
794 );
795 });
796
797 it.effect("cancels an active preparation so the next request can run", () => {
798 const releaseCheck = Deferred.makeUnsafe<void>();
799 const harness = makeHarness({ checkForUpdates: Deferred.await(releaseCheck) });
800
801 return runRemoteUpdatesTest(harness, ({ requests, cancellations }) =>
802 Effect.gen(function* () {
803 yield* Queue.offer(requests, request("req-cancel"));
804 yield* settle;
805 yield* Queue.offer(cancellations, {
806 version: 1,
807 type: "cancelDesktopUpdate",
808 requestId: "req-cancel",
809 });
810 yield* settle;
811 yield* Deferred.succeed(releaseCheck, undefined);
812 yield* Queue.offer(requests, request("req-next"));
813 yield* settle;
814
815 assert.equal(harness.checkCount(), 2);
816 }),
817 );
818 });
819
820 it.effect("remembers a cancellation that arrives before its request starts", () => {
821 const harness = makeHarness();
822
823 return runRemoteUpdatesTest(harness, ({ requests, cancellations }) =>
824 Effect.gen(function* () {
825 yield* Queue.offer(cancellations, {
826 version: 1,
827 type: "cancelDesktopUpdate",
828 requestId: "req-early-cancel",
829 });
830 yield* Queue.offer(requests, request("req-early-cancel"));
831 yield* settle;
832
833 assert.equal(harness.checkCount(), 0);
834
835 yield* Queue.offer(requests, request("req-after-early-cancel"));
836 yield* settle;
837 assert.equal(harness.checkCount(), 1);
838 }),
839 );
840 });
841
842 it.effect("does not install after cancellation wins the commit claim", () => {
843 const harness = makeHarness();
844
845 return runRemoteUpdatesTest(harness, ({ reports, requests, commits, cancellations }) =>
846 Effect.gen(function* () {
847 yield* Queue.offer(requests, request("req-cancel-before-commit"));
848 yield* settle;
849 harness.emit("update-available", { version: "1.2.4" });
850 yield* settle;
851 harness.emit("update-downloaded", { version: "1.2.4" });
852 yield* settle;
853
854 yield* Queue.offer(cancellations, {
855 version: 1,
856 type: "cancelDesktopUpdate",
857 requestId: "req-cancel-before-commit",
858 });
859 yield* settle;
860 yield* Queue.offer(commits, {
861 version: 1,
862 type: "commitDesktopUpdate",
863 requestId: "req-cancel-before-commit",
864 });
865 yield* settle;
866
867 assert.equal(harness.quitAndInstalls(), 0);
868 assert.deepEqual(
869 terminalReports(reports)
870 .filter((report) => report.requestId === "req-cancel-before-commit")
871 .map((report) => report.outcome),
872 ["ready-to-install", "failed"],
873 );
874 }),
875 );
876 });
877
878 it.effect("retains an install failure after cancellation loses the commit claim", () => {
879 const installStarted = Deferred.makeUnsafe<void>();
880 const releaseInstall = Deferred.makeUnsafe<void>();
881 const harness = makeHarness({
882 quitAndInstall: Deferred.succeed(installStarted, undefined).pipe(
883 Effect.andThen(Deferred.await(releaseInstall)),
884 Effect.andThen(
885 Effect.fail(
886 new ElectronUpdater.ElectronUpdaterQuitAndInstallError({
887 channel: "latest",
888 isSilent: true,
889 isForceRunAfter: true,
890 cause: new Error("installer refused"),
891 }),
892 ),
893 ),
894 ),
895 });
896
897 return runRemoteUpdatesTest(harness, ({ reports, requests, commits, cancellations }) =>
898 Effect.gen(function* () {
899 yield* Queue.offer(requests, request("req-cancel-after-commit"));
900 yield* settle;
901 harness.emit("update-available", { version: "1.2.4" });
902 yield* settle;
903 harness.emit("update-downloaded", { version: "1.2.4" });
904 yield* settle;
905 const commit = {
906 version: 1,
907 type: "commitDesktopUpdate",
908 requestId: "req-cancel-after-commit",
909 } as const;
910 yield* Queue.offer(commits, commit);
911 yield* Deferred.await(installStarted);
912 yield* Queue.offer(cancellations, {
913 version: 1,
914 type: "cancelDesktopUpdate",
915 requestId: "req-cancel-after-commit",
916 });
917 yield* settle;
918 yield* Deferred.succeed(releaseInstall, undefined);
919 yield* settle;
920 yield* Queue.offer(commits, commit);
921 yield* settle;
922
923 assert.deepEqual(
924 terminalReports(reports)
925 .filter((report) => report.requestId === "req-cancel-after-commit")
926 .map((report) => report.outcome),
927 ["ready-to-install", "failed", "failed"],
928 );
929 }),
930 );
931 });
932
933 it.effect("reports up-to-date without installing when there is no update", () => {
934 const harness = makeHarness();
935
936 return runRemoteUpdatesTest(harness, ({ reports, requests }) =>
937 Effect.gen(function* () {
938 yield* Queue.offer(requests, request("req-2"));
939 yield* settle;
940 harness.emit("update-not-available");
941 yield* settle;
942
943 const terminals = terminalReports(reports);
944 assert.equal(terminals.length, 1);
945 assert.equal(terminals[0]?.outcome, "up-to-date");
946 assert.equal(terminals[0]?.requestId, "req-2");
947 assert.equal(harness.quitAndInstalls(), 0);
948 }),
949 );
950 });
951});