The best way to control Claude Code, Codex and more from any computer or phone

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