@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

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