/* Copyright 2026 Lotus Guild SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial Please see LICENSE in the repository root for full details. */ import { BehaviorSubject } from "rxjs"; import { afterEach, describe, expect, it, vi } from "vitest"; const sent: { action: string; data: unknown }[] = []; const handlers = new Map void>(); vi.mock("../widget", () => ({ widget: { api: { transport: { reply: vi.fn() } }, lazyActions: { on: (a: string, h: (ev: CustomEvent) => void): void => void handlers.set(a, h), off: (a: string): void => void handlers.delete(a), }, }, })); vi.mock("./lotusWidget", () => ({ lotusFlag: (name: string): boolean => name === "lotusDeviceCheck", lotusSendToHost: (action: string, data: unknown): boolean => { sent.push({ action, data }); return true; }, })); const { checkDevice, lotusDeviceGate, startLotusDevices, audioInputName, audioOutputName, } = await import("./lotusDevices"); const { audioInput, audioOutput } = await import("../settings/settings"); const named = ( ...names: [string, string][] ): Map => new Map(names.map(([id, name]) => [id, { type: "name", name }])); type Labels = Map; const source = ( labels: Labels, ): { available$: BehaviorSubject; select: (id: string) => void } => ({ available$: new BehaviorSubject(labels), select: vi.fn<(id: string) => void>(), }); const devices = ( inputs: Labels, outputs: Labels = named(["", "Default"]), ): { audioInput: ReturnType; audioOutput: ReturnType; } => ({ audioInput: source(inputs), audioOutput: source(outputs) }); afterEach(() => { sent.length = 0; handlers.clear(); audioInput.setValue(undefined); audioOutput.setValue(undefined); audioInputName.setValue(undefined); audioOutputName.setValue(undefined); }); describe("[cinny #121] checkDevice", () => { const avail = named(["laptop", "Laptop Mic"], ["arctis2", "Arctis 7"]); it("is fine when the usual device is there, or nothing is remembered", () => { expect(checkDevice("audioinput", "arctis2", "Arctis 7", avail).result).toBe( "ok", ); expect(checkDevice("audioinput", undefined, undefined, avail).result).toBe( "ok", ); expect(checkDevice("audioinput", "default", "x", avail).result).toBe("ok"); }); it("re-selects a device that came back under a new id", () => { expect(checkDevice("audioinput", "arctis1", "Arctis 7", avail)).toEqual({ kind: "audioinput", result: "reselect", id: "arctis2", }); }); it("reports a missing device and what EC would use instead", () => { expect( checkDevice( "audioinput", "arctis1", "Arctis 7", named(["laptop", "Laptop Mic"]), ), ).toEqual({ kind: "audioinput", result: "missing", expected: "Arctis 7", fallback: "Laptop Mic", }); }); it("fails open without device names (no permission yet)", () => { const unnamed = new Map([["x", { type: "number" }]]); expect( checkDevice("audioinput", "arctis1", "Arctis 7", unnamed).result, ).toBe("ok"); }); }); describe("[cinny #121] lotusDeviceGate", () => { it("joins straight away when everything is there", async () => { audioInput.setValue("arctis"); audioInputName.setValue("Arctis 7"); await expect( lotusDeviceGate(devices(named(["arctis", "Arctis 7"]))), ).resolves.toBe(true); expect(sent).toEqual([]); }); it("asks the host and waits for the answer", async () => { audioInput.setValue("arctis"); audioInputName.setValue("Arctis 7"); const gate = lotusDeviceGate(devices(named(["laptop", "Laptop Mic"]))); await vi.waitFor(() => expect(sent).toHaveLength(1)); expect(sent[0]).toEqual({ action: "io.lotus.device_missing", data: { missing: [ { kind: "audioinput", expected: "Arctis 7", fallback: "Laptop Mic" }, ], }, }); handlers.get("io.lotus.device_decision")!( new CustomEvent("x", { detail: { data: { join: false } } }), ); await expect(gate).resolves.toBe(false); }); it("re-selects a re-plugged device without asking", async () => { audioInput.setValue("old-id"); audioInputName.setValue("Arctis 7"); const d = devices(named(["new-id", "Arctis 7"])); await expect(lotusDeviceGate(d)).resolves.toBe(true); expect(d.audioInput.select).toHaveBeenCalledWith("new-id"); expect(sent).toEqual([]); }); it("joins when device names never arrive", async () => { audioInput.setValue("arctis"); audioInputName.setValue("Arctis 7"); const d = devices( new Map([["x", { type: "number" }]]), new Map([["y", { type: "number" }]]), ); await expect(lotusDeviceGate(d, 50)).resolves.toBe(true); }); }); describe("[cinny #121] startLotusDevices", () => { it("remembers the picked device's name and reports when it goes", () => { const d = devices(named(["laptop", "Laptop Mic"], ["arctis", "Arctis 7"])); const stop = startLotusDevices(d); audioInput.setValue("arctis"); expect(audioInputName.getValue()).toBe("Arctis 7"); d.audioInput.available$.next(named(["laptop", "Laptop Mic"])); expect(sent).toEqual([ { action: "io.lotus.device_lost", data: { kind: "audioinput", expected: "Arctis 7", fallback: "Laptop Mic", }, }, ]); // Name kept, so the next join can still recognise it. expect(audioInputName.getValue()).toBe("Arctis 7"); stop(); }); it("choosing the system default clears the remembered name", () => { audioInputName.setValue("Arctis 7"); const stop = startLotusDevices(devices(named(["", "Default"]))); audioInput.setValue(""); expect(audioInputName.getValue()).toBeUndefined(); stop(); }); });