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 whenReady: Effect.void,
44 quit: Effect.void,
45 exit: () => Effect.void,
46 relaunch: () => Effect.void,
47 setPath: () => Effect.void,
48 setName: (name) =>
49 Effect.sync(() => {
50 calls.setName.push(name);
51 }),
52 setAboutPanelOptions: (options) =>
53 Effect.sync(() => {
54 calls.setAboutPanelOptions.push(options);
55 }),
56 setAppUserModelId: () => Effect.void,
57 requestSingleInstanceLock: Effect.succeed(true),
58 isDefaultProtocolClient: () => Effect.succeed(false),
59 setAsDefaultProtocolClient: () => Effect.succeed(true),
60 setDesktopName: () => Effect.void,
61 setDockIcon: (iconPath) =>
62 Effect.sync(() => {
63 calls.setDockIcon.push(iconPath);
64 }),
65 appendCommandLineSwitch: () => Effect.void,
66 on: () => Effect.void,
67 } satisfies ElectronApp.ElectronApp["Service"]);
68
69const makeAssetsLayer = (png: Option.Option<string>) =>
70 Layer.succeed(DesktopAssets.DesktopAssets, {
71 iconPaths: Effect.succeed({
72 ico: Option.none(),
73 icns: Option.none(),
74 png,
75 }),
76 resolveResourcePath: () => Effect.succeed(Option.none()),
77 } satisfies DesktopAssets.DesktopAssets["Service"]);
78
79const makeEnvironmentLayer = (overrides: TestEnvironmentInput = {}) => {
80 const { env, ...environmentOverrides } = overrides;
81 return DesktopEnvironment.layer({
82 ...defaultEnvironmentInput,
83 ...environmentOverrides,
84 }).pipe(
85 Layer.provide(
86 Layer.mergeAll(
87 NodeServices.layer,
88 DesktopConfig.layerTest({
89 ...env,
90 }),
91 ),
92 ),
93 );
94};
95
96const withIdentity = <A, E, R>(
97 effect: Effect.Effect<
98 A,
99 E,
100 | R
101 | DesktopAppIdentity.DesktopAppIdentity
102 | DesktopEnvironment.DesktopEnvironment
103 | FileSystem.FileSystem
104 >,
105 input: {
106 readonly calls?: ElectronAppCalls;
107 readonly environment?: TestEnvironmentInput;
108 readonly legacyPathExists?: boolean;
109 readonly legacyPathProbeError?: PlatformError.PlatformError;
110 readonly packageJson?: string;
111 readonly pngIconPath?: Option.Option<string>;
112 } = {},
113) => {
114 const calls: ElectronAppCalls = input.calls ?? {
115 setAboutPanelOptions: [],
116 setDockIcon: [],
117 setName: [],
118 };
119
120 return effect.pipe(
121 Effect.provide(
122 DesktopAppIdentity.layer.pipe(
123 Layer.provideMerge(
124 FileSystem.layerNoop({
125 exists: (path) =>
126 input.legacyPathProbeError
127 ? Effect.fail(input.legacyPathProbeError)
128 : Effect.succeed(
129 input.legacyPathExists === true && path.includes("T3 Code (Alpha)"),
130 ),
131 readFileString: () =>
132 Effect.succeed(input.packageJson ?? '{"t3codeCommitHash":"abcdef1234567890"}'),
133 }),
134 ),
135 Layer.provideMerge(makeAssetsLayer(input.pngIconPath ?? Option.none())),
136 Layer.provideMerge(makeElectronAppLayer(calls)),
137 Layer.provideMerge(makeEnvironmentLayer(input.environment)),
138 ),
139 ),
140 );
141};
142
143describe("DesktopAppIdentity", () => {
144 it.effect("keeps using the legacy userData path when it already exists", () =>
145 withIdentity(
146 Effect.gen(function* () {
147 const identity = yield* DesktopAppIdentity.DesktopAppIdentity;
148 const userDataPath = yield* identity.resolveUserDataPath;
149
150 assert.equal(userDataPath, "/Users/alice/Library/Application Support/T3 Code (Alpha)");
151 }),
152 { legacyPathExists: true },
153 ),
154 );
155
156 it.effect("preserves failures while inspecting the legacy userData path", () => {
157 const legacyPath = "/Users/alice/Library/Application Support/T3 Code (Alpha)";
158 const cause = PlatformError.systemError({
159 _tag: "PermissionDenied",
160 module: "FileSystem",
161 method: "exists",
162 description: "permission denied",
163 pathOrDescriptor: legacyPath,
164 });
165
166 return withIdentity(
167 Effect.gen(function* () {
168 const identity = yield* DesktopAppIdentity.DesktopAppIdentity;
169 const error = yield* identity.resolveUserDataPath.pipe(Effect.flip);
170
171 assert.instanceOf(error, DesktopAppIdentity.DesktopUserDataPathResolutionError);
172 assert.equal(error.legacyPath, legacyPath);
173 assert.strictEqual(error.cause, cause);
174 assert.equal(
175 error.message,
176 `Failed to inspect legacy desktop user-data path at "${legacyPath}".`,
177 );
178 }),
179 { legacyPathProbeError: cause },
180 );
181 });
182
183 it.effect("configures app identity from the environment commit override", () => {
184 const calls: ElectronAppCalls = {
185 setAboutPanelOptions: [],
186 setDockIcon: [],
187 setName: [],
188 };
189
190 return withIdentity(
191 Effect.gen(function* () {
192 const identity = yield* DesktopAppIdentity.DesktopAppIdentity;
193 yield* identity.configure;
194
195 assert.deepEqual(calls.setName, ["T3 Code (Alpha)"]);
196 assert.equal(calls.setAboutPanelOptions[0]?.applicationName, "T3 Code (Alpha)");
197 assert.equal(calls.setAboutPanelOptions[0]?.applicationVersion, "1.2.3");
198 assert.equal(calls.setAboutPanelOptions[0]?.version, "0123456789ab");
199 assert.deepEqual(calls.setDockIcon, ["/icon.png"]);
200 }),
201 {
202 calls,
203 environment: {
204 env: {
205 T3CODE_COMMIT_HASH: "0123456789abcdef",
206 },
207 },
208 pngIconPath: Option.some("/icon.png"),
209 },
210 );
211 });
212});