Open History to see the last commit
1import * as NodeModule from "node:module";
2
3import { assert, describe, it } from "@effect/vitest";
4import { DESKTOP_UPDATE_RESTART_MARKER_FILE } from "@t3tools/contracts";
5import { computeReleaseNotes } from "electron-updater/out/providers/GitHubProvider.js";
6import * as Cause from "effect/Cause";
7import * as Deferred from "effect/Deferred";
8import * as Duration from "effect/Duration";
9import * as Effect from "effect/Effect";
10import * as Fiber from "effect/Fiber";
11import * as Layer from "effect/Layer";
12import * as Logger from "effect/Logger";
13import * as Option from "effect/Option";
14import * as References from "effect/References";
15import * as Ref from "effect/Ref";
16import * as Stream from "effect/Stream";
17import * as TestClock from "effect/testing/TestClock";
18
19import * as ElectronUpdater from "../electron/ElectronUpdater.ts";
20import * as DesktopBackendPool from "../backend/DesktopBackendPool.ts";
21import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts";
22import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
23import * as DesktopState from "../app/DesktopState.ts";
24import * as DesktopUpdatePolicy from "./DesktopUpdatePolicy.ts";
25import * as DesktopUpdates from "./DesktopUpdates.ts";
26import { flushCallbacks, makeHarness } from "./updatesTestHarness.ts";
27
28type ReleaseFeed = Parameters<typeof computeReleaseNotes>[2];
29type RunningVersion = Parameters<typeof computeReleaseNotes>[0];
30
31// electron-updater's own dependencies, which apps/desktop does not declare.
32const requireFromUpdater = NodeModule.createRequire(
33 NodeModule.createRequire(import.meta.url).resolve("electron-updater"),
34);
35const { parseXml } = requireFromUpdater("builder-util-runtime") as {
36 parseXml: (xml: string) => ReleaseFeed;
37};
38const semver = requireFromUpdater("semver") as { parse: (version: string) => RunningVersion };
39
40const releaseTitle = (version: string) =>
41 version.includes("-nightly.")
42 ? `T3 Code Nightly ${version} (0123456789ab)`
43 : `T3 Code v${version}`;
44
45const releaseFeed = (...versions: ReadonlyArray<string>) =>
46 parseXml(
47 `<feed xmlns="http://www.w3.org/2005/Atom">${versions
48 .map(
49 (version) =>
50 `<entry><link rel="alternate" type="text/html" href="https://github.com/pingdotgg/t3code/releases/tag/v${version}"/><title>${releaseTitle(version)}</title><content type="html"><ul><li>${version} change</li></ul></content></entry>`,
51 )
52 .join("")}</feed>`,
53 );
54
55/** The update info electron-updater's GitHub provider reports when /releases/latest names `version`. */
56const stableUpdateInfo = (
57 feed: ReleaseFeed,
58 version: string,
59 runningVersion: string,
60 fullChangelog: boolean,
61) => {
62 const latestRelease =
63 feed
64 .getElements("entry")
65 .find((entry) => entry.element("link").attribute("href").endsWith(`/tag/v${version}`)) ??
66 feed.element("entry");
67 return {
68 version,
69 releaseName: latestRelease.elementValueOrEmpty("title"),
70 releaseNotes: computeReleaseNotes(
71 semver.parse(runningVersion),
72 fullChangelog,
73 feed,
74 latestRelease,
75 ),
76 };
77};
78
79describe("DesktopUpdates", () => {
80 it.effect("updates Linux .deb installs and leaves other non-AppImage installs off", () =>
81 Effect.gen(function* () {
82 const linuxState = (packageType: string | undefined) =>
83 Effect.scoped(
84 Effect.gen(function* () {
85 const updates = yield* DesktopUpdates.DesktopUpdates;
86 const desktopState = yield* DesktopState.DesktopState;
87 yield* updates.configure;
88 return {
89 state: yield* updates.getState,
90 canInstallUpdates: yield* Ref.get(desktopState.canInstallUpdates),
91 };
92 }),
93 ).pipe(Effect.provide(makeHarness({ platform: "linux", packageType }).layer));
94
95 const deb = yield* linuxState("deb\n");
96 assert.equal(deb.state.status, "idle");
97 assert.isTrue(deb.canInstallUpdates);
98
99 const unmarked = yield* linuxState(undefined);
100 assert.equal(unmarked.state.status, "disabled");
101 assert.isFalse(unmarked.canInstallUpdates);
102 }),
103 );
104
105 it.effect("keeps the disabled reason available after configuration and channel changes", () => {
106 const harness = makeHarness({ env: { T3CODE_DISABLE_AUTO_UPDATE: "true" } });
107 const reason = "Automatic updates are disabled by the T3CODE_DISABLE_AUTO_UPDATE setting.";
108
109 return Effect.scoped(
110 Effect.gen(function* () {
111 const updates = yield* DesktopUpdates.DesktopUpdates;
112 yield* updates.configure;
113
114 const configured = yield* updates.getState;
115 assert.equal(configured.message, reason);
116
117 const changed = yield* updates.setChannel("nightly");
118 assert.equal(changed.channel, "nightly");
119 assert.equal(changed.enabled, false);
120 assert.equal(changed.status, "disabled");
121 assert.equal(changed.message, reason);
122 assert.equal(harness.sentStates.at(-1)?.message, reason);
123 assert.equal(harness.checkCount(), 0);
124 }),
125 ).pipe(Effect.provide(harness.layer));
126 });
127
128 it.effect("checks for newer releases after an update has been downloaded", () => {
129 const harness = makeHarness();
130
131 return Effect.scoped(
132 Effect.gen(function* () {
133 const updates = yield* DesktopUpdates.DesktopUpdates;
134 yield* updates.configure;
135
136 harness.emit("update-available", {
137 version: "1.2.4",
138 releaseNotes: "## What's changed\n- fix: queued update",
139 });
140 yield* flushCallbacks;
141 harness.emit("update-downloaded", { version: "1.2.4" });
142 yield* flushCallbacks;
143
144 const result = yield* updates.check("poll");
145 assert.isTrue(result.checked);
146
147 harness.emit("update-available", { version: "1.2.4" });
148 yield* flushCallbacks;
149
150 const unchangedState = yield* updates.getState;
151 assert.equal(unchangedState.status, "downloaded");
152 assert.equal(unchangedState.downloadedVersion, "1.2.4");
153 assert.deepEqual(unchangedState.releaseNotes, [
154 { version: "1.2.4", items: ["fix: queued update"], totalItems: 1 },
155 ]);
156 assert.equal(unchangedState.omittedReleaseCount, 0);
157
158 const nextResult = yield* updates.check("poll");
159 assert.isTrue(nextResult.checked);
160
161 harness.emit("update-available", { version: "1.2.5" });
162 yield* flushCallbacks;
163
164 const state = yield* updates.getState;
165 assert.equal(state.status, "available");
166 assert.equal(state.availableVersion, "1.2.5");
167 assert.isNull(state.downloadedVersion);
168 }),
169 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
170 });
171
172 it.effect.each([
173 {
174 name: "shows no notes for a Stable release missing from the releases feed",
175 runningVersion: "1.2.3",
176 feedVersions: ["1.2.5-nightly.20261102.1", "1.2.5-nightly.20261101.1"],
177 expected: [],
178 },
179 {
180 name: "shows a Stable release's own notes from the releases feed",
181 runningVersion: "1.2.3",
182 feedVersions: ["1.2.5-nightly.20261102.1", "1.2.4", "1.2.4-nightly.20261030.1"],
183 expected: [{ version: "1.2.4", items: ["1.2.4 change"], totalItems: 1 }],
184 },
185 {
186 name: "shows a Stable release's notes when switching down from a newer Nightly",
187 runningVersion: "1.2.5-nightly.20261027.1",
188 feedVersions: ["1.2.5-nightly.20261102.1", "1.2.4"],
189 expected: [{ version: "1.2.4", items: ["1.2.4 change"], totalItems: 1 }],
190 },
191 ])("$name", ({ runningVersion, feedVersions, expected }) => {
192 const harness = makeHarness();
193
194 return Effect.scoped(
195 Effect.gen(function* () {
196 const updates = yield* DesktopUpdates.DesktopUpdates;
197 yield* updates.configure;
198
199 harness.emit(
200 "update-available",
201 stableUpdateInfo(
202 releaseFeed(...feedVersions),
203 "1.2.4",
204 runningVersion,
205 harness.fullChangelog(),
206 ),
207 );
208 yield* flushCallbacks;
209
210 const state = yield* updates.getState;
211 assert.equal(state.availableVersion, "1.2.4");
212 assert.deepEqual(state.releaseNotes, expected);
213 }),
214 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
215 });
216
217 it.effect("preserves a queued installer when the feed has no update", () => {
218 const harness = makeHarness();
219
220 return Effect.scoped(
221 Effect.gen(function* () {
222 const updates = yield* DesktopUpdates.DesktopUpdates;
223 yield* updates.configure;
224
225 harness.emit("update-available", {
226 version: "1.2.4",
227 releaseNotes: "## What's changed\n- fix: queued update",
228 });
229 yield* flushCallbacks;
230 harness.emit("update-downloaded", { version: "1.2.4" });
231 yield* flushCallbacks;
232
233 yield* updates.check("poll");
234 harness.emit("update-not-available");
235 yield* flushCallbacks;
236
237 const state = yield* updates.getState;
238 assert.equal(state.status, "downloaded");
239 assert.equal(state.availableVersion, "1.2.4");
240 assert.equal(state.downloadedVersion, "1.2.4");
241 assert.deepEqual(state.releaseNotes, [
242 { version: "1.2.4", items: ["fix: queued update"], totalItems: 1 },
243 ]);
244 assert.equal(state.omittedReleaseCount, 0);
245 assert.equal(state.downloadPercent, 100);
246 }),
247 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
248 });
249
250 it.effect("checks shortly after startup and then every four hours", () => {
251 const harness = makeHarness();
252
253 return Effect.scoped(
254 Effect.gen(function* () {
255 const updates = yield* DesktopUpdates.DesktopUpdates;
256 yield* updates.configure;
257
258 yield* TestClock.adjust("15 seconds");
259 assert.equal(harness.checkCount(), 1);
260
261 yield* TestClock.adjust("3 hours");
262 assert.equal(harness.checkCount(), 1);
263
264 yield* TestClock.adjust("1 hour");
265 assert.equal(harness.checkCount(), 2);
266 }),
267 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
268 });
269
270 it.effect(
271 "rejects install while a refresh check is in progress and releases the reservation",
272 () =>
273 Effect.gen(function* () {
274 const checkStarted = yield* Deferred.make<void>();
275 const releaseCheck = yield* Deferred.make<void>();
276 const harness = makeHarness({
277 checkForUpdates: Deferred.succeed(checkStarted, undefined).pipe(
278 Effect.andThen(Deferred.await(releaseCheck)),
279 ),
280 });
281
282 yield* Effect.scoped(
283 Effect.gen(function* () {
284 const updates = yield* DesktopUpdates.DesktopUpdates;
285 yield* updates.configure;
286 harness.emit("update-downloaded", { version: "1.2.4" });
287 yield* flushCallbacks;
288
289 const checkFiber = yield* updates.check("manual").pipe(Effect.forkScoped);
290 yield* Deferred.await(checkStarted);
291
292 const installResult = yield* updates.install;
293 assert.isFalse(installResult.accepted);
294
295 yield* Deferred.succeed(releaseCheck, undefined);
296 const checkResult = yield* Fiber.join(checkFiber);
297 assert.isTrue(checkResult.checked);
298
299 const followUpCheck = yield* updates.check("manual");
300 assert.isTrue(followUpCheck.checked);
301 assert.equal(harness.checkCount(), 2);
302 }),
303 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
304 }),
305 );
306
307 it.effect("rejects refresh checks while install is in progress", () =>
308 Effect.gen(function* () {
309 const installStarted = yield* Deferred.make<void>();
310 const releaseInstall = yield* Deferred.make<void>();
311 const harness = makeHarness({
312 stopBackend: Deferred.succeed(installStarted, undefined).pipe(
313 Effect.andThen(Deferred.await(releaseInstall)),
314 ),
315 });
316
317 yield* Effect.scoped(
318 Effect.gen(function* () {
319 const updates = yield* DesktopUpdates.DesktopUpdates;
320 yield* updates.configure;
321 harness.emit("update-downloaded", { version: "1.2.4" });
322 yield* flushCallbacks;
323
324 const installFiber = yield* updates.install.pipe(Effect.forkScoped);
325 yield* Deferred.await(installStarted);
326
327 const checkResult = yield* updates.check("manual");
328 assert.isFalse(checkResult.checked);
329 assert.equal(harness.checkCount(), 0);
330
331 yield* Deferred.succeed(releaseInstall, undefined);
332 const installResult = yield* Fiber.join(installFiber);
333 assert.isTrue(installResult.accepted);
334 }),
335 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
336 }),
337 );
338
339 it.effect("preserves a queued installer after a background updater error", () => {
340 const harness = makeHarness();
341
342 return Effect.scoped(
343 Effect.gen(function* () {
344 const updates = yield* DesktopUpdates.DesktopUpdates;
345 yield* updates.configure;
346 harness.emit("update-downloaded", { version: "1.2.4" });
347 yield* flushCallbacks;
348
349 harness.emit("error", new Error("background updater failure"));
350 yield* flushCallbacks;
351
352 const state = yield* updates.getState;
353 assert.equal(state.status, "error");
354 assert.equal(state.downloadedVersion, "1.2.4");
355 assert.isNull(state.errorContext);
356
357 const result = yield* updates.install;
358 assert.isTrue(result.accepted);
359 }),
360 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
361 });
362
363 it.effect("logs the redacted cause of a rejected update check", () => {
364 const cause = Object.assign(
365 new Error("request failed for https://user:secret@example.com/update?token=secret"),
366 { code: "ERR_UPDATER_LATEST_VERSION_NOT_FOUND" },
367 );
368 const updaterError = new ElectronUpdater.ElectronUpdaterCheckForUpdatesError({
369 channel: null,
370 cause,
371 });
372 const harness = makeHarness({ checkForUpdates: Effect.fail(updaterError) });
373 const loggedAnnotations: Array<Record<string, unknown>> = [];
374 const logger = Logger.make(({ fiber }) => {
375 const annotations = fiber.getRef(References.CurrentLogAnnotations);
376 if (annotations.errorTag === "ElectronUpdaterCheckForUpdatesError") {
377 loggedAnnotations.push(annotations);
378 }
379 });
380
381 return Effect.scoped(
382 Effect.gen(function* () {
383 const updates = yield* DesktopUpdates.DesktopUpdates;
384 yield* updates.configure;
385
386 yield* updates.check("manual");
387
388 const state = yield* updates.getState;
389 const loggedAnnotation = loggedAnnotations.at(-1);
390 assert.isDefined(loggedAnnotation);
391 assert.equal(loggedAnnotation.errorTag, "ElectronUpdaterCheckForUpdatesError");
392 assert.isNull(loggedAnnotation.channel);
393 assert.equal(
394 loggedAnnotation.cause,
395 "Error [ERR_UPDATER_LATEST_VERSION_NOT_FOUND]: request failed for [redacted URL]",
396 );
397 assert.notProperty(loggedAnnotation, "error");
398 assert.notInclude(Object.values(loggedAnnotation).map(String).join(" "), "secret");
399 assert.equal(
400 state.message,
401 "Electron updater failed to check for updates on channel default.",
402 );
403 assert.notInclude(state.message ?? "", "secret");
404 }),
405 ).pipe(
406 Effect.provide(
407 Layer.mergeAll(
408 TestClock.layer(),
409 harness.layer,
410 Logger.layer([logger], { mergeWithExisting: false }),
411 ),
412 ),
413 );
414 });
415
416 it.effect("recovers download state after an unexpected setup failure", () => {
417 let disableDifferentialCalls = 0;
418 const harness = makeHarness({
419 setDisableDifferentialDownload: Effect.suspend(() => {
420 disableDifferentialCalls += 1;
421 return disableDifferentialCalls === 1
422 ? Effect.void
423 : Effect.die(new Error("download setup failed"));
424 }),
425 });
426
427 return Effect.scoped(
428 Effect.gen(function* () {
429 const updates = yield* DesktopUpdates.DesktopUpdates;
430 yield* updates.configure;
431 harness.emit("update-available", { version: "1.2.4" });
432 yield* flushCallbacks;
433
434 const result = yield* updates.download;
435 assert.isTrue(result.accepted);
436 assert.isFalse(result.completed);
437
438 const failedState = yield* updates.getState;
439 assert.equal(failedState.status, "available");
440 assert.equal(failedState.errorContext, "download");
441 assert.equal(failedState.message, "Desktop update download action failed unexpectedly.");
442
443 const changedState = yield* updates.setChannel("nightly");
444 assert.equal(changedState.channel, "nightly");
445 }),
446 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
447 });
448
449 it.effect("shows why a download failed, not only the channel", () => {
450 const harness = makeHarness({
451 downloadUpdate: Effect.fail(
452 new ElectronUpdater.ElectronUpdaterDownloadUpdateError({
453 channel: "nightly",
454 cause: new Error("net::ERR_CONTENT_LENGTH_MISMATCH"),
455 }),
456 ),
457 });
458
459 return Effect.scoped(
460 Effect.gen(function* () {
461 const updates = yield* DesktopUpdates.DesktopUpdates;
462 yield* updates.configure;
463 harness.emit("update-available", { version: "1.2.4" });
464 yield* flushCallbacks;
465
466 const result = yield* updates.download;
467 assert.isFalse(result.completed);
468
469 const state = yield* updates.getState;
470 assert.equal(state.errorContext, "download");
471 assert.include(state.message ?? "", "ERR_CONTENT_LENGTH_MISMATCH");
472 }),
473 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
474 });
475
476 it.effect("restores download state and permits retry after interruption", () =>
477 Effect.gen(function* () {
478 const actionStarted = yield* Deferred.make<void>();
479 let disableDifferentialCalls = 0;
480 const harness = makeHarness({
481 setDisableDifferentialDownload: Effect.suspend(() => {
482 disableDifferentialCalls += 1;
483 if (disableDifferentialCalls === 1) {
484 return Effect.void;
485 }
486 if (disableDifferentialCalls === 2) {
487 return Deferred.succeed(actionStarted, undefined).pipe(Effect.andThen(Effect.never));
488 }
489 return Effect.void;
490 }),
491 });
492
493 yield* Effect.scoped(
494 Effect.gen(function* () {
495 const updates = yield* DesktopUpdates.DesktopUpdates;
496 yield* updates.configure;
497 harness.emit("update-available", { version: "1.2.4" });
498 yield* flushCallbacks;
499
500 const downloadFiber = yield* updates.download.pipe(Effect.forkScoped);
501 yield* Deferred.await(actionStarted);
502 yield* Fiber.interrupt(downloadFiber);
503
504 const interruptedState = yield* updates.getState;
505 assert.equal(interruptedState.status, "available");
506 assert.isNull(interruptedState.message);
507
508 const retry = yield* updates.download;
509 assert.isTrue(retry.accepted);
510 assert.isTrue(retry.completed);
511 }),
512 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
513 }),
514 );
515
516 it.effect("clears quitting state after an unexpected install setup failure", () => {
517 const harness = makeHarness({
518 stopBackend: Effect.die(new Error("backend stop failed")),
519 });
520
521 return Effect.scoped(
522 Effect.gen(function* () {
523 const desktopState = yield* DesktopState.DesktopState;
524 const updates = yield* DesktopUpdates.DesktopUpdates;
525 yield* updates.configure;
526 harness.emit("update-downloaded", { version: "1.2.4" });
527 yield* flushCallbacks;
528
529 const result = yield* updates.install;
530 assert.isTrue(result.accepted);
531 assert.isFalse(result.completed);
532 assert.isFalse(yield* Ref.get(desktopState.quitting));
533
534 const failedState = yield* updates.getState;
535 assert.equal(failedState.status, "downloaded");
536 assert.equal(failedState.errorContext, "install");
537 assert.equal(failedState.message, "Desktop update install action failed unexpectedly.");
538
539 const changedState = yield* updates.setChannel("nightly");
540 assert.equal(changedState.channel, "nightly");
541 }),
542 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
543 });
544
545 it.effect("marks the backend stop for an install as an update restart", () => {
546 let markersAtStop: ReadonlyArray<string> = [];
547 let inProgressAtStop: string | undefined;
548 const harness = makeHarness({
549 stopBackend: Effect.sync(() => {
550 markersAtStop = [...harness.updateRestartMarkers];
551 inProgressAtStop = harness.updateInProgressMarker();
552 }),
553 });
554
555 return Effect.scoped(
556 Effect.gen(function* () {
557 yield* TestClock.setTime(1_000);
558 const environment = yield* DesktopEnvironment.DesktopEnvironment;
559 const updates = yield* DesktopUpdates.DesktopUpdates;
560 yield* updates.configure;
561 harness.emit("update-downloaded", { version: "1.2.4" });
562 yield* flushCallbacks;
563
564 assert.isTrue((yield* updates.install).accepted);
565 assert.deepEqual(markersAtStop, [
566 environment.path.join(environment.baseDir, "runtime", DESKTOP_UPDATE_RESTART_MARKER_FILE),
567 ]);
568 // SSH launchers wait on this until the replacement backend publishes its runtime.
569 assert.deepEqual(JSON.parse(inProgressAtStop ?? "null"), {
570 startedAt: 1_000,
571 expiresAt: 61_000,
572 });
573 assert.isDefined(harness.updateInProgressMarker());
574 }),
575 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
576 });
577
578 it.effect("records the version an install hands to the installer", () => {
579 let recordsAtStop: ReadonlyArray<string> = [];
580 const harness = makeHarness({
581 stopBackend: Effect.sync(() => {
582 recordsAtStop = [...harness.installRecords.values()];
583 }),
584 });
585
586 return Effect.scoped(
587 Effect.gen(function* () {
588 const updates = yield* DesktopUpdates.DesktopUpdates;
589 yield* updates.configure;
590 harness.emit("update-downloaded", { version: "1.2.4" });
591 yield* flushCallbacks;
592
593 assert.isTrue((yield* updates.install).accepted);
594 assert.deepEqual(
595 recordsAtStop.map((record) => JSON.parse(record)),
596 [{ fromVersion: "1.2.3", targetVersion: "1.2.4" }],
597 );
598 }),
599 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
600 });
601
602 it.effect("flags an install that did not apply and keeps the flag through checks", () => {
603 const harness = makeHarness({
604 installRecord: JSON.stringify({ fromVersion: "1.2.3", targetVersion: "1.2.4" }),
605 });
606
607 return Effect.scoped(
608 Effect.gen(function* () {
609 const updates = yield* DesktopUpdates.DesktopUpdates;
610 yield* updates.configure;
611 assert.equal((yield* updates.getState).notAppliedVersion, "1.2.4");
612 assert.isDefined(harness.previousInstallRecord());
613
614 harness.emit("update-available", { version: "1.2.4" });
615 yield* flushCallbacks;
616 const state = yield* updates.getState;
617 assert.equal(state.status, "available");
618 assert.equal(state.notAppliedVersion, "1.2.4");
619 }),
620 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
621 });
622
623 // regression: #19291 an install that did apply was still flagged as not applied
624 it.effect("does not flag an install that applied", () => {
625 const harness = makeHarness({
626 installRecord: JSON.stringify({ fromVersion: "1.2.2", targetVersion: "1.2.3" }),
627 });
628
629 return Effect.scoped(
630 Effect.gen(function* () {
631 const updates = yield* DesktopUpdates.DesktopUpdates;
632 yield* updates.configure;
633 assert.isUndefined((yield* updates.getState).notAppliedVersion);
634 assert.isUndefined(harness.previousInstallRecord());
635 }),
636 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
637 });
638
639 it.effect("drops the update restart marker when an install is interrupted", () =>
640 Effect.gen(function* () {
641 const stopping = yield* Deferred.make<void>();
642 const harness = makeHarness({
643 stopBackend: Deferred.succeed(stopping, undefined).pipe(Effect.andThen(Effect.never)),
644 });
645
646 yield* Effect.scoped(
647 Effect.gen(function* () {
648 const updates = yield* DesktopUpdates.DesktopUpdates;
649 yield* updates.configure;
650 harness.emit("update-downloaded", { version: "1.2.4" });
651 yield* flushCallbacks;
652
653 const installFiber = yield* updates.install.pipe(Effect.forkScoped);
654 yield* Deferred.await(stopping);
655 assert.equal(harness.updateRestartMarkers.size, 1);
656 assert.isDefined(harness.updateInProgressMarker());
657
658 yield* Fiber.interrupt(installFiber);
659 assert.equal(harness.updateRestartMarkers.size, 0);
660 assert.isUndefined(harness.updateInProgressMarker());
661 }),
662 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
663 }),
664 );
665
666 it.effect("keeps windows and restarts backends when quitAndInstall fails", () => {
667 const harness = makeHarness({
668 quitAndInstall: Effect.fail(
669 new ElectronUpdater.ElectronUpdaterQuitAndInstallError({
670 channel: "latest",
671 isSilent: true,
672 isForceRunAfter: true,
673 cause: new Error("installer refused"),
674 }),
675 ),
676 });
677
678 return Effect.scoped(
679 Effect.gen(function* () {
680 const desktopState = yield* DesktopState.DesktopState;
681 const updates = yield* DesktopUpdates.DesktopUpdates;
682 yield* updates.configure;
683 harness.emit("update-downloaded", { version: "1.2.4" });
684 yield* flushCallbacks;
685
686 const result = yield* updates.install;
687 assert.isTrue(result.accepted);
688 assert.isFalse(yield* Ref.get(desktopState.quitting));
689 assert.deepEqual(harness.installSteps, [
690 "closeSshTunnels",
691 "quitAndInstall",
692 "startBackend",
693 ]);
694 assert.equal(harness.updateRestartMarkers.size, 0);
695 assert.isUndefined(harness.updateInProgressMarker());
696 assert.equal(harness.installRecords.size, 0);
697 }),
698 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
699 });
700
701 it.effect("keeps the disabled primary stopped when quitAndInstall fails", () => {
702 const harness = makeHarness({
703 backendDesiredRunning: false,
704 quitAndInstall: Effect.fail(
705 new ElectronUpdater.ElectronUpdaterQuitAndInstallError({
706 channel: "latest",
707 isSilent: true,
708 isForceRunAfter: true,
709 cause: new Error("installer refused"),
710 }),
711 ),
712 });
713
714 return Effect.scoped(
715 Effect.gen(function* () {
716 const updates = yield* DesktopUpdates.DesktopUpdates;
717 const pool = yield* DesktopBackendPool.DesktopBackendPool;
718 yield* updates.configure;
719 harness.emit("update-downloaded", { version: "1.2.4" });
720 yield* flushCallbacks;
721
722 const result = yield* updates.install;
723 assert.equal(result.state.errorContext, "install");
724 assert.deepEqual(harness.installSteps, ["closeSshTunnels", "quitAndInstall"]);
725 assert.isFalse((yield* (yield* pool.primary).snapshot).desiredRunning);
726 }),
727 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
728 });
729
730 it.effect("restores only desired-running instances after an installer error event", () => {
731 const starts: string[] = [];
732 const stops: string[] = [];
733 const makeInstance = (id: DesktopBackendPool.BackendInstanceId, initiallyRunning: boolean) => {
734 let desiredRunning = initiallyRunning;
735 return {
736 id,
737 label: Effect.succeed(id),
738 currentConfig: Effect.succeedNone,
739 start: Effect.sync(() => {
740 starts.push(id);
741 desiredRunning = true;
742 }),
743 stop: () =>
744 Effect.sync(() => {
745 stops.push(id);
746 desiredRunning = false;
747 }),
748 snapshot: Effect.sync(() => ({
749 desiredRunning,
750 ready: false,
751 activePid: Option.none(),
752 restartAttempt: 0,
753 restartScheduled: false,
754 })),
755 waitForReady: () => Effect.succeed(true),
756 } satisfies DesktopBackendPool.DesktopBackendInstance;
757 };
758 const primary = makeInstance(DesktopBackendPool.PRIMARY_INSTANCE_ID, false);
759 const wsl = makeInstance(DesktopBackendPool.BackendInstanceId("wsl:default"), true);
760 const harness = makeHarness({ backendInstances: [primary, wsl] });
761
762 return Effect.scoped(
763 Effect.gen(function* () {
764 const updates = yield* DesktopUpdates.DesktopUpdates;
765 yield* updates.configure;
766 harness.emit("update-downloaded", { version: "1.2.4" });
767 yield* flushCallbacks;
768
769 yield* updates.install;
770 assert.sameMembers(stops, [primary.id, wsl.id]);
771 assert.isFalse((yield* wsl.snapshot).desiredRunning);
772 const { changes } = yield* updates.subscribe;
773 const recovered = yield* Stream.runHead(changes).pipe(Effect.forkChild);
774 yield* flushCallbacks;
775 harness.emit("error", new Error("native installer refused"));
776 assert.equal(Option.getOrThrow(yield* Fiber.join(recovered)).errorContext, "install");
777
778 assert.deepEqual(starts, [wsl.id]);
779 assert.isFalse((yield* primary.snapshot).desiredRunning);
780 assert.isTrue((yield* wsl.snapshot).desiredRunning);
781 }),
782 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
783 });
784
785 it.effect("holds the install reservation until failed-install recovery finishes", () => {
786 const recoveryStarted = Deferred.makeUnsafe<void>();
787 const releaseRecovery = Deferred.makeUnsafe<void>();
788 const harness = makeHarness({
789 quitAndInstall: Effect.fail(
790 new ElectronUpdater.ElectronUpdaterQuitAndInstallError({
791 channel: "latest",
792 isSilent: true,
793 isForceRunAfter: true,
794 cause: new Error("installer refused"),
795 }),
796 ),
797 startBackend: Deferred.succeed(recoveryStarted, undefined).pipe(
798 Effect.andThen(Deferred.await(releaseRecovery)),
799 ),
800 });
801
802 return Effect.scoped(
803 Effect.gen(function* () {
804 const updates = yield* DesktopUpdates.DesktopUpdates;
805 yield* updates.configure;
806 harness.emit("update-downloaded", { version: "1.2.4" });
807 yield* flushCallbacks;
808
809 const failedInstall = yield* updates.install.pipe(Effect.forkChild);
810 yield* Deferred.await(recoveryStarted);
811 assert.isFalse(yield* updates.isInstallActive);
812
813 const overlappingInstall = yield* updates.install;
814 assert.isFalse(overlappingInstall.accepted);
815 assert.equal(harness.quitAndInstalls(), 1);
816 harness.emit("error", new Error("duplicate native installer error"));
817 yield* flushCallbacks;
818 assert.deepEqual(harness.installSteps, [
819 "closeSshTunnels",
820 "quitAndInstall",
821 "startBackend",
822 ]);
823
824 yield* Deferred.succeed(releaseRecovery, undefined);
825 const failedResult = yield* Fiber.join(failedInstall);
826 assert.equal(failedResult.state.errorContext, "install");
827
828 const retry = yield* updates.install;
829 assert.isTrue(retry.accepted);
830 assert.equal(harness.quitAndInstalls(), 2);
831 }),
832 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
833 });
834
835 it.effect("rejects a prepared install when the downloaded version changed", () => {
836 const harness = makeHarness();
837
838 return Effect.scoped(
839 Effect.gen(function* () {
840 const updates = yield* DesktopUpdates.DesktopUpdates;
841 yield* updates.configure;
842 harness.emit("update-downloaded", { version: "1.2.5" });
843 yield* flushCallbacks;
844
845 const result = yield* updates.installPrepared("1.2.4");
846 assert.isFalse(result.accepted);
847 assert.equal(harness.quitAndInstalls(), 0);
848 }),
849 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
850 });
851
852 it.effect("persists channel changes through the settings service", () => {
853 const harness = makeHarness();
854
855 return Effect.scoped(
856 Effect.gen(function* () {
857 const settings = yield* DesktopAppSettings.DesktopAppSettings;
858 const updates = yield* DesktopUpdates.DesktopUpdates;
859 yield* updates.configure;
860
861 const state = yield* updates.setChannel("nightly");
862 const persistedSettings = yield* settings.get;
863
864 assert.equal(state.channel, "nightly");
865 assert.equal(persistedSettings.updateChannel, "nightly");
866 assert.equal(persistedSettings.updateChannelConfiguredByUser, true);
867 }),
868 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
869 });
870
871 it.effect("ignores a finished download from a track it no longer follows", () => {
872 const harness = makeHarness();
873
874 return Effect.scoped(
875 Effect.gen(function* () {
876 const updates = yield* DesktopUpdates.DesktopUpdates;
877 yield* updates.configure;
878 // A backed-out switch's download keeps reporting progress until it finishes.
879 harness.emit("download-progress", { percent: 40 });
880 yield* flushCallbacks;
881 harness.emit("update-downloaded", { version: "1.2.4-nightly.20261027.1" });
882 yield* flushCallbacks;
883
884 const state = yield* updates.getState;
885 assert.equal(state.downloadedVersion, null);
886 assert.notEqual(state.status, "downloading");
887 }),
888 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
889 });
890
891 it.effect("offers older releases only to checks that leave the running build's track", () => {
892 const harness = makeHarness({ beforeSetUpdateChannel: Effect.void });
893
894 return Effect.scoped(
895 Effect.gen(function* () {
896 const updates = yield* DesktopUpdates.DesktopUpdates;
897 yield* updates.configure;
898 const before = harness.checkAllowedDowngrade.length;
899 yield* updates.setChannel("nightly");
900 yield* updates.setChannel("latest");
901 yield* updates.check("remote-update", { allowDowngrade: true });
902
903 // Back on its own Stable track the build never needs an older release.
904 assert.deepEqual(harness.checkAllowedDowngrade.slice(before), [true, false, true]);
905 assert.isFalse(harness.allowDowngrade());
906 }),
907 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
908 });
909
910 it.effect("re-checks a saved track that differs from the running build's track", () => {
911 const harness = makeHarness({
912 appVersion: "1.2.4-nightly.20261027.1",
913 beforeSetUpdateChannel: Effect.void,
914 });
915
916 return Effect.scoped(
917 Effect.gen(function* () {
918 const updates = yield* DesktopUpdates.DesktopUpdates;
919 yield* updates.configure;
920 yield* updates.setChannel("latest");
921 const checks = harness.checkCount();
922
923 yield* updates.setChannel("latest");
924
925 assert.equal(harness.checkCount(), checks + 1);
926 }),
927 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
928 });
929
930 it.effect("does not persist an unchanged update channel as a user preference", () => {
931 const harness = makeHarness();
932
933 return Effect.scoped(
934 Effect.gen(function* () {
935 const settings = yield* DesktopAppSettings.DesktopAppSettings;
936 const updates = yield* DesktopUpdates.DesktopUpdates;
937 yield* updates.configure;
938
939 const state = yield* updates.setChannel("latest");
940 const persistedSettings = yield* settings.get;
941
942 assert.equal(state.channel, "latest");
943 assert.equal(persistedSettings.updateChannel, "latest");
944 assert.equal(persistedSettings.updateChannelConfiguredByUser, false);
945 }),
946 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
947 });
948
949 it.effect("offers nothing below a track switch's checked release", () => {
950 const harness = makeHarness({
951 appVersion: "1.2.4-nightly.20261027.1",
952 beforeSetUpdateChannel: Effect.void,
953 });
954
955 return Effect.scoped(
956 Effect.gen(function* () {
957 const updates = yield* DesktopUpdates.DesktopUpdates;
958 yield* updates.configure;
959 yield* updates.setChannel("latest", { minimumVersion: "1.2.3" });
960
961 harness.emit("update-available", { version: "1.2.2" });
962 yield* flushCallbacks;
963 assert.isNull((yield* updates.getState).availableVersion);
964 harness.emit("update-downloaded", { version: "1.2.2" });
965 yield* flushCallbacks;
966 assert.isNull((yield* updates.getState).downloadedVersion);
967
968 harness.emit("update-available", { version: "1.2.3" });
969 yield* flushCallbacks;
970 assert.equal((yield* updates.getState).availableVersion, "1.2.3");
971 }),
972 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
973 });
974
975 it.effect("keeps the current track's offers when a track switch fails to save", () => {
976 const harness = makeHarness({
977 setUpdateChannelError: new DesktopAppSettings.DesktopSettingsWriteError({
978 operation: "replace-settings-file",
979 path: "desktop-settings.json",
980 cause: new Error("rename failed"),
981 }),
982 });
983
984 return Effect.scoped(
985 Effect.gen(function* () {
986 const updates = yield* DesktopUpdates.DesktopUpdates;
987 yield* updates.configure;
988 const error = yield* updates
989 .setChannel("nightly", { minimumVersion: "1.2.5-nightly.20261028.1" })
990 .pipe(Effect.flip);
991 assert.equal(error._tag, "DesktopUpdateChannelPersistenceError");
992 assert.equal((yield* updates.getState).channel, "latest");
993
994 harness.emit("update-available", { version: "1.2.4" });
995 yield* flushCallbacks;
996 assert.equal((yield* updates.getState).availableVersion, "1.2.4");
997 }),
998 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
999 });
1000
1001 it.effect("keeps a saved Stable track across a restart without offering an older release", () => {
1002 // A Nightly build whose switch to Stable did not finish before the app quit.
1003 const harness = makeHarness({ appVersion: "1.2.4-nightly.20261027.1", savedChannel: "latest" });
1004
1005 return Effect.scoped(
1006 Effect.gen(function* () {
1007 const updates = yield* DesktopUpdates.DesktopUpdates;
1008 yield* updates.configure;
1009 yield* updates.check("startup");
1010
1011 assert.equal((yield* updates.getState).channel, "latest");
1012 assert.deepEqual(harness.checkAllowedDowngrade, [false]);
1013 // Semver ranks Stable 1.2.4 above this Nightly, but a Nightly built before the release's
1014 // version bump can hold migrations 1.2.4 lacks. Crossing needs a guarded switch.
1015 harness.emit("update-available", { version: "1.2.4" });
1016 yield* flushCallbacks;
1017 assert.isNull((yield* updates.getState).availableVersion);
1018 }),
1019 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
1020 });
1021
1022 it.effect("keeps a saved Nightly track across a restart without crossing on its own", () => {
1023 // A Stable build whose switch to Nightly was refused before the app quit; its floor is gone.
1024 const harness = makeHarness({ savedChannel: "nightly" });
1025
1026 return Effect.scoped(
1027 Effect.gen(function* () {
1028 const updates = yield* DesktopUpdates.DesktopUpdates;
1029 yield* updates.configure;
1030 yield* updates.check("startup");
1031 harness.emit("update-available", { version: "1.2.4-nightly.20261027.1" });
1032 yield* flushCallbacks;
1033
1034 assert.equal((yield* updates.getState).channel, "nightly");
1035 assert.isNull((yield* updates.getState).availableVersion);
1036 assert.deepEqual(harness.checkAllowedDowngrade, [false]);
1037 }),
1038 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
1039 });
1040
1041 it.effect("never offers an older Nightly to a Nightly build's own checks", () => {
1042 const harness = makeHarness({ appVersion: "1.2.4-nightly.20261027.1" });
1043
1044 return Effect.scoped(
1045 Effect.gen(function* () {
1046 const updates = yield* DesktopUpdates.DesktopUpdates;
1047 yield* updates.configure;
1048 yield* updates.check("poll");
1049
1050 assert.equal((yield* updates.getState).channel, "nightly");
1051 assert.deepEqual(harness.checkAllowedDowngrade, [false]);
1052 }),
1053 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
1054 });
1055
1056 it.effect("waits out a background check instead of refusing a track change", () =>
1057 Effect.gen(function* () {
1058 const checkStarted = yield* Deferred.make<void>();
1059 const releaseCheck = yield* Deferred.make<void>();
1060 const harness = makeHarness({
1061 beforeSetUpdateChannel: Effect.void,
1062 checkForUpdates: Deferred.succeed(checkStarted, undefined).pipe(
1063 Effect.andThen(Deferred.await(releaseCheck)),
1064 ),
1065 });
1066
1067 yield* Effect.scoped(
1068 Effect.gen(function* () {
1069 const updates = yield* DesktopUpdates.DesktopUpdates;
1070 yield* updates.configure;
1071 yield* updates.check("poll").pipe(Effect.forkScoped);
1072 yield* Deferred.await(checkStarted);
1073
1074 const channelFiber = yield* updates.setChannel("nightly").pipe(Effect.forkScoped);
1075 yield* Deferred.succeed(releaseCheck, undefined);
1076 yield* TestClock.adjust(Duration.seconds(2));
1077 const state = yield* Fiber.join(channelFiber);
1078
1079 assert.equal(state.channel, "nightly");
1080 }),
1081 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
1082 }),
1083 );
1084
1085 it.effect("rejects checks while an update channel change is being persisted", () =>
1086 Effect.gen(function* () {
1087 const channelChangeStarted = yield* Deferred.make<void>();
1088 const releaseChannelChange = yield* Deferred.make<void>();
1089 const harness = makeHarness({
1090 beforeSetUpdateChannel: Deferred.succeed(channelChangeStarted, undefined).pipe(
1091 Effect.andThen(Deferred.await(releaseChannelChange)),
1092 ),
1093 });
1094
1095 yield* Effect.scoped(
1096 Effect.gen(function* () {
1097 const updates = yield* DesktopUpdates.DesktopUpdates;
1098 yield* updates.configure;
1099
1100 const channelFiber = yield* updates.setChannel("nightly").pipe(Effect.forkScoped);
1101 yield* Deferred.await(channelChangeStarted);
1102
1103 const checkResult = yield* updates.check("manual");
1104 assert.isFalse(checkResult.checked);
1105 assert.equal(harness.checkCount(), 0);
1106
1107 yield* Deferred.succeed(releaseChannelChange, undefined);
1108 const state = yield* Fiber.join(channelFiber);
1109
1110 assert.equal(state.channel, "nightly");
1111 assert.equal(harness.checkCount(), 1);
1112 }),
1113 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
1114 }),
1115 );
1116});
1117
1118describe("describeUpdateErrorCause", () => {
1119 it("removes URLs, credentials, response bodies, and headers from summaries", () => {
1120 const messages = [
1121 "Cannot download https://user:url-secret@example.com/update?token=query-secret, status 403",
1122 "Request failed: Bearer bearer-secret",
1123 "Request failed: Basic basic-secret",
1124 "Request failed: Authorization: Bearer header-secret",
1125 "Request failed: Cookie=session-secret",
1126 "Request failed: api_key=\"key-secret\" password='password-secret' access_token=token-secret",
1127 '404 Not Found\n{"body":"body-secret"}\nHeaders: cookie=header-secret',
1128 "Request failed\rHeaders: cookie=header-secret",
1129 ];
1130
1131 for (const message of messages) {
1132 const summary = DesktopUpdates.describeUpdateErrorCause(new Error(message));
1133 assert.include(summary, "Error:");
1134 assert.notInclude(summary, "secret");
1135 assert.notInclude(summary, "https://");
1136 assert.notInclude(summary, "Headers:");
1137 }
1138 assert.equal(
1139 DesktopUpdates.describeUpdateErrorCause("404 Not Found\nbody-secret"),
1140 "404 Not Found",
1141 );
1142 });
1143
1144 it("bounds long messages, parallel failures, and cyclic causes", () => {
1145 const longError = new Error("x".repeat(10_000));
1146 const parallel = Cause.combine(Cause.fail(longError), Cause.die(longError));
1147 assert.isAtMost(DesktopUpdates.describeUpdateErrorCause(parallel).length, 1024);
1148
1149 const cyclic = new Error("retry failed");
1150 cyclic.cause = cyclic;
1151 assert.include(DesktopUpdates.describeUpdateErrorCause(cyclic), "[truncated cause]");
1152 assert.isAtMost(DesktopUpdates.describeUpdateErrorCause(cyclic).length, 1024);
1153 });
1154
1155 it("does not serialize arbitrary payloads or let unreadable errors break logging", () => {
1156 assert.equal(DesktopUpdates.describeUpdateErrorCause({ payload: "secret" }), "object");
1157 const unreadable = Object.defineProperty({}, "message", {
1158 get() {
1159 throw new Error("unreadable error");
1160 },
1161 });
1162 assert.equal(DesktopUpdates.describeUpdateErrorCause(unreadable), "UnknownError");
1163 });
1164});
1165
1166describe("DesktopUpdates under a managed policy", () => {
1167 const message = "Example Corp manages T3 Code updates.";
1168 const managed = (
1169 policy: Partial<DesktopUpdatePolicy.UpdatePolicy>,
1170 ): DesktopUpdatePolicy.UpdatePolicy => ({
1171 ...DesktopUpdatePolicy.unmanagedUpdatePolicy,
1172 message,
1173 ...policy,
1174 });
1175
1176 it.effect("never checks when updates are off", () => {
1177 const harness = makeHarness({ policy: managed({ mode: "off" }) });
1178
1179 return Effect.scoped(
1180 Effect.gen(function* () {
1181 const updates = yield* DesktopUpdates.DesktopUpdates;
1182 yield* updates.configure;
1183 yield* TestClock.adjust("10 minutes");
1184
1185 const state = yield* updates.getState;
1186 assert.equal(state.status, "disabled");
1187 assert.equal(state.message, message);
1188 assert.isFalse((yield* updates.check("menu")).checked);
1189 assert.deepEqual(yield* updates.disabledReason, Option.some(message));
1190 assert.equal(harness.checkCount(), 0);
1191 assert.equal(harness.listenerCount(), 0);
1192 }),
1193 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
1194 });
1195
1196 it.effect("updates from the administrator's feed", () => {
1197 const harness = makeHarness({
1198 // Any user can set the mock flag; it must not redirect a managed feed.
1199 env: { T3CODE_DESKTOP_MOCK_UPDATES: "true" },
1200 policy: managed({ feedUrl: "https://updates.example.com/t3code" }),
1201 });
1202
1203 return Effect.scoped(
1204 Effect.gen(function* () {
1205 const updates = yield* DesktopUpdates.DesktopUpdates;
1206 yield* updates.configure;
1207 assert.equal((yield* updates.getState).status, "idle");
1208 assert.deepEqual(harness.feedUrls(), [
1209 { provider: "generic", url: "https://updates.example.com/t3code" },
1210 ]);
1211 }),
1212 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
1213 });
1214
1215 it.effect("refuses the nightly channel without nightly", () => {
1216 const harness = makeHarness({ policy: managed({ allowNightly: false }) });
1217
1218 return Effect.scoped(
1219 Effect.gen(function* () {
1220 const updates = yield* DesktopUpdates.DesktopUpdates;
1221 yield* updates.configure;
1222 const error = yield* updates.setChannel("nightly").pipe(Effect.flip);
1223 assert.equal(error._tag, "DesktopUpdateChannelManagedError");
1224 assert.equal((yield* updates.getState).channel, "latest");
1225 }),
1226 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
1227 });
1228
1229 it.effect("does not report the channel as the only block when updates are off anyway", () => {
1230 const harness = makeHarness({
1231 appVersion: "1.2.3-nightly.20261028.1",
1232 env: { T3CODE_DISABLE_AUTO_UPDATE: "true" },
1233 policy: managed({ allowNightly: false, feedUrl: "https://updates.example.com/t3code" }),
1234 });
1235
1236 return Effect.scoped(
1237 Effect.gen(function* () {
1238 const updates = yield* DesktopUpdates.DesktopUpdates;
1239 yield* updates.configure;
1240 const state = yield* updates.getState;
1241 assert.isFalse(state.enabled);
1242 assert.isUndefined(state.channelBlockedByPolicy);
1243 }),
1244 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
1245 });
1246
1247 it.effect("holds a nightly install until its user switches to stable", () => {
1248 const harness = makeHarness({
1249 appVersion: "1.2.3-nightly.20261028.1",
1250 // A managed app ignores the mock feed, so give it a real one.
1251 policy: managed({ allowNightly: false, feedUrl: "https://updates.example.com/t3code" }),
1252 });
1253
1254 return Effect.scoped(
1255 Effect.gen(function* () {
1256 const updates = yield* DesktopUpdates.DesktopUpdates;
1257 yield* updates.configure;
1258 yield* TestClock.adjust("10 minutes");
1259
1260 const held = yield* updates.getState;
1261 assert.equal(held.channel, "nightly");
1262 assert.equal(held.status, "disabled");
1263 assert.include(held.message ?? "", message);
1264 assert.isTrue(held.channelBlockedByPolicy);
1265 assert.isFalse((yield* updates.check("menu")).checked);
1266 assert.equal(harness.checkCount(), 0);
1267
1268 const stable = yield* updates.setChannel("latest");
1269 assert.equal(stable.channel, "latest");
1270 assert.isTrue(stable.enabled);
1271 assert.isUndefined(stable.channelBlockedByPolicy);
1272 assert.equal(harness.checkCount(), 1);
1273 }),
1274 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
1275 });
1276});