Files
element-call/src/lotus/lotusDevices.test.ts
T
Lotus CIandClaude Opus 5.5 a366243836
CI / Build embedded bundle (push) Skipped
CI / Build embedded bundle (pull_request) Successful in 4m21s
CI / Publish to Gitea npm registry (pull_request) Skipped
feat(lotus): expected-device check before joining (cinny #121)
EC remembers the picked mic / output by id and silently falls back to the
first device when it is missing, so a user who always joins with a headset
lands on the laptop mic. With lotusDeviceCheck=1:
- the last-picked device's NAME is remembered next to its id
  (lotus-audio-{input,output}-name), since ids can change on re-plug;
- before the automatic join, a usual device that is gone is reported to the
  host (io.lotus.device_missing) and EC waits for io.lotus.device_decision
  { join }; one that came back under a new id is just re-selected;
- in a call, losing it is reported (io.lotus.device_lost) for a toast.
Fails open (no names / no host / nothing remembered). Without the flag the
join stays synchronous, as upstream. The gate runs once per call: CallView's
effect re-runs while its inputs settle and must not restart it.

Embedded version 0.28.0-lotus.2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017PCb59uY3UKqDqt7B6hzd5
2026-10-24 19:42:23 -04:00

189 lines
5.9 KiB
TypeScript

/*
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<string, (ev: CustomEvent) => 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<string, { type: string; name?: string }> =>
new Map(names.map(([id, name]) => [id, { type: "name", name }]));
type Labels = Map<string, { type: string; name?: string }>;
const source = (
labels: Labels,
): { available$: BehaviorSubject<Labels>; select: (id: string) => void } => ({
available$: new BehaviorSubject(labels),
select: vi.fn<(id: string) => void>(),
});
const devices = (
inputs: Labels,
outputs: Labels = named(["", "Default"]),
): {
audioInput: ReturnType<typeof source>;
audioOutput: ReturnType<typeof source>;
} => ({ 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();
});
});