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