Skip to content

Commit 21a30a5

Browse files
committed
fix(desktop): bound Linux MIME cache refresh wait
1 parent 1b8e9bb commit 21a30a5

2 files changed

Lines changed: 69 additions & 31 deletions

File tree

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

Lines changed: 68 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
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";
68
import * as PlatformError from "effect/PlatformError";
79
import * as Sink from "effect/Sink";
810
import * as Stream from "effect/Stream";
11+
import * as TestClock from "effect/testing/TestClock";
912
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
1013

1114
import * as DesktopEnvironment from "./DesktopEnvironment.ts";
@@ -31,10 +34,10 @@ const makeEnvironment = (overrides: Record<string, unknown> = {}) =>
3134
...overrides,
3235
} as unknown as DesktopEnvironment.DesktopEnvironment["Service"]);
3336

34-
const mockProcess = (exitCode: number) =>
37+
const mockProcess = (exitCode: number, stalled = false) =>
3538
ChildProcessSpawner.makeHandle({
3639
pid: ChildProcessSpawner.ProcessId(1),
37-
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(exitCode)),
40+
exitCode: stalled ? Effect.never : Effect.succeed(ChildProcessSpawner.ExitCode(exitCode)),
3841
isRunning: Effect.succeed(false),
3942
kill: () => Effect.void,
4043
unref: Effect.succeed(Effect.void),
@@ -51,6 +54,8 @@ const makeHandlerLayer = (
5154
input: {
5255
readonly environment?: Record<string, unknown>;
5356
readonly updateDesktopDatabaseExitCode?: number;
57+
readonly updateDesktopDatabaseStalled?: boolean;
58+
readonly updateDesktopDatabaseStarted?: Deferred.Deferred<void>;
5459
readonly xdgMimeExitCode?: number;
5560
readonly writeError?: PlatformError.PlatformError;
5661
readonly existingEntry?: string;
@@ -88,7 +93,17 @@ const makeHandlerLayer = (
8893
childProcess.command === "update-desktop-database"
8994
? (input.updateDesktopDatabaseExitCode ?? 0)
9095
: (input.xdgMimeExitCode ?? 0);
91-
return Effect.succeed(mockProcess(exitCode));
96+
const handle = mockProcess(
97+
exitCode,
98+
childProcess.command === "update-desktop-database" &&
99+
input.updateDesktopDatabaseStalled === true,
100+
);
101+
return childProcess.command === "update-desktop-database" &&
102+
input.updateDesktopDatabaseStarted
103+
? Deferred.succeed(input.updateDesktopDatabaseStarted, undefined).pipe(
104+
Effect.as(handle),
105+
)
106+
: Effect.succeed(handle);
92107
}),
93108
),
94109
),
@@ -159,35 +174,38 @@ describe("DesktopLinuxUrlHandler", () => {
159174
);
160175
});
161176

162-
it.effect("writes the handler entry, refreshes the MIME cache, and claims the scheme default", () => {
163-
const recorded = emptyRecording();
177+
it.effect(
178+
"writes the handler entry, refreshes the MIME cache, and claims the scheme default",
179+
() => {
180+
const recorded = emptyRecording();
164181

165-
return Effect.gen(function* () {
166-
yield* runRegister(recorded);
182+
return Effect.gen(function* () {
183+
yield* runRegister(recorded);
167184

168-
assert.deepEqual(recorded.directories, ["/home/alice/.local/share/applications"]);
169-
assert.equal(recorded.files.length, 1);
170-
assert.equal(
171-
recorded.files[0]?.path,
172-
"/home/alice/.local/share/applications/com.t3tools.T3Code.desktop",
173-
);
174-
assert.include(
175-
recorded.files[0]?.content,
176-
'Exec="/home/alice/Applications/T3-Code.AppImage" %U',
177-
);
178-
assert.include(recorded.files[0]?.content, "MimeType=x-scheme-handler/t3code;");
179-
assert.deepEqual(recorded.commands, [
180-
{
181-
command: "update-desktop-database",
182-
args: ["/home/alice/.local/share/applications"],
183-
},
184-
{
185-
command: "xdg-mime",
186-
args: ["default", "com.t3tools.T3Code.desktop", "x-scheme-handler/t3code"],
187-
},
188-
]);
189-
});
190-
});
185+
assert.deepEqual(recorded.directories, ["/home/alice/.local/share/applications"]);
186+
assert.equal(recorded.files.length, 1);
187+
assert.equal(
188+
recorded.files[0]?.path,
189+
"/home/alice/.local/share/applications/com.t3tools.T3Code.desktop",
190+
);
191+
assert.include(
192+
recorded.files[0]?.content,
193+
'Exec="/home/alice/Applications/T3-Code.AppImage" %U',
194+
);
195+
assert.include(recorded.files[0]?.content, "MimeType=x-scheme-handler/t3code;");
196+
assert.deepEqual(recorded.commands, [
197+
{
198+
command: "update-desktop-database",
199+
args: ["/home/alice/.local/share/applications"],
200+
},
201+
{
202+
command: "xdg-mime",
203+
args: ["default", "com.t3tools.T3Code.desktop", "x-scheme-handler/t3code"],
204+
},
205+
]);
206+
});
207+
},
208+
);
191209

192210
it.effect("falls back to the process executable outside an AppImage", () => {
193211
const recorded = emptyRecording();
@@ -277,4 +295,24 @@ describe("DesktopLinuxUrlHandler", () => {
277295
assert.deepEqual(writeFailed.commands, []);
278296
});
279297
});
298+
299+
it.effect("continues to xdg-mime when the desktop MIME cache refresh stalls", () =>
300+
Effect.gen(function* () {
301+
const recorded = emptyRecording();
302+
const started = yield* Deferred.make<void>();
303+
const registration = yield* runRegister(recorded, {
304+
updateDesktopDatabaseStalled: true,
305+
updateDesktopDatabaseStarted: started,
306+
}).pipe(Effect.forkChild);
307+
308+
yield* Deferred.await(started);
309+
yield* TestClock.adjust("5 seconds");
310+
yield* Fiber.join(registration);
311+
312+
assert.deepEqual(
313+
recorded.commands.map(({ command }) => command),
314+
["update-desktop-database", "xdg-mime"],
315+
);
316+
}),
317+
);
280318
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export const make = Effect.gen(function* () {
160160
},
161161
);
162162
const handle = yield* spawner.spawn(command);
163-
const exitCode = yield* handle.exitCode;
163+
const exitCode = yield* handle.exitCode.pipe(Effect.timeout("5 seconds"));
164164
if (exitCode !== 0) {
165165
return yield* new DesktopLinuxUrlHandlerCacheRefreshError({
166166
applicationsDir: environment.linuxApplicationsDir,

0 commit comments

Comments
 (0)