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

DesktopRemoteUpdates.test.ts

841 lines29.6 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// The remote flow hops between the test runtime's fibers and the updater's
26// runPromise-driven event handlers, so settling needs real microtask turns,
27// not just fiber yields.
28const settle = Effect.gen(function* () {
29 for (let i = 0; i < 20; i += 1) {
30 yield* Effect.yieldNow;
31 yield* Effect.promise(() => Promise.resolve());
32 }
33});
34​
35const request = (requestId: string): DesktopTelemetryRequestDesktopUpdate => ({
36 version: 1,
37 type: "requestDesktopUpdate",
38 requestId,
39});
40​
41function runRemoteUpdatesTest(
42 harness: ReturnType<typeof makeHarness>,
43 body: (context: {
44 readonly reports: DesktopUpdateStatusReport[];
45 readonly requests: Queue.Queue<DesktopTelemetryRequestDesktopUpdate>;
46 readonly commits: Queue.Queue<DesktopTelemetryCommitDesktopUpdate>;
47 readonly cancellations: Queue.Queue<DesktopTelemetryCancelDesktopUpdate>;
48 }) => Effect.Effect<void, never, DesktopUpdates.DesktopUpdates | DesktopState.DesktopState>,
49) {
50 return Effect.scoped(
51 Effect.gen(function* () {
52 const requests = yield* Queue.unbounded<DesktopTelemetryRequestDesktopUpdate>();
53 const commits = yield* Queue.unbounded<DesktopTelemetryCommitDesktopUpdate>();
54 const cancellations = yield* Queue.unbounded<DesktopTelemetryCancelDesktopUpdate>();
55 const reports: 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 }),
66 updateRequests: Stream.fromQueue(requests),
67 updateCommits: Stream.fromQueue(commits),
68 updateCancellations: Stream.fromQueue(cancellations),
69 });
70​
71 const updates = yield* DesktopUpdates.DesktopUpdates;
72 yield* updates.configure;
73 yield* DesktopRemoteUpdates.listen.pipe(
74 Effect.provideService(DesktopTelemetryPublisher.DesktopTelemetryPublisher, publisher),
75 );
76 yield* settle;
77 yield* body({ reports, requests, commits, cancellations });
78 }),
79 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
81​
82function terminalReports(reports: DesktopUpdateStatusReport[]): DesktopUpdateStatusReport[] {
83 return reports.filter((report) => report.outcome !== undefined);
85​
86describe("DesktopRemoteUpdates", () => {
87 it.effect("drives check, download, and install with no local confirmation", () => {
88 const harness = makeHarness();
89​
90 return runRemoteUpdatesTest(harness, ({ reports, requests, commits }) =>
91 Effect.gen(function* () {
92 yield* Queue.offer(requests, request("req-1"));
93 yield* settle;
94 assert.equal(harness.checkCount(), 1);
95​
96 harness.emit("update-available", { version: "1.2.4" });
97 yield* settle;
98 harness.emit("update-downloaded", { version: "1.2.4" });
99 yield* settle;
100​
101 const terminals = terminalReports(reports);
102 assert.equal(terminals.length, 1);
103 assert.equal(terminals[0]?.outcome, "ready-to-install");
104 assert.equal(terminals[0]?.requestId, "req-1");
105 assert.equal(harness.quitAndInstalls(), 0);
106​
107 yield* Queue.offer(commits, {
108 version: 1,
109 type: "commitDesktopUpdate",
110 requestId: "req-1",
111 });
112 yield* settle;
113 assert.equal(harness.quitAndInstalls(), 1);
114​
115 // The mirror stamped the in-run state changes with the request id.
116 const statuses = reports
117 .filter((report) => report.requestId === "req-1")
118 .map((report) => report.state.status);
119 assert.include(statuses, "available");
120 assert.include(statuses, "downloaded");
121 }),
122 );
123 });
124​
125 it.effect("reports a failed outcome when quitAndInstall fails", () => {
126 const harness = makeHarness({
127 quitAndInstall: Effect.fail(
128 new ElectronUpdater.ElectronUpdaterQuitAndInstallError({
129 channel: "latest",
130 isSilent: true,
131 isForceRunAfter: true,
132 cause: new Error("spawn failed"),
133 }),
134 ),
135 });
136​
137 return runRemoteUpdatesTest(harness, ({ reports, requests, commits }) =>
138 Effect.gen(function* () {
139 yield* Queue.offer(requests, request("req-4"));
140 yield* settle;
141 harness.emit("update-available", { version: "1.2.4" });
142 yield* settle;
143 harness.emit("update-downloaded", { version: "1.2.4" });
144 yield* settle;
145​
146 yield* Queue.offer(commits, {
147 version: 1,
148 type: "commitDesktopUpdate",
149 requestId: "req-4",
150 });
151 yield* settle;
152​
153 // Install failures reduce to status "downloaded" + errorContext
154 // "install"; the prepared result is followed by the commit failure.
155 const terminals = terminalReports(reports);
156 assert.deepEqual(
157 terminals.map((report) => report.outcome),
158 ["ready-to-install", "failed"],
159 );
160 assert.equal(terminals[1]?.state.errorContext, "install");
161 }),
162 );
163 });
164​
165 it.effect("joins an in-progress install on commit instead of failing the token", () => {
166 const installStarted = Deferred.makeUnsafe<void>();
167 const releaseInstall = Deferred.makeUnsafe<void>();
168 const harness = makeHarness({
169 stopBackend: Deferred.succeed(installStarted, undefined).pipe(
170 Effect.andThen(Deferred.await(releaseInstall)),
171 ),
172 });
173​
174 return runRemoteUpdatesTest(harness, ({ reports, requests, commits }) =>
175 Effect.gen(function* () {
176 const updates = yield* DesktopUpdates.DesktopUpdates;
177 yield* Queue.offer(requests, request("req-join-commit"));
178 yield* settle;
179 harness.emit("update-available", { version: "1.2.4" });
180 yield* settle;
181 harness.emit("update-downloaded", { version: "1.2.4" });
182 yield* settle;
183 assert.deepEqual(
184 terminalReports(reports).map((report) => report.outcome),
185 ["ready-to-install"],
186 );
187​
188 // A local install takes the updater reservation and starts shutdown.
189 const localInstall = yield* updates.install.pipe(Effect.forkChild);
190 yield* Deferred.await(installStarted);
191 yield* Queue.offer(commits, {
192 version: 1,
193 type: "commitDesktopUpdate",
194 requestId: "req-join-commit",
195 });
196 yield* settle;
197​
198 // No "failed" marker: that install relaunches the app and the
199 // client proves the handoff by reconnecting on the target version.
200 assert.deepEqual(
201 terminalReports(reports).map((report) => report.outcome),
202 ["ready-to-install"],
203 );
204 assert.equal(harness.quitAndInstalls(), 0);
205​
206 yield* Deferred.succeed(releaseInstall, undefined);
207 yield* Fiber.join(localInstall);
208 }),
209 );
210 });
211​
212 it.effect("does not join a normal app quit as an update install", () => {
213 const harness = makeHarness();
214​
215 return runRemoteUpdatesTest(harness, ({ reports, requests, commits }) =>
216 Effect.gen(function* () {
217 const desktopState = yield* DesktopState.DesktopState;
218 yield* Queue.offer(requests, request("req-normal-quit"));
219 yield* settle;
220 harness.emit("update-available", { version: "1.2.4" });
221 yield* settle;
222 harness.emit("update-downloaded", { version: "1.2.4" });
223 yield* settle;
224​
225 yield* Ref.set(desktopState.quitting, true);
226 yield* Queue.offer(commits, {
227 version: 1,
228 type: "commitDesktopUpdate",
229 requestId: "req-normal-quit",
230 });
231 yield* settle;
232​
233 assert.deepEqual(
234 terminalReports(reports).map((report) => report.outcome),
235 ["ready-to-install", "failed"],
236 );
237 assert.equal(harness.quitAndInstalls(), 0);
238 }),
239 );
240 });
241​
242 it.effect("does not misread a lingering install error as a failed retry", () => {
243 let installAttempts = 0;
244 const harness = makeHarness({
245 quitAndInstall: Effect.suspend(() => {
246 installAttempts += 1;
247 return installAttempts === 1
248 ? Effect.fail(
249 new ElectronUpdater.ElectronUpdaterQuitAndInstallError({
250 channel: "latest",
251 isSilent: true,
252 isForceRunAfter: true,
253 cause: new Error("first attempt failed"),
254 }),
255 )
256 : Effect.void;
257 }),
258 });
259​
260 return runRemoteUpdatesTest(harness, ({ reports, requests, commits }) =>
261 Effect.gen(function* () {
262 yield* Queue.offer(requests, request("req-5"));
263 yield* settle;
264 harness.emit("update-available", { version: "1.2.4" });
265 yield* settle;
266 harness.emit("update-downloaded", { version: "1.2.4" });
267 yield* settle;
268 yield* Queue.offer(commits, {
269 version: 1,
270 type: "commitDesktopUpdate",
271 requestId: "req-5",
272 });
273 yield* settle;
274 // First run failed; state is "downloaded" with a lingering
275 // errorContext "install". The retry succeeds and must not report
276 // that leftover as a fresh failure.
277 yield* Queue.offer(requests, request("req-6"));
278 yield* settle;
279 yield* Queue.offer(commits, {
280 version: 1,
281 type: "commitDesktopUpdate",
282 requestId: "req-6",
283 });
284 yield* settle;
285​
286 const retryTerminals = terminalReports(reports).filter(
287 (report) => report.requestId === "req-6",
288 );
289 assert.deepEqual(
290 retryTerminals.map((report) => report.outcome),
291 ["ready-to-install"],
292 );
293 assert.equal(harness.quitAndInstalls(), 2);
294 }),
295 );
296 });
297​
298 it.effect("ignores an unrelated updater event while an install is starting", () => {
299 const stopStarted = Deferred.makeUnsafe<void>();
300 const releaseStop = Deferred.makeUnsafe<void>();
301 const harness = makeHarness({
302 stopBackend: Deferred.succeed(stopStarted, undefined).pipe(
303 Effect.andThen(Deferred.await(releaseStop)),
304 ),
305 });
306​
307 return runRemoteUpdatesTest(harness, ({ reports, requests, commits }) =>
308 Effect.gen(function* () {
309 yield* Queue.offer(requests, request("req-late-event"));
310 yield* settle;
311 harness.emit("update-available", { version: "1.2.4" });
312 yield* settle;
313 harness.emit("update-downloaded", { version: "1.2.4" });
314 yield* settle;
315​
316 yield* Queue.offer(commits, {
317 version: 1,
318 type: "commitDesktopUpdate",
319 requestId: "req-late-event",
320 });
321 yield* Deferred.await(stopStarted);
322 harness.emit("download-progress", { percent: 90 });
323 yield* settle;
324 yield* Deferred.succeed(releaseStop, undefined);
325 yield* settle;
326​
327 assert.deepEqual(
328 terminalReports(reports)
329 .filter((report) => report.requestId === "req-late-event")
330 .map((report) => report.outcome),
331 ["ready-to-install"],
332 );
333 }),
334 );
335 });
336​
337 it.effect("retries a download refused while the check still holds the reservation", () => {
338 // electron-updater emits update-available from inside checkForUpdates,
339 // before the check action releases its reservation. The download the
340 // remote flow forks in response is refused and must be retried once the
341 // reservation frees up, without burning a download attempt.
342 const releaseCheck = Deferred.makeUnsafe<void>();
343 const harness = makeHarness({ checkForUpdates: Deferred.await(releaseCheck) });
344​
345 return runRemoteUpdatesTest(harness, ({ reports, requests }) =>
346 Effect.gen(function* () {
347 yield* Queue.offer(requests, request("req-8"));
348 yield* settle;
349 assert.equal(harness.checkCount(), 1);
350​
351 // Fire "available" while the check reservation is still held.
352 harness.emit("update-available", { version: "1.2.4" });
353 yield* settle;
354 assert.equal(harness.downloadCount(), 0);
355​
356 yield* Deferred.succeed(releaseCheck, undefined);
357 yield* settle;
358 yield* TestClock.adjust(Duration.millis(300));
359 yield* settle;
360 assert.equal(harness.downloadCount(), 1);
361​
362 harness.emit("update-downloaded", { version: "1.2.4" });
363 yield* settle;
364 assert.deepEqual(
365 terminalReports(reports).map((report) => report.outcome),
366 ["ready-to-install"],
367 );
368 assert.equal(harness.quitAndInstalls(), 0);
369 }),
370 );
371 });
372​
373 it.effect("waits for the download reservation before reporting prepared", () => {
374 // update-downloaded fires from inside downloadUpdate. If the flow
375 // reported "installing" right then, install would be refused for the
376 // held reservation after the irrevocable terminal already went out.
377 const releaseDownload = Deferred.makeUnsafe<void>();
378 const harness = makeHarness({ downloadUpdate: Deferred.await(releaseDownload) });
379​
380 return runRemoteUpdatesTest(harness, ({ reports, requests }) =>
381 Effect.gen(function* () {
382 yield* Queue.offer(requests, request("req-10"));
383 yield* settle;
384 harness.emit("update-available", { version: "1.2.4" });
385 yield* settle;
386 harness.emit("update-downloaded", { version: "1.2.4" });
387 yield* settle;
388 assert.deepEqual(terminalReports(reports), []);
389 assert.equal(harness.quitAndInstalls(), 0);
390​
391 yield* Deferred.succeed(releaseDownload, undefined);
392 yield* settle;
393 yield* TestClock.adjust(Duration.millis(300));
394 yield* settle;
395 assert.deepEqual(
396 terminalReports(reports).map((report) => report.outcome),
397 ["ready-to-install"],
398 );
399 assert.equal(harness.quitAndInstalls(), 0);
400 }),
401 );
402 });
403​
404 it.effect("joins an install that is already tearing the app down", () => {
405 const harness = makeHarness();
406​
407 return runRemoteUpdatesTest(harness, ({ reports, requests }) =>
408 Effect.gen(function* () {
409 const desktopState = yield* DesktopState.DesktopState;
410 harness.emit("update-downloaded", { version: "1.2.4" });
411 yield* settle;
412 // Another install already owns the shutdown.
413 yield* Ref.set(desktopState.quitting, true);
414​
415 yield* Queue.offer(requests, request("req-9"));
416 yield* settle;
417​
418 // Report "installing" once, no "failed" after the refusal: that
419 // install will relaunch the app and this request rides along.
420 assert.deepEqual(
421 terminalReports(reports).map((report) => report.outcome),
422 ["ready-to-install"],
423 );
424 assert.equal(harness.quitAndInstalls(), 0);
425 }),
426 );
427 });
428​
429 it.effect("does not run the installer twice for a repeated commit", () => {
430 const harness = makeHarness();
431​
432 return runRemoteUpdatesTest(harness, ({ requests, commits }) =>
433 Effect.gen(function* () {
434 yield* Queue.offer(requests, request("req-repeat"));
435 yield* settle;
436 harness.emit("update-available", { version: "1.2.4" });
437 yield* settle;
438 harness.emit("update-downloaded", { version: "1.2.4" });
439 yield* settle;
440​
441 const commit = {
442 version: 1,
443 type: "commitDesktopUpdate",
444 requestId: "req-repeat",
445 } as const;
446 yield* Queue.offer(commits, commit);
447 yield* settle;
448 yield* Queue.offer(commits, commit);
449 yield* settle;
450​
451 assert.equal(harness.quitAndInstalls(), 1);
452 }),
453 );
454 });
455​
456 it.effect("waits for a background check before installing a prepared update", () => {
457 const backgroundCheckStarted = Deferred.makeUnsafe<void>();
458 const releaseBackgroundCheck = Deferred.makeUnsafe<void>();
459 let checks = 0;
460 const harness = makeHarness({
461 checkForUpdates: Effect.suspend(() => {
462 checks += 1;
463 return checks === 2
464 ? Deferred.succeed(backgroundCheckStarted, undefined).pipe(
465 Effect.andThen(Deferred.await(releaseBackgroundCheck)),
466 )
467 : Effect.void;
468 }),
469 });
470​
471 return runRemoteUpdatesTest(harness, ({ requests, commits }) =>
472 Effect.gen(function* () {
473 const updates = yield* DesktopUpdates.DesktopUpdates;
474 yield* Queue.offer(requests, request("req-background-check"));
475 yield* settle;
476 harness.emit("update-available", { version: "1.2.4" });
477 yield* settle;
478 harness.emit("update-downloaded", { version: "1.2.4" });
479 yield* settle;
480​
481 const backgroundCheck = yield* updates.check("poll").pipe(Effect.forkChild);
482 yield* Deferred.await(backgroundCheckStarted);
483 assert.equal((yield* updates.getState).status, "checking");
484​
485 yield* Queue.offer(commits, {
486 version: 1,
487 type: "commitDesktopUpdate",
488 requestId: "req-background-check",
489 });
490 yield* settle;
491 assert.equal(harness.quitAndInstalls(), 0);
492​
493 harness.emit("update-downloaded", { version: "1.2.4" });
494 yield* Deferred.succeed(releaseBackgroundCheck, undefined);
495 yield* Fiber.join(backgroundCheck);
496 yield* settle;
497 assert.equal(harness.quitAndInstalls(), 1);
498 }),
499 );
500 });
501​
502 it.effect("fails a prepared install when its background check stays blocked", () => {
503 const backgroundCheckStarted = Deferred.makeUnsafe<void>();
504 const releaseBackgroundCheck = Deferred.makeUnsafe<void>();
505 let checks = 0;
506 const harness = makeHarness({
507 checkForUpdates: Effect.suspend(() => {
508 checks += 1;
509 return checks === 2
510 ? Deferred.succeed(backgroundCheckStarted, undefined).pipe(
511 Effect.andThen(Deferred.await(releaseBackgroundCheck)),
512 )
513 : Effect.void;
514 }),
515 });
516​
517 return runRemoteUpdatesTest(harness, ({ reports, requests, commits }) =>
518 Effect.gen(function* () {
519 const updates = yield* DesktopUpdates.DesktopUpdates;
520 yield* Queue.offer(requests, request("req-blocked-background-check"));
521 yield* settle;
522 harness.emit("update-available", { version: "1.2.4" });
523 yield* settle;
524 harness.emit("update-downloaded", { version: "1.2.4" });
525 yield* settle;
526​
527 const backgroundCheck = yield* updates.check("poll").pipe(Effect.forkChild);
528 yield* Deferred.await(backgroundCheckStarted);
529 const commit = {
530 version: 1,
531 type: "commitDesktopUpdate",
532 requestId: "req-blocked-background-check",
533 } as const;
534 yield* Queue.offer(commits, commit);
535 yield* settle;
536 yield* TestClock.adjust(Duration.seconds(91));
537 yield* settle;
538​
539 assert.equal(harness.quitAndInstalls(), 0);
540 assert.deepEqual(
541 terminalReports(reports)
542 .filter((report) => report.requestId === "req-blocked-background-check")
543 .map((report) => report.outcome),
544 ["ready-to-install", "failed"],
545 );
546​
547 harness.emit("update-downloaded", { version: "1.2.4" });
548 yield* Deferred.succeed(releaseBackgroundCheck, undefined);
549 yield* Fiber.join(backgroundCheck);
550 yield* settle;
551 assert.equal(harness.quitAndInstalls(), 0);
552​
553 yield* Queue.offer(commits, commit);
554 yield* settle;
555 assert.deepEqual(
556 terminalReports(reports)
557 .filter((report) => report.requestId === "req-blocked-background-check")
558 .map((report) => report.outcome),
559 ["ready-to-install", "failed", "failed"],
560 );
561 }),
562 );
563 });
564​
565 it.effect("rejects a new preparation while an install commit is active", () => {
566 const installStarted = Deferred.makeUnsafe<void>();
567 const releaseInstall = Deferred.makeUnsafe<void>();
568 const harness = makeHarness({
569 quitAndInstall: Deferred.succeed(installStarted, undefined).pipe(
570 Effect.andThen(Deferred.await(releaseInstall)),
571 Effect.andThen(
572 Effect.fail(
573 new ElectronUpdater.ElectronUpdaterQuitAndInstallError({
574 channel: "latest",
575 isSilent: true,
576 isForceRunAfter: true,
577 cause: new Error("installer refused"),
578 }),
579 ),
580 ),
581 ),
582 });
583​
584 return runRemoteUpdatesTest(harness, ({ reports, requests, commits }) =>
585 Effect.gen(function* () {
586 yield* Queue.offer(requests, request("req-active"));
587 yield* settle;
588 harness.emit("update-available", { version: "1.2.4" });
589 yield* settle;
590 harness.emit("update-downloaded", { version: "1.2.4" });
591 yield* settle;
592 yield* Queue.offer(commits, {
593 version: 1,
594 type: "commitDesktopUpdate",
595 requestId: "req-active",
596 });
597 yield* Deferred.await(installStarted);
598​
599 yield* Queue.offer(requests, request("req-overlap"));
600 yield* settle;
601 const overlap = terminalReports(reports).find(
602 (report) => report.requestId === "req-overlap",
603 );
604 assert.equal(overlap?.outcome, "failed");
605 assert.equal(overlap?.reason, "A prepared desktop update is already in progress.");
606​
607 yield* Deferred.succeed(releaseInstall, undefined);
608 yield* settle;
609 }),
610 );
611 });
612​
613 it.effect("cancels an active preparation so the next request can run", () => {
614 const releaseCheck = Deferred.makeUnsafe<void>();
615 const harness = makeHarness({ checkForUpdates: Deferred.await(releaseCheck) });
616​
617 return runRemoteUpdatesTest(harness, ({ requests, cancellations }) =>
618 Effect.gen(function* () {
619 yield* Queue.offer(requests, request("req-cancel"));
620 yield* settle;
621 yield* Queue.offer(cancellations, {
622 version: 1,
623 type: "cancelDesktopUpdate",
624 requestId: "req-cancel",
625 });
626 yield* settle;
627 yield* Deferred.succeed(releaseCheck, undefined);
628 yield* Queue.offer(requests, request("req-next"));
629 yield* settle;
630​
631 assert.equal(harness.checkCount(), 2);
632 }),
633 );
634 });
635​
636 it.effect("remembers a cancellation that arrives before its request starts", () => {
637 const harness = makeHarness();
638​
639 return runRemoteUpdatesTest(harness, ({ requests, cancellations }) =>
640 Effect.gen(function* () {
641 yield* Queue.offer(cancellations, {
642 version: 1,
643 type: "cancelDesktopUpdate",
644 requestId: "req-early-cancel",
645 });
646 yield* Queue.offer(requests, request("req-early-cancel"));
647 yield* settle;
648​
649 assert.equal(harness.checkCount(), 0);
650​
651 yield* Queue.offer(requests, request("req-after-early-cancel"));
652 yield* settle;
653 assert.equal(harness.checkCount(), 1);
654 }),
655 );
656 });
657​
658 it.effect("keeps every queued cancellation until its request starts", () => {
659 const harness = makeHarness();
660​
661 return runRemoteUpdatesTest(harness, ({ requests, cancellations }) =>
662 Effect.gen(function* () {
663 for (let index = 0; index < 40; index += 1) {
664 yield* Queue.offer(cancellations, {
665 version: 1,
666 type: "cancelDesktopUpdate",
667 requestId: `req-queued-cancel-${index}`,
668 });
669 }
670 yield* settle;
671 for (let index = 0; index < 40; index += 1) {
672 yield* Queue.offer(requests, request(`req-queued-cancel-${index}`));
673 }
674 yield* settle;
675 assert.equal(harness.checkCount(), 0);
676​
677 yield* Queue.offer(requests, request("req-after-queued-cancels"));
678 yield* settle;
679 assert.equal(harness.checkCount(), 1);
680 }),
681 );
682 });
683​
684 it.effect("does not install after cancellation wins the commit claim", () => {
685 const harness = makeHarness();
686​
687 return runRemoteUpdatesTest(harness, ({ reports, requests, commits, cancellations }) =>
688 Effect.gen(function* () {
689 yield* Queue.offer(requests, request("req-cancel-before-commit"));
690 yield* settle;
691 harness.emit("update-available", { version: "1.2.4" });
692 yield* settle;
693 harness.emit("update-downloaded", { version: "1.2.4" });
694 yield* settle;
695​
696 yield* Queue.offer(cancellations, {
697 version: 1,
698 type: "cancelDesktopUpdate",
699 requestId: "req-cancel-before-commit",
700 });
701 yield* settle;
702 yield* Queue.offer(commits, {
703 version: 1,
704 type: "commitDesktopUpdate",
705 requestId: "req-cancel-before-commit",
706 });
707 yield* settle;
708​
709 assert.equal(harness.quitAndInstalls(), 0);
710 assert.deepEqual(
711 terminalReports(reports)
712 .filter((report) => report.requestId === "req-cancel-before-commit")
713 .map((report) => report.outcome),
714 ["ready-to-install", "failed"],
715 );
716 }),
717 );
718 });
719​
720 it.effect("retains an install failure after cancellation loses the commit claim", () => {
721 const installStarted = Deferred.makeUnsafe<void>();
722 const releaseInstall = Deferred.makeUnsafe<void>();
723 const harness = makeHarness({
724 quitAndInstall: Deferred.succeed(installStarted, undefined).pipe(
725 Effect.andThen(Deferred.await(releaseInstall)),
726 Effect.andThen(
727 Effect.fail(
728 new ElectronUpdater.ElectronUpdaterQuitAndInstallError({
729 channel: "latest",
730 isSilent: true,
731 isForceRunAfter: true,
732 cause: new Error("installer refused"),
733 }),
734 ),
735 ),
736 ),
737 });
738​
739 return runRemoteUpdatesTest(harness, ({ reports, requests, commits, cancellations }) =>
740 Effect.gen(function* () {
741 yield* Queue.offer(requests, request("req-cancel-after-commit"));
742 yield* settle;
743 harness.emit("update-available", { version: "1.2.4" });
744 yield* settle;
745 harness.emit("update-downloaded", { version: "1.2.4" });
746 yield* settle;
747 const commit = {
748 version: 1,
749 type: "commitDesktopUpdate",
750 requestId: "req-cancel-after-commit",
751 } as const;
752 yield* Queue.offer(commits, commit);
753 yield* Deferred.await(installStarted);
754 yield* Queue.offer(cancellations, {
755 version: 1,
756 type: "cancelDesktopUpdate",
757 requestId: "req-cancel-after-commit",
758 });
759 yield* settle;
760 yield* Deferred.succeed(releaseInstall, undefined);
761 yield* settle;
762 yield* Queue.offer(commits, commit);
763 yield* settle;
764​
765 assert.deepEqual(
766 terminalReports(reports)
767 .filter((report) => report.requestId === "req-cancel-after-commit")
768 .map((report) => report.outcome),
769 ["ready-to-install", "failed", "failed"],
770 );
771 }),
772 );
773 });
774​
775 it.effect("expires an uncommitted preparation", () => {
776 const harness = makeHarness();
777​
778 return runRemoteUpdatesTest(harness, ({ reports, requests, commits }) =>
779 Effect.gen(function* () {
780 yield* Queue.offer(requests, request("req-stale"));
781 yield* settle;
782 harness.emit("update-available", { version: "1.2.4" });
783 yield* settle;
784 harness.emit("update-downloaded", { version: "1.2.4" });
785 yield* settle;
786​
787 yield* TestClock.setTime(Duration.toMillis(Duration.minutes(6)));
788 yield* Queue.offer(commits, {
789 version: 1,
790 type: "commitDesktopUpdate",
791 requestId: "req-stale",
792 });
793 yield* settle;
794​
795 const outcomes = terminalReports(reports)
796 .filter((report) => report.requestId === "req-stale")
797 .map((report) => report.outcome);
798 assert.deepEqual(outcomes, ["ready-to-install", "failed"]);
799 }),
800 );
801 });
802​
803 it.effect("reports up-to-date without installing when there is no update", () => {
804 const harness = makeHarness();
805​
806 return runRemoteUpdatesTest(harness, ({ reports, requests }) =>
807 Effect.gen(function* () {
808 yield* Queue.offer(requests, request("req-2"));
809 yield* settle;
810 harness.emit("update-not-available");
811 yield* settle;
812​
813 const terminals = terminalReports(reports);
814 assert.equal(terminals.length, 1);
815 assert.equal(terminals[0]?.outcome, "up-to-date");
816 assert.equal(terminals[0]?.requestId, "req-2");
817 assert.equal(harness.quitAndInstalls(), 0);
818 }),
819 );
820 });
821​
822 it.effect("fails fast with the disabled reason when updates are off", () => {
823 const harness = makeHarness({ env: { T3CODE_DISABLE_AUTO_UPDATE: "true" } });
824​
825 return runRemoteUpdatesTest(harness, ({ reports, requests }) =>
826 Effect.gen(function* () {
827 yield* Queue.offer(requests, request("req-3"));
828 yield* settle;
829​
830 const terminals = terminalReports(reports);
831 assert.equal(terminals.length, 1);
832 assert.equal(terminals[0]?.outcome, "failed");
833 assert.equal(
834 terminals[0]?.reason,
835 "Automatic updates are disabled by the T3CODE_DISABLE_AUTO_UPDATE setting.",
836 );
837 assert.equal(harness.quitAndInstalls(), 0);
838 }),
839 );
840 });
841});