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

DesktopPreReadyPlatform.test.ts

327 lines11.9 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as HostProcess from "@t3tools/shared/HostProcess";
3import * as Context from "effect/Context";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import { beforeEach, vi } from "vite-plus/test";
7​
8const {
9 appendSwitchMock,
10 getSwitchValueMock,
11 hasSwitchMock,
12 registerSchemesMock,
13 setDesktopNameMock,
14 mkdirSyncMock,
15 writeFileSyncMock,
16 copyFileSyncMock,
17 execFileSyncMock,
18 readFileSyncMock,
19} = vi.hoisted(() => ({
20 appendSwitchMock: vi.fn(),
21 getSwitchValueMock: vi.fn(),
22 hasSwitchMock: vi.fn(),
23 registerSchemesMock: vi.fn(),
24 setDesktopNameMock: vi.fn(),
25 mkdirSyncMock: vi.fn(),
26 writeFileSyncMock: vi.fn(),
27 copyFileSyncMock: vi.fn(),
28 execFileSyncMock: vi.fn(),
29 readFileSyncMock: vi.fn(),
30}));
31​
32vi.mock("electron", () => ({
33 app: {
34 setDesktopName: setDesktopNameMock,
35 getVersion: () => "0.0.37",
36 isPackaged: true,
37 getAppPath: () => "/tmp/.mount_T3/resources/app.asar",
38 commandLine: {
39 appendSwitch: appendSwitchMock,
40 getSwitchValue: getSwitchValueMock,
41 hasSwitch: hasSwitchMock,
42 },
43 },
44 protocol: {
45 registerSchemesAsPrivileged: registerSchemesMock,
46 },
47}));
48​
49vi.mock("node:fs", () => ({
50 readFileSync: readFileSyncMock,
51 mkdirSync: mkdirSyncMock,
52 writeFileSync: writeFileSyncMock,
53 copyFileSync: copyFileSyncMock,
54}));
55​
56vi.mock("node:child_process", () => ({
57 execFileSync: execFileSyncMock,
58}));
59​
60import * as DesktopPreReadyPlatform from "./DesktopPreReadyPlatform.ts";
61​
62describe("DesktopPreReadyPlatform", () => {
63 beforeEach(() => {
64 appendSwitchMock.mockReset();
65 getSwitchValueMock.mockReset();
66 hasSwitchMock.mockReset();
67 registerSchemesMock.mockReset();
68 setDesktopNameMock.mockReset();
69 mkdirSyncMock.mockReset();
70 writeFileSyncMock.mockReset();
71 copyFileSyncMock.mockReset();
72 execFileSyncMock.mockReset();
73 readFileSyncMock.mockReset().mockReturnValue("{}");
74 });
75​
76 it.effect("persists Linux scaling synchronously across relaunches and settings rewrites", () => {
77 vi.stubEnv("T3CODE_HOME", "/isolated");
78 vi.stubEnv("VITE_DEV_SERVER_URL", "");
79 const settingsPath = "/isolated/userdata/desktop-settings.json";
80 const files = new Map([[settingsPath, "{}"]]);
81 const commandLine = new Map<string, string>();
82 readFileSyncMock.mockImplementation((path: string) => {
83 const contents = files.get(path);
84 if (contents === undefined) throw new Error("ENOENT");
85 return contents;
86 });
87 writeFileSyncMock.mockImplementation((path: string, contents: string) => {
88 files.set(path, contents);
89 });
90 hasSwitchMock.mockImplementation((name: string) => commandLine.has(name));
91 getSwitchValueMock.mockImplementation((name: string) => commandLine.get(name) ?? "");
92 appendSwitchMock.mockImplementation((name: string, value: string) => {
93 commandLine.set(name, value);
94 });
95 const launch = Effect.gen(function* () {
96 const startup = Promise.resolve().then(() => commandLine.get("force-device-scale-factor"));
97 yield* DesktopPreReadyPlatform.make;
98 return yield* Effect.promise(() => startup);
99 });
100​
101 return Effect.gen(function* () {
102 for (const explicitScale of ["1.75", "1"]) {
103 commandLine.clear();
104 commandLine.set("force-device-scale-factor", explicitScale);
105 appendSwitchMock.mockClear();
106 assert.equal(yield* launch, explicitScale);
107 assert.isFalse(
108 appendSwitchMock.mock.calls.some(([name]) => name === "force-device-scale-factor"),
109 );
110​
111 // The settings service discards fields it does not own when rewriting its document.
112 files.set(settingsPath, JSON.stringify({ linuxPasswordStore: "basic" }));
113 commandLine.clear();
114 assert.equal(yield* launch, explicitScale);
115 }
116 }).pipe(
117 Effect.provideService(HostProcess.Platform, "linux"),
118 Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs())),
119 );
120 });
121​
122 it.effect.each(["darwin", "win32"] as const)("leaves device scaling alone on %s", (platform) => {
123 readFileSyncMock.mockReturnValue("1.75");
124 hasSwitchMock.mockImplementation((name: string) => name === "force-device-scale-factor");
125 getSwitchValueMock.mockReturnValue("2");
126​
127 return Effect.gen(function* () {
128 yield* DesktopPreReadyPlatform.make;
129 assert.deepEqual(readFileSyncMock.mock.calls, []);
130 assert.deepEqual(writeFileSyncMock.mock.calls, []);
131 assert.deepEqual(appendSwitchMock.mock.calls, []);
132 }).pipe(Effect.provideService(HostProcess.Platform, platform));
133 });
134​
135 it.effect("preserves an explicit Linux password-store switch", () => {
136 hasSwitchMock.mockImplementation((switchName) => switchName === "password-store");
137 getSwitchValueMock.mockReturnValue(" basic ");
138​
139 return Effect.gen(function* () {
140 const options = yield* DesktopPreReadyPlatform.DesktopPreReadyElectronOptions;
141​
142 assert.equal(options.linuxPasswordStoreCommandLine, "basic");
143 assert.isFalse(appendSwitchMock.mock.calls.some(([name]) => name === "password-store"));
144 }).pipe(
145 Effect.provide(
146 DesktopPreReadyPlatform.layer.pipe(
147 Layer.provide(Layer.succeed(HostProcess.Platform, "linux")),
148 ),
149 ),
150 );
151 });
152​
153 it.effect.each([
154 {
155 label: "merges the color-manager fallback into disabled features on Hyprland 0.48.1",
156 switches: { "ozone-platform": "wayland", "disable-features": "Vulkan" },
157 hyprlandVersion: "0.48.1",
158 hyprlandCommit: "29e2e59fdbab8ed2cc23a20e3c6043d5decb5cdc",
159 expected: [["disable-features", "Vulkan,WaylandWpColorManagerV1"]],
160 },
161 {
162 label: "preserves color management on Hyprland 0.51.0",
163 switches: { "ozone-platform": "wayland", "disable-features": "Vulkan" },
164 hyprlandVersion: "0.51.0",
165 hyprlandCommit: "0123456789abcdef0123456789abcdef01234567",
166 expected: [],
167 },
168 {
169 label: "preserves color management when Electron resolved X11",
170 switches: { "ozone-platform": "x11", "disable-features": "Vulkan" },
171 hyprlandVersion: "0.48.1",
172 hyprlandCommit: "29e2e59fdbab8ed2cc23a20e3c6043d5decb5cdc",
173 expected: [],
174 },
175 ])("$label", ({ switches, hyprlandVersion, hyprlandCommit, expected }) => {
176 vi.stubEnv("XDG_CURRENT_DESKTOP", "Hyprland");
177 vi.stubEnv("XDG_SESSION_TYPE", "wayland");
178 const commandLine = new Map<string, string>(Object.entries(switches));
179 hasSwitchMock.mockImplementation((switchName: string) => commandLine.has(switchName));
180 getSwitchValueMock.mockImplementation(
181 (switchName: string) => commandLine.get(switchName) ?? "",
182 );
183 execFileSyncMock.mockReturnValue(
184 JSON.stringify({
185 commit: hyprlandCommit,
186 version: hyprlandVersion,
187 dirty: false,
188 tag: `v${hyprlandVersion}`,
189 }),
190 );
191​
192 return DesktopPreReadyPlatform.make.pipe(
193 Effect.provideService(HostProcess.Platform, "linux"),
194 Effect.map(() => {
195 const featureSwitches = appendSwitchMock.mock.calls.filter(([name]) =>
196 ["disable-features", "enable-features", "ozone-platform"].includes(name),
197 );
198 assert.deepEqual(featureSwitches, expected);
199 }),
200 Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs())),
201 );
202 });
203​
204 it.effect.each([
205 { previousEntry: undefined, label: "missing" },
206 { previousEntry: 'Exec="/Applications/deleted-previous.AppImage" %U', label: "stale" },
207 ])("prepares a $label Linux desktop entry before startup yields", ({ previousEntry }) => {
208 vi.stubEnv("VITE_DEV_SERVER_URL", "");
209 vi.stubEnv("XDG_DATA_HOME", "/xdg");
210 vi.stubEnv("APPIMAGE", "/Applications/current.AppImage");
211 getSwitchValueMock.mockReturnValue("");
212 let desktopName = "t3code.desktop";
213 let desktopEntry = previousEntry;
214 let iconInstalled = false;
215 copyFileSyncMock.mockImplementation((_source: string, destination: string) => {
216 iconInstalled = destination === "/xdg/icons/com.t3tools.T3Code.desktop.png";
217 });
218 setDesktopNameMock.mockImplementation((name: string) => {
219 desktopName = name;
220 });
221 writeFileSyncMock.mockImplementation((path: string, contents: string) => {
222 if (path === "/xdg/applications/com.t3tools.T3Code.desktop") desktopEntry = contents;
223 });
224​
225 return Effect.scoped(
226 Effect.gen(function* () {
227 const portalIdentity = Promise.resolve().then(() => ({
228 desktopName,
229 desktopEntry,
230 iconInstalled,
231 }));
232 yield* Layer.build(
233 DesktopPreReadyPlatform.layer.pipe(
234 Layer.provide(Layer.succeed(HostProcess.Platform, "linux")),
235 ),
236 );
237 const identity = yield* Effect.promise(() => portalIdentity);
238 assert.equal(identity.desktopName, "com.t3tools.T3Code.desktop");
239 assert.include(identity.desktopEntry ?? "", "Exec=/Applications/current.AppImage %U");
240 assert.include(identity.desktopEntry ?? "", "Name=T3 Code (Beta)");
241 assert.include(identity.desktopEntry ?? "", "MimeType=x-scheme-handler/t3code;");
242 assert.include(
243 identity.desktopEntry ?? "",
244 "Icon=/xdg/icons/com.t3tools.T3Code.desktop.png",
245 );
246 assert.isTrue(identity.iconInstalled);
247 }),
248 ).pipe(Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs())));
249 });
250​
251 it.effect("keeps startup available when the early desktop entry cannot be written", () => {
252 getSwitchValueMock.mockReturnValue("");
253 mkdirSyncMock.mockImplementation(() => {
254 throw new Error("read-only filesystem");
255 });
256​
257 return DesktopPreReadyPlatform.make.pipe(
258 Effect.provideService(HostProcess.Platform, "linux"),
259 Effect.asVoid,
260 );
261 });
262​
263 it.effect("still prepares the portal entry when the bundled icon cannot be copied", () => {
264 getSwitchValueMock.mockReturnValue("");
265 copyFileSyncMock.mockImplementation(() => {
266 throw new Error("missing bundled icon");
267 });
268 return Effect.gen(function* () {
269 yield* DesktopPreReadyPlatform.make;
270 const contents = writeFileSyncMock.mock.calls[0]?.[1];
271 assert.include(contents, "MimeType=x-scheme-handler/t3code;");
272 assert.include(contents, "Icon=");
273 assert.equal(setDesktopNameMock.mock.calls.length, 1);
274 }).pipe(Effect.provideService(HostProcess.Platform, "linux"));
275 });
276​
277 it.effect(
278 "acquires a synchronous pre-ready layer before an asynchronous Clerk-shaped layer",
279 () =>
280 Effect.gen(function* () {
281 class ClerkShaped extends Context.Service<ClerkShaped, { readonly ready: true }>()(
282 "@t3tools/desktop/app/DesktopPreReadyPlatform.test/ClerkShaped",
283 ) {}
284​
285 const events: Array<string> = [];
286 registerSchemesMock.mockImplementation(() => {
287 events.push("pre-ready");
288 });
289​
290 const layerPreReady = DesktopPreReadyPlatform.layer.pipe(
291 Layer.provide(Layer.succeed(HostProcess.Platform, "darwin")),
292 );
293​
294 const layerClerkShaped = Layer.effect(
295 ClerkShaped,
296 Effect.promise(() => Promise.resolve()).pipe(
297 Effect.map(() => {
298 events.push("clerk");
299 return { ready: true as const };
300 }),
301 ),
302 );
303​
304 const layerRuntime = layerClerkShaped.pipe(
305 Layer.flatMap((clerkContext) => Layer.succeedContext(clerkContext)),
306 Layer.provideMerge(layerPreReady),
307 );
308​
309 const result = yield* Effect.all({
310 clerk: ClerkShaped,
311 preReady: DesktopPreReadyPlatform.DesktopPreReadyElectronOptions,
312 }).pipe(Effect.provide(layerRuntime));
313​
314 assert.deepEqual(result, {
315 clerk: { ready: true },
316 preReady: {
317 linux: null,
318 linuxPasswordStoreCommandLine: null,
319 },
320 });
321 assert.deepEqual(events, ["pre-ready", "clerk"]);
322 assert.equal(registerSchemesMock.mock.calls.length, 1);
323 assert.equal(appendSwitchMock.mock.calls.length, 0);
324 assert.equal(setDesktopNameMock.mock.calls.length, 0);
325 }),
326 );
327});