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

DesktopRemoteUpdates.test.ts

1,113 lines39.2 KB

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