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