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";
9
10import { readGtkPrimaryPasteEnabled } from "./DesktopPrimaryPaste.ts";
11
12const encoder = new TextEncoder();
13
14const spawnerWithStdout = (stdout: Stream.Stream<Uint8Array>) =>
15 ChildProcessSpawner.make(() =>
16 Effect.succeed(
17 ChildProcessSpawner.makeHandle({
18 pid: ChildProcessSpawner.ProcessId(1),
19 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(0)),
20 isRunning: Effect.succeed(false),
21 kill: () => Effect.void,
22 unref: Effect.succeed(Effect.void),
23 stdin: Sink.drain,
24 stdout,
25 stderr: Stream.empty,
26 all: Stream.empty,
27 getInputFd: () => Sink.drain,
28 getOutputFd: () => Stream.empty,
29 }),
30 ),
31 );
32
33const read = (spawner: ChildProcessSpawner.ChildProcessSpawner["Service"]) =>
34 readGtkPrimaryPasteEnabled.pipe(
35 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
36 );
37
38describe("readGtkPrimaryPasteEnabled", () => {
39 it.effect("is disabled only when gsettings prints false", () =>
40 Effect.gen(function* () {
41 assert.isFalse(yield* read(spawnerWithStdout(Stream.make(encoder.encode("false\n")))));
42 assert.isTrue(yield* read(spawnerWithStdout(Stream.make(encoder.encode("true\n")))));
43 // A missing schema prints nothing on stdout.
44 assert.isTrue(yield* read(spawnerWithStdout(Stream.empty)));
45 }),
46 );
47
48 it.effect("stays enabled when gsettings cannot run", () =>
49 Effect.gen(function* () {
50 const missing = ChildProcessSpawner.make(() =>
51 Effect.fail(
52 PlatformError.systemError({
53 _tag: "NotFound",
54 module: "ChildProcess",
55 method: "spawn",
56 description: "gsettings not found",
57 }),
58 ),
59 );
60 assert.isTrue(yield* read(missing));
61 }),
62 );
63
64 it.effect("stays enabled when gsettings hangs", () =>
65 Effect.gen(function* () {
66 const fiber = yield* read(spawnerWithStdout(Stream.never)).pipe(Effect.forkChild);
67 yield* TestClock.adjust("2 seconds");
68 assert.isTrue(yield* Fiber.join(fiber));
69 }),
70 );
71});