Skip to content
72 changes: 72 additions & 0 deletions apps/desktop/src/app/DesktopStartupRecovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,75 @@ it.effect.each([
}),
).pipe(Effect.provide(DesktopState.layer)),
);

it.effect("installs the build the update action downloaded, not the one it offered", () =>
Effect.scoped(
Effect.gen(function* () {
const installed = yield* Deferred.make<string>();
const shown = yield* Deferred.make<void>();
let navigate: ((event: { preventDefault: () => void }, url: string) => void) | undefined;
const recovery = {
isDestroyed: () => false,
loadURL: () => Promise.resolve(),
once: () => {},
webContents: {
setWindowOpenHandler: () => {},
on: (_event: string, listener: typeof navigate) => {
navigate = listener;
Deferred.doneUnsafe(shown, Effect.void);
},
},
} as unknown as Electron.BrowserWindow;
const offered = updateState({ status: "available", availableVersion: "0.1.4" });
const state = yield* DesktopState.DesktopState;
yield* listen.pipe(
Effect.provide(
Layer.mergeAll(
FileSystem.layerNoop({ readFileString: () => Effect.succeed("0.1.3") }),
Layer.succeed(DesktopEnvironment.DesktopEnvironment, {
appVersion: "0.1.3",
stateDir: "/task/state",
logDir: "/task/logs",
path: { join: (...parts: string[]) => parts.join("/") },
} as unknown as DesktopEnvironment.DesktopEnvironment["Service"]),
Layer.succeed(ElectronApp.ElectronApp, {
on: () => Effect.void,
} as unknown as ElectronApp.ElectronApp["Service"]),
Layer.succeed(
ElectronShell.ElectronShell,
{} as ElectronShell.ElectronShell["Service"],
),
Layer.succeed(ElectronTheme.ElectronTheme, {
shouldUseDarkColors: Effect.succeed(false),
} as ElectronTheme.ElectronTheme["Service"]),
Layer.succeed(ElectronWindow.ElectronWindow, {
main: Effect.succeedNone,
create: () => Effect.succeed(recovery),
} as unknown as ElectronWindow.ElectronWindow["Service"]),
Layer.succeed(DesktopUpdates.DesktopUpdates, {
subscribe: Effect.succeed({ latest: offered, changes: Stream.empty }),
check: () => Effect.void,
getState: Effect.succeed(offered),
// A release published since the offer: the download fetches it instead.
download: Effect.succeed({
accepted: true,
completed: true,
state: updateState({ status: "downloaded", downloadedVersion: "0.1.5" }),
}),
installPrepared: (version: string) => Deferred.succeed(installed, version),
} as unknown as DesktopUpdates.DesktopUpdates["Service"]),
),
),
);
yield* SubscriptionRef.set(state.primaryBackendStartup, {
_tag: "Failing",
reason: "Server failed before readiness",
managedPolicy: false,
output: "Failure details",
});
yield* Deferred.await(shown);
navigate!({ preventDefault: () => {} }, "https://startup-recovery.invalid/update");
expect(yield* Deferred.await(installed)).toBe("0.1.5");
}),
).pipe(Effect.provide(DesktopState.layer)),
);
9 changes: 6 additions & 3 deletions apps/desktop/src/app/DesktopStartupRecovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,15 @@ export const listen: Effect.Effect<
switch (action) {
case "update": {
if (current._tag !== "Update") return;
const state = yield* updates.getState;
if (state.downloadedVersion === null) {
let { downloadedVersion } = yield* updates.getState;
if (downloadedVersion === null) {
// The download re-checks first, so it can fetch a newer build than the one offered.
const download = yield* updates.download;
if (!download.completed) return;
downloadedVersion = download.state.downloadedVersion;
}
yield* updates.installPrepared(current.version);
if (downloadedVersion === null) return;
yield* updates.installPrepared(downloadedVersion);
return;
}
case "reinstall":
Expand Down
12 changes: 6 additions & 6 deletions apps/desktop/src/updates/DesktopRemoteUpdates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -733,16 +733,15 @@ describe("DesktopRemoteUpdates", () => {
it.effect("waits for a background check before installing a prepared update", () => {
const backgroundCheckStarted = Deferred.makeUnsafe<void>();
const releaseBackgroundCheck = Deferred.makeUnsafe<void>();
let checks = 0;
let blockCheck = false;
const harness = makeHarness({
checkForUpdates: Effect.suspend(() => {
checks += 1;
return checks === 2
checkForUpdates: Effect.suspend(() =>
blockCheck
? Deferred.succeed(backgroundCheckStarted, undefined).pipe(
Effect.andThen(Deferred.await(releaseBackgroundCheck)),
)
: Effect.void;
}),
: Effect.void,
),
});

return runRemoteUpdatesTest(harness, ({ requests, commits }) =>
Expand All @@ -755,6 +754,7 @@ describe("DesktopRemoteUpdates", () => {
harness.emit("update-downloaded", { version: "1.2.4" });
yield* settle;

blockCheck = true;
const backgroundCheck = yield* updates.check("poll").pipe(Effect.forkChild);
yield* Deferred.await(backgroundCheckStarted);
assert.isTrue(yield* updates.isActionActive);
Expand Down
146 changes: 144 additions & 2 deletions apps/desktop/src/updates/DesktopUpdates.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,10 +344,11 @@ describe("DesktopUpdates", () => {

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(), 0);
assert.equal(harness.checkCount(), checksBefore);

yield* Deferred.succeed(releaseInstall, undefined);
const installResult = yield* Fiber.join(installFiber);
Expand All @@ -358,7 +359,9 @@ describe("DesktopUpdates", () => {
);

it.effect("preserves a queued installer after a background updater error", () => {
const harness = makeHarness();
const harness = makeHarness({
checkForUpdates: Effect.sync(() => harness.emit("update-not-available")),
});

return Effect.scoped(
Effect.gen(function* () {
Expand Down Expand Up @@ -494,6 +497,145 @@ describe("DesktopUpdates", () => {
).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("restores download state and permits retry after interruption", () =>
Effect.gen(function* () {
const actionStarted = yield* Deferred.make<void>();
Expand Down
Loading
Loading