Skip to content

Commit b772aee

Browse files
test(desktop): test the .deb relaunch at one layer each
- Assert relaunch ordering and isForceRunAfter through the stub update harness instead of driving a real DebUpdater from DesktopUpdates. - Keep the real DebUpdater only where it proves electron-updater emits install errors instead of throwing them. - Read relaunch args from HostProcess.Arguments. - Add preferredSystemLanguages to the ElectronApp stub after the merge. AI-assisted; verified with the tests and tools listed above.
1 parent 7bcfa4b commit b772aee

5 files changed

Lines changed: 123 additions & 236 deletions

File tree

‎apps/desktop/src/electron/ElectronApp.test.ts‎

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,14 @@ import * as Effect from "effect/Effect";
1313
import * as Layer from "effect/Layer";
1414
import { beforeEach, vi } from "vite-plus/test";
1515

16-
const { getSystemLocaleMock, getUserDefaultMock, relaunchMock } = vi.hoisted(() => ({
17-
relaunchMock: vi.fn(),
16+
const { getSystemLocaleMock, getUserDefaultMock } = vi.hoisted(() => ({
1817
getSystemLocaleMock: vi.fn(() => "en-GB"),
1918
getUserDefaultMock: vi.fn((_key: string) => false),
2019
}));
2120

2221
vi.mock("electron", () => ({
2322
app: {
2423
getSystemLocale: getSystemLocaleMock,
25-
relaunch: relaunchMock,
2624
},
2725
systemPreferences: {
2826
getUserDefault: getUserDefaultMock,
@@ -37,7 +35,6 @@ describe("ElectronApp", () => {
3735
beforeEach(() => {
3836
getSystemLocaleMock.mockReset();
3937
getUserDefaultMock.mockReset();
40-
relaunchMock.mockClear();
4138
});
4239

4340
it("keeps the macOS region override and hour cycle in the locale tag", () => {
@@ -79,32 +76,16 @@ describe("ElectronApp", () => {
7976
}).pipe(Effect.provide(electronAppLayer)),
8077
);
8178

82-
it.effect("relaunches through app.relaunch() off Linux", () =>
83-
Effect.gen(function* () {
84-
const electronApp = yield* ElectronApp.ElectronApp;
85-
yield* electronApp.relaunch({ args: ["--flag"] });
86-
87-
assert.deepEqual(relaunchMock.mock.calls, [[{ args: ["--flag"] }]]);
88-
}).pipe(
89-
Effect.provide(electronAppLayer),
90-
Effect.provideService(HostProcess.Platform, "darwin"),
91-
),
92-
);
93-
9479
it("relaunches the AppImage only when its mount contains the current executable", () => {
9580
const currentExecPath = "/tmp/t3-mount/t3code";
9681
for (const [execPath, env, expected] of [
9782
[currentExecPath, {}, currentExecPath],
98-
[currentExecPath, { APPIMAGE: "/Apps/T3.AppImage" }, currentExecPath],
99-
[currentExecPath, { APPDIR: "/tmp/t3-mount" }, currentExecPath],
10083
[
10184
currentExecPath,
10285
{ APPIMAGE: "/Apps/T3.AppImage", APPDIR: "/tmp/t3-mount/" },
10386
"/Apps/T3.AppImage",
10487
],
10588
[currentExecPath, { APPIMAGE: "/Apps/T3.AppImage", APPDIR: "/tmp/t3" }, currentExecPath],
106-
[currentExecPath, { APPIMAGE: "/Apps/T3.AppImage", APPDIR: "/unrelated" }, currentExecPath],
107-
[currentExecPath, { APPIMAGE: "/Apps/T3.AppImage", APPDIR: "tmp/t3-mount" }, currentExecPath],
10889
[
10990
currentExecPath,
11091
{ APPIMAGE: "/Apps/T3.AppImage", APPDIR: "/tmp/t3-mount/../other" },
@@ -214,11 +195,6 @@ describe("ElectronApp", () => {
214195
assert.isNumber(shellPid);
215196
assert.isTrue(ready.running, "relauncher survives its Effect scope closing");
216197
assert.match(ready.status, /^NoNewPrivs:\s+0$/m);
217-
const stat = await NodeFSP.readFile(`/proc/${shellPid}/stat`, "utf8");
218-
const [, ppid, group, session] = stat.slice(stat.lastIndexOf(")") + 2).split(" ");
219-
assert.equal(Number(ppid), parent.pid);
220-
assert.equal(Number(group), shellPid, "detached process group");
221-
assert.equal(Number(session), shellPid, "detached session");
222198
assert.equal(
223199
await NodeFSP.readlink(`/proc/${shellPid}/exe`),
224200
await NodeFSP.realpath("/bin/sh"),

‎apps/desktop/src/electron/ElectronApp.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ const makeWithLinuxRelaunch = Effect.gen(function* () {
278278
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
279279
const env = yield* HostProcess.Environment;
280280
const currentExecPath = yield* HostProcess.ExecutablePath;
281+
const currentArgs = yield* HostProcess.Arguments;
281282
if (platform !== "linux") return make;
282283

283284
return ElectronApp.of({
@@ -298,7 +299,7 @@ const makeWithLinuxRelaunch = Effect.gen(function* () {
298299
currentExecPath,
299300
env,
300301
}),
301-
...(options.args ?? process.argv.slice(1)),
302+
...(options.args ?? currentArgs.slice(1)),
302303
],
303304
{ detached: true, stdin: "pipe", stdout: "ignore", stderr: "ignore" },
304305
),
Lines changed: 80 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { assert, describe, it } from "@effect/vitest";
2-
import * as Effect from "effect/Effect";
32
import * as Cause from "effect/Cause";
3+
import * as Effect from "effect/Effect";
4+
import * as Exit from "effect/Exit";
5+
import { DebUpdater } from "electron-updater/out/DebUpdater.js";
6+
import { DownloadedUpdateHelper } from "electron-updater/out/DownloadedUpdateHelper.js";
47
import { vi } from "vite-plus/test";
58

6-
const { installed } = vi.hoisted(() => ({
7-
installed: { updater: undefined as typeof import("electron-updater").autoUpdater | undefined },
9+
const installed = vi.hoisted(() => ({
10+
updater: undefined as typeof import("electron-updater").autoUpdater | undefined,
811
}));
912

1013
vi.mock("electron-updater", () => ({
@@ -14,37 +17,82 @@ vi.mock("electron-updater", () => ({
1417
}));
1518

1619
import * as ElectronUpdater from "./ElectronUpdater.ts";
17-
import { makeInstalledDebUpdater } from "../updates/updatesTestHarness.ts";
20+
21+
// Real BaseUpdater.quitAndInstall and DebUpdater.doInstall; only the package manager call fails.
22+
async function makeFailingDebUpdater(installError: Error) {
23+
class FailingDebUpdater extends DebUpdater {
24+
override hasCommand() {
25+
return true;
26+
}
27+
override detectPackageManager() {
28+
return "apt";
29+
}
30+
override runCommandWithSudoIfNeeded(): string {
31+
throw installError;
32+
}
33+
setDownloaded(helper: DownloadedUpdateHelper) {
34+
this.downloadedUpdateHelper = helper;
35+
}
36+
}
37+
const updater = new FailingDebUpdater(null, {
38+
version: "1.2.3",
39+
name: "Test",
40+
isPackaged: true,
41+
appUpdateConfigPath: "/unused/app-update.yml",
42+
userDataPath: "/unused/data",
43+
baseCachePath: "/unused/cache",
44+
whenReady: async () => {},
45+
relaunch: () => {},
46+
quit: () => {},
47+
onQuit: () => {},
48+
});
49+
updater.logger = null;
50+
const helper = new DownloadedUpdateHelper("/unused/cache");
51+
const info = { url: "test.deb", sha512: "test" };
52+
await helper.setDownloadedFile(
53+
"/unused/test.deb",
54+
null,
55+
{
56+
version: "1.2.4",
57+
releaseDate: "2026-10-01",
58+
files: [info],
59+
path: info.url,
60+
sha512: info.sha512,
61+
},
62+
{ url: new URL("https://example.invalid/test.deb"), info },
63+
"test.deb",
64+
false,
65+
);
66+
updater.setDownloaded(helper);
67+
return updater;
68+
}
1869

1970
describe("ElectronUpdater", () => {
20-
it.effect(
21-
"rejects an install error emitted by the installed DebUpdater and removes its listener",
22-
() =>
23-
Effect.gen(function* () {
24-
const cause = new Error("pkexec exited with code 126: authentication cancelled");
25-
const native = yield* Effect.promise(() => makeInstalledDebUpdater(cause));
26-
installed.updater = native.updater;
27-
try {
28-
const updater = yield* ElectronUpdater.ElectronUpdater;
29-
const listenersBefore = native.updater.listenerCount("error");
30-
const exit = yield* updater
31-
.quitAndInstall({ isSilent: true, isForceRunAfter: false })
32-
.pipe(Effect.exit);
33-
assert.equal(
34-
exit._tag,
35-
"Failure",
36-
"the emitted installer error must fail the service call",
37-
);
38-
if (exit._tag === "Success") return;
39-
const error = Cause.squash(exit.cause);
40-
assert.instanceOf(error, ElectronUpdater.ElectronUpdaterQuitAndInstallError);
41-
assert.strictEqual(error.cause, cause);
42-
assert.deepEqual(native.steps, ["install"]);
43-
assert.equal(native.updater.listenerCount("error"), listenersBefore);
44-
} finally {
71+
it.effect("fails quitAndInstall when the installer reports an error event", () =>
72+
Effect.gen(function* () {
73+
const cause = new Error("pkexec exited with code 126: authentication cancelled");
74+
const native = yield* Effect.promise(() => makeFailingDebUpdater(cause));
75+
installed.updater = native;
76+
const listenersBefore = native.listenerCount("error");
77+
78+
const updater = yield* ElectronUpdater.ElectronUpdater;
79+
const exit = yield* updater
80+
.quitAndInstall({ isSilent: true, isForceRunAfter: false })
81+
.pipe(Effect.exit);
82+
83+
assert.isTrue(Exit.isFailure(exit));
84+
if (Exit.isSuccess(exit)) return;
85+
const error = Cause.squash(exit.cause);
86+
assert.instanceOf(error, ElectronUpdater.ElectronUpdaterQuitAndInstallError);
87+
assert.strictEqual(error.cause, cause);
88+
assert.equal(native.listenerCount("error"), listenersBefore);
89+
}).pipe(
90+
Effect.provide(ElectronUpdater.layer),
91+
Effect.ensuring(
92+
Effect.sync(() => {
4593
installed.updater = undefined;
46-
native.restore();
47-
}
48-
}).pipe(Effect.provide(ElectronUpdater.layer)),
94+
}),
95+
),
96+
),
4997
);
5098
});

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

Lines changed: 34 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import * as NodeEvents from "node:events";
21
import * as NodeModule from "node:module";
3-
import { vi } from "vite-plus/test";
2+
43
import { assert, describe, it } from "@effect/vitest";
54
import { DESKTOP_UPDATE_RESTART_MARKER_FILE } from "@t3tools/contracts";
65
import { computeReleaseNotes } from "electron-updater/out/providers/GitHubProvider.js";
@@ -24,17 +23,7 @@ import * as DesktopEnvironment from "../app/DesktopEnvironment.ts";
2423
import * as DesktopState from "../app/DesktopState.ts";
2524
import * as DesktopUpdatePolicy from "./DesktopUpdatePolicy.ts";
2625
import * as DesktopUpdates from "./DesktopUpdates.ts";
27-
import { flushCallbacks, makeHarness, makeInstalledDebUpdater } from "./updatesTestHarness.ts";
28-
29-
const installed = vi.hoisted(() => ({
30-
updater: undefined as typeof import("electron-updater").autoUpdater | undefined,
31-
}));
32-
vi.mock("electron-updater", () => ({
33-
get autoUpdater() {
34-
if (!installed.updater) throw new Error("Unexpected native updater access");
35-
return installed.updater;
36-
},
37-
}));
26+
import { flushCallbacks, makeHarness } from "./updatesTestHarness.ts";
3827

3928
type ReleaseFeed = Parameters<typeof computeReleaseNotes>[2];
4029
type RunningVersion = Parameters<typeof computeReleaseNotes>[0];
@@ -585,81 +574,38 @@ describe("DesktopUpdates", () => {
585574
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
586575
});
587576

588-
it.effect.each(["success", "failed"] as const)(
589-
"handles a %s .deb install through the installed updater",
590-
(outcome) =>
591-
Effect.scoped(
592-
Effect.gen(function* () {
593-
const native = yield* Effect.acquireRelease(
594-
Effect.promise(() =>
595-
makeInstalledDebUpdater(
596-
outcome === "success" ? undefined : new Error("pkexec exited with code 1"),
597-
),
598-
),
599-
(native) =>
600-
Effect.sync(() => {
601-
installed.updater = undefined;
602-
native.restore();
603-
}),
604-
);
605-
installed.updater = native.updater;
606-
const install = vi.spyOn(native.updater, "quitAndInstall");
607-
const harness = makeHarness({
608-
platform: "linux",
609-
packageType: "deb",
610-
updater: ElectronUpdater.make,
611-
});
612-
yield* Effect.scoped(
613-
Effect.gen(function* () {
614-
const updates = yield* DesktopUpdates.DesktopUpdates;
615-
const desktopState = yield* DesktopState.DesktopState;
616-
const pool = yield* DesktopBackendPool.DesktopBackendPool;
617-
yield* updates.configure;
618-
const { changes } = yield* updates.subscribe;
619-
native.updater.emit("update-downloaded", {
620-
version: "1.2.4",
621-
downloadedFile: "/unused/test.deb",
622-
releaseDate: "2026-10-01",
623-
files: [],
624-
path: "test.deb",
625-
sha512: "test",
626-
});
627-
yield* changes.pipe(
628-
Stream.filter((state) => state.status === "downloaded"),
629-
Stream.runHead,
630-
);
631-
const quit =
632-
outcome === "success"
633-
? NodeEvents.EventEmitter.once(native.events, "quit")
634-
: undefined;
635-
636-
const result = yield* updates.install;
637-
assert.isTrue(result.accepted);
638-
assert.isFalse(result.completed);
639-
if (quit) yield* Effect.promise(() => quit);
640-
assert.deepEqual(install.mock.calls, [[true, false]]);
641-
if (quit) {
642-
assert.deepEqual(native.steps, ["install", "quit"]);
643-
assert.deepEqual(harness.installSteps, ["relaunch"]);
644-
assert.isFalse((yield* (yield* pool.primary).snapshot).desiredRunning);
645-
} else {
646-
yield* changes.pipe(
647-
Stream.filter((state) => state.errorContext === "install"),
648-
Stream.runHead,
649-
);
650-
assert.deepEqual(native.steps, ["install"]);
651-
assert.deepEqual(harness.installSteps, ["startBackend"]);
652-
assert.isFalse(yield* Ref.get(desktopState.quitting));
653-
assert.isTrue((yield* (yield* pool.primary).snapshot).desiredRunning);
654-
assert.equal(harness.updateRestartMarkers.size, 0);
655-
assert.equal(harness.installRecords.size, 0);
656-
assert.equal((yield* updates.getState).errorContext, "install");
657-
}
658-
}),
659-
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
660-
}),
661-
),
662-
);
577+
it.effect.each([
578+
{ outcome: "succeeds", steps: ["quitAndInstall", "relaunch"] },
579+
{ outcome: "fails", steps: ["quitAndInstall", "startBackend"] },
580+
] as const)("relaunches a .deb install itself only after it $outcome", ({ outcome, steps }) => {
581+
const harness = makeHarness({
582+
platform: "linux",
583+
packageType: "deb",
584+
...(outcome === "fails" && {
585+
quitAndInstall: Effect.fail(
586+
new ElectronUpdater.ElectronUpdaterQuitAndInstallError({
587+
channel: "latest",
588+
isSilent: true,
589+
isForceRunAfter: false,
590+
cause: new Error("pkexec exited with code 126"),
591+
}),
592+
),
593+
}),
594+
});
595+
596+
return Effect.scoped(
597+
Effect.gen(function* () {
598+
const updates = yield* DesktopUpdates.DesktopUpdates;
599+
yield* updates.configure;
600+
harness.emit("update-downloaded", { version: "1.2.4" });
601+
yield* flushCallbacks;
602+
603+
yield* updates.install;
604+
assert.deepEqual(harness.quitAndInstallForceRunAfter, [false]);
605+
assert.deepEqual(harness.installSteps, [...steps]);
606+
}),
607+
).pipe(Effect.provide(Layer.merge(TestClock.layer(), harness.layer)));
608+
});
663609

664610
it.effect("drops the update restart marker when an install is interrupted", () =>
665611
Effect.gen(function* () {

0 commit comments

Comments
 (0)