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