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

DesktopRemoteUpdates.test.ts

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