Skip to content

Commit a354dd9

Browse files
fix(desktop): refresh queued updates before install (pingdotgg#6269)
Co-authored-by: shivam <[email protected]>
1 parent 51341f2 commit a354dd9

6 files changed

Lines changed: 524 additions & 113 deletions

File tree

‎apps/desktop/src/updates/DesktopUpdates.test.ts‎

Lines changed: 267 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ interface UpdatesHarnessOptions {
2727
void,
2828
ElectronUpdater.ElectronUpdaterCheckForUpdatesError
2929
>;
30+
readonly beforeSetUpdateChannel?: Effect.Effect<void>;
3031
readonly setUpdateChannelError?: DesktopAppSettings.DesktopSettingsWriteError;
3132
readonly setDisableDifferentialDownload?: Effect.Effect<void>;
3233
readonly stopBackend?: Effect.Effect<void>;
@@ -153,22 +154,41 @@ function makeHarness(options: UpdatesHarnessOptions = {}) {
153154
),
154155
);
155156

157+
let testSettings: DesktopAppSettings.DesktopSettings = {
158+
...DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS,
159+
};
156160
const setUpdateChannelError = options.setUpdateChannelError;
157-
const settingsLayer = setUpdateChannelError
158-
? Layer.succeed(DesktopAppSettings.DesktopAppSettings, {
159-
get: Effect.succeed(DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS),
160-
load: Effect.succeed(DesktopAppSettings.DEFAULT_DESKTOP_SETTINGS),
161-
setMainWindowBounds: () => Effect.die("unexpected main window bounds update"),
162-
setServerExposureMode: () => Effect.die("unexpected server exposure update"),
163-
setTailscaleServe: () => Effect.die("unexpected Tailscale Serve update"),
164-
setUpdateChannel: () => Effect.fail(setUpdateChannelError),
165-
setWslBackendEnabled: () => Effect.die("unexpected WSL backend toggle"),
166-
setWslDistro: () => Effect.die("unexpected WSL distro change"),
167-
setWslOnly: () => Effect.die("unexpected WSL-only toggle"),
168-
applyWslWindowsFallback: Effect.die("unexpected WSL Windows fallback"),
169-
applyWslWindowsFallbackInMemory: Effect.die("unexpected WSL Windows fallback"),
170-
} satisfies DesktopAppSettings.DesktopAppSettings["Service"])
171-
: DesktopAppSettings.layer;
161+
const settingsLayer =
162+
setUpdateChannelError || options.beforeSetUpdateChannel
163+
? Layer.succeed(DesktopAppSettings.DesktopAppSettings, {
164+
get: Effect.sync(() => testSettings),
165+
load: Effect.sync(() => testSettings),
166+
setMainWindowBounds: () => Effect.die("unexpected main window bounds update"),
167+
setServerExposureMode: () => Effect.die("unexpected server exposure update"),
168+
setTailscaleServe: () => Effect.die("unexpected Tailscale Serve update"),
169+
setUpdateChannel: (channel) =>
170+
setUpdateChannelError
171+
? Effect.fail(setUpdateChannelError)
172+
: (options.beforeSetUpdateChannel ?? Effect.void).pipe(
173+
Effect.andThen(
174+
Effect.sync(() => {
175+
const changed = testSettings.updateChannel !== channel;
176+
testSettings = {
177+
...testSettings,
178+
updateChannel: channel,
179+
updateChannelConfiguredByUser: true,
180+
};
181+
return { settings: testSettings, changed };
182+
}),
183+
),
184+
),
185+
setWslBackendEnabled: () => Effect.die("unexpected WSL backend toggle"),
186+
setWslDistro: () => Effect.die("unexpected WSL distro change"),
187+
setWslOnly: () => Effect.die("unexpected WSL-only toggle"),
188+
applyWslWindowsFallback: Effect.die("unexpected WSL Windows fallback"),
189+
applyWslWindowsFallbackInMemory: Effect.die("unexpected WSL Windows fallback"),
190+
} satisfies DesktopAppSettings.DesktopAppSettings["Service"])
191+
: DesktopAppSettings.layer;
172192

173193
const layer = DesktopUpdates.layer.pipe(
174194
Layer.provideMerge(updaterLayer),
@@ -337,6 +357,178 @@ describe("DesktopUpdates", () => {
337357
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
338358
});
339359

360+
it.effect("checks for newer releases after an update has been downloaded", () => {
361+
const harness = makeHarness();
362+
363+
return Effect.scoped(
364+
Effect.gen(function* () {
365+
const updates = yield* DesktopUpdates.DesktopUpdates;
366+
yield* updates.configure;
367+
368+
harness.emit("update-available", {
369+
version: "1.2.4",
370+
releaseNotes: "## What's changed\n- fix: queued update",
371+
});
372+
yield* flushCallbacks;
373+
harness.emit("update-downloaded", { version: "1.2.4" });
374+
yield* flushCallbacks;
375+
376+
const result = yield* updates.check("poll");
377+
assert.isTrue(result.checked);
378+
379+
harness.emit("update-available", { version: "1.2.4" });
380+
yield* flushCallbacks;
381+
382+
const unchangedState = yield* updates.getState;
383+
assert.equal(unchangedState.status, "downloaded");
384+
assert.equal(unchangedState.downloadedVersion, "1.2.4");
385+
assert.deepEqual(unchangedState.releaseNotes, [
386+
{ version: "1.2.4", items: ["fix: queued update"] },
387+
]);
388+
389+
const nextResult = yield* updates.check("poll");
390+
assert.isTrue(nextResult.checked);
391+
392+
harness.emit("update-available", { version: "1.2.5" });
393+
yield* flushCallbacks;
394+
395+
const state = yield* updates.getState;
396+
assert.equal(state.status, "available");
397+
assert.equal(state.availableVersion, "1.2.5");
398+
assert.isNull(state.downloadedVersion);
399+
}),
400+
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
401+
});
402+
403+
it.effect("preserves a queued installer when the feed has no update", () => {
404+
const harness = makeHarness();
405+
406+
return Effect.scoped(
407+
Effect.gen(function* () {
408+
const updates = yield* DesktopUpdates.DesktopUpdates;
409+
yield* updates.configure;
410+
411+
harness.emit("update-available", {
412+
version: "1.2.4",
413+
releaseNotes: "## What's changed\n- fix: queued update",
414+
});
415+
yield* flushCallbacks;
416+
harness.emit("update-downloaded", { version: "1.2.4" });
417+
yield* flushCallbacks;
418+
419+
yield* updates.check("poll");
420+
harness.emit("update-not-available");
421+
yield* flushCallbacks;
422+
423+
const state = yield* updates.getState;
424+
assert.equal(state.status, "downloaded");
425+
assert.equal(state.availableVersion, "1.2.4");
426+
assert.equal(state.downloadedVersion, "1.2.4");
427+
assert.deepEqual(state.releaseNotes, [{ version: "1.2.4", items: ["fix: queued update"] }]);
428+
assert.equal(state.downloadPercent, 100);
429+
}),
430+
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
431+
});
432+
433+
it.effect("preserves a queued installer when the feed offers another channel", () => {
434+
const harness = makeHarness();
435+
436+
return Effect.scoped(
437+
Effect.gen(function* () {
438+
const updates = yield* DesktopUpdates.DesktopUpdates;
439+
yield* updates.configure;
440+
441+
harness.emit("update-available", {
442+
version: "1.2.4",
443+
releaseNotes: "## What's changed\n- fix: queued update",
444+
});
445+
yield* flushCallbacks;
446+
harness.emit("update-downloaded", { version: "1.2.4" });
447+
yield* flushCallbacks;
448+
449+
yield* updates.check("poll");
450+
harness.emit("update-available", { version: "1.2.5-nightly.20260710.1" });
451+
yield* flushCallbacks;
452+
453+
const state = yield* updates.getState;
454+
assert.equal(state.status, "downloaded");
455+
assert.equal(state.availableVersion, "1.2.4");
456+
assert.equal(state.downloadedVersion, "1.2.4");
457+
assert.deepEqual(state.releaseNotes, [{ version: "1.2.4", items: ["fix: queued update"] }]);
458+
assert.equal(state.downloadPercent, 100);
459+
}),
460+
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
461+
});
462+
463+
it.effect(
464+
"rejects install while a refresh check is in progress and releases the reservation",
465+
() =>
466+
Effect.gen(function* () {
467+
const checkStarted = yield* Deferred.make<void>();
468+
const releaseCheck = yield* Deferred.make<void>();
469+
const harness = makeHarness({
470+
checkForUpdates: Deferred.succeed(checkStarted, undefined).pipe(
471+
Effect.andThen(Deferred.await(releaseCheck)),
472+
),
473+
});
474+
475+
yield* Effect.scoped(
476+
Effect.gen(function* () {
477+
const updates = yield* DesktopUpdates.DesktopUpdates;
478+
yield* updates.configure;
479+
harness.emit("update-downloaded", { version: "1.2.4" });
480+
yield* flushCallbacks;
481+
482+
const checkFiber = yield* updates.check("manual").pipe(Effect.forkScoped);
483+
yield* Deferred.await(checkStarted);
484+
485+
const installResult = yield* updates.install;
486+
assert.isFalse(installResult.accepted);
487+
488+
yield* Deferred.succeed(releaseCheck, undefined);
489+
const checkResult = yield* Fiber.join(checkFiber);
490+
assert.isTrue(checkResult.checked);
491+
492+
const followUpCheck = yield* updates.check("manual");
493+
assert.isTrue(followUpCheck.checked);
494+
assert.equal(harness.checkCount(), 2);
495+
}),
496+
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
497+
}),
498+
);
499+
500+
it.effect("rejects refresh checks while install is in progress", () =>
501+
Effect.gen(function* () {
502+
const installStarted = yield* Deferred.make<void>();
503+
const releaseInstall = yield* Deferred.make<void>();
504+
const harness = makeHarness({
505+
stopBackend: Deferred.succeed(installStarted, undefined).pipe(
506+
Effect.andThen(Deferred.await(releaseInstall)),
507+
),
508+
});
509+
510+
yield* Effect.scoped(
511+
Effect.gen(function* () {
512+
const updates = yield* DesktopUpdates.DesktopUpdates;
513+
yield* updates.configure;
514+
harness.emit("update-downloaded", { version: "1.2.4" });
515+
yield* flushCallbacks;
516+
517+
const installFiber = yield* updates.install.pipe(Effect.forkScoped);
518+
yield* Deferred.await(installStarted);
519+
520+
const checkResult = yield* updates.check("manual");
521+
assert.isFalse(checkResult.checked);
522+
assert.equal(harness.checkCount(), 0);
523+
524+
yield* Deferred.succeed(releaseInstall, undefined);
525+
const installResult = yield* Fiber.join(installFiber);
526+
assert.isTrue(installResult.accepted);
527+
}),
528+
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
529+
}),
530+
);
531+
340532
it.effect("keeps raw updater event failures out of update state", () => {
341533
const harness = makeHarness();
342534
const cause = new Error(
@@ -359,6 +551,30 @@ describe("DesktopUpdates", () => {
359551
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
360552
});
361553

554+
it.effect("preserves a queued installer after a background updater error", () => {
555+
const harness = makeHarness();
556+
557+
return Effect.scoped(
558+
Effect.gen(function* () {
559+
const updates = yield* DesktopUpdates.DesktopUpdates;
560+
yield* updates.configure;
561+
harness.emit("update-downloaded", { version: "1.2.4" });
562+
yield* flushCallbacks;
563+
564+
harness.emit("error", new Error("background updater failure"));
565+
yield* flushCallbacks;
566+
567+
const state = yield* updates.getState;
568+
assert.equal(state.status, "error");
569+
assert.equal(state.downloadedVersion, "1.2.4");
570+
assert.isNull(state.errorContext);
571+
572+
const result = yield* updates.install;
573+
assert.isTrue(result.accepted);
574+
}),
575+
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
576+
});
577+
362578
it.effect("logs bounded updater failure context without exposing the cause", () => {
363579
const cause = new Error(
364580
"request failed for https://user:[email protected]/update?token=secret",
@@ -581,6 +797,38 @@ describe("DesktopUpdates", () => {
581797
}),
582798
);
583799

800+
it.effect("rejects checks while an update channel change is being persisted", () =>
801+
Effect.gen(function* () {
802+
const channelChangeStarted = yield* Deferred.make<void>();
803+
const releaseChannelChange = yield* Deferred.make<void>();
804+
const harness = makeHarness({
805+
beforeSetUpdateChannel: Deferred.succeed(channelChangeStarted, undefined).pipe(
806+
Effect.andThen(Deferred.await(releaseChannelChange)),
807+
),
808+
});
809+
810+
yield* Effect.scoped(
811+
Effect.gen(function* () {
812+
const updates = yield* DesktopUpdates.DesktopUpdates;
813+
yield* updates.configure;
814+
815+
const channelFiber = yield* updates.setChannel("nightly").pipe(Effect.forkScoped);
816+
yield* Deferred.await(channelChangeStarted);
817+
818+
const checkResult = yield* updates.check("manual");
819+
assert.isFalse(checkResult.checked);
820+
assert.equal(harness.checkCount(), 0);
821+
822+
yield* Deferred.succeed(releaseChannelChange, undefined);
823+
const state = yield* Fiber.join(channelFiber);
824+
825+
assert.equal(state.channel, "nightly");
826+
assert.equal(harness.checkCount(), 1);
827+
}),
828+
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
829+
}),
830+
);
831+
584832
it.effect("preserves settings failure context when an update channel cannot be persisted", () => {
585833
const diskFailure = new Error("disk exploded");
586834
const settingsFailure = new DesktopAppSettings.DesktopSettingsWriteError({
@@ -604,6 +852,10 @@ describe("DesktopUpdates", () => {
604852
assert.strictEqual(error.cause.cause, diskFailure);
605853
assert.equal(error.message, "Failed to persist the nightly desktop update channel.");
606854
assert.notInclude(error.message, diskFailure.message);
855+
856+
const checkResult = yield* updates.check("manual");
857+
assert.isTrue(checkResult.checked);
858+
assert.equal(harness.checkCount(), 1);
607859
}),
608860
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
609861
});

0 commit comments

Comments
 (0)