Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Fiber from "effect/Fiber";
4import * as PlatformError from "effect/PlatformError";
5import * as Sink from "effect/Sink";
6import * as Stream from "effect/Stream";
7import * as TestClock from "effect/testing/TestClock";
8import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
9import * as HostProcess from "@t3tools/shared/HostProcess";
10
11import { readGtkPrimaryPasteEnabled } from "./DesktopPrimaryPaste.ts";
12
13const encoder = new TextEncoder();
14
15const spawnerWithStdout = (
16 stdout: Stream.Stream<Uint8Array> | ((command: string) => Stream.Stream<Uint8Array>),
17) =>
18 ChildProcessSpawner.make((command) =>
19 Effect.succeed(
20 ChildProcessSpawner.makeHandle({
21 pid: ChildProcessSpawner.ProcessId(1),
22 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
23 exitStatus: Effect.succeed({ _tag: "Exited", code: ChildProcessSpawner.ExitCode(0) }),
24 isRunning: Effect.succeed(false),
25 kill: () => Effect.void,
26 unref: Effect.succeed(Effect.void),
27 stdin: Sink.drain,
28 stdout:
29 typeof stdout === "function"
30 ? stdout(command._tag === "StandardCommand" ? command.command : "")
31 : stdout,
32 stderr: Stream.empty,
33 all: Stream.empty,
34 getInputFd: () => Sink.drain,
35 getOutputFd: () => Stream.empty,
36 }),
37 ),
38 );
39
40const notFound = PlatformError.systemError({
41 _tag: "NotFound",
42 module: "ChildProcess",
43 method: "spawn",
44 description: "not found",
45});
46
47const read = (
48 spawner: ChildProcessSpawner.ChildProcessSpawner["Service"],
49 desktop = "ubuntu:GNOME",
50) =>
51 readGtkPrimaryPasteEnabled.pipe(
52 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
53 Effect.provideService(HostProcess.Environment, { XDG_CURRENT_DESKTOP: desktop }),
54 );
55
56describe("readGtkPrimaryPasteEnabled", () => {
57 it.effect("is disabled only when gsettings prints false", () =>
58 Effect.gen(function* () {
59 assert.isFalse(yield* read(spawnerWithStdout(Stream.make(encoder.encode("false\n")))));
60 assert.isTrue(yield* read(spawnerWithStdout(Stream.make(encoder.encode("true\n")))));
61 // A missing schema prints nothing on stdout.
62 assert.isTrue(yield* read(spawnerWithStdout(Stream.empty)));
63 }),
64 );
65
66 it.effect("outside GNOME, honours only a value the user set", () =>
67 Effect.gen(function* () {
68 // gsettings reports the schema default, which only GNOME applies; dconf prints nothing when unset.
69 const unset = spawnerWithStdout((command) =>
70 command === "gsettings" ? Stream.make(encoder.encode("false\n")) : Stream.empty,
71 );
72 assert.isFalse(yield* read(unset, "ubuntu:GNOME"));
73 assert.isTrue(yield* read(unset, "XFCE"));
74 assert.isTrue(yield* read(unset, ""));
75 const userSet = spawnerWithStdout((command) =>
76 command === "dconf" ? Stream.make(encoder.encode("false\n")) : Stream.empty,
77 );
78 assert.isFalse(yield* read(userSet, "Hyprland"));
79 // Without dconf-cli, the effective value is the best evidence of an opt-out.
80 const noDconf = ChildProcessSpawner.make((command) =>
81 command._tag === "StandardCommand" && command.command === "dconf"
82 ? Effect.fail(notFound)
83 : spawnerWithStdout(Stream.make(encoder.encode("false\n"))).spawn(command),
84 );
85 assert.isFalse(yield* read(noDconf, "XFCE"));
86 }),
87 );
88
89 it.effect("stays enabled when gsettings cannot run", () =>
90 Effect.gen(function* () {
91 const missing = ChildProcessSpawner.make(() => Effect.fail(notFound));
92 assert.isTrue(yield* read(missing));
93 }),
94 );
95
96 it.effect("stays enabled when gsettings hangs", () =>
97 Effect.gen(function* () {
98 const fiber = yield* read(spawnerWithStdout(Stream.never)).pipe(Effect.forkChild);
99 yield* TestClock.adjust("2 seconds");
100 assert.isTrue(yield* Fiber.join(fiber));
101 }),
102 );
103});