122 lines
7.5 KiB
TypeScript
122 lines
7.5 KiB
TypeScript
import { createElement } from "react";
|
|
import { renderToStaticMarkup } from "react-dom/server";
|
|
import { expect, test, vi } from "vitest";
|
|
import { ChannelVoiceRooms } from "../src/client/channel/components/ChannelVoiceRooms";
|
|
import type { ChannelController, ReceiveStage } from "../src/client/channel/types";
|
|
import { setCopy, t } from "../src/client/ui/copy";
|
|
import { roomVoiceStatus, voicePlaybackPublications, waitingVoicePublication } from "../src/client/channel/use-voice-playback";
|
|
import { listeningVoicePublications } from "../src/client/channel/presentation";
|
|
import type { ChannelPublication, ChannelSnapshot } from "../src/client/channel/protocol";
|
|
|
|
function fixture() {
|
|
const publications: ChannelPublication[] = ["a", "b"].map(roomId => ({ id: `voice-${roomId}`, kind: "voice",
|
|
roomId, sessionId: "speaker", userId: "friend", audience: [], paused: false }));
|
|
const state = { publications, voice: ["a", "b"].flatMap(roomId => [
|
|
{ roomId, sessionId: "listener", muted: true, deafened: false },
|
|
{ roomId, sessionId: "speaker", muted: false, deafened: true },
|
|
]), subscriptions: publications.map(publication => publication.id) } as ChannelSnapshot;
|
|
const tracks = new Map(publications.map(publication => [publication.id,
|
|
{ id: `${publication.id}-track`, readyState: "live", muted: false, enabled: true } as MediaStreamTrack]));
|
|
const streams = new Map(publications.map(publication => [publication.id,
|
|
{ getAudioTracks: () => [tracks.get(publication.id)!] } as MediaStream]));
|
|
const ids = (previous: readonly string[] = []) => voicePlaybackPublications(state, "listener", streams, previous).map(publication => publication.id);
|
|
return { state, tracks, streams, ids };
|
|
}
|
|
|
|
test("one listening output follows a source endpoint while each room keeps its voice controls", () => {
|
|
const { state, streams, ids } = fixture();
|
|
expect(listeningVoicePublications(state, "listener", streams).map(publication => publication.id)).toEqual(["voice-a", "voice-b"]);
|
|
// Our muted microphone and the sender's deafened listening do not silence their speech.
|
|
expect(ids()).toEqual(["voice-a"]);
|
|
state.publications.push({ ...state.publications[0], id: "other-endpoint", sessionId: "another-speaker" });
|
|
state.voice.push({ roomId: "a", sessionId: "another-speaker", muted: false, deafened: false });
|
|
state.subscriptions.push("other-endpoint"); streams.set("other-endpoint", streams.get("voice-b")!);
|
|
expect(ids()).toEqual(["voice-a", "other-endpoint"]); // Same user, different microphone owner.
|
|
expect(state.subscriptions).toEqual(["voice-a", "voice-b", "other-endpoint"]);
|
|
});
|
|
|
|
test("an eligible output stays selected across reordering and a sibling becoming available", () => {
|
|
const { state, tracks, ids } = fixture();
|
|
expect(ids(["voice-b"])).toEqual(["voice-b"]);
|
|
state.publications.reverse();
|
|
expect(ids(["voice-a"])).toEqual(["voice-a"]);
|
|
Object.defineProperty(tracks.get("voice-a"), "muted", { configurable: true, value: true });
|
|
expect(ids(["voice-a"])).toEqual(["voice-b"]);
|
|
Object.defineProperty(tracks.get("voice-a"), "muted", { value: false });
|
|
expect(ids(["voice-b"])).toEqual(["voice-b"]);
|
|
});
|
|
|
|
test("sender room mute, listener room deafen and leave exclude only that room's output", () => {
|
|
const { state, ids } = fixture();
|
|
state.voice.find(voice => voice.sessionId === "speaker" && voice.roomId === "a")!.muted = true;
|
|
expect(ids(["voice-a"])).toEqual(["voice-b"]);
|
|
state.voice.find(voice => voice.sessionId === "speaker" && voice.roomId === "a")!.muted = false;
|
|
state.voice.find(voice => voice.sessionId === "listener" && voice.roomId === "a")!.deafened = true;
|
|
expect(ids(["voice-a"])).toEqual(["voice-b"]);
|
|
state.voice = state.voice.filter(voice => voice.sessionId !== "listener" || voice.roomId !== "b");
|
|
expect(ids(["voice-b"])).toEqual([]);
|
|
expect(state.subscriptions).toEqual(["voice-a", "voice-b"]);
|
|
});
|
|
|
|
test("selection requires the current live audio track rather than a retained or unready binding", () => {
|
|
const { state, streams, tracks, ids } = fixture();
|
|
const unready = { getAudioTracks: () => [{ readyState: "live", muted: true, enabled: true }] } as MediaStream;
|
|
streams.set("voice-a", unready);
|
|
expect(ids(["voice-a"])).toEqual(["voice-b"]);
|
|
Object.defineProperty(tracks.get("voice-b"), "readyState", { value: "ended" });
|
|
expect(ids(["voice-b"])).toEqual([]);
|
|
streams.set("voice-a", { getAudioTracks: () => [] } as unknown as MediaStream);
|
|
expect(ids()).toEqual([]);
|
|
streams.set("voice-a", { getAudioTracks: () => [{ readyState: "live", muted: false, enabled: true }] } as MediaStream);
|
|
state.publications[0].paused = true;
|
|
expect(ids()).toEqual([]);
|
|
state.publications[0].paused = false;
|
|
expect(ids()).toEqual(["voice-a"]);
|
|
});
|
|
|
|
test("voice rendering is independent of room membership and subscription", () => {
|
|
const { state } = fixture();
|
|
const rendering = new Map<string, "playing" | "blocked">();
|
|
const status = (roomId: string) => roomVoiceStatus(state, "listener", roomId, rendering);
|
|
// The connecting line is about the voice that was asked for and is not heard yet.
|
|
const waiting = (roomId: string) => waitingVoicePublication(state, "listener", roomId, rendering)?.id;
|
|
expect([status("a"), status("b")]).toEqual(["joining", "joining"]);
|
|
expect([waiting("a"), waiting("b")]).toEqual(["voice-a", "voice-b"]);
|
|
rendering.set("speaker", "playing");
|
|
expect([status("a"), status("b")]).toEqual(["connected", "connected"]);
|
|
expect(waiting("a")).toBeUndefined();
|
|
rendering.set("speaker", "blocked");
|
|
expect(status("a")).toBe("joined");
|
|
expect(waiting("a")).toBeUndefined();
|
|
rendering.clear();
|
|
state.voice.find(voice => voice.sessionId === "listener" && voice.roomId === "a")!.deafened = true;
|
|
state.subscriptions = state.subscriptions.filter(id => id !== "voice-b");
|
|
expect([status("a"), status("b")]).toEqual(["joined", "joined"]);
|
|
expect([waiting("a"), waiting("b")]).toEqual([undefined, undefined]);
|
|
});
|
|
|
|
test.each<[ReceiveStage | undefined, string]>([
|
|
[undefined, t("en", "channel.voiceDock.voiceConnecting")],
|
|
[{ step: "routing" }, t("en", "channel.voiceDock.voiceConnecting")],
|
|
[{ step: "connecting", peerId: "speaker" }, t("en", "screen.stage.connecting", { name: "Friend" })],
|
|
[{ step: "connecting", peerId: "relay" }, t("en", "screen.stage.connectingThrough", { name: "Relay" })],
|
|
[{ step: "connecting", peerId: "nobody" }, t("en", "channel.voiceDock.voiceConnecting")],
|
|
[{ step: "connected", peerId: "speaker" }, t("en", "channel.voiceDock.voiceConnecting")],
|
|
])("a room's connecting line names the step its awaited voice is at: %j", (stage, line) => {
|
|
setCopy({ lang: "en" });
|
|
const { state } = fixture();
|
|
Object.assign(state, {
|
|
channel: { rooms: [{ id: "a", name: "Room A", participantSessionIds: [] }], hostingMode: "lightweight", ownerId: "owner" },
|
|
users: [{ id: "friend", name: "Friend" }, { id: "other", name: "Relay" }],
|
|
sessions: [{ id: "speaker", userId: "friend" }, { id: "relay", userId: "other" }],
|
|
});
|
|
const receiveStage = vi.fn(() => stage);
|
|
const channel = { sessionId: "listener", receiveStage, voiceActivity: async () => new Map(), voiceMicrophonePending: () => false,
|
|
canUnmute: () => true } as unknown as ChannelController;
|
|
const html = renderToStaticMarkup(createElement(ChannelVoiceRooms, { channel, state,
|
|
joined: state.voice.filter(voice => voice.sessionId === "listener" && voice.roomId === "a"), browsingRoomId: "a", onBrowse() {},
|
|
renderingBySpeaker: new Map(), shortcuts: { bindings: {} as never, mac: false } }));
|
|
expect(html).toContain(`<small role="status" data-status="joining">${line}</small>`);
|
|
expect(receiveStage).toHaveBeenCalledWith("voice-a");
|
|
});
|