GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(desktop): install AppImage updates via staged atomic swap · Lucenx9/t3code@0f97043

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/desktop/src
    • electron
      • ElectronUpdater.test.ts
      • ElectronUpdater.ts
    • updates
      • DesktopUpdates.test.ts
      • DesktopUpdates.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

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

Copy file name to clipboardExpand all lines: apps/desktop/src/electron/ElectronUpdater.test.ts
+305-1Lines changed: 305 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
// @effect-diagnostics nodeBuiltinImport:off - safe-install tests drive the real filesystem boundary the updater swaps across.
12
import { assert, describe, it } from "@effect/vitest";
3+
import * as NodeCrypto from "node:crypto";
4+
import * as NodeFS from "node:fs";
5+
import * as NodeOS from "node:os";
6+
import * as NodePath from "node:path";
7+
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
28
import * as Effect from "effect/Effect";
3-
import { beforeEach, vi } from "vite-plus/test";
9+
import { afterEach, beforeEach, vi } from "vite-plus/test";
410

511
const { autoUpdaterMock } = vi.hoisted(() => ({
612
autoUpdaterMock: {
@@ -11,6 +17,18 @@ const { autoUpdaterMock } = vi.hoisted(() => ({
1117
channel: "latest",
1218
disableDifferentialDownload: false,
1319
fullChangelog: false,
20+
downloadedUpdateHelper: null as null | {
21+
readonly file: string | null;
22+
readonly downloadedFileInfo: {
23+
readonly fileName: string;
24+
readonly sha512: string;
25+
} | null;
26+
},
27+
logger: null as null | {
28+
readonly info: (...args: ReadonlyArray<unknown>) => void;
29+
readonly warn: (...args: ReadonlyArray<unknown>) => void;
30+
readonly error: (...args: ReadonlyArray<unknown>) => void;
31+
},
1432
checkForUpdates: vi.fn(() => Promise.resolve(null)),
1533
downloadUpdate: vi.fn(() => Promise.resolve([])),
1634
on: vi.fn(),
@@ -20,10 +38,43 @@ const { autoUpdaterMock } = vi.hoisted(() => ({
2038
},
2139
}));
2240

41+
const {
42+
appQuitMock,
43+
autoUpdaterEmitMock,
44+
execFileSyncMock,
45+
loggerErrorMock,
46+
loggerInfoMock,
47+
loggerWarnMock,
48+
spawnMock,
49+
spawnOnMock,
50+
spawnUnrefMock,
51+
} = vi.hoisted(() => ({
52+
appQuitMock: vi.fn(),
53+
autoUpdaterEmitMock: vi.fn(),
54+
execFileSyncMock: vi.fn(),
55+
loggerErrorMock: vi.fn(),
56+
loggerInfoMock: vi.fn(),
57+
loggerWarnMock: vi.fn(),
58+
spawnMock: vi.fn(() => ({ pid: 4242, on: spawnOnMock, unref: spawnUnrefMock })),
59+
spawnOnMock: vi.fn(),
60+
spawnUnrefMock: vi.fn(),
61+
}));
62+
2363
vi.mock("electron-updater", () => ({
2464
autoUpdater: autoUpdaterMock,
2565
}));
2666

67+
vi.mock("electron", () => ({
68+
app: { quit: appQuitMock },
69+
autoUpdater: { emit: autoUpdaterEmitMock },
70+
}));
71+
72+
vi.mock("node:child_process", async (importOriginal) => ({
73+
...(await importOriginal<typeof import("node:child_process")>()),
74+
spawn: spawnMock,
75+
execFileSync: execFileSyncMock,
76+
}));
77+
2778
import * as ElectronUpdater from "./ElectronUpdater.ts";
2879

2980
describe("ElectronUpdater", () => {
@@ -139,4 +190,257 @@ describe("ElectronUpdater", () => {
139190
assert.deepEqual(autoUpdaterMock.quitAndInstall.mock.calls, [[true, false]]);
140191
}).pipe(Effect.provide(ElectronUpdater.layer)),
141192
);
193+
194+
describe("safe AppImage install", () => {
195+
const ambientAppImage = process.env.APPIMAGE;
196+
let tempDirs: string[] = [];
197+
198+
const trackTempDir = () => {
199+
const dir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "safe-appimage-install-"));
200+
tempDirs.push(dir);
201+
return dir;
202+
};
203+
204+
const sha512Base64 = (contents: string) =>
205+
NodeCrypto.createHash("sha512").update(contents, "utf8").digest("base64");
206+
207+
const flushImmediate = () =>
208+
Effect.promise(() => new Promise<void>((resolve) => setImmediate(resolve)));
209+
210+
beforeEach(() => {
211+
tempDirs = [];
212+
delete process.env.APPIMAGE;
213+
autoUpdaterMock.downloadedUpdateHelper = null;
214+
autoUpdaterMock.logger = {
215+
info: loggerInfoMock,
216+
warn: loggerWarnMock,
217+
error: loggerErrorMock,
218+
};
219+
appQuitMock.mockClear();
220+
autoUpdaterEmitMock.mockClear();
221+
execFileSyncMock.mockClear();
222+
loggerErrorMock.mockClear();
223+
loggerInfoMock.mockClear();
224+
loggerWarnMock.mockClear();
225+
spawnMock.mockClear();
226+
spawnOnMock.mockClear();
227+
spawnUnrefMock.mockClear();
228+
});
229+
230+
afterEach(() => {
231+
if (ambientAppImage === undefined) {
232+
delete process.env.APPIMAGE;
233+
} else {
234+
process.env.APPIMAGE = ambientAppImage;
235+
}
236+
for (const dir of tempDirs) {
237+
NodeFS.rmSync(dir, { recursive: true, force: true });
238+
}
239+
tempDirs = [];
240+
});
241+
242+
it.effect("refuses to install when the downloaded AppImage is empty", () =>
243+
Effect.gen(function* () {
244+
const dir = trackTempDir();
245+
const appImage = NodePath.join(dir, "T3-Code-x86_64.AppImage");
246+
const installer = NodePath.join(dir, "pending.AppImage");
247+
NodeFS.writeFileSync(appImage, "running-binary");
248+
// The exact #10685 symptom: a 0-byte download where a verified artifact was expected.
249+
NodeFS.writeFileSync(installer, "");
250+
process.env.APPIMAGE = appImage;
251+
autoUpdaterMock.downloadedUpdateHelper = {
252+
file: installer,
253+
downloadedFileInfo: {
254+
fileName: "T3-Code-x86_64.AppImage",
255+
sha512: sha512Base64("expected-binary"),
256+
},
257+
};
258+
259+
const updater = yield* ElectronUpdater.ElectronUpdater;
260+
const error = yield* updater
261+
.quitAndInstall({ isSilent: true, isForceRunAfter: true })
262+
.pipe(Effect.flip);
263+
264+
assert.instanceOf(error, ElectronUpdater.ElectronUpdaterQuitAndInstallError);
265+
assert.equal(autoUpdaterMock.quitAndInstall.mock.calls.length, 0);
266+
assert.equal(appQuitMock.mock.calls.length, 0);
267+
assert.equal(NodeFS.readFileSync(appImage, "utf8"), "running-binary");
268+
assert.deepEqual(
269+
NodeFS.readdirSync(dir).filter((entry) => entry.startsWith(".")),
270+
[],
271+
);
272+
}).pipe(
273+
Effect.provide(ElectronUpdater.layer),
274+
Effect.provideService(HostProcessPlatform, "linux"),
275+
),
276+
);
277+
278+
it.effect("refuses to install when the installer hash does not match", () =>
279+
Effect.gen(function* () {
280+
const dir = trackTempDir();
281+
const appImage = NodePath.join(dir, "T3-Code-x86_64.AppImage");
282+
const installer = NodePath.join(dir, "pending.AppImage");
283+
NodeFS.writeFileSync(appImage, "running-binary");
284+
NodeFS.writeFileSync(installer, "corrupted-binary");
285+
process.env.APPIMAGE = appImage;
286+
autoUpdaterMock.downloadedUpdateHelper = {
287+
file: installer,
288+
downloadedFileInfo: {
289+
fileName: "T3-Code-x86_64.AppImage",
290+
sha512: sha512Base64("expected-binary"),
291+
},
292+
};
293+
294+
const updater = yield* ElectronUpdater.ElectronUpdater;
295+
const error = yield* updater
296+
.quitAndInstall({ isSilent: true, isForceRunAfter: true })
297+
.pipe(Effect.flip);
298+
299+
assert.instanceOf(error, ElectronUpdater.ElectronUpdaterQuitAndInstallError);
300+
assert.equal(autoUpdaterMock.quitAndInstall.mock.calls.length, 0);
301+
assert.equal(appQuitMock.mock.calls.length, 0);
302+
assert.equal(NodeFS.readFileSync(appImage, "utf8"), "running-binary");
303+
}).pipe(
304+
Effect.provide(ElectronUpdater.layer),
305+
Effect.provideService(HostProcessPlatform, "linux"),
306+
),
307+
);
308+
309+
it.effect("refuses to install when no update was downloaded", () =>
310+
Effect.gen(function* () {
311+
const dir = trackTempDir();
312+
const appImage = NodePath.join(dir, "T3-Code-x86_64.AppImage");
313+
NodeFS.writeFileSync(appImage, "running-binary");
314+
process.env.APPIMAGE = appImage;
315+
autoUpdaterMock.downloadedUpdateHelper = null;
316+
317+
const updater = yield* ElectronUpdater.ElectronUpdater;
318+
const error = yield* updater
319+
.quitAndInstall({ isSilent: true, isForceRunAfter: true })
320+
.pipe(Effect.flip);
321+
322+
assert.instanceOf(error, ElectronUpdater.ElectronUpdaterQuitAndInstallError);
323+
assert.equal(autoUpdaterMock.quitAndInstall.mock.calls.length, 0);
324+
assert.equal(appQuitMock.mock.calls.length, 0);
325+
}).pipe(
326+
Effect.provide(ElectronUpdater.layer),
327+
Effect.provideService(HostProcessPlatform, "linux"),
328+
),
329+
);
330+
331+
it.effect("installs atomically on the same filesystem and relaunches", () =>
332+
Effect.gen(function* () {
333+
const dir = trackTempDir();
334+
const appImage = NodePath.join(dir, "T3-Code-x86_64.AppImage");
335+
const installer = NodePath.join(dir, "pending.AppImage");
336+
NodeFS.writeFileSync(appImage, "running-binary");
337+
NodeFS.writeFileSync(installer, "new-binary");
338+
process.env.APPIMAGE = appImage;
339+
autoUpdaterMock.downloadedUpdateHelper = {
340+
file: installer,
341+
downloadedFileInfo: {
342+
fileName: "T3-Code-x86_64.AppImage",
343+
sha512: sha512Base64("new-binary"),
344+
},
345+
};
346+
347+
const updater = yield* ElectronUpdater.ElectronUpdater;
348+
yield* updater.quitAndInstall({ isSilent: true, isForceRunAfter: true });
349+
350+
assert.equal(NodeFS.readFileSync(appImage, "utf8"), "new-binary");
351+
assert.equal(NodeFS.statSync(appImage).mode & 0o777, 0o755);
352+
assert.deepEqual(
353+
NodeFS.readdirSync(dir).filter((entry) => entry.startsWith(".")),
354+
[],
355+
);
356+
assert.equal(autoUpdaterMock.quitAndInstall.mock.calls.length, 0);
357+
assert.equal(spawnMock.mock.calls.length, 1);
358+
const [command, args, options] = spawnMock.mock.calls[0] as unknown as [
359+
string,
360+
ReadonlyArray<string>,
361+
{ readonly detached: boolean; readonly env: Record<string, string | undefined> },
362+
];
363+
assert.equal(command, appImage);
364+
assert.deepEqual(args, []);
365+
assert.equal(options.detached, true);
366+
assert.equal(options.env.APPIMAGE_SILENT_INSTALL, "true");
367+
assert.equal(spawnUnrefMock.mock.calls.length, 1);
368+
assert.isAbove(loggerInfoMock.mock.calls.length, 0);
369+
370+
yield* flushImmediate();
371+
assert.deepEqual(autoUpdaterEmitMock.mock.calls, [["before-quit-for-update"]]);
372+
assert.equal(appQuitMock.mock.calls.length, 1);
373+
}).pipe(
374+
Effect.provide(ElectronUpdater.layer),
375+
Effect.provideService(HostProcessPlatform, "linux"),
376+
),
377+
);
378+
379+
it.effect("runs the installer in place when relaunch after install is disabled", () =>
380+
Effect.gen(function* () {
381+
const dir = trackTempDir();
382+
const appImage = NodePath.join(dir, "T3-Code-x86_64.AppImage");
383+
const installer = NodePath.join(dir, "pending.AppImage");
384+
NodeFS.writeFileSync(appImage, "running-binary");
385+
NodeFS.writeFileSync(installer, "new-binary");
386+
process.env.APPIMAGE = appImage;
387+
autoUpdaterMock.downloadedUpdateHelper = {
388+
file: installer,
389+
downloadedFileInfo: {
390+
fileName: "T3-Code-x86_64.AppImage",
391+
sha512: sha512Base64("new-binary"),
392+
},
393+
};
394+
395+
const updater = yield* ElectronUpdater.ElectronUpdater;
396+
yield* updater.quitAndInstall({ isSilent: true, isForceRunAfter: false });
397+
398+
assert.equal(NodeFS.readFileSync(appImage, "utf8"), "new-binary");
399+
assert.equal(spawnMock.mock.calls.length, 0);
400+
assert.equal(execFileSyncMock.mock.calls.length, 1);
401+
const [command, args, options] = execFileSyncMock.mock.calls[0] as unknown as [
402+
string,
403+
ReadonlyArray<string>,
404+
{ readonly env: Record<string, string | undefined> },
405+
];
406+
assert.equal(command, appImage);
407+
assert.deepEqual(args, []);
408+
assert.equal(options.env.APPIMAGE_EXIT_AFTER_INSTALL, "true");
409+
410+
yield* flushImmediate();
411+
assert.equal(appQuitMock.mock.calls.length, 1);
412+
}).pipe(
413+
Effect.provide(ElectronUpdater.layer),
414+
Effect.provideService(HostProcessPlatform, "linux"),
415+
),
416+
);
417+
418+
it.effect("delegates to stock quitAndInstall when APPIMAGE is not set", () =>
419+
Effect.gen(function* () {
420+
const updater = yield* ElectronUpdater.ElectronUpdater;
421+
422+
yield* updater.quitAndInstall({ isSilent: true, isForceRunAfter: true });
423+
424+
assert.deepEqual(autoUpdaterMock.quitAndInstall.mock.calls, [[true, true]]);
425+
assert.equal(appQuitMock.mock.calls.length, 0);
426+
}).pipe(
427+
Effect.provide(ElectronUpdater.layer),
428+
Effect.provideService(HostProcessPlatform, "linux"),
429+
),
430+
);
431+
432+
it.effect("wires the electron-updater logger", () =>
433+
Effect.gen(function* () {
434+
const updater = yield* ElectronUpdater.ElectronUpdater;
435+
const logger = { info: () => {}, warn: () => {}, error: () => {} };
436+
437+
yield* updater.setLogger(logger);
438+
439+
assert.strictEqual(autoUpdaterMock.logger, logger);
440+
}).pipe(
441+
Effect.provide(ElectronUpdater.layer),
442+
Effect.provideService(HostProcessPlatform, "linux"),
443+
),
444+
);
445+
});
142446
});

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.