Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import { beforeEach, vi } from "vite-plus/test";
4
5const { autoUpdaterMock } = vi.hoisted(() => ({
6 autoUpdaterMock: {
7 allowDowngrade: false,
8 allowPrerelease: false,
9 autoDownload: true,
10 autoInstallOnAppQuit: true,
11 channel: "latest",
12 disableDifferentialDownload: false,
13 checkForUpdates: vi.fn(() => Promise.resolve(null)),
14 downloadUpdate: vi.fn(() => Promise.resolve([])),
15 on: vi.fn(),
16 quitAndInstall: vi.fn(),
17 removeListener: vi.fn(),
18 setFeedURL: vi.fn(),
19 },
20}));
21
22vi.mock("electron-updater", () => ({
23 autoUpdater: autoUpdaterMock,
24}));
25
26import * as ElectronUpdater from "./ElectronUpdater.ts";
27
28describe("ElectronUpdater", () => {
29 beforeEach(() => {
30 autoUpdaterMock.allowDowngrade = false;
31 autoUpdaterMock.allowPrerelease = false;
32 autoUpdaterMock.autoDownload = true;
33 autoUpdaterMock.autoInstallOnAppQuit = true;
34 autoUpdaterMock.channel = "latest";
35 autoUpdaterMock.disableDifferentialDownload = false;
36 autoUpdaterMock.checkForUpdates.mockClear();
37 autoUpdaterMock.checkForUpdates.mockImplementation(() => Promise.resolve(null));
38 autoUpdaterMock.downloadUpdate.mockClear();
39 autoUpdaterMock.downloadUpdate.mockImplementation(() => Promise.resolve([]));
40 autoUpdaterMock.on.mockClear();
41 autoUpdaterMock.quitAndInstall.mockClear();
42 autoUpdaterMock.removeListener.mockClear();
43 autoUpdaterMock.setFeedURL.mockClear();
44 });
45
46 it.effect("scopes updater event listeners", () =>
47 Effect.gen(function* () {
48 const listener = vi.fn();
49
50 yield* Effect.scoped(
51 Effect.gen(function* () {
52 const updater = yield* ElectronUpdater.ElectronUpdater;
53 yield* updater.on("update-available", listener);
54 }),
55 );
56
57 assert.deepEqual(autoUpdaterMock.on.mock.calls, [["update-available", listener]]);
58 assert.deepEqual(autoUpdaterMock.removeListener.mock.calls, [["update-available", listener]]);
59 }).pipe(Effect.provide(ElectronUpdater.layer)),
60 );
61
62 it.effect("wraps rejected update checks in the method-specific typed error", () =>
63 Effect.gen(function* () {
64 const cause = new Error("network unavailable");
65 autoUpdaterMock.checkForUpdates.mockImplementationOnce(() => Promise.reject(cause));
66 const updater = yield* ElectronUpdater.ElectronUpdater;
67 autoUpdaterMock.channel = "beta";
68
69 const error = yield* updater.checkForUpdates.pipe(Effect.flip);
70
71 assert.instanceOf(error, ElectronUpdater.ElectronUpdaterCheckForUpdatesError);
72 assert.isTrue(ElectronUpdater.isElectronUpdaterError(error));
73 assert.equal(error.channel, "beta");
74 assert.strictEqual(error.cause, cause);
75 assert.equal(error.message, "Electron updater failed to check for updates on channel beta.");
76 assert.notInclude(error.message, cause.message);
77 }).pipe(Effect.provide(ElectronUpdater.layer)),
78 );
79
80 it.effect("preserves the execution-time channel on download failures", () =>
81 Effect.gen(function* () {
82 const cause = new Error("download unavailable");
83 autoUpdaterMock.downloadUpdate.mockImplementationOnce(() => Promise.reject(cause));
84 const updater = yield* ElectronUpdater.ElectronUpdater;
85 autoUpdaterMock.channel = "nightly";
86
87 const error = yield* updater.downloadUpdate.pipe(Effect.flip);
88
89 assert.instanceOf(error, ElectronUpdater.ElectronUpdaterDownloadUpdateError);
90 assert.isTrue(ElectronUpdater.isElectronUpdaterError(error));
91 assert.equal(error.channel, "nightly");
92 assert.strictEqual(error.cause, cause);
93 assert.equal(
94 error.message,
95 "Electron updater failed to download the update on channel nightly.",
96 );
97 assert.notInclude(error.message, cause.message);
98 }).pipe(Effect.provide(ElectronUpdater.layer)),
99 );
100
101 it.effect("preserves quit-and-install flags and the execution-time channel", () =>
102 Effect.gen(function* () {
103 const cause = new Error("quit and install failed");
104 autoUpdaterMock.quitAndInstall.mockImplementationOnce(() => {
105 throw cause;
106 });
107 const updater = yield* ElectronUpdater.ElectronUpdater;
108 autoUpdaterMock.channel = "alpha";
109
110 const error = yield* updater
111 .quitAndInstall({ isSilent: true, isForceRunAfter: false })
112 .pipe(Effect.flip);
113
114 assert.instanceOf(error, ElectronUpdater.ElectronUpdaterQuitAndInstallError);
115 assert.isTrue(ElectronUpdater.isElectronUpdaterError(error));
116 assert.equal(error.channel, "alpha");
117 assert.equal(error.isSilent, true);
118 assert.equal(error.isForceRunAfter, false);
119 assert.strictEqual(error.cause, cause);
120 assert.equal(
121 error.message,
122 "Electron updater failed to quit and install the update on channel alpha (silent: true, force run after: false).",
123 );
124 assert.notInclude(error.message, cause.message);
125 assert.deepEqual(autoUpdaterMock.quitAndInstall.mock.calls, [[true, false]]);
126 }).pipe(Effect.provide(ElectronUpdater.layer)),
127 );
128});