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