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 const harness = makeHarness({
548 stopBackend: Effect.sync(() => {
549 markersAtStop = [...harness.updateRestartMarkers];
550 }),
551 });
552
553 return Effect.scoped(
554 Effect.gen(function* () {
555 const environment = yield* DesktopEnvironment.DesktopEnvironment;
556 const updates = yield* DesktopUpdates.DesktopUpdates;
557 yield* updates.configure;
558 harness.emit("update-downloaded", { version: "1.2.4" });
559 yield* flushCallbacks;
560
561 assert.isTrue((yield* updates.install).accepted);
562 assert.deepEqual(markersAtStop, [
563 environment.path.join(environment.baseDir, "runtime", DESKTOP_UPDATE_RESTART_MARKER_FILE),
564 ]);
565 }),
566 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
567 });
568
569 it.effect("records the version an install hands to the installer", () => {
570 let recordsAtStop: ReadonlyArray<string> = [];
571 const harness = makeHarness({
572 stopBackend: Effect.sync(() => {
573 recordsAtStop = [...harness.installRecords.values()];
574 }),
575 });
576
577 return Effect.scoped(
578 Effect.gen(function* () {
579 const updates = yield* DesktopUpdates.DesktopUpdates;
580 yield* updates.configure;
581 harness.emit("update-downloaded", { version: "1.2.4" });
582 yield* flushCallbacks;
583
584 assert.isTrue((yield* updates.install).accepted);
585 assert.deepEqual(
586 recordsAtStop.map((record) => JSON.parse(record)),
587 [{ fromVersion: "1.2.3", targetVersion: "1.2.4" }],
588 );
589 }),
590 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
591 });
592
593 it.effect("flags an install that did not apply and keeps the flag through checks", () => {
594 const harness = makeHarness({
595 installRecord: JSON.stringify({ fromVersion: "1.2.3", targetVersion: "1.2.4" }),
596 });
597
598 return Effect.scoped(
599 Effect.gen(function* () {
600 const updates = yield* DesktopUpdates.DesktopUpdates;
601 yield* updates.configure;
602 assert.equal((yield* updates.getState).notAppliedVersion, "1.2.4");
603 assert.isDefined(harness.previousInstallRecord());
604
605 harness.emit("update-available", { version: "1.2.4" });
606 yield* flushCallbacks;
607 const state = yield* updates.getState;
608 assert.equal(state.status, "available");
609 assert.equal(state.notAppliedVersion, "1.2.4");
610 }),
611 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
612 });
613
614 // regression: #19291 an install that did apply was still flagged as not applied
615 it.effect("does not flag an install that applied", () => {
616 const harness = makeHarness({
617 installRecord: JSON.stringify({ fromVersion: "1.2.2", targetVersion: "1.2.3" }),
618 });
619
620 return Effect.scoped(
621 Effect.gen(function* () {
622 const updates = yield* DesktopUpdates.DesktopUpdates;
623 yield* updates.configure;
624 assert.isUndefined((yield* updates.getState).notAppliedVersion);
625 assert.isUndefined(harness.previousInstallRecord());
626 }),
627 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
628 });
629
630 it.effect("drops the update restart marker when an install is interrupted", () =>
631 Effect.gen(function* () {
632 const stopping = yield* Deferred.make<void>();
633 const harness = makeHarness({
634 stopBackend: Deferred.succeed(stopping, undefined).pipe(Effect.andThen(Effect.never)),
635 });
636
637 yield* Effect.scoped(
638 Effect.gen(function* () {
639 const updates = yield* DesktopUpdates.DesktopUpdates;
640 yield* updates.configure;
641 harness.emit("update-downloaded", { version: "1.2.4" });
642 yield* flushCallbacks;
643
644 const installFiber = yield* updates.install.pipe(Effect.forkScoped);
645 yield* Deferred.await(stopping);
646 assert.equal(harness.updateRestartMarkers.size, 1);
647
648 yield* Fiber.interrupt(installFiber);
649 assert.equal(harness.updateRestartMarkers.size, 0);
650 }),
651 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
652 }),
653 );
654
655 it.effect("keeps windows and restarts backends when quitAndInstall fails", () => {
656 const harness = makeHarness({
657 quitAndInstall: Effect.fail(
658 new ElectronUpdater.ElectronUpdaterQuitAndInstallError({
659 channel: "latest",
660 isSilent: true,
661 isForceRunAfter: true,
662 cause: new Error("installer refused"),
663 }),
664 ),
665 });
666
667 return Effect.scoped(
668 Effect.gen(function* () {
669 const desktopState = yield* DesktopState.DesktopState;
670 const updates = yield* DesktopUpdates.DesktopUpdates;
671 yield* updates.configure;
672 harness.emit("update-downloaded", { version: "1.2.4" });
673 yield* flushCallbacks;
674
675 const result = yield* updates.install;
676 assert.isTrue(result.accepted);
677 assert.isFalse(yield* Ref.get(desktopState.quitting));
678 assert.deepEqual(harness.installSteps, [
679 "closeSshTunnels",
680 "quitAndInstall",
681 "startBackend",
682 ]);
683 assert.equal(harness.updateRestartMarkers.size, 0);
684 assert.equal(harness.installRecords.size, 0);
685 }),
686 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
687 });
688
689 it.effect("keeps the disabled primary stopped when quitAndInstall fails", () => {
690 const harness = makeHarness({
691 backendDesiredRunning: false,
692 quitAndInstall: Effect.fail(
693 new ElectronUpdater.ElectronUpdaterQuitAndInstallError({
694 channel: "latest",
695 isSilent: true,
696 isForceRunAfter: true,
697 cause: new Error("installer refused"),
698 }),
699 ),
700 });
701
702 return Effect.scoped(
703 Effect.gen(function* () {
704 const updates = yield* DesktopUpdates.DesktopUpdates;
705 const pool = yield* DesktopBackendPool.DesktopBackendPool;
706 yield* updates.configure;
707 harness.emit("update-downloaded", { version: "1.2.4" });
708 yield* flushCallbacks;
709
710 const result = yield* updates.install;
711 assert.equal(result.state.errorContext, "install");
712 assert.deepEqual(harness.installSteps, ["closeSshTunnels", "quitAndInstall"]);
713 assert.isFalse((yield* (yield* pool.primary).snapshot).desiredRunning);
714 }),
715 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
716 });
717
718 it.effect("restores only desired-running instances after an installer error event", () => {
719 const starts: string[] = [];
720 const stops: string[] = [];
721 const makeInstance = (id: DesktopBackendPool.BackendInstanceId, initiallyRunning: boolean) => {
722 let desiredRunning = initiallyRunning;
723 return {
724 id,
725 label: Effect.succeed(id),
726 currentConfig: Effect.succeedNone,
727 start: Effect.sync(() => {
728 starts.push(id);
729 desiredRunning = true;
730 }),
731 stop: () =>
732 Effect.sync(() => {
733 stops.push(id);
734 desiredRunning = false;
735 }),
736 snapshot: Effect.sync(() => ({
737 desiredRunning,
738 ready: false,
739 activePid: Option.none(),
740 restartAttempt: 0,
741 restartScheduled: false,
742 })),
743 waitForReady: () => Effect.succeed(true),
744 } satisfies DesktopBackendPool.DesktopBackendInstance;
745 };
746 const primary = makeInstance(DesktopBackendPool.PRIMARY_INSTANCE_ID, false);
747 const wsl = makeInstance(DesktopBackendPool.BackendInstanceId("wsl:default"), true);
748 const harness = makeHarness({ backendInstances: [primary, wsl] });
749
750 return Effect.scoped(
751 Effect.gen(function* () {
752 const updates = yield* DesktopUpdates.DesktopUpdates;
753 yield* updates.configure;
754 harness.emit("update-downloaded", { version: "1.2.4" });
755 yield* flushCallbacks;
756
757 yield* updates.install;
758 assert.sameMembers(stops, [primary.id, wsl.id]);
759 assert.isFalse((yield* wsl.snapshot).desiredRunning);
760 const { changes } = yield* updates.subscribe;
761 const recovered = yield* Stream.runHead(changes).pipe(Effect.forkChild);
762 yield* flushCallbacks;
763 harness.emit("error", new Error("native installer refused"));
764 assert.equal(Option.getOrThrow(yield* Fiber.join(recovered)).errorContext, "install");
765
766 assert.deepEqual(starts, [wsl.id]);
767 assert.isFalse((yield* primary.snapshot).desiredRunning);
768 assert.isTrue((yield* wsl.snapshot).desiredRunning);
769 }),
770 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
771 });
772
773 it.effect("holds the install reservation until failed-install recovery finishes", () => {
774 const recoveryStarted = Deferred.makeUnsafe<void>();
775 const releaseRecovery = Deferred.makeUnsafe<void>();
776 const harness = makeHarness({
777 quitAndInstall: Effect.fail(
778 new ElectronUpdater.ElectronUpdaterQuitAndInstallError({
779 channel: "latest",
780 isSilent: true,
781 isForceRunAfter: true,
782 cause: new Error("installer refused"),
783 }),
784 ),
785 startBackend: Deferred.succeed(recoveryStarted, undefined).pipe(
786 Effect.andThen(Deferred.await(releaseRecovery)),
787 ),
788 });
789
790 return Effect.scoped(
791 Effect.gen(function* () {
792 const updates = yield* DesktopUpdates.DesktopUpdates;
793 yield* updates.configure;
794 harness.emit("update-downloaded", { version: "1.2.4" });
795 yield* flushCallbacks;
796
797 const failedInstall = yield* updates.install.pipe(Effect.forkChild);
798 yield* Deferred.await(recoveryStarted);
799 assert.isFalse(yield* updates.isInstallActive);
800
801 const overlappingInstall = yield* updates.install;
802 assert.isFalse(overlappingInstall.accepted);
803 assert.equal(harness.quitAndInstalls(), 1);
804 harness.emit("error", new Error("duplicate native installer error"));
805 yield* flushCallbacks;
806 assert.deepEqual(harness.installSteps, [
807 "closeSshTunnels",
808 "quitAndInstall",
809 "startBackend",
810 ]);
811
812 yield* Deferred.succeed(releaseRecovery, undefined);
813 const failedResult = yield* Fiber.join(failedInstall);
814 assert.equal(failedResult.state.errorContext, "install");
815
816 const retry = yield* updates.install;
817 assert.isTrue(retry.accepted);
818 assert.equal(harness.quitAndInstalls(), 2);
819 }),
820 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
821 });
822
823 it.effect("rejects a prepared install when the downloaded version changed", () => {
824 const harness = makeHarness();
825
826 return Effect.scoped(
827 Effect.gen(function* () {
828 const updates = yield* DesktopUpdates.DesktopUpdates;
829 yield* updates.configure;
830 harness.emit("update-downloaded", { version: "1.2.5" });
831 yield* flushCallbacks;
832
833 const result = yield* updates.installPrepared("1.2.4");
834 assert.isFalse(result.accepted);
835 assert.equal(harness.quitAndInstalls(), 0);
836 }),
837 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
838 });
839
840 it.effect("persists channel changes through the settings service", () => {
841 const harness = makeHarness();
842
843 return Effect.scoped(
844 Effect.gen(function* () {
845 const settings = yield* DesktopAppSettings.DesktopAppSettings;
846 const updates = yield* DesktopUpdates.DesktopUpdates;
847 yield* updates.configure;
848
849 const state = yield* updates.setChannel("nightly");
850 const persistedSettings = yield* settings.get;
851
852 assert.equal(state.channel, "nightly");
853 assert.equal(persistedSettings.updateChannel, "nightly");
854 assert.equal(persistedSettings.updateChannelConfiguredByUser, true);
855 }),
856 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
857 });
858
859 it.effect("ignores a finished download from a track it no longer follows", () => {
860 const harness = makeHarness();
861
862 return Effect.scoped(
863 Effect.gen(function* () {
864 const updates = yield* DesktopUpdates.DesktopUpdates;
865 yield* updates.configure;
866 // A backed-out switch's download keeps reporting progress until it finishes.
867 harness.emit("download-progress", { percent: 40 });
868 yield* flushCallbacks;
869 harness.emit("update-downloaded", { version: "1.2.4-nightly.20261027.1" });
870 yield* flushCallbacks;
871
872 const state = yield* updates.getState;
873 assert.equal(state.downloadedVersion, null);
874 assert.notEqual(state.status, "downloading");
875 }),
876 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
877 });
878
879 it.effect("offers older releases only to checks that leave the running build's track", () => {
880 const harness = makeHarness({ beforeSetUpdateChannel: Effect.void });
881
882 return Effect.scoped(
883 Effect.gen(function* () {
884 const updates = yield* DesktopUpdates.DesktopUpdates;
885 yield* updates.configure;
886 const before = harness.checkAllowedDowngrade.length;
887 yield* updates.setChannel("nightly");
888 yield* updates.setChannel("latest");
889 yield* updates.check("remote-update", { allowDowngrade: true });
890
891 // Back on its own Stable track the build never needs an older release.
892 assert.deepEqual(harness.checkAllowedDowngrade.slice(before), [true, false, true]);
893 assert.isFalse(harness.allowDowngrade());
894 }),
895 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
896 });
897
898 it.effect("re-checks a saved track that differs from the running build's track", () => {
899 const harness = makeHarness({
900 appVersion: "1.2.4-nightly.20261027.1",
901 beforeSetUpdateChannel: Effect.void,
902 });
903
904 return Effect.scoped(
905 Effect.gen(function* () {
906 const updates = yield* DesktopUpdates.DesktopUpdates;
907 yield* updates.configure;
908 yield* updates.setChannel("latest");
909 const checks = harness.checkCount();
910
911 yield* updates.setChannel("latest");
912
913 assert.equal(harness.checkCount(), checks + 1);
914 }),
915 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
916 });
917
918 it.effect("does not persist an unchanged update channel as a user preference", () => {
919 const harness = makeHarness();
920
921 return Effect.scoped(
922 Effect.gen(function* () {
923 const settings = yield* DesktopAppSettings.DesktopAppSettings;
924 const updates = yield* DesktopUpdates.DesktopUpdates;
925 yield* updates.configure;
926
927 const state = yield* updates.setChannel("latest");
928 const persistedSettings = yield* settings.get;
929
930 assert.equal(state.channel, "latest");
931 assert.equal(persistedSettings.updateChannel, "latest");
932 assert.equal(persistedSettings.updateChannelConfiguredByUser, false);
933 }),
934 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
935 });
936
937 it.effect("offers nothing below a track switch's checked release", () => {
938 const harness = makeHarness({
939 appVersion: "1.2.4-nightly.20261027.1",
940 beforeSetUpdateChannel: Effect.void,
941 });
942
943 return Effect.scoped(
944 Effect.gen(function* () {
945 const updates = yield* DesktopUpdates.DesktopUpdates;
946 yield* updates.configure;
947 yield* updates.setChannel("latest", { minimumVersion: "1.2.3" });
948
949 harness.emit("update-available", { version: "1.2.2" });
950 yield* flushCallbacks;
951 assert.isNull((yield* updates.getState).availableVersion);
952 harness.emit("update-downloaded", { version: "1.2.2" });
953 yield* flushCallbacks;
954 assert.isNull((yield* updates.getState).downloadedVersion);
955
956 harness.emit("update-available", { version: "1.2.3" });
957 yield* flushCallbacks;
958 assert.equal((yield* updates.getState).availableVersion, "1.2.3");
959 }),
960 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
961 });
962
963 it.effect("keeps the current track's offers when a track switch fails to save", () => {
964 const harness = makeHarness({
965 setUpdateChannelError: new DesktopAppSettings.DesktopSettingsWriteError({
966 operation: "replace-settings-file",
967 path: "desktop-settings.json",
968 cause: new Error("rename failed"),
969 }),
970 });
971
972 return Effect.scoped(
973 Effect.gen(function* () {
974 const updates = yield* DesktopUpdates.DesktopUpdates;
975 yield* updates.configure;
976 const error = yield* updates
977 .setChannel("nightly", { minimumVersion: "1.2.5-nightly.20261028.1" })
978 .pipe(Effect.flip);
979 assert.equal(error._tag, "DesktopUpdateChannelPersistenceError");
980 assert.equal((yield* updates.getState).channel, "latest");
981
982 harness.emit("update-available", { version: "1.2.4" });
983 yield* flushCallbacks;
984 assert.equal((yield* updates.getState).availableVersion, "1.2.4");
985 }),
986 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
987 });
988
989 it.effect("keeps a saved Stable track across a restart without offering an older release", () => {
990 // A Nightly build whose switch to Stable did not finish before the app quit.
991 const harness = makeHarness({ appVersion: "1.2.4-nightly.20261027.1", savedChannel: "latest" });
992
993 return Effect.scoped(
994 Effect.gen(function* () {
995 const updates = yield* DesktopUpdates.DesktopUpdates;
996 yield* updates.configure;
997 yield* updates.check("startup");
998
999 assert.equal((yield* updates.getState).channel, "latest");
1000 assert.deepEqual(harness.checkAllowedDowngrade, [false]);
1001 // Semver ranks Stable 1.2.4 above this Nightly, but a Nightly built before the release's
1002 // version bump can hold migrations 1.2.4 lacks. Crossing needs a guarded switch.
1003 harness.emit("update-available", { version: "1.2.4" });
1004 yield* flushCallbacks;
1005 assert.isNull((yield* updates.getState).availableVersion);
1006 }),
1007 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
1008 });
1009
1010 it.effect("keeps a saved Nightly track across a restart without crossing on its own", () => {
1011 // A Stable build whose switch to Nightly was refused before the app quit; its floor is gone.
1012 const harness = makeHarness({ savedChannel: "nightly" });
1013
1014 return Effect.scoped(
1015 Effect.gen(function* () {
1016 const updates = yield* DesktopUpdates.DesktopUpdates;
1017 yield* updates.configure;
1018 yield* updates.check("startup");
1019 harness.emit("update-available", { version: "1.2.4-nightly.20261027.1" });
1020 yield* flushCallbacks;
1021
1022 assert.equal((yield* updates.getState).channel, "nightly");
1023 assert.isNull((yield* updates.getState).availableVersion);
1024 assert.deepEqual(harness.checkAllowedDowngrade, [false]);
1025 }),
1026 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
1027 });
1028
1029 it.effect("never offers an older Nightly to a Nightly build's own checks", () => {
1030 const harness = makeHarness({ appVersion: "1.2.4-nightly.20261027.1" });
1031
1032 return Effect.scoped(
1033 Effect.gen(function* () {
1034 const updates = yield* DesktopUpdates.DesktopUpdates;
1035 yield* updates.configure;
1036 yield* updates.check("poll");
1037
1038 assert.equal((yield* updates.getState).channel, "nightly");
1039 assert.deepEqual(harness.checkAllowedDowngrade, [false]);
1040 }),
1041 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
1042 });
1043
1044 it.effect("waits out a background check instead of refusing a track change", () =>
1045 Effect.gen(function* () {
1046 const checkStarted = yield* Deferred.make<void>();
1047 const releaseCheck = yield* Deferred.make<void>();
1048 const harness = makeHarness({
1049 beforeSetUpdateChannel: Effect.void,
1050 checkForUpdates: Deferred.succeed(checkStarted, undefined).pipe(
1051 Effect.andThen(Deferred.await(releaseCheck)),
1052 ),
1053 });
1054
1055 yield* Effect.scoped(
1056 Effect.gen(function* () {
1057 const updates = yield* DesktopUpdates.DesktopUpdates;
1058 yield* updates.configure;
1059 yield* updates.check("poll").pipe(Effect.forkScoped);
1060 yield* Deferred.await(checkStarted);
1061
1062 const channelFiber = yield* updates.setChannel("nightly").pipe(Effect.forkScoped);
1063 yield* Deferred.succeed(releaseCheck, undefined);
1064 yield* TestClock.adjust(Duration.seconds(2));
1065 const state = yield* Fiber.join(channelFiber);
1066
1067 assert.equal(state.channel, "nightly");
1068 }),
1069 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
1070 }),
1071 );
1072
1073 it.effect("rejects checks while an update channel change is being persisted", () =>
1074 Effect.gen(function* () {
1075 const channelChangeStarted = yield* Deferred.make<void>();
1076 const releaseChannelChange = yield* Deferred.make<void>();
1077 const harness = makeHarness({
1078 beforeSetUpdateChannel: Deferred.succeed(channelChangeStarted, undefined).pipe(
1079 Effect.andThen(Deferred.await(releaseChannelChange)),
1080 ),
1081 });
1082
1083 yield* Effect.scoped(
1084 Effect.gen(function* () {
1085 const updates = yield* DesktopUpdates.DesktopUpdates;
1086 yield* updates.configure;
1087
1088 const channelFiber = yield* updates.setChannel("nightly").pipe(Effect.forkScoped);
1089 yield* Deferred.await(channelChangeStarted);
1090
1091 const checkResult = yield* updates.check("manual");
1092 assert.isFalse(checkResult.checked);
1093 assert.equal(harness.checkCount(), 0);
1094
1095 yield* Deferred.succeed(releaseChannelChange, undefined);
1096 const state = yield* Fiber.join(channelFiber);
1097
1098 assert.equal(state.channel, "nightly");
1099 assert.equal(harness.checkCount(), 1);
1100 }),
1101 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
1102 }),
1103 );
1104});
1105
1106describe("describeUpdateErrorCause", () => {
1107 it("removes URLs, credentials, response bodies, and headers from summaries", () => {
1108 const messages = [
1109 "Cannot download https://user:url-secret@example.com/update?token=query-secret, status 403",
1110 "Request failed: Bearer bearer-secret",
1111 "Request failed: Basic basic-secret",
1112 "Request failed: Authorization: Bearer header-secret",
1113 "Request failed: Cookie=session-secret",
1114 "Request failed: api_key=\"key-secret\" password='password-secret' access_token=token-secret",
1115 '404 Not Found\n{"body":"body-secret"}\nHeaders: cookie=header-secret',
1116 "Request failed\rHeaders: cookie=header-secret",
1117 ];
1118
1119 for (const message of messages) {
1120 const summary = DesktopUpdates.describeUpdateErrorCause(new Error(message));
1121 assert.include(summary, "Error:");
1122 assert.notInclude(summary, "secret");
1123 assert.notInclude(summary, "https://");
1124 assert.notInclude(summary, "Headers:");
1125 }
1126 assert.equal(
1127 DesktopUpdates.describeUpdateErrorCause("404 Not Found\nbody-secret"),
1128 "404 Not Found",
1129 );
1130 });
1131
1132 it("bounds long messages, parallel failures, and cyclic causes", () => {
1133 const longError = new Error("x".repeat(10_000));
1134 const parallel = Cause.combine(Cause.fail(longError), Cause.die(longError));
1135 assert.isAtMost(DesktopUpdates.describeUpdateErrorCause(parallel).length, 1024);
1136
1137 const cyclic = new Error("retry failed");
1138 cyclic.cause = cyclic;
1139 assert.include(DesktopUpdates.describeUpdateErrorCause(cyclic), "[truncated cause]");
1140 assert.isAtMost(DesktopUpdates.describeUpdateErrorCause(cyclic).length, 1024);
1141 });
1142
1143 it("does not serialize arbitrary payloads or let unreadable errors break logging", () => {
1144 assert.equal(DesktopUpdates.describeUpdateErrorCause({ payload: "secret" }), "object");
1145 const unreadable = Object.defineProperty({}, "message", {
1146 get() {
1147 throw new Error("unreadable error");
1148 },
1149 });
1150 assert.equal(DesktopUpdates.describeUpdateErrorCause(unreadable), "UnknownError");
1151 });
1152});
1153
1154describe("DesktopUpdates under a managed policy", () => {
1155 const message = "Example Corp manages T3 Code updates.";
1156 const managed = (
1157 policy: Partial<DesktopUpdatePolicy.UpdatePolicy>,
1158 ): DesktopUpdatePolicy.UpdatePolicy => ({
1159 ...DesktopUpdatePolicy.unmanagedUpdatePolicy,
1160 message,
1161 ...policy,
1162 });
1163
1164 it.effect("never checks when updates are off", () => {
1165 const harness = makeHarness({ policy: managed({ mode: "off" }) });
1166
1167 return Effect.scoped(
1168 Effect.gen(function* () {
1169 const updates = yield* DesktopUpdates.DesktopUpdates;
1170 yield* updates.configure;
1171 yield* TestClock.adjust("10 minutes");
1172
1173 const state = yield* updates.getState;
1174 assert.equal(state.status, "disabled");
1175 assert.equal(state.message, message);
1176 assert.isFalse((yield* updates.check("menu")).checked);
1177 assert.deepEqual(yield* updates.disabledReason, Option.some(message));
1178 assert.equal(harness.checkCount(), 0);
1179 assert.equal(harness.listenerCount(), 0);
1180 }),
1181 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
1182 });
1183
1184 it.effect("updates from the administrator's feed", () => {
1185 const harness = makeHarness({
1186 // Any user can set the mock flag; it must not redirect a managed feed.
1187 env: { T3CODE_DESKTOP_MOCK_UPDATES: "true" },
1188 policy: managed({ feedUrl: "https://updates.example.com/t3code" }),
1189 });
1190
1191 return Effect.scoped(
1192 Effect.gen(function* () {
1193 const updates = yield* DesktopUpdates.DesktopUpdates;
1194 yield* updates.configure;
1195 assert.equal((yield* updates.getState).status, "idle");
1196 assert.deepEqual(harness.feedUrls(), [
1197 { provider: "generic", url: "https://updates.example.com/t3code" },
1198 ]);
1199 }),
1200 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
1201 });
1202
1203 it.effect("refuses the nightly channel without nightly", () => {
1204 const harness = makeHarness({ policy: managed({ allowNightly: false }) });
1205
1206 return Effect.scoped(
1207 Effect.gen(function* () {
1208 const updates = yield* DesktopUpdates.DesktopUpdates;
1209 yield* updates.configure;
1210 const error = yield* updates.setChannel("nightly").pipe(Effect.flip);
1211 assert.equal(error._tag, "DesktopUpdateChannelManagedError");
1212 assert.equal((yield* updates.getState).channel, "latest");
1213 }),
1214 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
1215 });
1216
1217 it.effect("does not report the channel as the only block when updates are off anyway", () => {
1218 const harness = makeHarness({
1219 appVersion: "1.2.3-nightly.20261028.1",
1220 env: { T3CODE_DISABLE_AUTO_UPDATE: "true" },
1221 policy: managed({ allowNightly: false, feedUrl: "https://updates.example.com/t3code" }),
1222 });
1223
1224 return Effect.scoped(
1225 Effect.gen(function* () {
1226 const updates = yield* DesktopUpdates.DesktopUpdates;
1227 yield* updates.configure;
1228 const state = yield* updates.getState;
1229 assert.isFalse(state.enabled);
1230 assert.isUndefined(state.channelBlockedByPolicy);
1231 }),
1232 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
1233 });
1234
1235 it.effect("holds a nightly install until its user switches to stable", () => {
1236 const harness = makeHarness({
1237 appVersion: "1.2.3-nightly.20261028.1",
1238 // A managed app ignores the mock feed, so give it a real one.
1239 policy: managed({ allowNightly: false, feedUrl: "https://updates.example.com/t3code" }),
1240 });
1241
1242 return Effect.scoped(
1243 Effect.gen(function* () {
1244 const updates = yield* DesktopUpdates.DesktopUpdates;
1245 yield* updates.configure;
1246 yield* TestClock.adjust("10 minutes");
1247
1248 const held = yield* updates.getState;
1249 assert.equal(held.channel, "nightly");
1250 assert.equal(held.status, "disabled");
1251 assert.include(held.message ?? "", message);
1252 assert.isTrue(held.channelBlockedByPolicy);
1253 assert.isFalse((yield* updates.check("menu")).checked);
1254 assert.equal(harness.checkCount(), 0);
1255
1256 const stable = yield* updates.setChannel("latest");
1257 assert.equal(stable.channel, "latest");
1258 assert.isTrue(stable.enabled);
1259 assert.isUndefined(stable.channelBlockedByPolicy);
1260 assert.equal(harness.checkCount(), 1);
1261 }),
1262 ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
1263 });
1264});