Open History to see the last commit
1// @effect-diagnostics nodeBuiltinImport:off - Exercises AppImage replacement on a real temporary filesystem.
2import * as NodeCrypto from "node:crypto";
3import * as NodeFS from "node:fs";
4import * as NodeOS from "node:os";
5import * as NodePath from "node:path";
6
7import { assert, describe, it } from "@effect/vitest";
8import * as HostProcess from "@t3tools/shared/HostProcess";
9import { afterEach, vi } from "vite-plus/test";
10
11vi.mock("node:fs", async (importOriginal) => {
12 const actual = await importOriginal<typeof NodeFS>();
13 return {
14 ...actual,
15 copyFileSync: vi.fn(actual.copyFileSync),
16 fsyncSync: vi.fn(actual.fsyncSync),
17 renameSync: vi.fn(actual.renameSync),
18 realpathSync: vi.fn(actual.realpathSync),
19 rmSync: vi.fn(actual.rmSync),
20 };
21});
22
23import { installAppImageUpdate } from "./AppImageInstall.ts";
24
25describe.skipIf(HostProcess.Platform.defaultValue() === "win32")("installAppImageUpdate", () => {
26 const oldAppImage = "#!/bin/sh\n# old build\nexit 0\n";
27 const newAppImage = `#!/bin/sh\n# new build\nexit 0\n#${"x".repeat(4096)}\n`;
28 const sha512 = (content: string) =>
29 NodeCrypto.createHash("sha512").update(content).digest("base64");
30 const directories: Array<string> = [];
31
32 function fixture(appImageName = "T3-Code.AppImage") {
33 const root = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-appimage-install-"));
34 directories.push(root);
35 const applications = NodePath.join(root, "Applications");
36 const pending = NodePath.join(root, "cache", "pending");
37 NodeFS.mkdirSync(applications);
38 NodeFS.mkdirSync(pending, { recursive: true });
39 const appImageFile = NodePath.join(applications, appImageName);
40 const installerPath = NodePath.join(pending, "T3-Code-1.2.3-x86_64.AppImage");
41 NodeFS.writeFileSync(appImageFile, oldAppImage, { mode: 0o755 });
42 NodeFS.writeFileSync(installerPath, newAppImage, { mode: 0o644 });
43 return { root, applications, appImageFile, installerPath };
44 }
45
46 function assertUntouched(input: ReturnType<typeof fixture>, cached = newAppImage) {
47 assert.equal(NodeFS.readFileSync(input.appImageFile, "utf8"), oldAppImage);
48 assert.equal(NodeFS.readFileSync(input.installerPath, "utf8"), cached);
49 assert.deepEqual(NodeFS.readdirSync(input.applications), [
50 NodePath.basename(input.appImageFile),
51 ]);
52 }
53
54 afterEach(() => {
55 vi.mocked(NodeFS.copyFileSync).mockReset();
56 vi.mocked(NodeFS.fsyncSync).mockReset();
57 vi.mocked(NodeFS.renameSync).mockReset();
58 vi.mocked(NodeFS.realpathSync).mockReset();
59 vi.mocked(NodeFS.rmSync).mockReset();
60 for (const directory of directories.splice(0)) {
61 NodeFS.rmSync(directory, { recursive: true, force: true });
62 }
63 });
64
65 it.each([
66 { installed: "T3-Code.AppImage", destination: "T3-Code.AppImage", releaseSize: undefined },
67 {
68 installed: "T3-Code-1.2.2-x86_64.AppImage",
69 destination: "T3-Code-1.2.3-x86_64.AppImage",
70 releaseSize: Buffer.byteLength(newAppImage),
71 },
72 ])("replaces $installed with a verified executable at $destination", (names) => {
73 const input = fixture(names.installed);
74 const copy = vi.mocked(NodeFS.copyFileSync).getMockImplementation()!;
75 vi.mocked(NodeFS.copyFileSync).mockImplementationOnce((from, to, mode) => {
76 assert.equal(NodeFS.readFileSync(input.appImageFile, "utf8"), oldAppImage);
77 const stageDirectory = NodePath.dirname(String(to));
78 assert.equal(NodePath.dirname(stageDirectory), input.applications);
79 assert.equal(NodeFS.statSync(stageDirectory).mode & 0o777, 0o700);
80 copy(from, to, mode);
81 });
82
83 const destination = installAppImageUpdate({
84 ...input,
85 sha512: sha512(newAppImage),
86 size: names.releaseSize,
87 });
88
89 assert.equal(destination, NodePath.join(input.applications, names.destination));
90 assert.equal(NodeFS.readFileSync(destination, "utf8"), newAppImage);
91 assert.equal(NodeFS.statSync(destination).mode & 0o777, 0o755);
92 assert.deepEqual(NodeFS.readdirSync(input.applications), [names.destination]);
93 assert.isFalse(NodeFS.existsSync(input.installerPath));
94 });
95
96 it("keeps the installed app and cached download when the update cannot be verified or committed", () => {
97 const cases: Array<{
98 source: string;
99 stage?: string;
100 digest: string;
101 size?: number;
102 rename?: boolean;
103 }> = [
104 { source: "", digest: sha512("") },
105 { source: newAppImage, stage: "short", digest: sha512("short") },
106 { source: newAppImage, digest: sha512(newAppImage), size: newAppImage.length + 1 },
107 {
108 source: newAppImage,
109 stage: newAppImage.replace("new", "bad"),
110 digest: sha512(newAppImage),
111 },
112 { source: newAppImage, digest: sha512(newAppImage), rename: true },
113 ];
114 for (const { source, stage, digest, size, rename } of cases) {
115 const input = fixture();
116 NodeFS.writeFileSync(input.installerPath, source);
117 if (stage !== undefined) {
118 vi.mocked(NodeFS.copyFileSync).mockImplementationOnce((_from, to) => {
119 NodeFS.writeFileSync(to, stage);
120 });
121 }
122 if (rename) {
123 vi.mocked(NodeFS.renameSync).mockImplementationOnce(() => {
124 throw new Error("cannot commit update");
125 });
126 }
127
128 assert.throws(() => installAppImageUpdate({ ...input, sha512: digest, size }));
129
130 assertUntouched(input, source);
131 }
132 });
133
134 it.each(["symlink", "mount alias"] as const)(
135 "keeps the committed destination when the cache reaches it through a %s",
136 (aliasKind) => {
137 const input = fixture("T3-Code-1.2.2-x86_64.AppImage");
138 const cacheDirectory = NodePath.join(input.root, "alias");
139 NodeFS.symlinkSync(input.applications, cacheDirectory);
140 const installerPath = NodePath.join(cacheDirectory, NodePath.basename(input.installerPath));
141 NodeFS.renameSync(input.installerPath, installerPath);
142 if (aliasKind === "mount alias") {
143 // Bind mounts share directory entries without realpath resolving their aliases.
144 vi.mocked(NodeFS.realpathSync).mockImplementation(((path: NodeFS.PathLike) =>
145 NodePath.resolve(String(path))) as typeof NodeFS.realpathSync);
146 }
147
148 const destination = installAppImageUpdate({
149 ...input,
150 installerPath,
151 sha512: sha512(newAppImage),
152 });
153
154 assert.equal(
155 destination,
156 NodePath.join(input.applications, NodePath.basename(installerPath)),
157 );
158 assert.equal(NodeFS.readFileSync(installerPath, "utf8"), newAppImage);
159 assert.deepEqual(NodeFS.readdirSync(input.applications), [NodePath.basename(installerPath)]);
160 },
161 );
162
163 it("still succeeds when removing the cached download fails after commit", () => {
164 const input = fixture();
165 const remove = vi.mocked(NodeFS.rmSync).getMockImplementation()!;
166 vi.mocked(NodeFS.rmSync).mockImplementation((path, options) => {
167 if (path === input.installerPath) throw new Error("cache cleanup failed");
168 remove(path, options);
169 });
170
171 const destination = installAppImageUpdate({ ...input, sha512: sha512(newAppImage) });
172
173 assert.equal(NodeFS.readFileSync(destination, "utf8"), newAppImage);
174 assert.deepEqual(NodeFS.readdirSync(input.applications), [NodePath.basename(destination)]);
175 });
176
177 it("keeps the old AppImage and cached download when the directory sync fails", () => {
178 const input = fixture("T3-Code-1.2.2-x86_64.AppImage");
179 const fsync = vi.mocked(NodeFS.fsyncSync).getMockImplementation()!;
180 vi.mocked(NodeFS.fsyncSync)
181 .mockImplementationOnce(fsync)
182 .mockImplementationOnce(() => {
183 throw new Error("EIO");
184 });
185
186 const destination = installAppImageUpdate({ ...input, sha512: sha512(newAppImage) });
187
188 assert.equal(NodeFS.readFileSync(destination, "utf8"), newAppImage);
189 assert.equal(NodeFS.readFileSync(input.appImageFile, "utf8"), oldAppImage);
190 assert.equal(NodeFS.readFileSync(input.installerPath, "utf8"), newAppImage);
191 });
192});