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

DesktopAppIdentity.test.ts

214 lines6.9 KB

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