import * as NodeModule from "node:module"; import { assert, describe, it } from "@effect/vitest"; import { DESKTOP_UPDATE_RESTART_MARKER_FILE, DesktopUpdateStateSchema } from "@t3tools/contracts"; import { computeReleaseNotes } from "electron-updater/out/providers/GitHubProvider.js"; import * as Cause from "effect/Cause"; import * as Deferred from "effect/Deferred"; import * as Duration from "effect/Duration"; import * as Effect from "effect/Effect"; import * as Fiber from "effect/Fiber"; import * as Layer from "effect/Layer"; import * as Logger from "effect/Logger"; import * as Option from "effect/Option"; import * as References from "effect/References"; import * as Ref from "effect/Ref"; import * as Schema from "effect/Schema"; import * as Stream from "effect/Stream"; import * as TestClock from "effect/testing/TestClock"; import * as ElectronUpdater from "../electron/ElectronUpdater.ts"; import * as DesktopBackendPool from "../backend/DesktopBackendPool.ts"; import * as DesktopAppSettings from "../settings/DesktopAppSettings.ts"; import * as DesktopEnvironment from "../app/DesktopEnvironment.ts"; import * as DesktopState from "../app/DesktopState.ts"; import * as DesktopUpdatePolicy from "./DesktopUpdatePolicy.ts"; import * as DesktopUpdates from "./DesktopUpdates.ts"; import { flushCallbacks, makeHarness } from "./updatesTestHarness.ts"; const encodeUpdateState = Schema.encodeEffect(DesktopUpdateStateSchema); type ReleaseFeed = Parameters[2]; type RunningVersion = Parameters[0]; // electron-updater's own dependencies, which apps/desktop does not declare. const requireFromUpdater = NodeModule.createRequire( NodeModule.createRequire(import.meta.url).resolve("electron-updater"), ); const { parseXml } = requireFromUpdater("builder-util-runtime") as { parseXml: (xml: string) => ReleaseFeed; }; const semver = requireFromUpdater("semver") as { parse: (version: string) => RunningVersion }; const releaseTitle = (version: string) => version.includes("-nightly.") ? `T3 Code Nightly ${version} (0123456789ab)` : `T3 Code v${version}`; const releaseFeed = (...versions: ReadonlyArray) => parseXml( `${versions .map( (version) => `${releaseTitle(version)}<ul><li>${version} change</li></ul>`, ) .join("")}`, ); /** The update info electron-updater's GitHub provider reports when /releases/latest names `version`. */ const stableUpdateInfo = ( feed: ReleaseFeed, version: string, runningVersion: string, fullChangelog: boolean, ) => { const latestRelease = feed .getElements("entry") .find((entry) => entry.element("link").attribute("href").endsWith(`/tag/v${version}`)) ?? feed.element("entry"); return { version, releaseName: latestRelease.elementValueOrEmpty("title"), releaseNotes: computeReleaseNotes( semver.parse(runningVersion), fullChangelog, feed, latestRelease, ), }; }; // Subscribes before emitting, so the state the emit publishes is the one awaited, not a scheduler yield. const nextPublishedState = (emit: () => void) => Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; const { changes } = yield* updates.subscribe; const published = yield* Stream.runHead(changes).pipe(Effect.forkChild); emit(); yield* Fiber.join(published); return yield* updates.getState; }); describe("DesktopUpdates", () => { it.effect("updates Linux .deb installs and leaves other non-AppImage installs off", () => Effect.gen(function* () { const linuxState = (packageType: string | undefined) => Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; const desktopState = yield* DesktopState.DesktopState; yield* updates.configure; return { state: yield* updates.getState, canInstallUpdates: yield* Ref.get(desktopState.canInstallUpdates), }; }), ).pipe(Effect.provide(makeHarness({ platform: "linux", packageType }).layer)); const deb = yield* linuxState("deb\n"); assert.equal(deb.state.status, "idle"); assert.isTrue(deb.canInstallUpdates); const unmarked = yield* linuxState(undefined); assert.equal(unmarked.state.status, "disabled"); assert.isFalse(unmarked.canInstallUpdates); }), ); it.effect("keeps the disabled reason available after configuration and channel changes", () => { const harness = makeHarness({ env: { T3CODE_DISABLE_AUTO_UPDATE: "true" } }); const reason = "Automatic updates are disabled by the T3CODE_DISABLE_AUTO_UPDATE setting."; return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; const configured = yield* updates.getState; assert.equal(configured.message, reason); const changed = yield* updates.setChannel("nightly"); assert.equal(changed.channel, "nightly"); assert.equal(changed.enabled, false); assert.equal(changed.status, "disabled"); assert.equal(changed.message, reason); assert.equal(harness.sentStates.at(-1)?.message, reason); assert.equal(harness.checkCount(), 0); }), ).pipe(Effect.provide(harness.layer)); }); it.effect("re-checks an offered update without announcing it again", () => { const harness = makeHarness(); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; harness.emit("update-available", { version: "1.2.4", releaseNotes: "## What's changed\n- fix: queued update", }); yield* flushCallbacks; const offeredStates = harness.sentStates.length; yield* updates.check("poll"); harness.emit("update-available", { version: "1.2.4" }); yield* flushCallbacks; assert.equal(harness.sentStates.length, offeredStates); harness.emit("update-downloaded", { version: "1.2.4" }); yield* flushCallbacks; const downloadedStates = harness.sentStates.length; const result = yield* updates.check("poll"); assert.isTrue(result.checked); harness.emit("update-available", { version: "1.2.4" }); yield* flushCallbacks; assert.equal(harness.sentStates.length, downloadedStates); const unchangedState = yield* updates.getState; assert.equal(unchangedState.status, "downloaded"); assert.equal(unchangedState.downloadedVersion, "1.2.4"); assert.deepEqual(unchangedState.releaseNotes, [ { version: "1.2.4", items: ["fix: queued update"], totalItems: 1 }, ]); assert.equal(unchangedState.omittedReleaseCount, 0); const nextResult = yield* updates.check("poll"); assert.isTrue(nextResult.checked); harness.emit("update-available", { version: "1.2.5" }); yield* flushCallbacks; const state = yield* updates.getState; assert.equal(state.status, "available"); assert.equal(state.availableVersion, "1.2.5"); assert.isNull(state.downloadedVersion); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect.each([ { name: "shows no notes for a Stable release missing from the releases feed", runningVersion: "1.2.3", feedVersions: ["1.2.5-nightly.20261102.1", "1.2.5-nightly.20261101.1"], expected: [], }, { name: "shows a Stable release's own notes from the releases feed", runningVersion: "1.2.3", feedVersions: ["1.2.5-nightly.20261102.1", "1.2.4", "1.2.4-nightly.20261030.1"], expected: [{ version: "1.2.4", items: ["1.2.4 change"], totalItems: 1 }], }, { name: "shows a Stable release's notes when switching down from a newer Nightly", runningVersion: "1.2.5-nightly.20261027.1", feedVersions: ["1.2.5-nightly.20261102.1", "1.2.4"], expected: [{ version: "1.2.4", items: ["1.2.4 change"], totalItems: 1 }], }, ])("$name", ({ runningVersion, feedVersions, expected }) => { const harness = makeHarness(); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; harness.emit( "update-available", stableUpdateInfo( releaseFeed(...feedVersions), "1.2.4", runningVersion, harness.fullChangelog(), ), ); yield* flushCallbacks; const state = yield* updates.getState; assert.equal(state.availableVersion, "1.2.4"); assert.deepEqual(state.releaseNotes, expected); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("preserves a queued installer when the feed has no update", () => { const harness = makeHarness(); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; harness.emit("update-available", { version: "1.2.4", releaseNotes: "## What's changed\n- fix: queued update", }); yield* flushCallbacks; harness.emit("update-downloaded", { version: "1.2.4" }); yield* flushCallbacks; yield* updates.check("poll"); harness.emit("update-not-available"); yield* flushCallbacks; const state = yield* updates.getState; assert.equal(state.status, "downloaded"); assert.equal(state.availableVersion, "1.2.4"); assert.equal(state.downloadedVersion, "1.2.4"); assert.deepEqual(state.releaseNotes, [ { version: "1.2.4", items: ["fix: queued update"], totalItems: 1 }, ]); assert.equal(state.omittedReleaseCount, 0); assert.equal(state.downloadPercent, 100); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("checks shortly after startup and then every four hours", () => { const harness = makeHarness(); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; yield* TestClock.adjust("15 seconds"); assert.equal(harness.checkCount(), 1); yield* TestClock.adjust("3 hours"); assert.equal(harness.checkCount(), 1); yield* TestClock.adjust("1 hour"); assert.equal(harness.checkCount(), 2); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect.each([ { action: "download", offer: "update-available", found: null, accepted: true }, { action: "install", offer: "update-downloaded", found: null, accepted: true }, { action: "download", offer: "update-available", found: "1.2.5", accepted: true }, { action: "install", offer: "update-downloaded", found: "1.2.5", accepted: false }, ] as const)( "a $action waiting out a refresh check acts on what it found ($found)", ({ action, offer, found, accepted }) => Effect.gen(function* () { const checkStarted = yield* Deferred.make(); const releaseCheck = yield* Deferred.make(); const harness = makeHarness({ checkForUpdates: Deferred.succeed(checkStarted, undefined).pipe( Effect.andThen(Deferred.await(releaseCheck)), // electron-updater emits the check's outcome before its promise resolves. Effect.andThen( Effect.sync(() => { if (found !== null) harness.emit("update-available", { version: found }); }), ), ), }); yield* Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; harness.emit(offer, { version: "1.2.4" }); yield* flushCallbacks; const checkFiber = yield* updates.check("poll").pipe(Effect.forkScoped); yield* Deferred.await(checkStarted); const actionFiber = yield* updates[action].pipe(Effect.forkScoped); yield* flushCallbacks; yield* Deferred.succeed(releaseCheck, undefined); assert.isTrue((yield* Fiber.join(checkFiber)).checked); assert.equal((yield* Fiber.join(actionFiber)).accepted, accepted); const state = yield* updates.getState; assert.equal(state.availableVersion, found ?? "1.2.4"); if (action === "download") assert.equal(state.status, "downloading"); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }), ); it.effect("rejects refresh checks while install is in progress", () => Effect.gen(function* () { const installStarted = yield* Deferred.make(); const releaseInstall = yield* Deferred.make(); const harness = makeHarness({ stopBackend: Deferred.succeed(installStarted, undefined).pipe( Effect.andThen(Deferred.await(releaseInstall)), ), }); yield* Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; harness.emit("update-downloaded", { version: "1.2.4" }); yield* flushCallbacks; const installFiber = yield* updates.install.pipe(Effect.forkScoped); yield* Deferred.await(installStarted); const checksBefore = harness.checkCount(); const checkResult = yield* updates.check("manual"); assert.isFalse(checkResult.checked); assert.equal(harness.checkCount(), checksBefore); yield* Deferred.succeed(releaseInstall, undefined); const installResult = yield* Fiber.join(installFiber); assert.isTrue(installResult.accepted); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }), ); it.effect("preserves a queued installer after a background updater error", () => { const harness = makeHarness({ checkForUpdates: Effect.sync(() => harness.emit("update-not-available")), }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; harness.emit("update-downloaded", { version: "1.2.4" }); yield* flushCallbacks; harness.emit("error", new Error("background updater failure")); yield* flushCallbacks; const state = yield* updates.getState; assert.equal(state.status, "error"); assert.equal(state.downloadedVersion, "1.2.4"); assert.isNull(state.errorContext); const result = yield* updates.install; assert.isTrue(result.accepted); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("logs the redacted cause of a rejected update check", () => { const cause = Object.assign( new Error("request failed for https://user:secret@example.com/update?token=secret"), { code: "ERR_UPDATER_LATEST_VERSION_NOT_FOUND" }, ); const updaterError = new ElectronUpdater.ElectronUpdaterCheckForUpdatesError({ channel: null, cause, }); const harness = makeHarness({ checkForUpdates: Effect.fail(updaterError) }); const loggedAnnotations: Array> = []; const logger = Logger.make(({ fiber }) => { const annotations = fiber.getRef(References.CurrentLogAnnotations); if (annotations.errorTag === "ElectronUpdaterCheckForUpdatesError") { loggedAnnotations.push(annotations); } }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; yield* updates.check("manual"); const state = yield* updates.getState; const loggedAnnotation = loggedAnnotations.at(-1); assert.isDefined(loggedAnnotation); assert.equal(loggedAnnotation.errorTag, "ElectronUpdaterCheckForUpdatesError"); assert.isNull(loggedAnnotation.channel); assert.equal( loggedAnnotation.cause, "Error [ERR_UPDATER_LATEST_VERSION_NOT_FOUND]: request failed for [redacted URL]", ); assert.notProperty(loggedAnnotation, "error"); assert.notInclude(Object.values(loggedAnnotation).map(String).join(" "), "secret"); assert.equal( state.message, "Electron updater failed to check for updates on channel default.", ); assert.notInclude(state.message ?? "", "secret"); }), ).pipe( Effect.provide( Layer.mergeAll( TestClock.layer(), harness.layer, Logger.layer([logger], { mergeWithExisting: false }), ), ), ); }); it.effect("recovers download state after an unexpected setup failure", () => { let disableDifferentialCalls = 0; const harness = makeHarness({ setDisableDifferentialDownload: Effect.suspend(() => { disableDifferentialCalls += 1; return disableDifferentialCalls === 1 ? Effect.void : Effect.die(new Error("download setup failed")); }), }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; harness.emit("update-available", { version: "1.2.4" }); yield* flushCallbacks; const result = yield* updates.download; assert.isTrue(result.accepted); assert.isFalse(result.completed); const failedState = yield* updates.getState; assert.equal(failedState.status, "available"); assert.equal(failedState.errorContext, "download"); assert.equal(failedState.message, "Desktop update download action failed unexpectedly."); const changedState = yield* updates.setChannel("nightly"); assert.equal(changedState.channel, "nightly"); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("shows why a download failed, not only the channel", () => { const harness = makeHarness({ downloadUpdate: Effect.fail( new ElectronUpdater.ElectronUpdaterDownloadUpdateError({ channel: "nightly", cause: new Error("net::ERR_CONTENT_LENGTH_MISMATCH"), }), ), }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; harness.emit("update-available", { version: "1.2.4" }); yield* flushCallbacks; const result = yield* updates.download; assert.isFalse(result.completed); const state = yield* updates.getState; assert.equal(state.errorContext, "download"); assert.include(state.message ?? "", "ERR_CONTENT_LENGTH_MISMATCH"); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("downloads the newest release published since the last check", () => { // electron-updater downloads the release its last checkForUpdates found. let published = "1.2.4"; let lastChecked: string | undefined; const harness = makeHarness({ checkForUpdates: Effect.sync(() => { lastChecked = published; harness.emit("update-available", { version: published }); }), downloadUpdate: Effect.sync(() => { harness.emit("update-downloaded", { version: lastChecked }); }), }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; yield* updates.check("poll"); yield* flushCallbacks; assert.equal((yield* updates.getState).availableVersion, "1.2.4"); published = "1.2.5"; const result = yield* updates.download; yield* flushCallbacks; assert.isTrue(result.completed); assert.equal((yield* updates.getState).downloadedVersion, "1.2.5"); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("installs a release published after the retained download", () => { let published = "1.2.4"; let lastChecked: string | undefined; let installedVersion: string | null | undefined; const harness = makeHarness({ checkForUpdates: Effect.sync(() => { lastChecked = published; harness.emit("update-available", { version: published }); }), downloadUpdate: Effect.sync(() => { harness.emit("update-downloaded", { version: lastChecked }); }), quitAndInstall: Effect.sync(() => { installedVersion = harness.sentStates.at(-1)?.downloadedVersion; }), }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; yield* updates.check("poll"); yield* flushCallbacks; yield* updates.download; yield* flushCallbacks; assert.equal((yield* updates.getState).downloadedVersion, "1.2.4"); published = "1.2.5"; const checksBefore = harness.checkCount(); const result = yield* updates.installPrepared("1.2.4"); assert.isTrue(result.accepted); // A second check before downloading could find a stale feed's older release. assert.equal(harness.checkCount(), checksBefore + 1); assert.equal(harness.quitAndInstalls(), 1); assert.equal(installedVersion, "1.2.5"); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("downloads the known offer when the check before downloading fails", () => { let checks = 0; const harness = makeHarness({ checkForUpdates: Effect.suspend(() => { checks += 1; if (checks > 1) { return Effect.fail( new ElectronUpdater.ElectronUpdaterCheckForUpdatesError({ channel: null, cause: new Error("offline"), }), ); } harness.emit("update-available", { version: "1.2.4" }); return Effect.void; }), downloadUpdate: Effect.sync(() => { harness.emit("update-downloaded", { version: "1.2.4" }); }), }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; yield* updates.check("poll"); yield* flushCallbacks; const result = yield* updates.download; yield* flushCallbacks; assert.isTrue(result.completed); assert.equal((yield* updates.getState).downloadedVersion, "1.2.4"); // Remote requests treat a published error after their own check as final. assert.notInclude( harness.sentStates.map((state) => state.status), "error", ); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("installs the retained download when a stale feed offers an older release", () => { let published = "1.2.5"; let installedVersion: string | null | undefined; const harness = makeHarness({ checkForUpdates: Effect.sync(() => harness.emit("update-available", { version: published })), downloadUpdate: Effect.sync(() => harness.emit("update-downloaded", { version: "1.2.5" })), quitAndInstall: Effect.sync(() => { installedVersion = harness.sentStates.at(-1)?.downloadedVersion; }), }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; yield* updates.check("poll"); yield* updates.download; yield* flushCallbacks; published = "1.2.4"; const result = yield* updates.installPrepared("1.2.5"); assert.isTrue(result.accepted); assert.equal(harness.downloadCount(), 1); assert.equal(installedVersion, "1.2.5"); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("downloads again when the downloaded update file was deleted before install", () => { const downloadedFile = "/cache/pending/T3-Code-1.2.4-arm64.zip"; let installedFileExists: boolean | undefined; const harness = makeHarness({ checkForUpdates: Effect.sync(() => harness.emit("update-available", { version: "1.2.4" })), downloadUpdate: Effect.sync(() => { harness.deletedFiles.delete(downloadedFile); harness.emit("update-downloaded", { version: "1.2.4", downloadedFile }); }), quitAndInstall: Effect.sync(() => { installedFileExists = !harness.deletedFiles.has(downloadedFile); }), }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; yield* updates.check("poll"); yield* updates.download; yield* flushCallbacks; assert.equal(harness.downloadCount(), 1); harness.deletedFiles.add(downloadedFile); const result = yield* updates.installPrepared("1.2.4"); assert.isTrue(result.accepted); assert.equal(harness.downloadCount(), 2); assert.equal(harness.quitAndInstalls(), 1); assert.isTrue(installedFileExists); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("restores download state and permits retry after interruption", () => Effect.gen(function* () { const actionStarted = yield* Deferred.make(); let disableDifferentialCalls = 0; const harness = makeHarness({ setDisableDifferentialDownload: Effect.suspend(() => { disableDifferentialCalls += 1; if (disableDifferentialCalls === 1) { return Effect.void; } if (disableDifferentialCalls === 2) { return Deferred.succeed(actionStarted, undefined).pipe(Effect.andThen(Effect.never)); } return Effect.void; }), }); yield* Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; harness.emit("update-available", { version: "1.2.4" }); yield* flushCallbacks; const downloadFiber = yield* updates.download.pipe(Effect.forkScoped); yield* Deferred.await(actionStarted); yield* Fiber.interrupt(downloadFiber); const interruptedState = yield* updates.getState; assert.equal(interruptedState.status, "available"); assert.isNull(interruptedState.message); const retry = yield* updates.download; assert.isTrue(retry.accepted); assert.isTrue(retry.completed); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }), ); it.effect("clears quitting state after an unexpected install setup failure", () => { const harness = makeHarness({ stopBackend: Effect.die(new Error("backend stop failed")), }); return Effect.scoped( Effect.gen(function* () { const desktopState = yield* DesktopState.DesktopState; const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; harness.emit("update-downloaded", { version: "1.2.4" }); yield* flushCallbacks; const result = yield* updates.install; assert.isTrue(result.accepted); assert.isFalse(result.completed); assert.isFalse(yield* Ref.get(desktopState.quitting)); const failedState = yield* updates.getState; assert.equal(failedState.status, "downloaded"); assert.equal(failedState.errorContext, "install"); assert.equal(failedState.message, "Desktop update install action failed unexpectedly."); const changedState = yield* updates.setChannel("nightly"); assert.equal(changedState.channel, "nightly"); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("marks the backend stop for an install as an update restart", () => { let markersAtStop: ReadonlyArray = []; let inProgressAtStop: string | undefined; const harness = makeHarness({ stopBackend: Effect.sync(() => { markersAtStop = [...harness.updateRestartMarkers]; inProgressAtStop = harness.updateInProgressMarker(); }), }); return Effect.scoped( Effect.gen(function* () { yield* TestClock.setTime(1_000); const environment = yield* DesktopEnvironment.DesktopEnvironment; const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; harness.emit("update-downloaded", { version: "1.2.4" }); yield* flushCallbacks; assert.isTrue((yield* updates.install).accepted); assert.deepEqual(markersAtStop, [ environment.path.join(environment.baseDir, "runtime", DESKTOP_UPDATE_RESTART_MARKER_FILE), ]); // SSH launchers wait on this until the replacement backend publishes its runtime. assert.deepEqual(JSON.parse(inProgressAtStop ?? "null"), { startedAt: 1_000, expiresAt: 61_000, }); assert.isDefined(harness.updateInProgressMarker()); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("records the version an install hands to the installer", () => { let recordsAtStop: ReadonlyArray = []; const harness = makeHarness({ stopBackend: Effect.sync(() => { recordsAtStop = [...harness.installRecords.values()]; }), }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; harness.emit("update-downloaded", { version: "1.2.4" }); yield* flushCallbacks; assert.isTrue((yield* updates.install).accepted); assert.deepEqual( recordsAtStop.map((record) => JSON.parse(record)), [{ fromVersion: "1.2.3", targetVersion: "1.2.4" }], ); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("flags an install that did not apply and keeps the flag through checks", () => { const harness = makeHarness({ installRecord: JSON.stringify({ fromVersion: "1.2.3", targetVersion: "1.2.4" }), }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; assert.equal((yield* updates.getState).notAppliedVersion, "1.2.4"); assert.isDefined(harness.previousInstallRecord()); harness.emit("update-available", { version: "1.2.4" }); yield* flushCallbacks; const state = yield* updates.getState; assert.equal(state.status, "available"); assert.equal(state.notAppliedVersion, "1.2.4"); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); // regression: #19291 an install that did apply was still flagged as not applied it.effect("does not flag an install that applied", () => { const harness = makeHarness({ installRecord: JSON.stringify({ fromVersion: "1.2.2", targetVersion: "1.2.3" }), }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; assert.isUndefined((yield* updates.getState).notAppliedVersion); assert.isUndefined(harness.previousInstallRecord()); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("drops the update restart marker when an install is interrupted", () => Effect.gen(function* () { const stopping = yield* Deferred.make(); const harness = makeHarness({ stopBackend: Deferred.succeed(stopping, undefined).pipe(Effect.andThen(Effect.never)), }); yield* Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; harness.emit("update-downloaded", { version: "1.2.4" }); yield* flushCallbacks; const installFiber = yield* updates.install.pipe(Effect.forkScoped); yield* Deferred.await(stopping); assert.equal(harness.updateRestartMarkers.size, 1); assert.isDefined(harness.updateInProgressMarker()); yield* Fiber.interrupt(installFiber); assert.equal(harness.updateRestartMarkers.size, 0); assert.isUndefined(harness.updateInProgressMarker()); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }), ); it.effect("keeps windows and restarts backends when quitAndInstall fails", () => { const harness = makeHarness({ quitAndInstall: Effect.fail( new ElectronUpdater.ElectronUpdaterQuitAndInstallError({ channel: "latest", isSilent: true, isForceRunAfter: true, cause: new Error("installer refused"), }), ), }); return Effect.scoped( Effect.gen(function* () { const desktopState = yield* DesktopState.DesktopState; const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; harness.emit("update-downloaded", { version: "1.2.4" }); yield* flushCallbacks; const result = yield* updates.install; assert.isTrue(result.accepted); assert.isFalse(yield* Ref.get(desktopState.quitting)); assert.deepEqual(harness.installSteps, [ "closeSshTunnels", "quitAndInstall", "startBackend", ]); assert.equal(harness.updateRestartMarkers.size, 0); assert.isUndefined(harness.updateInProgressMarker()); assert.equal(harness.installRecords.size, 0); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("keeps the disabled primary stopped when quitAndInstall fails", () => { const harness = makeHarness({ backendDesiredRunning: false, quitAndInstall: Effect.fail( new ElectronUpdater.ElectronUpdaterQuitAndInstallError({ channel: "latest", isSilent: true, isForceRunAfter: true, cause: new Error("installer refused"), }), ), }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; const pool = yield* DesktopBackendPool.DesktopBackendPool; yield* updates.configure; harness.emit("update-downloaded", { version: "1.2.4" }); yield* flushCallbacks; const result = yield* updates.install; assert.equal(result.state.errorContext, "install"); assert.deepEqual(harness.installSteps, ["closeSshTunnels", "quitAndInstall"]); assert.isFalse((yield* (yield* pool.primary).snapshot).desiredRunning); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("restores only desired-running instances after an installer error event", () => { const starts: string[] = []; const stops: string[] = []; const makeInstance = (id: DesktopBackendPool.BackendInstanceId, initiallyRunning: boolean) => { let desiredRunning = initiallyRunning; return { id, label: Effect.succeed(id), currentConfig: Effect.succeedNone, start: Effect.sync(() => { starts.push(id); desiredRunning = true; }), stop: () => Effect.sync(() => { stops.push(id); desiredRunning = false; }), snapshot: Effect.sync(() => ({ desiredRunning, ready: false, activePid: Option.none(), restartAttempt: 0, restartScheduled: false, })), waitForReady: () => Effect.succeed(true), } satisfies DesktopBackendPool.DesktopBackendInstance; }; const primary = makeInstance(DesktopBackendPool.PRIMARY_INSTANCE_ID, false); const wsl = makeInstance(DesktopBackendPool.BackendInstanceId("wsl:default"), true); const harness = makeHarness({ backendInstances: [primary, wsl] }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; harness.emit("update-downloaded", { version: "1.2.4" }); yield* flushCallbacks; yield* updates.install; assert.sameMembers(stops, [primary.id, wsl.id]); assert.isFalse((yield* wsl.snapshot).desiredRunning); const { changes } = yield* updates.subscribe; const recovered = yield* Stream.runHead(changes).pipe(Effect.forkChild); yield* flushCallbacks; harness.emit("error", new Error("native installer refused")); assert.equal(Option.getOrThrow(yield* Fiber.join(recovered)).errorContext, "install"); assert.deepEqual(starts, [wsl.id]); assert.isFalse((yield* primary.snapshot).desiredRunning); assert.isTrue((yield* wsl.snapshot).desiredRunning); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("holds the install reservation until failed-install recovery finishes", () => { const recoveryStarted = Deferred.makeUnsafe(); const releaseRecovery = Deferred.makeUnsafe(); const harness = makeHarness({ quitAndInstall: Effect.fail( new ElectronUpdater.ElectronUpdaterQuitAndInstallError({ channel: "latest", isSilent: true, isForceRunAfter: true, cause: new Error("installer refused"), }), ), startBackend: Deferred.succeed(recoveryStarted, undefined).pipe( Effect.andThen(Deferred.await(releaseRecovery)), ), }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; harness.emit("update-downloaded", { version: "1.2.4" }); yield* flushCallbacks; const failedInstall = yield* updates.install.pipe(Effect.forkChild); yield* Deferred.await(recoveryStarted); assert.isFalse(yield* updates.isInstallActive); const overlappingInstall = yield* updates.install; assert.isFalse(overlappingInstall.accepted); assert.equal(harness.quitAndInstalls(), 1); harness.emit("error", new Error("duplicate native installer error")); yield* flushCallbacks; assert.deepEqual(harness.installSteps, [ "closeSshTunnels", "quitAndInstall", "startBackend", ]); yield* Deferred.succeed(releaseRecovery, undefined); const failedResult = yield* Fiber.join(failedInstall); assert.equal(failedResult.state.errorContext, "install"); const retry = yield* updates.install; assert.isTrue(retry.accepted); assert.equal(harness.quitAndInstalls(), 2); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("rejects a prepared install when the downloaded version changed", () => { const harness = makeHarness(); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; harness.emit("update-downloaded", { version: "1.2.5" }); yield* flushCallbacks; const result = yield* updates.installPrepared("1.2.4"); assert.isFalse(result.accepted); assert.equal(harness.quitAndInstalls(), 0); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("persists channel changes through the settings service", () => { const harness = makeHarness(); return Effect.scoped( Effect.gen(function* () { const settings = yield* DesktopAppSettings.DesktopAppSettings; const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; const state = yield* updates.setChannel("nightly"); const persistedSettings = yield* settings.get; assert.equal(state.channel, "nightly"); assert.equal(persistedSettings.updateChannel, "nightly"); assert.equal(persistedSettings.updateChannelConfiguredByUser, true); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("ignores a finished download from a track it no longer follows", () => { const harness = makeHarness(); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; // A backed-out switch's download keeps reporting progress until it finishes. harness.emit("download-progress", { percent: 40 }); yield* flushCallbacks; harness.emit("update-downloaded", { version: "1.2.4-nightly.20261027.1" }); yield* flushCallbacks; const state = yield* updates.getState; assert.equal(state.downloadedVersion, null); assert.notEqual(state.status, "downloading"); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("offers older releases only to checks that leave the running build's track", () => { const harness = makeHarness({ beforeSetUpdateChannel: Effect.void }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; const before = harness.checkAllowedDowngrade.length; yield* updates.setChannel("nightly"); yield* updates.setChannel("latest"); yield* updates.check("remote-update", { allowDowngrade: true }); // Back on its own Stable track the build never needs an older release. assert.deepEqual(harness.checkAllowedDowngrade.slice(before), [true, false, true]); assert.isFalse(harness.allowDowngrade()); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("re-checks a saved track that differs from the running build's track", () => { const harness = makeHarness({ appVersion: "1.2.4-nightly.20261027.1", beforeSetUpdateChannel: Effect.void, }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; yield* updates.setChannel("latest"); const checks = harness.checkCount(); yield* updates.setChannel("latest"); assert.equal(harness.checkCount(), checks + 1); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("does not persist an unchanged update channel as a user preference", () => { const harness = makeHarness(); return Effect.scoped( Effect.gen(function* () { const settings = yield* DesktopAppSettings.DesktopAppSettings; const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; const state = yield* updates.setChannel("latest"); const persistedSettings = yield* settings.get; assert.equal(state.channel, "latest"); assert.equal(persistedSettings.updateChannel, "latest"); assert.equal(persistedSettings.updateChannelConfiguredByUser, false); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("offers nothing below a track switch's checked release", () => { const harness = makeHarness({ appVersion: "1.2.4-nightly.20261027.1", beforeSetUpdateChannel: Effect.void, }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; yield* updates.setChannel("latest", { minimumVersion: "1.2.3" }); harness.emit("update-available", { version: "1.2.2" }); yield* flushCallbacks; assert.isNull((yield* updates.getState).availableVersion); harness.emit("update-downloaded", { version: "1.2.2" }); yield* flushCallbacks; assert.isNull((yield* updates.getState).downloadedVersion); harness.emit("update-available", { version: "1.2.3" }); yield* flushCallbacks; assert.equal((yield* updates.getState).availableVersion, "1.2.3"); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("keeps the current track's offers when a track switch fails to save", () => { const harness = makeHarness({ setUpdateChannelError: new DesktopAppSettings.DesktopSettingsWriteError({ operation: "replace-settings-file", path: "desktop-settings.json", cause: new Error("rename failed"), }), }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; const error = yield* updates .setChannel("nightly", { minimumVersion: "1.2.5-nightly.20261028.1" }) .pipe(Effect.flip); assert.equal(error._tag, "DesktopUpdateChannelPersistenceError"); assert.equal((yield* updates.getState).channel, "latest"); harness.emit("update-available", { version: "1.2.4" }); yield* flushCallbacks; assert.equal((yield* updates.getState).availableVersion, "1.2.4"); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("keeps a saved Stable track across a restart without offering an older release", () => { // A Nightly build whose switch to Stable did not finish before the app quit. const harness = makeHarness({ appVersion: "1.2.4-nightly.20261027.1", savedChannel: "latest" }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; yield* updates.check("startup"); assert.equal((yield* updates.getState).channel, "latest"); assert.deepEqual(harness.checkAllowedDowngrade, [false]); // Semver ranks Stable 1.2.4 above this Nightly, but a Nightly built before the release's // version bump can hold migrations 1.2.4 lacks. Crossing needs a guarded switch. harness.emit("update-available", { version: "1.2.4" }); yield* flushCallbacks; assert.isNull((yield* updates.getState).availableVersion); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("keeps a saved Nightly track across a restart without crossing on its own", () => { // A Stable build whose switch to Nightly was refused before the app quit; its floor is gone. const harness = makeHarness({ savedChannel: "nightly" }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; yield* updates.check("startup"); const state = yield* nextPublishedState(() => harness.emit("update-available", { version: "1.2.4-nightly.20261027.1" }), ); assert.equal(state.channel, "nightly"); assert.isNull(state.availableVersion); assert.equal(state.status, "error"); assert.equal(state.errorContext, "check"); assert.isTrue(state.canRetry); assert.deepEqual(state.trackMismatch, { buildChannel: "latest" }); assert.deepEqual(harness.checkAllowedDowngrade, [false]); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("reports a track mismatch when the feed offers no release at all", () => { const harness = makeHarness({ savedChannel: "nightly" }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; yield* updates.check("startup"); const state = yield* nextPublishedState(() => harness.emit("update-not-available")); assert.equal(state.status, "error"); assert.equal(state.errorContext, "check"); assert.isTrue(state.canRetry); assert.deepEqual(state.trackMismatch, { buildChannel: "latest" }); // A later failure has its own reason, so the mismatch must not stay attached to it. const failed = yield* nextPublishedState(() => harness.emit("error", new Error("background updater failure")), ); assert.isUndefined(failed.trackMismatch); const mismatched = yield* nextPublishedState(() => harness.emit("update-not-available")); assert.isDefined(mismatched.trackMismatch); yield* updates.check("menu"); assert.equal((yield* updates.getState).status, "checking"); assert.isUndefined((yield* updates.getState).trackMismatch); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("never offers an older Nightly to a Nightly build's own checks", () => { const harness = makeHarness({ appVersion: "1.2.4-nightly.20261027.1" }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; yield* updates.check("poll"); assert.equal((yield* updates.getState).channel, "nightly"); assert.deepEqual(harness.checkAllowedDowngrade, [false]); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("waits out a background check instead of refusing a track change", () => Effect.gen(function* () { const checkStarted = yield* Deferred.make(); const releaseCheck = yield* Deferred.make(); const harness = makeHarness({ beforeSetUpdateChannel: Effect.void, checkForUpdates: Deferred.succeed(checkStarted, undefined).pipe( Effect.andThen(Deferred.await(releaseCheck)), ), }); yield* Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; yield* updates.check("poll").pipe(Effect.forkScoped); yield* Deferred.await(checkStarted); const channelFiber = yield* updates.setChannel("nightly").pipe(Effect.forkScoped); yield* Deferred.succeed(releaseCheck, undefined); yield* TestClock.adjust(Duration.seconds(2)); const state = yield* Fiber.join(channelFiber); assert.equal(state.channel, "nightly"); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }), ); it.effect("rejects checks while an update channel change is being persisted", () => Effect.gen(function* () { const channelChangeStarted = yield* Deferred.make(); const releaseChannelChange = yield* Deferred.make(); const harness = makeHarness({ beforeSetUpdateChannel: Deferred.succeed(channelChangeStarted, undefined).pipe( Effect.andThen(Deferred.await(releaseChannelChange)), ), }); yield* Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; const channelFiber = yield* updates.setChannel("nightly").pipe(Effect.forkScoped); yield* Deferred.await(channelChangeStarted); const checkResult = yield* updates.check("manual"); assert.isFalse(checkResult.checked); assert.equal(harness.checkCount(), 0); yield* Deferred.succeed(releaseChannelChange, undefined); const state = yield* Fiber.join(channelFiber); assert.equal(state.channel, "nightly"); assert.equal(harness.checkCount(), 1); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }), ); }); describe("describeUpdateErrorCause", () => { it("removes URLs, credentials, response bodies, and headers from summaries", () => { const messages = [ "Cannot download https://user:url-secret@example.com/update?token=query-secret, status 403", "Request failed: Bearer bearer-secret", "Request failed: Basic basic-secret", "Request failed: Authorization: Bearer header-secret", "Request failed: Cookie=session-secret", "Request failed: api_key=\"key-secret\" password='password-secret' access_token=token-secret", '404 Not Found\n{"body":"body-secret"}\nHeaders: cookie=header-secret', "Request failed\rHeaders: cookie=header-secret", ]; for (const message of messages) { const summary = DesktopUpdates.describeUpdateErrorCause(new Error(message)); assert.include(summary, "Error:"); assert.notInclude(summary, "secret"); assert.notInclude(summary, "https://"); assert.notInclude(summary, "Headers:"); } assert.equal( DesktopUpdates.describeUpdateErrorCause("404 Not Found\nbody-secret"), "404 Not Found", ); }); it("bounds long messages, parallel failures, and cyclic causes", () => { const longError = new Error("x".repeat(10_000)); const parallel = Cause.combine(Cause.fail(longError), Cause.die(longError)); assert.isAtMost(DesktopUpdates.describeUpdateErrorCause(parallel).length, 1024); const cyclic = new Error("retry failed"); cyclic.cause = cyclic; assert.include(DesktopUpdates.describeUpdateErrorCause(cyclic), "[truncated cause]"); assert.isAtMost(DesktopUpdates.describeUpdateErrorCause(cyclic).length, 1024); const describeCause = (outer: string, inner: string) => DesktopUpdates.describeUpdateErrorCause( Object.assign(new Error(outer), { cause: new Error(inner) }), ); const a = "a".repeat(512); const b = "b".repeat(485); assert.equal(describeCause(`${a.slice(1)}🚀`, "b"), `Error: ${a.slice(1)}; caused by Error: b`); assert.equal(describeCause(a, `${b}🚀`), `Error: ${a}; caused by Error: ${b}`); }); it("does not serialize arbitrary payloads or let unreadable errors break logging", () => { assert.equal(DesktopUpdates.describeUpdateErrorCause({ payload: "secret" }), "object"); const unreadable = Object.defineProperty({}, "message", { get() { throw new Error("unreadable error"); }, }); assert.equal(DesktopUpdates.describeUpdateErrorCause(unreadable), "UnknownError"); }); }); describe("DesktopUpdates under a managed policy", () => { const message = "Example Corp manages T3 Code updates."; const managed = ( policy: Partial, ): DesktopUpdatePolicy.UpdatePolicy => ({ ...DesktopUpdatePolicy.unmanagedUpdatePolicy, message, ...policy, }); it.effect("never checks when updates are off", () => { const harness = makeHarness({ policy: managed({ mode: "off" }) }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; yield* TestClock.adjust("10 minutes"); const state = yield* updates.getState; assert.equal(state.status, "disabled"); assert.equal(state.message, message); assert.isFalse((yield* updates.check("menu")).checked); assert.deepEqual(yield* updates.disabledReason, Option.some(message)); assert.equal(harness.checkCount(), 0); assert.equal(harness.listenerCount(), 0); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("updates from the administrator's feed", () => { const harness = makeHarness({ // Any user can set the mock flag; it must not redirect a managed feed. env: { T3CODE_DESKTOP_MOCK_UPDATES: "true" }, policy: managed({ feedUrl: "https://updates.example.com/t3code" }), }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; assert.equal((yield* updates.getState).status, "idle"); assert.deepEqual(harness.feedUrls(), [ { provider: "generic", url: "https://updates.example.com/t3code" }, ]); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("shows the mirror's notes for a release name without its version", () => { const harness = makeHarness({ policy: managed({ feedUrl: "https://updates.example.com/t3code" }), }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; harness.emit("update-available", { version: "1.2.4", releaseName: "December rollout", releaseNotes: "- Fix startup", }); yield* flushCallbacks; const state = yield* updates.getState; assert.equal(state.availableVersion, "1.2.4"); assert.deepEqual(state.releaseNotes, [ { version: "1.2.4", items: ["Fix startup"], totalItems: 1 }, ]); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("refuses the nightly channel without nightly", () => { const harness = makeHarness({ policy: managed({ allowNightly: false }) }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; const error = yield* updates.setChannel("nightly").pipe(Effect.flip); assert.equal(error._tag, "DesktopUpdateChannelManagedError"); assert.equal((yield* updates.getState).channel, "latest"); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("does not name a blocked build track as the one to switch back to", () => { const harness = makeHarness({ appVersion: "1.2.4-nightly.20261027.1", savedChannel: "latest", policy: managed({ allowNightly: false, feedUrl: "https://updates.example.com/t3code" }), }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; yield* updates.check("startup"); const state = yield* nextPublishedState(() => harness.emit("update-not-available")); assert.equal(state.channel, "latest"); assert.equal(state.status, "error"); assert.equal(state.errorContext, "check"); assert.isUndefined(state.trackMismatch); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("does not report the channel as the only block when updates are off anyway", () => { const harness = makeHarness({ appVersion: "1.2.3-nightly.20261028.1", env: { T3CODE_DISABLE_AUTO_UPDATE: "true" }, policy: managed({ allowNightly: false, feedUrl: "https://updates.example.com/t3code" }), }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; const state = yield* updates.getState; assert.isFalse(state.enabled); assert.isUndefined(state.channelBlockedByPolicy); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); it.effect("holds a nightly install until its user switches to stable", () => { const harness = makeHarness({ appVersion: "1.2.3-nightly.20261028.1", // A managed app ignores the mock feed, so give it a real one. policy: managed({ allowNightly: false, feedUrl: "https://updates.example.com/t3code" }), }); return Effect.scoped( Effect.gen(function* () { const updates = yield* DesktopUpdates.DesktopUpdates; yield* updates.configure; yield* TestClock.adjust("10 minutes"); const held = yield* updates.getState; assert.equal(held.channel, "nightly"); assert.equal(held.status, "disabled"); assert.include(held.message ?? "", message); assert.isTrue(held.channelBlockedByPolicy); assert.isTrue((yield* encodeUpdateState(held)).channelBlockedByPolicy); assert.isFalse((yield* updates.check("menu")).checked); assert.equal(harness.checkCount(), 0); const stable = yield* updates.setChannel("latest"); assert.equal(stable.channel, "latest"); assert.isTrue(stable.enabled); assert.isUndefined(stable.channelBlockedByPolicy); assert.equal(harness.checkCount(), 1); }), ).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer))); }); });