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

DesktopLinuxUrlHandler.test.ts

256 lines8.9 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as FileSystem from "effect/FileSystem";
4import * as Layer from "effect/Layer";
5import * as Option from "effect/Option";
6import * as PlatformError from "effect/PlatformError";
7import * as Sink from "effect/Sink";
8import * as Stream from "effect/Stream";
9import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
10​
11import * as DesktopEnvironment from "./DesktopEnvironment.ts";
12import * as DesktopLinuxUrlHandler from "./DesktopLinuxUrlHandler.ts";
13​
14interface RecordedRegistration {
15 readonly directories: string[];
16 readonly files: Array<{ readonly path: string; readonly content: string }>;
17 readonly commands: Array<{ readonly command: string; readonly args: ReadonlyArray<string> }>;
19​
20const makeEnvironment = (overrides: Record<string, unknown> = {}) =>
21 DesktopEnvironment.DesktopEnvironment.of({
22 platform: "linux",
23 isPackaged: true,
24 isDevelopment: false,
25 displayName: "T3 Code (Alpha)",
26 linuxDesktopEntryName: "com.t3tools.T3Code.desktop",
27 linuxWmClass: "t3code",
28 linuxApplicationsDir: "/home/alice/.local/share/applications",
29 appImagePath: Option.some("/home/alice/Applications/T3-Code.AppImage"),
30 path: { join: (...parts: ReadonlyArray<string>) => parts.join("/") },
31 ...overrides,
32 } as unknown as DesktopEnvironment.DesktopEnvironment["Service"]);
33​
34const mockProcess = (exitCode: number) =>
35 ChildProcessSpawner.makeHandle({
36 pid: ChildProcessSpawner.ProcessId(1),
37 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(exitCode)),
38 isRunning: Effect.succeed(false),
39 kill: () => Effect.void,
40 unref: Effect.succeed(Effect.void),
41 stdin: Sink.drain,
42 stdout: Stream.empty,
43 stderr: Stream.empty,
44 all: Stream.empty,
45 getInputFd: () => Sink.drain,
46 getOutputFd: () => Stream.empty,
47 });
48​
49const makeHandlerLayer = (
50 recorded: RecordedRegistration,
51 input: {
52 readonly environment?: Record<string, unknown>;
53 readonly xdgMimeExitCode?: number;
54 readonly writeError?: PlatformError.PlatformError;
55 readonly existingEntry?: string;
56 } = {},
57) =>
58 DesktopLinuxUrlHandler.layer.pipe(
59 Layer.provide(
60 Layer.mergeAll(
61 Layer.succeed(DesktopEnvironment.DesktopEnvironment, makeEnvironment(input.environment)),
62 FileSystem.layerNoop({
63 readFileString: () => Effect.succeed(input.existingEntry ?? ""),
64 makeDirectory: (path) =>
65 Effect.sync(() => {
66 recorded.directories.push(path);
67 }),
68 writeFileString: (path, content) =>
69 input.writeError
70 ? Effect.fail(input.writeError)
71 : Effect.sync(() => {
72 recorded.files.push({ path, content });
73 }),
74 }),
75 Layer.succeed(
76 ChildProcessSpawner.ChildProcessSpawner,
77 ChildProcessSpawner.make((command) => {
78 const childProcess = command as unknown as {
79 readonly command: string;
80 readonly args: ReadonlyArray<string>;
81 };
82 recorded.commands.push({
83 command: childProcess.command,
84 args: childProcess.args,
85 });
86 return Effect.succeed(mockProcess(input.xdgMimeExitCode ?? 0));
87 }),
88 ),
89 ),
90 ),
91 );
92​
93const runRegister = (
94 recorded: RecordedRegistration,
95 input: Parameters<typeof makeHandlerLayer>[1] = {},
96) =>
97 Effect.gen(function* () {
98 const handler = yield* DesktopLinuxUrlHandler.DesktopLinuxUrlHandler;
99 yield* handler.register;
100 }).pipe(Effect.provide(makeHandlerLayer(recorded, input)));
101​
102const emptyRecording = (): RecordedRegistration => ({
103 directories: [],
104 files: [],
105 commands: [],
106});
107​
108describe("DesktopLinuxUrlHandler", () => {
109 it("renders a scheme-handler desktop entry with freedesktop Exec quoting", () => {
110 const entry = DesktopLinuxUrlHandler.renderUrlHandlerDesktopEntry({
111 displayName: "T3 Code (Nightly)",
112 execTarget: '/home/al ice/Apps/T3 "100%" $HOME\\x.AppImage',
113 scheme: "t3code",
114 });
115​
116 assert.include(entry, "[Desktop Entry]");
117 assert.include(entry, "Name=T3 Code (Nightly)");
118 // Exec composes both escaping layers: a literal backslash becomes four
119 // backslashes in the file, a quote three characters, a dollar sign two
120 // backslashes plus the sign.
121 assert.include(
122 entry,
123 'Exec="/home/al ice/Apps/T3 \\\\"100%%\\\\" \\\\$HOME\\\\\\\\x.AppImage" %U',
124 );
125 assert.include(entry, "NoDisplay=true");
126 assert.notInclude(entry, "StartupWMClass=");
127 assert.include(entry, "MimeType=x-scheme-handler/t3code;");
128 });
129​
130 it("carries structured context on registration errors", () => {
131 const writeError = new DesktopLinuxUrlHandler.DesktopLinuxUrlHandlerRegistrationError({
132 step: "write-desktop-entry",
133 scheme: "t3code",
134 desktopEntryPath: "/home/alice/.local/share/applications/com.t3tools.T3Code.desktop",
135 cause: new Error("boom"),
136 });
137 assert.equal(
138 writeError.message,
139 "Failed to register the t3code:// URL handler (step: write-desktop-entry).",
140 );
141 assert.equal(
142 writeError.desktopEntryPath,
143 "/home/alice/.local/share/applications/com.t3tools.T3Code.desktop",
144 );
145​
146 const exitError = new DesktopLinuxUrlHandler.DesktopLinuxUrlHandlerRegistrationError({
147 step: "set-default-handler",
148 scheme: "t3code",
149 exitCode: 4,
150 });
151 assert.equal(
152 exitError.message,
153 "Failed to register the t3code:// URL handler (step: set-default-handler, xdg-mime exit code 4).",
154 );
155 });
156​
157 it.effect("writes the handler entry and claims the scheme default via xdg-mime", () => {
158 const recorded = emptyRecording();
159​
160 return Effect.gen(function* () {
161 yield* runRegister(recorded);
162​
163 assert.deepEqual(recorded.directories, ["/home/alice/.local/share/applications"]);
164 assert.equal(recorded.files.length, 1);
165 assert.equal(
166 recorded.files[0]?.path,
167 "/home/alice/.local/share/applications/com.t3tools.T3Code.desktop",
168 );
169 assert.include(
170 recorded.files[0]?.content,
171 'Exec="/home/alice/Applications/T3-Code.AppImage" %U',
172 );
173 assert.include(recorded.files[0]?.content, "MimeType=x-scheme-handler/t3code;");
174 assert.deepEqual(recorded.commands, [
175 {
176 command: "xdg-mime",
177 args: ["default", "com.t3tools.T3Code.desktop", "x-scheme-handler/t3code"],
178 },
179 ]);
180 });
181 });
182​
183 it.effect("falls back to the process executable outside an AppImage", () => {
184 const recorded = emptyRecording();
185​
186 return Effect.gen(function* () {
187 yield* runRegister(recorded, { environment: { appImagePath: Option.none() } });
188​
189 assert.include(
190 recorded.files[0]?.content,
191 `Exec=${DesktopLinuxUrlHandler.escapeDesktopEntryExecArgument(process.execPath)} %U`,
192 );
193 });
194 });
195​
196 it.effect("does not rewrite the pre-ready entry while the portal can be reading it", () => {
197 const recorded = emptyRecording();
198​
199 return Effect.gen(function* () {
200 yield* runRegister(recorded, {
201 existingEntry: DesktopLinuxUrlHandler.renderUrlHandlerDesktopEntry({
202 displayName: "T3 Code (Alpha)",
203 execTarget: "/home/alice/Applications/T3-Code.AppImage",
204 scheme: "t3code",
205 }),
206 });
207​
208 assert.deepEqual(recorded.files, []);
209 assert.deepEqual(recorded.directories, []);
210 assert.equal(recorded.commands.length, 1);
211 });
212 });
213​
214 it.effect("writes the portal identity without claiming the URL scheme in development", () => {
215 const nonLinux = emptyRecording();
216 const unpackaged = emptyRecording();
217​
218 return Effect.gen(function* () {
219 yield* runRegister(nonLinux, { environment: { platform: "darwin" } });
220 yield* runRegister(unpackaged, {
221 environment: {
222 isPackaged: false,
223 linuxDesktopEntryName: "com.t3tools.T3Code.Development.desktop",
224 },
225 });
226​
227 assert.deepEqual(nonLinux.files, []);
228 assert.equal(
229 unpackaged.files[0]?.path,
230 "/home/alice/.local/share/applications/com.t3tools.T3Code.Development.desktop",
231 );
232 assert.deepEqual(unpackaged.commands, []);
233 });
234 });
235​
236 it.effect("never fails startup when registration cannot complete", () => {
237 const xdgMimeFailed = emptyRecording();
238 const writeFailed = emptyRecording();
239​
240 return Effect.gen(function* () {
241 yield* runRegister(xdgMimeFailed, { xdgMimeExitCode: 1 });
242 yield* runRegister(writeFailed, {
243 writeError: PlatformError.systemError({
244 _tag: "PermissionDenied",
245 module: "FileSystem",
246 method: "writeFileString",
247 description: "read-only filesystem",
248 pathOrDescriptor: "/home/alice/.local/share/applications/com.t3tools.T3Code.desktop",
249 }),
250 });
251​
252 assert.equal(xdgMimeFailed.files.length, 1);
253 assert.deepEqual(writeFailed.commands, []);
254 });
255 });
256});