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

DesktopAppIdentity.test.ts

242 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 setAsDefaultProtocolClient: () => Effect.succeed(true),
61 setDesktopName: () => Effect.void,
62 setDockIcon: (iconPath) =>
63 Effect.sync(() => {
64 calls.setDockIcon.push(iconPath);
65 }),
66 appendCommandLineSwitch: () => Effect.void,
67 onBeforeQuitForUpdate: () => Effect.void,
68 removeCommandLineSwitch: () => Effect.void,
69 on: () => Effect.void,
70 } satisfies ElectronApp.ElectronApp["Service"]);
71​
72const makeAssetsLayer = (png: Option.Option<string>) =>
73 Layer.succeed(DesktopAssets.DesktopAssets, {
74 iconPaths: Effect.succeed({
75 ico: Option.none(),
76 icns: Option.none(),
77 png,
78 }),
79 resolveResourcePath: () => Effect.succeed(Option.none()),
80 } satisfies DesktopAssets.DesktopAssets["Service"]);
81​
82const makeEnvironmentLayer = (overrides: TestEnvironmentInput = {}) => {
83 const { env, ...environmentOverrides } = overrides;
84 return DesktopEnvironment.layer({
85 ...defaultEnvironmentInput,
86 ...environmentOverrides,
87 }).pipe(
88 Layer.provide(
89 Layer.mergeAll(
90 NodeServices.layer,
91 NodePath.layerPosix,
92 DesktopConfig.layerTest({
93 ...env,
94 }),
95 ),
96 ),
97 );
98};
99​
100const withIdentity = <A, E, R>(
101 effect: Effect.Effect<
102 A,
103 E,
104 | R
105 | DesktopAppIdentity.DesktopAppIdentity
106 | DesktopEnvironment.DesktopEnvironment
107 | FileSystem.FileSystem
108 >,
109 input: {
110 readonly calls?: ElectronAppCalls;
111 readonly environment?: TestEnvironmentInput;
112 readonly legacyPathExists?: boolean;
113 readonly legacyPathProbeError?: PlatformError.PlatformError;
114 readonly packageJson?: string;
115 readonly pngIconPath?: Option.Option<string>;
116 } = {},
117) => {
118 const calls: ElectronAppCalls = input.calls ?? {
119 setAboutPanelOptions: [],
120 setDockIcon: [],
121 setName: [],
122 };
123​
124 return effect.pipe(
125 Effect.provide(
126 DesktopAppIdentity.layer.pipe(
127 Layer.provideMerge(
128 FileSystem.layerNoop({
129 exists: (path) =>
130 input.legacyPathProbeError
131 ? Effect.fail(input.legacyPathProbeError)
132 : Effect.succeed(
133 input.legacyPathExists === true && path.includes("T3 Code (Alpha)"),
134 ),
135 readFileString: () =>
136 Effect.succeed(input.packageJson ?? '{"t3codeCommitHash":"abcdef1234567890"}'),
137 }),
138 ),
139 Layer.provideMerge(makeAssetsLayer(input.pngIconPath ?? Option.none())),
140 Layer.provideMerge(makeElectronAppLayer(calls)),
141 Layer.provideMerge(makeEnvironmentLayer(input.environment)),
142 ),
143 ),
144 );
145};
146​
147describe("DesktopAppIdentity", () => {
148 it.effect("keeps using the legacy userData path when it already exists", () =>
149 withIdentity(
150 Effect.gen(function* () {
151 const identity = yield* DesktopAppIdentity.DesktopAppIdentity;
152 const userDataPath = yield* identity.resolveUserDataPath;
153​
154 assert.equal(userDataPath, "/Users/alice/Library/Application Support/T3 Code (Alpha)");
155 }),
156 { legacyPathExists: true },
157 ),
158 );
159​
160 it.effect("preserves failures while inspecting the legacy userData path", () => {
161 const legacyPath = "/Users/alice/Library/Application Support/T3 Code (Alpha)";
162 const cause = PlatformError.systemError({
163 _tag: "PermissionDenied",
164 module: "FileSystem",
165 method: "exists",
166 description: "permission denied",
167 pathOrDescriptor: legacyPath,
168 });
169​
170 return withIdentity(
171 Effect.gen(function* () {
172 const identity = yield* DesktopAppIdentity.DesktopAppIdentity;
173 const error = yield* identity.resolveUserDataPath.pipe(Effect.flip);
174​
175 assert.instanceOf(error, DesktopAppIdentity.DesktopUserDataPathResolutionError);
176 assert.equal(error.legacyPath, legacyPath);
177 assert.strictEqual(error.cause, cause);
178 assert.equal(
179 error.message,
180 `Failed to inspect legacy desktop user-data path at "${legacyPath}".`,
181 );
182 }),
183 { legacyPathProbeError: cause },
184 );
185 });
186​
187 it.effect("configures app identity from the environment commit override", () => {
188 const calls: ElectronAppCalls = {
189 setAboutPanelOptions: [],
190 setDockIcon: [],
191 setName: [],
192 };
193​
194 return withIdentity(
195 Effect.gen(function* () {
196 const identity = yield* DesktopAppIdentity.DesktopAppIdentity;
197 yield* identity.configure;
198​
199 assert.deepEqual(calls.setName, ["T3 Code (Alpha)"]);
200 assert.equal(calls.setAboutPanelOptions[0]?.applicationName, "T3 Code (Alpha)");
201 assert.equal(calls.setAboutPanelOptions[0]?.applicationVersion, "1.2.3");
202 assert.equal(calls.setAboutPanelOptions[0]?.version, "0123456789ab");
203 // Packaged: the bundle's own icon stands, so a custom one the user
204 // attached survives.
205 assert.deepEqual(calls.setDockIcon, []);
206 }),
207 {
208 calls,
209 environment: {
210 env: {
211 T3CODE_COMMIT_HASH: "0123456789abcdef",
212 },
213 },
214 pngIconPath: Option.some("/icon.png"),
215 },
216 );
217 });
218​
219 it.effect("sets the dock icon only when running unpackaged", () => {
220 const calls: ElectronAppCalls = {
221 setAboutPanelOptions: [],
222 setDockIcon: [],
223 setName: [],
224 };
225​
226 return withIdentity(
227 Effect.gen(function* () {
228 const identity = yield* DesktopAppIdentity.DesktopAppIdentity;
229 yield* identity.configure;
230​
231 // Electron shows a generic icon for an unpackaged run, which is the
232 // reason this call exists at all.
233 assert.deepEqual(calls.setDockIcon, ["/icon.png"]);
234 }),
235 {
236 calls,
237 environment: { isPackaged: false },
238 pngIconPath: Option.some("/icon.png"),
239 },
240 );
241 });
242});