The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // @effect-diagnostics nodeBuiltinImport:off -- Event-driven fake of the portal transport. |
| 2 | import * as NodeEvents from "node:events"; |
| 3 | import { DBusError, Message, MessageType, Variant, type MessageBus } from "dbus-next"; |
| 4 | import { expect, it, vi } from "vite-plus/test"; |
| 5 | |
| 6 | import { watchLinuxColorScheme, type LinuxColorScheme } from "./LinuxColorScheme.ts"; |
| 7 | |
| 8 | const PATH = "/org/freedesktop/portal/desktop"; |
| 9 | const SETTINGS = "org.freedesktop.portal.Settings"; |
| 10 | |
| 11 | class FakeBus extends NodeEvents.EventEmitter { |
| 12 | calls: Message[] = []; |
| 13 | disconnect = vi.fn(); |
| 14 | readonly settings: { readOne?: unknown; read?: unknown }; |
| 15 | constructor(settings: { readOne?: unknown; read?: unknown }) { |
| 16 | super(); |
| 17 | this.settings = settings; |
| 18 | } |
| 19 | |
| 20 | readonly readOneCalled = Promise.withResolvers<void>(); |
| 21 | |
| 22 | async call(message: Message) { |
| 23 | this.calls.push(message); |
| 24 | if (message.member === "AddMatch") |
| 25 | return new Message({ type: MessageType.METHOD_RETURN, replySerial: "1" }); |
| 26 | if (message.member === "ReadOne") this.readOneCalled.resolve(); |
| 27 | const value = await (message.member === "ReadOne" ? this.settings.readOne : this.settings.read); |
| 28 | if (value === undefined) throw new DBusError("org.freedesktop.DBus.Error.UnknownMethod", ""); |
| 29 | return new Message({ |
| 30 | type: MessageType.METHOD_RETURN, |
| 31 | replySerial: "1", |
| 32 | signature: "v", |
| 33 | body: [value], |
| 34 | }); |
| 35 | } |
| 36 | |
| 37 | changed(namespace: string, key: string, value: number) { |
| 38 | this.emit( |
| 39 | "message", |
| 40 | new Message({ |
| 41 | type: MessageType.SIGNAL, |
| 42 | path: PATH, |
| 43 | interface: SETTINGS, |
| 44 | member: "SettingChanged", |
| 45 | body: [namespace, key, new Variant("u", value)], |
| 46 | }), |
| 47 | ); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | function watch(bus: FakeBus) { |
| 52 | const schemes: LinuxColorScheme[] = []; |
| 53 | const { ready, stop } = watchLinuxColorScheme( |
| 54 | (scheme) => schemes.push(scheme), |
| 55 | bus as unknown as MessageBus, |
| 56 | ); |
| 57 | return { schemes, ready, stop }; |
| 58 | } |
| 59 | |
| 60 | it("reads the current color scheme and follows portal changes", async () => { |
| 61 | const bus = new FakeBus({ readOne: new Variant("u", 1) }); |
| 62 | const { schemes, ready, stop } = watch(bus); |
| 63 | await ready; |
| 64 | |
| 65 | bus.changed("org.freedesktop.appearance", "color-scheme", 2); |
| 66 | bus.changed("org.freedesktop.appearance", "accent-color", 1); |
| 67 | bus.changed("org.gnome.desktop.interface", "color-scheme", 1); |
| 68 | bus.changed("org.freedesktop.appearance", "color-scheme", 1); |
| 69 | // GNOME's light mode reports "no preference". |
| 70 | bus.changed("org.freedesktop.appearance", "color-scheme", 0); |
| 71 | bus.changed("org.freedesktop.appearance", "color-scheme", 3); |
| 72 | expect(schemes).toEqual(["dark", "light", "dark", "light", null]); |
| 73 | |
| 74 | stop(); |
| 75 | bus.changed("org.freedesktop.appearance", "color-scheme", 1); |
| 76 | expect(schemes).toEqual(["dark", "light", "dark", "light", null]); |
| 77 | expect(bus.disconnect).toHaveBeenCalledOnce(); |
| 78 | }); |
| 79 | |
| 80 | it("falls back to the nested Read reply on portals without ReadOne", async () => { |
| 81 | const bus = new FakeBus({ read: new Variant("v", new Variant("u", 2)) }); |
| 82 | const { schemes, ready } = watch(bus); |
| 83 | await ready; |
| 84 | |
| 85 | expect(schemes).toEqual(["light"]); |
| 86 | expect(bus.calls.map((call) => call.member)).toEqual(["AddMatch", "ReadOne", "Read"]); |
| 87 | }); |
| 88 | |
| 89 | it("keeps a change that arrives while the initial read is pending", async () => { |
| 90 | const reply = Promise.withResolvers<Variant>(); |
| 91 | const bus = new FakeBus({ readOne: reply.promise }); |
| 92 | const { schemes, ready } = watch(bus); |
| 93 | |
| 94 | await bus.readOneCalled.promise; |
| 95 | bus.changed("org.freedesktop.appearance", "color-scheme", 1); |
| 96 | reply.resolve(new Variant("u", 0)); |
| 97 | await ready; |
| 98 | |
| 99 | expect(schemes).toEqual(["dark"]); |
| 100 | }); |