|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import type { AudioState, BatteryState, SystemTime, WifiState } from "../../types"; |
| 3 | +import { LINUX_BRIDGE_CHANNELS } from "../bridge-contract"; |
| 4 | +import { createLinuxBridgeClient } from "../client"; |
| 5 | +import type { BridgeTransport } from "../transport"; |
| 6 | + |
| 7 | +interface Recorder { |
| 8 | + onCalls: Array<{ channel: string }>; |
| 9 | + sendCalls: Array<{ channel: string; payload: unknown }>; |
| 10 | + emit(channel: string, payload: unknown): void; |
| 11 | +} |
| 12 | + |
| 13 | +function makeTransport(): { transport: BridgeTransport; rec: Recorder } { |
| 14 | + const handlers = new Map<string, Set<(payload: unknown) => void>>(); |
| 15 | + const rec: Recorder = { |
| 16 | + onCalls: [], |
| 17 | + sendCalls: [], |
| 18 | + emit(channel, payload) { |
| 19 | + const set = handlers.get(channel); |
| 20 | + if (!set) return; |
| 21 | + for (const h of set) h(payload); |
| 22 | + }, |
| 23 | + }; |
| 24 | + const transport: BridgeTransport = { |
| 25 | + on<T>(channel: string, handler: (payload: T) => void) { |
| 26 | + rec.onCalls.push({ channel }); |
| 27 | + const cast = handler as (payload: unknown) => void; |
| 28 | + let set = handlers.get(channel); |
| 29 | + if (!set) { |
| 30 | + set = new Set(); |
| 31 | + handlers.set(channel, set); |
| 32 | + } |
| 33 | + set.add(cast); |
| 34 | + return () => { |
| 35 | + set?.delete(cast); |
| 36 | + }; |
| 37 | + }, |
| 38 | + send: vi.fn(async (channel: string, payload: unknown) => { |
| 39 | + rec.sendCalls.push({ channel, payload }); |
| 40 | + return { ok: true } as never; |
| 41 | + }), |
| 42 | + }; |
| 43 | + return { transport, rec }; |
| 44 | +} |
| 45 | + |
| 46 | +describe("createLinuxBridgeClient", () => { |
| 47 | + it("subscribes to wifi/audio/battery/time state channels", () => { |
| 48 | + const { transport, rec } = makeTransport(); |
| 49 | + const client = createLinuxBridgeClient(transport); |
| 50 | + |
| 51 | + const wifi: WifiState[] = []; |
| 52 | + const audio: AudioState[] = []; |
| 53 | + const battery: BatteryState[] = []; |
| 54 | + const time: SystemTime[] = []; |
| 55 | + |
| 56 | + const offW = client.subscribeWifi((s) => wifi.push(s)); |
| 57 | + const offA = client.subscribeAudio((s) => audio.push(s)); |
| 58 | + const offB = client.subscribeBattery((s) => battery.push(s)); |
| 59 | + const offT = client.subscribeTime((s) => time.push(s)); |
| 60 | + |
| 61 | + expect(rec.onCalls.map((c) => c.channel)).toEqual([ |
| 62 | + LINUX_BRIDGE_CHANNELS.wifi.state, |
| 63 | + LINUX_BRIDGE_CHANNELS.audio.state, |
| 64 | + LINUX_BRIDGE_CHANNELS.battery.state, |
| 65 | + LINUX_BRIDGE_CHANNELS.time.state, |
| 66 | + ]); |
| 67 | + |
| 68 | + rec.emit(LINUX_BRIDGE_CHANNELS.wifi.state, { connected: true, ssid: "x" }); |
| 69 | + rec.emit(LINUX_BRIDGE_CHANNELS.audio.state, { level: 0.3, muted: false }); |
| 70 | + rec.emit(LINUX_BRIDGE_CHANNELS.battery.state, { percent: 80, charging: true }); |
| 71 | + rec.emit(LINUX_BRIDGE_CHANNELS.time.state, { |
| 72 | + now: 123, |
| 73 | + locale: "en-US", |
| 74 | + timeZone: "UTC", |
| 75 | + }); |
| 76 | + |
| 77 | + expect(wifi).toHaveLength(1); |
| 78 | + expect(audio).toHaveLength(1); |
| 79 | + expect(battery).toHaveLength(1); |
| 80 | + expect(time).toHaveLength(1); |
| 81 | + |
| 82 | + offW(); |
| 83 | + offA(); |
| 84 | + offB(); |
| 85 | + offT(); |
| 86 | + |
| 87 | + rec.emit(LINUX_BRIDGE_CHANNELS.wifi.state, { connected: false }); |
| 88 | + expect(wifi).toHaveLength(1); |
| 89 | + }); |
| 90 | + |
| 91 | + it("sends typed audio + power + settings commands", async () => { |
| 92 | + const { transport, rec } = makeTransport(); |
| 93 | + const client = createLinuxBridgeClient(transport); |
| 94 | + |
| 95 | + await client.setAudioLevel(0.42); |
| 96 | + await client.setAudioMuted(true); |
| 97 | + await client.shutdown(); |
| 98 | + await client.restart(); |
| 99 | + await client.suspend(); |
| 100 | + await client.openSettings(); |
| 101 | + |
| 102 | + expect(rec.sendCalls).toEqual([ |
| 103 | + { channel: LINUX_BRIDGE_CHANNELS.audio.setLevel, payload: { level: 0.42 } }, |
| 104 | + { channel: LINUX_BRIDGE_CHANNELS.audio.setMuted, payload: { muted: true } }, |
| 105 | + { channel: LINUX_BRIDGE_CHANNELS.power.shutdown, payload: {} }, |
| 106 | + { channel: LINUX_BRIDGE_CHANNELS.power.restart, payload: {} }, |
| 107 | + { channel: LINUX_BRIDGE_CHANNELS.power.suspend, payload: {} }, |
| 108 | + { channel: LINUX_BRIDGE_CHANNELS.settings.open, payload: {} }, |
| 109 | + ]); |
| 110 | + }); |
| 111 | +}); |
0 commit comments