Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import * as Effect from "effect/Effect";
4import * as FileSystem from "effect/FileSystem";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7import * as PlatformError from "effect/PlatformError";
8import * as Path from "effect/Path";
9
10import * as DesktopConfig from "./DesktopConfig.ts";
11import * as DesktopEnvironment from "./DesktopEnvironment.ts";
12import * as DesktopInstallIntegrity from "./DesktopInstallIntegrity.ts";
13
14const makeEnvironmentLayer = (input: {
15 readonly resourcesPath: string;
16 readonly isPackaged: boolean;
17 readonly homeDirectory: string;
18 readonly platform?: NodeJS.Platform;
19}) =>
20 DesktopEnvironment.layer({
21 dirname: "/repo/apps/desktop/src",
22 homeDirectory: input.homeDirectory,
23 platform: input.platform ?? "win32",
24 processArch: "x64",
25 appVersion: "1.2.3",
26 appPath: "/repo",
27 isPackaged: input.isPackaged,
28 resourcesPath: input.resourcesPath,
29 runningUnderArm64Translation: false,
30 }).pipe(
31 Layer.provide(
32 Layer.mergeAll(
33 NodeServices.layer,
34 DesktopConfig.layerTest({
35 T3CODE_HOME: input.homeDirectory,
36 }),
37 ),
38 ),
39 );
40
41const withInstallDir = <A, E, R>(
42 build: (input: {
43 readonly resourcesPath: string;
44 readonly manifestPath: string;
45 }) => Effect.Effect<A, E, R>,
46) =>
47 Effect.gen(function* () {
48 const fileSystem = yield* FileSystem.FileSystem;
49 const path = yield* Path.Path;
50 const baseDir = yield* fileSystem.makeTempDirectoryScoped({
51 prefix: "t3-desktop-install-integrity-test-",
52 });
53 const resourcesPath = path.join(baseDir, "resources");
54 const manifestPath = path.join(
55 resourcesPath,
56 ...DesktopInstallIntegrity.DESKTOP_BUILD_MANIFEST_RELATIVE_PATH.split("/"),
57 );
58 yield* fileSystem.makeDirectory(path.dirname(manifestPath), { recursive: true });
59 return yield* build({ resourcesPath, manifestPath });
60 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer));
61
62describe("DesktopInstallIntegrity", () => {
63 it.effect("accepts an install whose unpacked build stamp matches the asar version", () =>
64 withInstallDir(({ resourcesPath, manifestPath }) =>
65 Effect.gen(function* () {
66 const fileSystem = yield* FileSystem.FileSystem;
67 yield* fileSystem.writeFileString(manifestPath, '{"version":"1.2.3"}\n');
68
69 const result = yield* DesktopInstallIntegrity.checkInstallIntegrity.pipe(
70 Effect.provide(
71 makeEnvironmentLayer({
72 resourcesPath,
73 isPackaged: true,
74 homeDirectory: `/tmp/t3-integrity-home-${process.pid}`,
75 }),
76 ),
77 );
78 assert.equal(result._tag, "Ok");
79 }),
80 ),
81 );
82
83 it.effect("flags a half-applied update whose unpacked files came from a different build", () =>
84 withInstallDir(({ resourcesPath, manifestPath }) =>
85 Effect.gen(function* () {
86 const fileSystem = yield* FileSystem.FileSystem;
87 // The observed corruption: app.asar (and its version) stayed at the
88 // old build while app.asar.unpacked was replaced by the new one.
89 yield* fileSystem.writeFileString(manifestPath, '{"version":"1.2.4-nightly.1"}\n');
90
91 const result = yield* DesktopInstallIntegrity.checkInstallIntegrity.pipe(
92 Effect.provide(
93 makeEnvironmentLayer({
94 resourcesPath,
95 isPackaged: true,
96 homeDirectory: `/tmp/t3-integrity-home-${process.pid}`,
97 }),
98 ),
99 );
100 assert.equal(result._tag, "Mismatch");
101 if (result._tag === "Mismatch") {
102 assert.equal(result.appVersion, "1.2.3");
103 assert.equal(result.unpackedVersion, "1.2.4-nightly.1");
104 }
105 }),
106 ),
107 );
108
109 it.effect("treats a missing manifest on packaged Windows as a half-applied update", () =>
110 withInstallDir(({ resourcesPath }) =>
111 Effect.gen(function* () {
112 // New app.asar (which ships this checker AND the stamp in the same
113 // build) with a pre-stamp unpacked tree = partial apply. Skipping
114 // here would launch straight into the mixed-build crash.
115 const result = yield* DesktopInstallIntegrity.checkInstallIntegrity.pipe(
116 Effect.provide(
117 makeEnvironmentLayer({
118 resourcesPath,
119 isPackaged: true,
120 homeDirectory: `/tmp/t3-integrity-home-${process.pid}`,
121 }),
122 ),
123 );
124 assert.equal(result._tag, "MissingManifest");
125 }),
126 ),
127 );
128
129 it.effect("skips a missing manifest on platforms that pack the server dist inside the asar", () =>
130 withInstallDir(({ resourcesPath }) =>
131 Effect.gen(function* () {
132 const result = yield* DesktopInstallIntegrity.checkInstallIntegrity.pipe(
133 Effect.provide(
134 makeEnvironmentLayer({
135 resourcesPath,
136 isPackaged: true,
137 platform: "darwin",
138 homeDirectory: `/tmp/t3-integrity-home-${process.pid}`,
139 }),
140 ),
141 );
142 assert.equal(result._tag, "Skipped");
143 }),
144 ),
145 );
146
147 it.effect("skips an unreadable (but present) manifest instead of blocking launch", () =>
148 withInstallDir(({ resourcesPath }) =>
149 Effect.gen(function* () {
150 // Access denied / I/O error is not evidence of a partial apply; a
151 // false MissingManifest here would brick every launch of a healthy
152 // install.
153 const failingFs = FileSystem.layerNoop({
154 readFileString: () =>
155 Effect.fail(
156 PlatformError.systemError({
157 _tag: "PermissionDenied",
158 module: "FileSystem",
159 method: "readFileString",
160 pathOrDescriptor: "desktop-build-manifest.json",
161 description: "EACCES",
162 }),
163 ),
164 });
165 const result = yield* DesktopInstallIntegrity.checkInstallIntegrity.pipe(
166 Effect.provide(
167 Layer.mergeAll(
168 makeEnvironmentLayer({
169 resourcesPath,
170 isPackaged: true,
171 homeDirectory: `/tmp/t3-integrity-home-${process.pid}`,
172 }),
173 failingFs,
174 ),
175 ),
176 );
177 assert.equal(result._tag, "Skipped");
178 }),
179 ),
180 );
181
182 it.effect("skips undecodable manifests instead of blocking launch", () =>
183 withInstallDir(({ resourcesPath, manifestPath }) =>
184 Effect.gen(function* () {
185 const fileSystem = yield* FileSystem.FileSystem;
186 yield* fileSystem.writeFileString(manifestPath, "not json");
187
188 const result = yield* DesktopInstallIntegrity.checkInstallIntegrity.pipe(
189 Effect.provide(
190 makeEnvironmentLayer({
191 resourcesPath,
192 isPackaged: true,
193 homeDirectory: `/tmp/t3-integrity-home-${process.pid}`,
194 }),
195 ),
196 );
197 assert.equal(result._tag, "Skipped");
198 }),
199 ),
200 );
201
202 it.effect("resolves the releases page from the packaged update feed", () =>
203 withInstallDir(({ resourcesPath }) =>
204 Effect.gen(function* () {
205 const fileSystem = yield* FileSystem.FileSystem;
206 const environmentLayer = makeEnvironmentLayer({
207 resourcesPath,
208 isPackaged: true,
209 homeDirectory: `/tmp/t3-integrity-home-${process.pid}`,
210 });
211 const appUpdateYmlPath = yield* DesktopEnvironment.DesktopEnvironment.pipe(
212 Effect.map((environment) => environment.appUpdateYmlPath),
213 Effect.provide(environmentLayer),
214 );
215 yield* fileSystem.makeDirectory(
216 appUpdateYmlPath.slice(0, appUpdateYmlPath.lastIndexOf("/")),
217 { recursive: true },
218 );
219 yield* fileSystem.writeFileString(
220 appUpdateYmlPath,
221 "provider: github\nowner: pingdotgg\nrepo: t3code\n",
222 );
223
224 const url = yield* DesktopInstallIntegrity.resolveDownloadPageUrl.pipe(
225 Effect.provide(environmentLayer),
226 );
227 assert.deepEqual(url, Option.some("https://github.com/pingdotgg/t3code/releases"));
228 }),
229 ),
230 );
231});