Skip to content

Commit 6b66421

Browse files
Merge upstream main into jj support
2 parents be45bb0 + d2c9281 commit 6b66421

41 files changed

Lines changed: 2250 additions & 196 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/desktop/src/app/DesktopLinuxUrlHandler.test.ts‎

Lines changed: 168 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
import { assert, describe, it } from "@effect/vitest";
2+
import * as Deferred from "effect/Deferred";
23
import * as Effect from "effect/Effect";
4+
import * as Fiber from "effect/Fiber";
35
import * as FileSystem from "effect/FileSystem";
46
import * as Layer from "effect/Layer";
57
import * as Option from "effect/Option";
8+
import * as Path from "effect/Path";
69
import * as PlatformError from "effect/PlatformError";
710
import * as Sink from "effect/Sink";
811
import * as Stream from "effect/Stream";
12+
import * as TestClock from "effect/testing/TestClock";
913
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
1014

1115
import * as DesktopEnvironment from "./DesktopEnvironment.ts";
16+
import * as DesktopAssets from "./DesktopAssets.ts";
1217
import * as DesktopLinuxUrlHandler from "./DesktopLinuxUrlHandler.ts";
1318

1419
interface RecordedRegistration {
1520
readonly directories: string[];
1621
readonly files: Array<{ readonly path: string; readonly content: string }>;
1722
readonly commands: Array<{ readonly command: string; readonly args: ReadonlyArray<string> }>;
23+
readonly copies: Array<{ readonly source: string; readonly destination: string }>;
1824
}
1925

20-
const makeEnvironment = (overrides: Record<string, unknown> = {}) =>
26+
const makeEnvironment = (path: Path.Path, overrides: Record<string, unknown> = {}) =>
2127
DesktopEnvironment.DesktopEnvironment.of({
2228
platform: "linux",
2329
isPackaged: true,
@@ -27,14 +33,14 @@ const makeEnvironment = (overrides: Record<string, unknown> = {}) =>
2733
linuxWmClass: "t3code",
2834
linuxApplicationsDir: "/home/alice/.local/share/applications",
2935
appImagePath: Option.some("/home/alice/Applications/T3-Code.AppImage"),
30-
path: { join: (...parts: ReadonlyArray<string>) => parts.join("/") },
36+
path,
3137
...overrides,
3238
} as unknown as DesktopEnvironment.DesktopEnvironment["Service"]);
3339

34-
const mockProcess = (exitCode: number) =>
40+
const mockProcess = (exitCode: number, stalled = false) =>
3541
ChildProcessSpawner.makeHandle({
3642
pid: ChildProcessSpawner.ProcessId(1),
37-
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(exitCode)),
43+
exitCode: stalled ? Effect.never : Effect.succeed(ChildProcessSpawner.ExitCode(exitCode)),
3844
isRunning: Effect.succeed(false),
3945
kill: () => Effect.void,
4046
unref: Effect.succeed(Effect.void),
@@ -50,16 +56,41 @@ const makeHandlerLayer = (
5056
recorded: RecordedRegistration,
5157
input: {
5258
readonly environment?: Record<string, unknown>;
59+
readonly updateDesktopDatabaseExitCode?: number;
60+
readonly updateDesktopDatabaseStalled?: boolean;
61+
readonly updateDesktopDatabaseStarted?: Deferred.Deferred<void>;
5362
readonly xdgMimeExitCode?: number;
5463
readonly writeError?: PlatformError.PlatformError;
5564
readonly existingEntry?: string;
65+
readonly iconSource?: string;
66+
readonly iconCopyError?: PlatformError.PlatformError;
5667
} = {},
5768
) =>
5869
DesktopLinuxUrlHandler.layer.pipe(
5970
Layer.provide(
6071
Layer.mergeAll(
61-
Layer.succeed(DesktopEnvironment.DesktopEnvironment, makeEnvironment(input.environment)),
72+
Layer.effect(
73+
DesktopEnvironment.DesktopEnvironment,
74+
Path.Path.pipe(
75+
Effect.map((path) => makeEnvironment(path, input.environment)),
76+
Effect.provide(Path.layer),
77+
),
78+
),
79+
Layer.succeed(DesktopAssets.DesktopAssets, {
80+
iconPaths: Effect.succeed({
81+
png: Option.fromUndefinedOr(input.iconSource),
82+
ico: Option.none(),
83+
icns: Option.none(),
84+
}),
85+
resolveResourcePath: () => Effect.succeedNone,
86+
}),
6287
FileSystem.layerNoop({
88+
copyFile: (source, destination) =>
89+
input.iconCopyError
90+
? Effect.fail(input.iconCopyError)
91+
: Effect.sync(() => {
92+
recorded.copies.push({ source, destination });
93+
}),
6394
readFileString: () => Effect.succeed(input.existingEntry ?? ""),
6495
makeDirectory: (path) =>
6596
Effect.sync(() => {
@@ -79,11 +110,31 @@ const makeHandlerLayer = (
79110
readonly command: string;
80111
readonly args: ReadonlyArray<string>;
81112
};
113+
if (childProcess.command === "update-desktop-database") {
114+
assert.isTrue(
115+
recorded.files.length > 0 || input.existingEntry !== undefined,
116+
"the desktop entry must exist before refreshing the MIME cache",
117+
);
118+
}
82119
recorded.commands.push({
83120
command: childProcess.command,
84121
args: childProcess.args,
85122
});
86-
return Effect.succeed(mockProcess(input.xdgMimeExitCode ?? 0));
123+
const exitCode =
124+
childProcess.command === "update-desktop-database"
125+
? (input.updateDesktopDatabaseExitCode ?? 0)
126+
: (input.xdgMimeExitCode ?? 0);
127+
const handle = mockProcess(
128+
exitCode,
129+
childProcess.command === "update-desktop-database" &&
130+
input.updateDesktopDatabaseStalled === true,
131+
);
132+
return childProcess.command === "update-desktop-database" &&
133+
input.updateDesktopDatabaseStarted
134+
? Deferred.succeed(input.updateDesktopDatabaseStarted, undefined).pipe(
135+
Effect.as(handle),
136+
)
137+
: Effect.succeed(handle);
87138
}),
88139
),
89140
),
@@ -103,6 +154,7 @@ const emptyRecording = (): RecordedRegistration => ({
103154
directories: [],
104155
files: [],
105156
commands: [],
157+
copies: [],
106158
});
107159

108160
describe("DesktopLinuxUrlHandler", () => {
@@ -111,6 +163,7 @@ describe("DesktopLinuxUrlHandler", () => {
111163
displayName: "T3 Code (Nightly)",
112164
execTarget: '/home/al ice/Apps/T3 "100%" $HOME\\x.AppImage',
113165
scheme: "t3code",
166+
iconPath: "/home/al ice/icons/T3\\x.png",
114167
});
115168

116169
assert.include(entry, "[Desktop Entry]");
@@ -125,6 +178,7 @@ describe("DesktopLinuxUrlHandler", () => {
125178
assert.include(entry, "NoDisplay=true");
126179
assert.notInclude(entry, "StartupWMClass=");
127180
assert.include(entry, "MimeType=x-scheme-handler/t3code;");
181+
assert.include(entry, "Icon=/home/al ice/icons/T3\\\\x.png");
128182
});
129183

130184
it("carries structured context on registration errors", () => {
@@ -154,31 +208,38 @@ describe("DesktopLinuxUrlHandler", () => {
154208
);
155209
});
156210

157-
it.effect("writes the handler entry and claims the scheme default via xdg-mime", () => {
158-
const recorded = emptyRecording();
211+
it.effect(
212+
"writes the handler entry, refreshes the MIME cache, and claims the scheme default",
213+
() => {
214+
const recorded = emptyRecording();
159215

160-
return Effect.gen(function* () {
161-
yield* runRegister(recorded);
216+
return Effect.gen(function* () {
217+
yield* runRegister(recorded);
162218

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-
});
219+
assert.deepEqual(recorded.directories, ["/home/alice/.local/share/applications"]);
220+
assert.equal(recorded.files.length, 1);
221+
assert.equal(
222+
recorded.files[0]?.path,
223+
"/home/alice/.local/share/applications/com.t3tools.T3Code.desktop",
224+
);
225+
assert.include(
226+
recorded.files[0]?.content,
227+
'Exec="/home/alice/Applications/T3-Code.AppImage" %U',
228+
);
229+
assert.include(recorded.files[0]?.content, "MimeType=x-scheme-handler/t3code;");
230+
assert.deepEqual(recorded.commands, [
231+
{
232+
command: "update-desktop-database",
233+
args: ["/home/alice/.local/share/applications"],
234+
},
235+
{
236+
command: "xdg-mime",
237+
args: ["default", "com.t3tools.T3Code.desktop", "x-scheme-handler/t3code"],
238+
},
239+
]);
240+
});
241+
},
242+
);
182243

183244
it.effect("falls back to the process executable outside an AppImage", () => {
184245
const recorded = emptyRecording();
@@ -202,12 +263,63 @@ describe("DesktopLinuxUrlHandler", () => {
202263
displayName: "T3 Code (Alpha)",
203264
execTarget: "/home/alice/Applications/T3-Code.AppImage",
204265
scheme: "t3code",
266+
iconPath: "/home/alice/.local/share/icons/com.t3tools.T3Code.desktop.png",
205267
}),
206268
});
207269

208270
assert.deepEqual(recorded.files, []);
209271
assert.deepEqual(recorded.directories, []);
210-
assert.equal(recorded.commands.length, 1);
272+
assert.deepEqual(recorded.commands, [
273+
{
274+
command: "update-desktop-database",
275+
args: ["/home/alice/.local/share/applications"],
276+
},
277+
{
278+
command: "xdg-mime",
279+
args: ["default", "com.t3tools.T3Code.desktop", "x-scheme-handler/t3code"],
280+
},
281+
]);
282+
});
283+
});
284+
285+
it.effect("installs a persistent icon even when the desktop entry is already current", () => {
286+
const recorded = emptyRecording();
287+
const iconPath = "/home/alice/.local/share/icons/com.t3tools.T3Code.desktop.png";
288+
return Effect.gen(function* () {
289+
yield* runRegister(recorded, {
290+
iconSource: "/tmp/.mount_T3/resources/icon.png",
291+
existingEntry: DesktopLinuxUrlHandler.renderUrlHandlerDesktopEntry({
292+
displayName: "T3 Code (Alpha)",
293+
execTarget: "/home/alice/Applications/T3-Code.AppImage",
294+
scheme: "t3code",
295+
iconPath,
296+
}),
297+
});
298+
assert.deepEqual(recorded.files, []);
299+
assert.deepEqual(recorded.copies, [
300+
{ source: "/tmp/.mount_T3/resources/icon.png", destination: iconPath },
301+
]);
302+
assert.equal(recorded.commands.at(-1)?.command, "xdg-mime");
303+
});
304+
});
305+
306+
it.effect("still registers the handler when copying its icon fails", () => {
307+
const recorded = emptyRecording();
308+
return Effect.gen(function* () {
309+
yield* runRegister(recorded, {
310+
iconSource: "/tmp/.mount_T3/resources/icon.png",
311+
iconCopyError: PlatformError.systemError({
312+
_tag: "PermissionDenied",
313+
module: "FileSystem",
314+
method: "copyFile",
315+
description: "read-only icon directory",
316+
}),
317+
});
318+
assert.equal(recorded.files.length, 1);
319+
assert.deepEqual(
320+
recorded.commands.map(({ command }) => command),
321+
["update-desktop-database", "xdg-mime"],
322+
);
211323
});
212324
});
213325

@@ -234,10 +346,12 @@ describe("DesktopLinuxUrlHandler", () => {
234346
});
235347

236348
it.effect("never fails startup when registration cannot complete", () => {
349+
const desktopDatabaseFailed = emptyRecording();
237350
const xdgMimeFailed = emptyRecording();
238351
const writeFailed = emptyRecording();
239352

240353
return Effect.gen(function* () {
354+
yield* runRegister(desktopDatabaseFailed, { updateDesktopDatabaseExitCode: 1 });
241355
yield* runRegister(xdgMimeFailed, { xdgMimeExitCode: 1 });
242356
yield* runRegister(writeFailed, {
243357
writeError: PlatformError.systemError({
@@ -249,8 +363,32 @@ describe("DesktopLinuxUrlHandler", () => {
249363
}),
250364
});
251365

366+
assert.deepEqual(
367+
desktopDatabaseFailed.commands.map(({ command }) => command),
368+
["update-desktop-database", "xdg-mime"],
369+
);
252370
assert.equal(xdgMimeFailed.files.length, 1);
253371
assert.deepEqual(writeFailed.commands, []);
254372
});
255373
});
374+
375+
it.effect("continues to xdg-mime when the desktop MIME cache refresh stalls", () =>
376+
Effect.gen(function* () {
377+
const recorded = emptyRecording();
378+
const started = yield* Deferred.make<void>();
379+
const registration = yield* runRegister(recorded, {
380+
updateDesktopDatabaseStalled: true,
381+
updateDesktopDatabaseStarted: started,
382+
}).pipe(Effect.forkChild);
383+
384+
yield* Deferred.await(started);
385+
yield* TestClock.adjust("5 seconds");
386+
yield* Fiber.join(registration);
387+
388+
assert.deepEqual(
389+
recorded.commands.map(({ command }) => command),
390+
["update-desktop-database", "xdg-mime"],
391+
);
392+
}),
393+
);
256394
});

0 commit comments

Comments
 (0)