Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ElectronUpdater.test.ts

142 lines5.5 KB

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