Open History to see the last commit
1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { assert, describe, it } from "@effect/vitest";
3import * as Deferred from "effect/Deferred";
4import * as Effect from "effect/Effect";
5import * as Fiber from "effect/Fiber";
6import * as FileSystem from "effect/FileSystem";
7import * as Layer from "effect/Layer";
8import * as Option from "effect/Option";
9import * as Path from "effect/Path";
10import * as PlatformError from "effect/PlatformError";
11import * as Sink from "effect/Sink";
12import * as Stream from "effect/Stream";
13import * as TestClock from "effect/testing/TestClock";
14import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
15
16import * as DesktopEnvironment from "./DesktopEnvironment.ts";
17import * as DesktopAssets from "./DesktopAssets.ts";
18import * as DesktopLinuxUrlHandler from "./DesktopLinuxUrlHandler.ts";
19
20interface RecordedRegistration {
21 readonly directories: string[];
22 readonly files: Array<{ readonly path: string; readonly content: string }>;
23 readonly commands: Array<{ readonly command: string; readonly args: ReadonlyArray<string> }>;
24 readonly copies: Array<{ readonly source: string; readonly destination: string }>;
25}
26
27const makeEnvironment = (path: Path.Path, overrides: Record<string, unknown> = {}) =>
28 DesktopEnvironment.DesktopEnvironment.of({
29 platform: "linux",
30 isPackaged: true,
31 isDevelopment: false,
32 displayName: "T3 Code (Alpha)",
33 linuxDesktopEntryName: "com.t3tools.T3Code.desktop",
34 linuxWmClass: "t3code",
35 linuxApplicationsDir: "/home/alice/.local/share/applications",
36 appImagePath: Option.some("/home/alice/Applications/T3-Code.AppImage"),
37 path,
38 ...overrides,
39 } as unknown as DesktopEnvironment.DesktopEnvironment["Service"]);
40
41const mockProcess = (exitCode: number, stalled = false) =>
42 ChildProcessSpawner.makeHandle({
43 pid: ChildProcessSpawner.ProcessId(1),
44 exitCode: stalled ? Effect.never : Effect.succeed(ChildProcessSpawner.ExitCode(exitCode)),
45 isRunning: Effect.succeed(false),
46 kill: () => Effect.void,
47 unref: Effect.succeed(Effect.void),
48 stdin: Sink.drain,
49 stdout: Stream.empty,
50 stderr: Stream.empty,
51 all: Stream.empty,
52 getInputFd: () => Sink.drain,
53 getOutputFd: () => Stream.empty,
54 });
55
56const layerHandler = (
57 recorded: RecordedRegistration,
58 input: {
59 readonly environment?: Record<string, unknown>;
60 readonly updateDesktopDatabaseExitCode?: number;
61 readonly updateDesktopDatabaseStalled?: boolean;
62 readonly updateDesktopDatabaseStarted?: Deferred.Deferred<void>;
63 readonly xdgMimeExitCode?: number;
64 readonly writeError?: PlatformError.PlatformError;
65 readonly existingEntry?: string;
66 readonly iconSource?: string;
67 readonly iconCopyError?: PlatformError.PlatformError;
68 } = {},
69) =>
70 DesktopLinuxUrlHandler.layer.pipe(
71 Layer.provide(
72 Layer.mergeAll(
73 Layer.effect(
74 DesktopEnvironment.DesktopEnvironment,
75 Path.Path.pipe(
76 Effect.map((path) => makeEnvironment(path, input.environment)),
77 Effect.provide(Path.layer),
78 ),
79 ),
80 Layer.succeed(DesktopAssets.DesktopAssets, {
81 iconPaths: Effect.succeed({
82 png: Option.fromUndefinedOr(input.iconSource),
83 ico: Option.none(),
84 icns: Option.none(),
85 }),
86 resolveResourcePath: () => Effect.succeedNone,
87 }),
88 FileSystem.layerNoop({
89 copyFile: (source, destination) =>
90 input.iconCopyError
91 ? Effect.fail(input.iconCopyError)
92 : Effect.sync(() => {
93 recorded.copies.push({ source, destination });
94 }),
95 readFileString: () => Effect.succeed(input.existingEntry ?? ""),
96 makeDirectory: (path) =>
97 Effect.sync(() => {
98 recorded.directories.push(path);
99 }),
100 writeFileString: (path, content) =>
101 input.writeError
102 ? Effect.fail(input.writeError)
103 : Effect.sync(() => {
104 recorded.files.push({ path, content });
105 }),
106 }),
107 Layer.succeed(
108 ChildProcessSpawner.ChildProcessSpawner,
109 ChildProcessSpawner.make((command) => {
110 const childProcess = command as unknown as {
111 readonly command: string;
112 readonly args: ReadonlyArray<string>;
113 };
114 if (childProcess.command === "update-desktop-database") {
115 assert.isTrue(
116 recorded.files.length > 0 || input.existingEntry !== undefined,
117 "the desktop entry must exist before refreshing the MIME cache",
118 );
119 }
120 recorded.commands.push({
121 command: childProcess.command,
122 args: childProcess.args,
123 });
124 const exitCode =
125 childProcess.command === "update-desktop-database"
126 ? (input.updateDesktopDatabaseExitCode ?? 0)
127 : (input.xdgMimeExitCode ?? 0);
128 const handle = mockProcess(
129 exitCode,
130 childProcess.command === "update-desktop-database" &&
131 input.updateDesktopDatabaseStalled === true,
132 );
133 return childProcess.command === "update-desktop-database" &&
134 input.updateDesktopDatabaseStarted
135 ? Deferred.succeed(input.updateDesktopDatabaseStarted, undefined).pipe(
136 Effect.as(handle),
137 )
138 : Effect.succeed(handle);
139 }),
140 ),
141 ),
142 ),
143 );
144
145const runRegister = (
146 recorded: RecordedRegistration,
147 input: Parameters<typeof layerHandler>[1] = {},
148) =>
149 Effect.gen(function* () {
150 const handler = yield* DesktopLinuxUrlHandler.DesktopLinuxUrlHandler;
151 yield* handler.register;
152 }).pipe(Effect.provide(layerHandler(recorded, input)));
153
154const emptyRecording = (): RecordedRegistration => ({
155 directories: [],
156 files: [],
157 commands: [],
158 copies: [],
159});
160
161describe("DesktopLinuxUrlHandler", () => {
162 it("renders a scheme-handler desktop entry with freedesktop Exec quoting", () => {
163 const entry = DesktopLinuxUrlHandler.renderUrlHandlerDesktopEntry({
164 displayName: "T3 Code (Nightly)",
165 execTarget: '/home/al ice/Apps/T3 "100%" $HOME\\x.AppImage',
166 scheme: "t3code",
167 iconPath: "/home/al ice/icons/T3\\x.png",
168 });
169
170 assert.include(entry, "[Desktop Entry]");
171 assert.include(entry, "Name=T3 Code (Nightly)");
172 assert.include(
173 entry,
174 'Exec="/home/al ice/Apps/T3 \\\\"100%%\\\\" \\\\$HOME\\\\\\\\x.AppImage" %U',
175 );
176 assert.include(entry, "NoDisplay=true");
177 assert.notInclude(entry, "StartupWMClass=");
178 assert.include(entry, "MimeType=x-scheme-handler/t3code;");
179 assert.include(entry, "Icon=/home/al ice/icons/T3\\\\x.png");
180 });
181
182 it("carries structured context on registration errors", () => {
183 const writeError = new DesktopLinuxUrlHandler.DesktopLinuxUrlHandlerRegistrationError({
184 step: "write-desktop-entry",
185 scheme: "t3code",
186 desktopEntryPath: "/home/alice/.local/share/applications/com.t3tools.T3Code.desktop",
187 cause: new Error("boom"),
188 });
189 assert.equal(
190 writeError.message,
191 "Failed to register the t3code:// URL handler (step: write-desktop-entry).",
192 );
193 assert.equal(
194 writeError.desktopEntryPath,
195 "/home/alice/.local/share/applications/com.t3tools.T3Code.desktop",
196 );
197
198 const exitError = new DesktopLinuxUrlHandler.DesktopLinuxUrlHandlerRegistrationError({
199 step: "set-default-handler",
200 scheme: "t3code",
201 exitCode: 4,
202 });
203 assert.equal(
204 exitError.message,
205 "Failed to register the t3code:// URL handler (step: set-default-handler, xdg-mime exit code 4).",
206 );
207 });
208
209 it.effect(
210 "writes the handler entry, refreshes the MIME cache, and claims the scheme default",
211 () => {
212 const recorded = emptyRecording();
213
214 return Effect.gen(function* () {
215 yield* runRegister(recorded);
216
217 assert.deepEqual(recorded.directories, ["/home/alice/.local/share/applications"]);
218 assert.equal(recorded.files.length, 1);
219 assert.equal(
220 recorded.files[0]?.path,
221 "/home/alice/.local/share/applications/com.t3tools.T3Code.desktop",
222 );
223 assert.include(
224 recorded.files[0]?.content,
225 'Exec="/home/alice/Applications/T3-Code.AppImage" %U',
226 );
227 assert.include(recorded.files[0]?.content, "MimeType=x-scheme-handler/t3code;");
228 assert.deepEqual(recorded.commands, [
229 {
230 command: "update-desktop-database",
231 args: ["/home/alice/.local/share/applications"],
232 },
233 {
234 command: "xdg-mime",
235 args: ["default", "com.t3tools.T3Code.desktop", "x-scheme-handler/t3code"],
236 },
237 ]);
238 });
239 },
240 );
241
242 it.effect("falls back to the process executable outside an AppImage", () => {
243 const recorded = emptyRecording();
244
245 return Effect.gen(function* () {
246 yield* runRegister(recorded, { environment: { appImagePath: Option.none() } });
247
248 assert.include(
249 recorded.files[0]?.content,
250 `Exec=${DesktopLinuxUrlHandler.escapeDesktopEntryExecArgument(process.execPath)} %U`,
251 );
252 });
253 });
254
255 it.effect("does not rewrite the pre-ready entry while the portal can be reading it", () => {
256 const recorded = emptyRecording();
257
258 return Effect.gen(function* () {
259 yield* runRegister(recorded, {
260 existingEntry: DesktopLinuxUrlHandler.renderUrlHandlerDesktopEntry({
261 displayName: "T3 Code (Alpha)",
262 execTarget: "/home/alice/Applications/T3-Code.AppImage",
263 scheme: "t3code",
264 iconPath: "/home/alice/.local/share/icons/com.t3tools.T3Code.desktop.png",
265 }),
266 });
267
268 assert.deepEqual(recorded.files, []);
269 assert.deepEqual(recorded.directories, []);
270 assert.deepEqual(recorded.commands, [
271 {
272 command: "update-desktop-database",
273 args: ["/home/alice/.local/share/applications"],
274 },
275 {
276 command: "xdg-mime",
277 args: ["default", "com.t3tools.T3Code.desktop", "x-scheme-handler/t3code"],
278 },
279 ]);
280 });
281 });
282
283 it.effect("installs a persistent icon even when the desktop entry is already current", () => {
284 const recorded = emptyRecording();
285 const iconPath = "/home/alice/.local/share/icons/com.t3tools.T3Code.desktop.png";
286 return Effect.gen(function* () {
287 yield* runRegister(recorded, {
288 iconSource: "/tmp/.mount_T3/resources/icon.png",
289 existingEntry: DesktopLinuxUrlHandler.renderUrlHandlerDesktopEntry({
290 displayName: "T3 Code (Alpha)",
291 execTarget: "/home/alice/Applications/T3-Code.AppImage",
292 scheme: "t3code",
293 iconPath,
294 }),
295 });
296 assert.deepEqual(recorded.files, []);
297 assert.deepEqual(recorded.copies, [
298 { source: "/tmp/.mount_T3/resources/icon.png", destination: iconPath },
299 ]);
300 assert.equal(recorded.commands.at(-1)?.command, "xdg-mime");
301 });
302 });
303
304 it.effect("still registers the handler when copying its icon fails", () => {
305 const recorded = emptyRecording();
306 return Effect.gen(function* () {
307 yield* runRegister(recorded, {
308 iconSource: "/tmp/.mount_T3/resources/icon.png",
309 iconCopyError: PlatformError.systemError({
310 _tag: "PermissionDenied",
311 module: "FileSystem",
312 method: "copyFile",
313 description: "read-only icon directory",
314 }),
315 });
316 assert.equal(recorded.files.length, 1);
317 assert.deepEqual(
318 recorded.commands.map(({ command }) => command),
319 ["update-desktop-database", "xdg-mime"],
320 );
321 });
322 });
323
324 it.effect("writes the portal identity without claiming the URL scheme in development", () => {
325 const nonLinux = emptyRecording();
326 const unpackaged = emptyRecording();
327
328 return Effect.gen(function* () {
329 yield* runRegister(nonLinux, { environment: { platform: "darwin" } });
330 yield* runRegister(unpackaged, {
331 environment: {
332 isPackaged: false,
333 linuxDesktopEntryName: "com.t3tools.T3Code.Development.desktop",
334 },
335 });
336
337 assert.deepEqual(nonLinux.files, []);
338 assert.equal(
339 unpackaged.files[0]?.path,
340 "/home/alice/.local/share/applications/com.t3tools.T3Code.Development.desktop",
341 );
342 assert.deepEqual(unpackaged.commands, []);
343 });
344 });
345
346 it.effect("never fails startup when registration cannot complete", () => {
347 const desktopDatabaseFailed = emptyRecording();
348 const xdgMimeFailed = emptyRecording();
349 const writeFailed = emptyRecording();
350
351 return Effect.gen(function* () {
352 yield* runRegister(desktopDatabaseFailed, { updateDesktopDatabaseExitCode: 1 });
353 yield* runRegister(xdgMimeFailed, { xdgMimeExitCode: 1 });
354 yield* runRegister(writeFailed, {
355 writeError: PlatformError.systemError({
356 _tag: "PermissionDenied",
357 module: "FileSystem",
358 method: "writeFileString",
359 description: "read-only filesystem",
360 pathOrDescriptor: "/home/alice/.local/share/applications/com.t3tools.T3Code.desktop",
361 }),
362 });
363
364 assert.deepEqual(
365 desktopDatabaseFailed.commands.map(({ command }) => command),
366 ["update-desktop-database", "xdg-mime"],
367 );
368 assert.equal(xdgMimeFailed.files.length, 1);
369 assert.deepEqual(writeFailed.commands, []);
370 });
371 });
372
373 it.effect("continues to xdg-mime when the desktop MIME cache refresh stalls", () =>
374 Effect.gen(function* () {
375 const recorded = emptyRecording();
376 const started = yield* Deferred.make<void>();
377 const registration = yield* runRegister(recorded, {
378 updateDesktopDatabaseStalled: true,
379 updateDesktopDatabaseStarted: started,
380 }).pipe(Effect.forkChild);
381
382 yield* Deferred.await(started);
383 yield* TestClock.adjust("5 seconds");
384 yield* Fiber.join(registration);
385
386 assert.deepEqual(
387 recorded.commands.map(({ command }) => command),
388 ["update-desktop-database", "xdg-mime"],
389 );
390 }),
391 );
392});
393
394interface ThemeIconRun {
395 readonly root: string;
396 readonly commands: Array<{ readonly command: string; readonly args: ReadonlyArray<string> }>;
397 readonly iconCacheOptions?: Array<Record<string, unknown>>;
398 readonly sourceMissing?: boolean;
399 readonly iconCacheExitCode?: number;
400 readonly iconCacheSpawnFails?: boolean;
401 readonly iconCacheStalled?: boolean;
402 readonly iconCacheStarted?: Deferred.Deferred<void>;
403 // Stands in for gtk-update-icon-cache rewriting the cache on success.
404 readonly onIconCacheRefresh?: Effect.Effect<void>;
405}
406
407const registerWithRealFileSystem = (run: ThemeIconRun) =>
408 Effect.gen(function* () {
409 const handler = yield* DesktopLinuxUrlHandler.DesktopLinuxUrlHandler;
410 yield* handler.register;
411 }).pipe(
412 Effect.provide(
413 DesktopLinuxUrlHandler.layer.pipe(
414 Layer.provide(
415 Layer.mergeAll(
416 Layer.effect(
417 DesktopEnvironment.DesktopEnvironment,
418 Path.Path.pipe(
419 Effect.map((path) =>
420 makeEnvironment(path, {
421 linuxApplicationsDir: path.join(run.root, "applications"),
422 }),
423 ),
424 ),
425 ),
426 Layer.effect(
427 DesktopAssets.DesktopAssets,
428 Path.Path.pipe(
429 Effect.map((path) => ({
430 iconPaths: Effect.succeed({
431 png: Option.none(),
432 ico: Option.none(),
433 icns: Option.none(),
434 }),
435 resolveResourcePath: (fileName: string) =>
436 Effect.succeed(
437 fileName === "icons/256x256.png"
438 ? Option.some(
439 path.join(
440 run.root,
441 run.sourceMissing ? "missing" : "resources",
442 fileName,
443 ),
444 )
445 : Option.none(),
446 ),
447 })),
448 ),
449 ),
450 Layer.succeed(
451 ChildProcessSpawner.ChildProcessSpawner,
452 ChildProcessSpawner.make((command) => {
453 const childProcess = command as unknown as {
454 readonly command: string;
455 readonly args: ReadonlyArray<string>;
456 };
457 run.commands.push({ command: childProcess.command, args: childProcess.args });
458 if (childProcess.command !== "gtk-update-icon-cache") {
459 return Effect.succeed(mockProcess(0));
460 }
461 run.iconCacheOptions?.push(
462 (command as unknown as { readonly options: Record<string, unknown> }).options,
463 );
464 if (run.iconCacheSpawnFails) {
465 return Effect.fail(
466 PlatformError.systemError({
467 _tag: "NotFound",
468 module: "ChildProcess",
469 method: "spawn",
470 description: "gtk-update-icon-cache is not installed",
471 }),
472 );
473 }
474 const exitCode = run.iconCacheExitCode ?? 0;
475 const handle = mockProcess(exitCode, run.iconCacheStalled === true);
476 const refreshed =
477 exitCode === 0 && run.onIconCacheRefresh ? run.onIconCacheRefresh : Effect.void;
478 return refreshed.pipe(
479 Effect.andThen(
480 run.iconCacheStarted
481 ? Deferred.succeed(run.iconCacheStarted, undefined)
482 : Effect.void,
483 ),
484 Effect.as(handle),
485 );
486 }),
487 ),
488 ),
489 ),
490 ),
491 ),
492 );
493
494const themeIconFixture = (source: Uint8Array) =>
495 Effect.gen(function* () {
496 const fileSystem = yield* FileSystem.FileSystem;
497 const path = yield* Path.Path;
498 const root = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-theme-icons-" });
499 yield* fileSystem.makeDirectory(path.join(root, "resources", "icons"), { recursive: true });
500 yield* fileSystem.writeFile(path.join(root, "resources", "icons", "256x256.png"), source);
501 const hicolorDir = path.join(root, "icons", "hicolor");
502 const appsDir = path.join(hicolorDir, "256x256", "apps");
503 const cachePath = path.join(hicolorDir, "icon-theme.cache");
504 // A fixed time in the past, in whole seconds as fs.utimes expects.
505 const pastSeconds = 1_600_000_000;
506 // An existing cache that predates any installed icon.
507 const seedCache = Effect.gen(function* () {
508 yield* fileSystem.makeDirectory(appsDir, { recursive: true });
509 yield* fileSystem.writeFile(cachePath, new Uint8Array([0]));
510 yield* fileSystem.utimes(cachePath, pastSeconds, pastSeconds);
511 });
512 const rewriteCache = fileSystem
513 .writeFile(cachePath, new Uint8Array([1]))
514 .pipe(Effect.orDie, Effect.provideService(FileSystem.FileSystem, fileSystem));
515 return { root, hicolorDir, appsDir, cachePath, pastSeconds, seedCache, rewriteCache };
516 });
517
518const iconCacheRuns = (run: ThemeIconRun) =>
519 run.commands.filter(({ command }) => command === "gtk-update-icon-cache").length;
520
521it.layer(NodeServices.layer)("DesktopLinuxUrlHandler theme icons", (it) => {
522 it.effect("installs both app id spellings without creating an icon cache", () =>
523 Effect.gen(function* () {
524 const fileSystem = yield* FileSystem.FileSystem;
525 const path = yield* Path.Path;
526 const icon = new Uint8Array([137, 80, 78, 71, 1, 2, 3]);
527 const fixture = yield* themeIconFixture(icon);
528 const run: ThemeIconRun = { root: fixture.root, commands: [] };
529
530 yield* registerWithRealFileSystem(run);
531 assert.deepEqual([...(yield* fileSystem.readDirectory(fixture.appsDir))].sort(), [
532 "com.t3tools.T3Code.png",
533 "com.t3tools.t3code.png",
534 ]);
535 for (const name of ["com.t3tools.t3code", "com.t3tools.T3Code"]) {
536 const target = path.join(fixture.appsDir, `${name}.png`);
537 assert.deepEqual([...(yield* fileSystem.readFile(target))], [...icon]);
538 yield* fileSystem.utimes(target, fixture.pastSeconds, fixture.pastSeconds);
539 }
540
541 yield* registerWithRealFileSystem(run);
542 for (const name of ["com.t3tools.t3code", "com.t3tools.T3Code"]) {
543 const info = yield* fileSystem.stat(path.join(fixture.appsDir, `${name}.png`));
544 assert.equal(Option.getOrThrow(info.mtime).getTime(), fixture.pastSeconds * 1000);
545 }
546 assert.isFalse(yield* fileSystem.exists(fixture.cachePath));
547 assert.equal(iconCacheRuns(run), 0);
548 }).pipe(Effect.scoped),
549 );
550
551 it.effect("replaces same-length stale icons and refreshes an existing cache once", () =>
552 Effect.gen(function* () {
553 const fileSystem = yield* FileSystem.FileSystem;
554 const path = yield* Path.Path;
555 const fixture = yield* themeIconFixture(new Uint8Array([1, 2, 3]));
556 yield* fixture.seedCache;
557 const lower = path.join(fixture.appsDir, "com.t3tools.t3code.png");
558 const upper = path.join(fixture.appsDir, "com.t3tools.T3Code.png");
559 yield* fileSystem.writeFile(lower, new Uint8Array([3, 2, 1]));
560 yield* fileSystem.writeFile(upper, new Uint8Array([1, 2, 3]));
561 for (const target of [lower, upper]) {
562 yield* fileSystem.utimes(target, fixture.pastSeconds, fixture.pastSeconds);
563 }
564 const run: ThemeIconRun = {
565 root: fixture.root,
566 commands: [],
567 onIconCacheRefresh: fixture.rewriteCache,
568 };
569
570 yield* registerWithRealFileSystem(run);
571 yield* registerWithRealFileSystem(run);
572
573 assert.deepEqual([...(yield* fileSystem.readFile(lower))], [1, 2, 3]);
574 const upperInfo = yield* fileSystem.stat(upper);
575 assert.equal(Option.getOrThrow(upperInfo.mtime).getTime(), fixture.pastSeconds * 1000);
576 assert.deepEqual(
577 run.commands.filter(({ command }) => command === "gtk-update-icon-cache"),
578 [{ command: "gtk-update-icon-cache", args: ["-f", "-t", fixture.hicolorDir] }],
579 );
580 }).pipe(Effect.scoped),
581 );
582
583 it.effect("refreshes when this launch changed an icon even if the cache looks newer", () =>
584 Effect.gen(function* () {
585 const fileSystem = yield* FileSystem.FileSystem;
586 const path = yield* Path.Path;
587 const fixture = yield* themeIconFixture(new Uint8Array([1, 2, 3]));
588 yield* fixture.seedCache;
589 const run: ThemeIconRun = {
590 root: fixture.root,
591 commands: [],
592 onIconCacheRefresh: fixture.rewriteCache,
593 };
594 // A cache stamped later than the new icons, as after a clock correction.
595 const future = fixture.pastSeconds + 10 * 365 * 24 * 60 * 60;
596 yield* fileSystem.utimes(fixture.cachePath, future, future);
597
598 yield* registerWithRealFileSystem(run);
599
600 assert.isTrue(yield* fileSystem.exists(path.join(fixture.appsDir, "com.t3tools.T3Code.png")));
601 assert.equal(iconCacheRuns(run), 1);
602 }).pipe(Effect.scoped),
603 );
604
605 it.effect("retries a failed icon cache refresh on the next launch", () =>
606 Effect.gen(function* () {
607 const fixture = yield* themeIconFixture(new Uint8Array([1, 2, 3]));
608 yield* fixture.seedCache;
609 const failing: ThemeIconRun = { root: fixture.root, commands: [], iconCacheExitCode: 1 };
610 yield* registerWithRealFileSystem(failing);
611 assert.equal(iconCacheRuns(failing), 1);
612 assert.deepEqual(failing.commands.map(({ command }) => command).slice(1), [
613 "update-desktop-database",
614 "xdg-mime",
615 ]);
616
617 const recovering: ThemeIconRun = {
618 root: fixture.root,
619 commands: [],
620 onIconCacheRefresh: fixture.rewriteCache,
621 };
622 yield* registerWithRealFileSystem(recovering);
623 yield* registerWithRealFileSystem(recovering);
624 assert.equal(iconCacheRuns(recovering), 1);
625 }).pipe(Effect.scoped),
626 );
627
628 it.effect("still registers the handler when the theme icon install fails", () =>
629 Effect.gen(function* () {
630 const fixture = yield* themeIconFixture(new Uint8Array([1, 2, 3]));
631 const missingSource: ThemeIconRun = { root: fixture.root, commands: [], sourceMissing: true };
632 yield* registerWithRealFileSystem(missingSource);
633 assert.deepEqual(
634 missingSource.commands.map(({ command }) => command),
635 ["update-desktop-database", "xdg-mime"],
636 );
637
638 yield* fixture.seedCache;
639 const missingTool: ThemeIconRun = {
640 root: fixture.root,
641 commands: [],
642 iconCacheSpawnFails: true,
643 };
644 yield* registerWithRealFileSystem(missingTool);
645 assert.deepEqual(
646 missingTool.commands.map(({ command }) => command),
647 ["gtk-update-icon-cache", "update-desktop-database", "xdg-mime"],
648 );
649 }).pipe(Effect.scoped),
650 );
651
652 it.effect("continues to xdg-mime when the icon cache refresh stalls", () =>
653 Effect.gen(function* () {
654 const fixture = yield* themeIconFixture(new Uint8Array([1, 2, 3]));
655 yield* fixture.seedCache;
656 const started = yield* Deferred.make<void>();
657 const iconCacheOptions: Array<Record<string, unknown>> = [];
658 const run: ThemeIconRun = {
659 root: fixture.root,
660 commands: [],
661 iconCacheOptions,
662 iconCacheStalled: true,
663 iconCacheStarted: started,
664 };
665
666 const registration = yield* registerWithRealFileSystem(run).pipe(Effect.forkChild);
667 yield* Deferred.await(started);
668 yield* TestClock.adjust("5 seconds");
669 yield* Fiber.join(registration);
670
671 assert.deepEqual(
672 run.commands.map(({ command }) => command),
673 ["gtk-update-icon-cache", "update-desktop-database", "xdg-mime"],
674 );
675 // Without a forced kill, releasing a helper that ignores SIGTERM after
676 // the timeout would still wait for it to exit.
677 assert.equal(iconCacheOptions[0]?.forceKillAfter, "1 second");
678 }).pipe(Effect.scoped),
679 );
680});