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

DesktopAppIdentity.test.ts

243 lines7.8 KB

1import * as NodePath from "@effect/platform-node/NodePath";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { assert, describe, it } from "@effect/vitest";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Layer from "effect/Layer";
7import * as Option from "effect/Option";
8import * as PlatformError from "effect/PlatformError";
9​
10import type * as Electron from "electron";
11​
12import * as ElectronApp from "../electron/ElectronApp.ts";
13import * as DesktopAppIdentity from "./DesktopAppIdentity.ts";
14import * as DesktopAssets from "./DesktopAssets.ts";
15import * as DesktopConfig from "./DesktopConfig.ts";
16import * as DesktopEnvironment from "./DesktopEnvironment.ts";
17​
18const defaultEnvironmentInput = {
19 dirname: "/repo/apps/desktop/dist-electron",
20 homeDirectory: "/Users/alice",
21 platform: "darwin",
22 processArch: "arm64",
23 appVersion: "1.2.3",
24 appPath: "/Applications/T3 Code.app/Contents/Resources/app.asar",
25 isPackaged: true,
26 resourcesPath: "/Applications/T3 Code.app/Contents/Resources",
27 runningUnderArm64Translation: false,
28} satisfies DesktopEnvironment.MakeDesktopEnvironmentInput;
29​
30type TestEnvironmentInput = Partial<DesktopEnvironment.MakeDesktopEnvironmentInput> & {
31 readonly env?: Record<string, string | undefined>;
32};
33​
34interface ElectronAppCalls {
35 readonly setAboutPanelOptions: Array<Electron.AboutPanelOptionsOptions>;
36 readonly setDockIcon: string[];
37 readonly setName: string[];
39​
40const makeElectronAppLayer = (calls: ElectronAppCalls) =>
41 Layer.succeed(ElectronApp.ElectronApp, {
42 metadata: Effect.die("unexpected metadata read"),
43 name: Effect.succeed("T3 Code"),
44 systemLocale: Effect.succeed("en-US"),
45 whenReady: Effect.void,
46 quit: Effect.void,
47 exit: () => Effect.void,
48 relaunch: () => Effect.void,
49 setPath: () => Effect.void,
50 setName: (name) =>
51 Effect.sync(() => {
52 calls.setName.push(name);
53 }),
54 setAboutPanelOptions: (options) =>
55 Effect.sync(() => {
56 calls.setAboutPanelOptions.push(options);
57 }),
58 setAppUserModelId: () => Effect.void,
59 getAppMetrics: Effect.succeed([]),
60 isDefaultProtocolClient: () => Effect.succeed(false),
61 setAsDefaultProtocolClient: () => Effect.succeed(true),
62 setDesktopName: () => Effect.void,
63 setDockIcon: (iconPath) =>
64 Effect.sync(() => {
65 calls.setDockIcon.push(iconPath);
66 }),
67 appendCommandLineSwitch: () => Effect.void,
68 onBeforeQuitForUpdate: () => Effect.void,
69 removeCommandLineSwitch: () => Effect.void,
70 on: () => Effect.void,
71 } satisfies ElectronApp.ElectronApp["Service"]);
72​
73const makeAssetsLayer = (png: Option.Option<string>) =>
74 Layer.succeed(DesktopAssets.DesktopAssets, {
75 iconPaths: Effect.succeed({
76 ico: Option.none(),
77 icns: Option.none(),
78 png,
79 }),
80 resolveResourcePath: () => Effect.succeed(Option.none()),
81 } satisfies DesktopAssets.DesktopAssets["Service"]);
82​
83const makeEnvironmentLayer = (overrides: TestEnvironmentInput = {}) => {
84 const { env, ...environmentOverrides } = overrides;
85 return DesktopEnvironment.layer({
86 ...defaultEnvironmentInput,
87 ...environmentOverrides,
88 }).pipe(
89 Layer.provide(
90 Layer.mergeAll(
91 NodeServices.layer,
92 NodePath.layerPosix,
93 DesktopConfig.layerTest({
94 ...env,
95 }),
96 ),
97 ),
98 );
99};
100​
101const withIdentity = <A, E, R>(
102 effect: Effect.Effect<
103 A,
104 E,
105 | R
106 | DesktopAppIdentity.DesktopAppIdentity
107 | DesktopEnvironment.DesktopEnvironment
108 | FileSystem.FileSystem
109 >,
110 input: {
111 readonly calls?: ElectronAppCalls;
112 readonly environment?: TestEnvironmentInput;
113 readonly legacyPathExists?: boolean;
114 readonly legacyPathProbeError?: PlatformError.PlatformError;
115 readonly packageJson?: string;
116 readonly pngIconPath?: Option.Option<string>;
117 } = {},
118) => {
119 const calls: ElectronAppCalls = input.calls ?? {
120 setAboutPanelOptions: [],
121 setDockIcon: [],
122 setName: [],
123 };
124​
125 return effect.pipe(
126 Effect.provide(
127 DesktopAppIdentity.layer.pipe(
128 Layer.provideMerge(
129 FileSystem.layerNoop({
130 exists: (path) =>
131 input.legacyPathProbeError
132 ? Effect.fail(input.legacyPathProbeError)
133 : Effect.succeed(
134 input.legacyPathExists === true && path.includes("T3 Code (Alpha)"),
135 ),
136 readFileString: () =>
137 Effect.succeed(input.packageJson ?? '{"t3codeCommitHash":"abcdef1234567890"}'),
138 }),
139 ),
140 Layer.provideMerge(makeAssetsLayer(input.pngIconPath ?? Option.none())),
141 Layer.provideMerge(makeElectronAppLayer(calls)),
142 Layer.provideMerge(makeEnvironmentLayer(input.environment)),
143 ),
144 ),
145 );
146};
147​
148describe("DesktopAppIdentity", () => {
149 it.effect("keeps using the legacy userData path when it already exists", () =>
150 withIdentity(
151 Effect.gen(function* () {
152 const identity = yield* DesktopAppIdentity.DesktopAppIdentity;
153 const userDataPath = yield* identity.resolveUserDataPath;
154​
155 assert.equal(userDataPath, "/Users/alice/Library/Application Support/T3 Code (Alpha)");
156 }),
157 { legacyPathExists: true },
158 ),
159 );
160​
161 it.effect("preserves failures while inspecting the legacy userData path", () => {
162 const legacyPath = "/Users/alice/Library/Application Support/T3 Code (Alpha)";
163 const cause = PlatformError.systemError({
164 _tag: "PermissionDenied",
165 module: "FileSystem",
166 method: "exists",
167 description: "permission denied",
168 pathOrDescriptor: legacyPath,
169 });
170​
171 return withIdentity(
172 Effect.gen(function* () {
173 const identity = yield* DesktopAppIdentity.DesktopAppIdentity;
174 const error = yield* identity.resolveUserDataPath.pipe(Effect.flip);
175​
176 assert.instanceOf(error, DesktopAppIdentity.DesktopUserDataPathResolutionError);
177 assert.equal(error.legacyPath, legacyPath);
178 assert.strictEqual(error.cause, cause);
179 assert.equal(
180 error.message,
181 `Failed to inspect legacy desktop user-data path at "${legacyPath}".`,
182 );
183 }),
184 { legacyPathProbeError: cause },
185 );
186 });
187​
188 it.effect("configures app identity from the environment commit override", () => {
189 const calls: ElectronAppCalls = {
190 setAboutPanelOptions: [],
191 setDockIcon: [],
192 setName: [],
193 };
194​
195 return withIdentity(
196 Effect.gen(function* () {
197 const identity = yield* DesktopAppIdentity.DesktopAppIdentity;
198 yield* identity.configure;
199​
200 assert.deepEqual(calls.setName, ["T3 Code (Alpha)"]);
201 assert.equal(calls.setAboutPanelOptions[0]?.applicationName, "T3 Code (Alpha)");
202 assert.equal(calls.setAboutPanelOptions[0]?.applicationVersion, "1.2.3");
203 assert.equal(calls.setAboutPanelOptions[0]?.version, "0123456789ab");
204 // Packaged: the bundle's own icon stands, so a custom one the user
205 // attached survives.
206 assert.deepEqual(calls.setDockIcon, []);
207 }),
208 {
209 calls,
210 environment: {
211 env: {
212 T3CODE_COMMIT_HASH: "0123456789abcdef",
213 },
214 },
215 pngIconPath: Option.some("/icon.png"),
216 },
217 );
218 });
219​
220 it.effect("sets the dock icon only when running unpackaged", () => {
221 const calls: ElectronAppCalls = {
222 setAboutPanelOptions: [],
223 setDockIcon: [],
224 setName: [],
225 };
226​
227 return withIdentity(
228 Effect.gen(function* () {
229 const identity = yield* DesktopAppIdentity.DesktopAppIdentity;
230 yield* identity.configure;
231​
232 // Electron shows a generic icon for an unpackaged run, which is the
233 // reason this call exists at all.
234 assert.deepEqual(calls.setDockIcon, ["/icon.png"]);
235 }),
236 {
237 calls,
238 environment: { isPackaged: false },
239 pngIconPath: Option.some("/icon.png"),
240 },
241 );
242 });
243});