|
| 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 | + async call(message: Message) { |
| 21 | + this.calls.push(message); |
| 22 | + if (message.member === "AddMatch") |
| 23 | + return new Message({ type: MessageType.METHOD_RETURN, replySerial: "1" }); |
| 24 | + const value = message.member === "ReadOne" ? this.settings.readOne : this.settings.read; |
| 25 | + if (value === undefined) throw new DBusError("org.freedesktop.DBus.Error.UnknownMethod", ""); |
| 26 | + return new Message({ |
| 27 | + type: MessageType.METHOD_RETURN, |
| 28 | + replySerial: "1", |
| 29 | + signature: "v", |
| 30 | + body: [value], |
| 31 | + }); |
| 32 | + } |
| 33 | + |
| 34 | + changed(namespace: string, key: string, value: number) { |
| 35 | + this.emit( |
| 36 | + "message", |
| 37 | + new Message({ |
| 38 | + type: MessageType.SIGNAL, |
| 39 | + path: PATH, |
| 40 | + interface: SETTINGS, |
| 41 | + member: "SettingChanged", |
| 42 | + body: [namespace, key, new Variant("u", value)], |
| 43 | + }), |
| 44 | + ); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +async function watch(bus: FakeBus) { |
| 49 | + const schemes: LinuxColorScheme[] = []; |
| 50 | + const read = Promise.withResolvers<void>(); |
| 51 | + const stop = watchLinuxColorScheme( |
| 52 | + (scheme) => { |
| 53 | + schemes.push(scheme); |
| 54 | + read.resolve(); |
| 55 | + }, |
| 56 | + bus as unknown as MessageBus, |
| 57 | + ); |
| 58 | + await read.promise; |
| 59 | + return { schemes, stop }; |
| 60 | +} |
| 61 | + |
| 62 | +it("reads the current color scheme and follows portal changes", async () => { |
| 63 | + const bus = new FakeBus({ readOne: new Variant("u", 1) }); |
| 64 | + const { schemes, stop } = await watch(bus); |
| 65 | + |
| 66 | + bus.changed("org.freedesktop.appearance", "color-scheme", 2); |
| 67 | + bus.changed("org.freedesktop.appearance", "accent-color", 1); |
| 68 | + bus.changed("org.gnome.desktop.interface", "color-scheme", 1); |
| 69 | + bus.changed("org.freedesktop.appearance", "color-scheme", 1); |
| 70 | + // GNOME's light mode reports "no preference". |
| 71 | + bus.changed("org.freedesktop.appearance", "color-scheme", 0); |
| 72 | + expect(schemes).toEqual(["dark", "light", "dark", "light"]); |
| 73 | + |
| 74 | + stop(); |
| 75 | + bus.changed("org.freedesktop.appearance", "color-scheme", 1); |
| 76 | + expect(schemes).toEqual(["dark", "light", "dark", "light"]); |
| 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 } = await watch(bus); |
| 83 | + |
| 84 | + expect(schemes).toEqual(["light"]); |
| 85 | + expect(bus.calls.map((call) => call.member)).toEqual(["AddMatch", "ReadOne", "Read"]); |
| 86 | +}); |
0 commit comments