191 lines
11 KiB
TypeScript
191 lines
11 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
import { createElement } from "react";
|
|
import { renderToStaticMarkup } from "react-dom/server";
|
|
|
|
import { nextViewerMediaBinding, playbackFailure, prepareViewerPlayback } from "../src/client/media/viewer-playback.ts";
|
|
import { ChannelVideo } from "../src/client/channel/components/ChannelMedia";
|
|
import { ChannelStage, type ChannelStageProps } from "../src/client/channel/components/ChannelStage";
|
|
import { PlaybackControlsView } from "../src/client/components/living/PlaybackControls";
|
|
import { setCopy } from "../src/client/ui/copy";
|
|
|
|
describe("Viewer playback binding", () => {
|
|
it("distinguishes canceled playback from autoplay denial and actual failures", () => {
|
|
expect(playbackFailure(new DOMException("Paused", "AbortError"))).toBeNull();
|
|
expect(playbackFailure({ name: "AbortError" })).toBeNull();
|
|
expect(playbackFailure(new DOMException("Blocked", "NotAllowedError"))).toBe("autoplay-blocked");
|
|
expect(playbackFailure(new DOMException("Unsupported", "NotSupportedError"))).toBe("playback-failed");
|
|
expect(playbackFailure(new Error("decode failed"))).toBe("playback-failed");
|
|
expect(playbackFailure(null)).toBe("playback-failed");
|
|
});
|
|
|
|
it("retains video identity across audio arrival, replacement and removal", () => {
|
|
let audio: { id: string }[] = [];
|
|
const stream = { getVideoTracks: () => [{ id: "video" }], getAudioTracks: () => audio } as MediaStream;
|
|
const original = nextViewerMediaBinding(null, stream, 3, 7);
|
|
let binding = original;
|
|
for (const nextAudio of [[{ id: "audio-a" }], [{ id: "audio-b" }], []]) {
|
|
audio = nextAudio;
|
|
binding = nextViewerMediaBinding(binding, stream, 4, 8);
|
|
expect(binding).toMatchObject({ generation: 7, boundAtRevision: 3, videoTrackKey: "video", audioTrackKey: nextAudio[0]?.id ?? "" });
|
|
expect(nextViewerMediaBinding(binding, stream, 5, 8)).toBe(binding);
|
|
}
|
|
expect(original.audioTrackKey).toBe("");
|
|
});
|
|
|
|
it("creates a new picture identity for a new video or stream, including after retirement", () => {
|
|
let id = "video-a";
|
|
const stream = { getVideoTracks: () => [{ id }], getAudioTracks: () => [] } as unknown as MediaStream;
|
|
const first = nextViewerMediaBinding(null, stream, 1, 1);
|
|
id = "video-b";
|
|
const changed = nextViewerMediaBinding(first, stream, 2, 2);
|
|
expect(changed).toMatchObject({ generation: 2, boundAtRevision: 2, videoTrackKey: "video-b" });
|
|
expect(nextViewerMediaBinding(changed, { ...stream }, 3, 3).generation).toBe(3);
|
|
expect(nextViewerMediaBinding(null, stream, 4, 4).generation).toBe(4);
|
|
});
|
|
|
|
it("arms every media generation even when the MediaStream identity is reused", () => {
|
|
const stream = {} as MediaStream;
|
|
const video = {
|
|
srcObject: null as MediaProvider | null,
|
|
pause: vi.fn(),
|
|
};
|
|
const pauseState = { active: false, resume: false };
|
|
|
|
expect(prepareViewerPlayback(video, stream, false, pauseState)).toBe(true);
|
|
expect(video.srcObject).toBe(stream);
|
|
expect(prepareViewerPlayback(video, stream, false, pauseState)).toBe(true);
|
|
expect(video.pause).not.toHaveBeenCalled();
|
|
|
|
expect(prepareViewerPlayback(video, stream, true, pauseState)).toBe(false);
|
|
expect(video.pause).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
it.each([false, true])("arms Host resume on a new binding regardless of the old local pause (%s)", (resume) => {
|
|
const stream = {} as MediaStream;
|
|
const video = { srcObject: stream, pause: vi.fn() };
|
|
const pauseState = { active: true, resume };
|
|
|
|
// A new video generation can reuse the same MediaStream object.
|
|
expect(prepareViewerPlayback(video, stream, true, pauseState)).toBe(false);
|
|
expect(video.pause).toHaveBeenCalledOnce();
|
|
expect(pauseState).toEqual({ active: true, resume: true });
|
|
|
|
// If the Host has already resumed, the new binding starts immediately.
|
|
expect(prepareViewerPlayback(video, stream, false, pauseState)).toBe(true);
|
|
expect(pauseState).toEqual({ active: false, resume: false });
|
|
});
|
|
});
|
|
|
|
describe("Playback audio controls", () => {
|
|
it("keeps source sound in publishing controls and allows immediate mute while capture is busy", () => {
|
|
setCopy({ lang: "zh", vis: false });
|
|
const stream = { id: "mixed", getAudioTracks: () => [{ kind: "audio", readyState: "live" }] } as unknown as MediaStream;
|
|
const sourceAudio = { hasAudio: false, available: false, level: 1, muted: false, busy: false, canMute: true, onChange: vi.fn() };
|
|
const source = { id: "screen", userId: "self", name: "Me", roomName: "Room", source: { kind: "screen" as const, name: "Own screen" },
|
|
title: null, owned: true, localSource: true, audience: [], subscribed: true, paused: false, stream };
|
|
const render = (sound = sourceAudio) => renderToStaticMarkup(createElement(ChannelStage, {
|
|
sources: [{ ...source, sourceAudio: sound }], focusedId: null, canSubscribe: true,
|
|
onFocus: vi.fn(), onWatch: vi.fn(), onUnwatch: vi.fn(), onPause: vi.fn(), onStop: vi.fn(),
|
|
onInputPolicy: vi.fn(), onInputDecide: vi.fn(), onInputSimultaneous: vi.fn(), onInputShortcut: vi.fn(),
|
|
}));
|
|
const html = render();
|
|
expect(html).toContain('aria-label="Own screen的分享控制"');
|
|
expect(html.split("<footer>")[0]).not.toContain("所有观众");
|
|
expect(html).not.toContain('class="c3-mini-volume"');
|
|
expect(html.match(/<button\b[^>]*aria-label="分享源没有音频"[^>]*>/)?.[0]).toContain("disabled");
|
|
|
|
const live = { ...sourceAudio, hasAudio: true, available: true };
|
|
const silent = render({ ...live, muted: true, level: 0 });
|
|
const silentRange = silent.match(/<input\b[^>]*type="range"[^>]*>/)![0];
|
|
expect(silentRange).not.toContain("disabled");
|
|
expect(silentRange).toContain('aria-valuetext="0%"');
|
|
expect(silent).toContain('aria-label="恢复所有观众的分享声音"');
|
|
const busy = render({ ...live, busy: true });
|
|
expect(busy.match(/<button\b[^>]*aria-label="关闭所有观众的分享声音"[^>]*>/)?.[0]).not.toContain("disabled");
|
|
const pendingUnmute = render({ ...live, busy: true, muted: true });
|
|
expect(pendingUnmute.match(/<button\b[^>]*aria-label="恢复所有观众的分享声音"[^>]*>/)?.[0]).toContain("disabled");
|
|
});
|
|
|
|
it("gives the shared volume slider the same no-audio label as its button", () => {
|
|
setCopy({ lang: "zh", vis: false });
|
|
const noop = () => {};
|
|
const html = renderToStaticMarkup(createElement(PlaybackControlsView, {
|
|
audio: { level: 1, muted: false, boostAvailable: true }, paused: false, hasAudio: false, canPlay: true,
|
|
sharingAudio: true, audioControlAvailable: false, theaterMode: false,
|
|
fullscreen: { supported: false, ready: false, active: false },
|
|
picture: { supported: false, active: false, failed: false, toggle: async () => {} },
|
|
togglePlayback: noop, toggleFullscreen: noop, toggleMute: noop, setLevel: noop, onToggleTheater: noop,
|
|
}));
|
|
const range = html.match(/<input\b[^>]*type="range"[^>]*>/)![0];
|
|
expect(range).toContain('aria-label="分享源没有音频"');
|
|
expect(range).toContain('aria-valuetext="分享源没有音频"');
|
|
expect(range).toContain("disabled");
|
|
});
|
|
});
|
|
|
|
describe("Local source playback", () => {
|
|
it("keeps owned=false/localSource=true silent and a remote same-user source unowned", () => {
|
|
setCopy({ lang: "en", vis: false });
|
|
const stream = { id: "same-machine", getAudioTracks: () => [{ readyState: "live" }] } as unknown as MediaStream;
|
|
const html = renderToStaticMarkup(createElement(ChannelVideo, {
|
|
stream, owned: false, localSource: true, label: "Local source", theaterMode: false, onToggleTheater: vi.fn(),
|
|
}));
|
|
expect(html).toContain("<video");
|
|
expect(html).not.toContain('class="c3-mini-volume"');
|
|
expect(html).not.toContain("No audio in this share");
|
|
const source = { id: "screen", userId: "self", name: "Me", roomName: "Room", source: { kind: "screen" as const, name: null },
|
|
title: null, owned: false, localSource: true, audience: [], subscribed: true, paused: false, stream };
|
|
const props: ChannelStageProps = { sources: [source], focusedId: null, canSubscribe: true,
|
|
onFocus: vi.fn(), onWatch: vi.fn(), onUnwatch: vi.fn(), onPause: vi.fn(), onStop: vi.fn(),
|
|
onInputPolicy: vi.fn(), onInputDecide: vi.fn(), onInputSimultaneous: vi.fn(), onInputShortcut: vi.fn() };
|
|
const local = renderToStaticMarkup(createElement(ChannelStage, props));
|
|
expect(local).toContain("Local preview");
|
|
expect(local).toContain('aria-label="Stop watching Shared screen"');
|
|
expect(local).not.toContain('aria-label="Stop sharing');
|
|
const remote = renderToStaticMarkup(createElement(ChannelStage, { ...props, sources: [{ ...source, localSource: false }] }));
|
|
expect(remote).not.toContain("Local preview");
|
|
expect(remote).toContain('class="c3-mini-volume"');
|
|
expect(remote).toContain('aria-label="Stop watching Shared screen"');
|
|
});
|
|
});
|
|
|
|
describe("Screen delivery result", () => {
|
|
it("shows the watch failure without replacing an existing picture or changing its controls", () => {
|
|
setCopy({ lang: "en", vis: false });
|
|
const source = { audience: [], id: "screen", userId: "friend", name: "Friend", roomName: "Room", source: { kind: "screen" as const, name: null }, title: null, owned: false, localSource: false,
|
|
subscribed: true, paused: false, deliveryFailed: true };
|
|
const props: ChannelStageProps = { sources: [source], focusedId: null, canSubscribe: true,
|
|
onFocus: vi.fn(), onWatch: vi.fn(), onUnwatch: vi.fn(), onPause: vi.fn(), onStop: vi.fn(),
|
|
onInputPolicy: vi.fn(), onInputDecide: vi.fn(), onInputSimultaneous: vi.fn(), onInputShortcut: vi.fn() };
|
|
const failed = renderToStaticMarkup(createElement(ChannelStage, props));
|
|
expect(failed).toContain("Picture delivery failed. Retrying.");
|
|
expect(failed).not.toContain("Connecting picture");
|
|
expect(failed).toContain('aria-label="Stop watching Shared screen"');
|
|
|
|
const stream = { id: "retained", getAudioTracks: () => [] } as unknown as MediaStream;
|
|
const retained = renderToStaticMarkup(createElement(ChannelStage, { ...props, sources: [{ ...source, stream }] }));
|
|
expect(retained).toContain("<video");
|
|
expect(retained).not.toContain("Picture delivery failed");
|
|
expect(retained).toContain('aria-label="Stop watching Shared screen"');
|
|
const paused = renderToStaticMarkup(createElement(ChannelStage, { ...props, sources: [{ ...source, paused: true }] }));
|
|
expect(paused).not.toContain("Picture delivery failed");
|
|
});
|
|
});
|
|
|
|
|
|
it("shows shared buffer intent on received screens, including limitations, and omits local previews", () => {
|
|
setCopy({ lang: "en", vis: false });
|
|
const stream = { id: "buffer-ui", getAudioTracks: () => [] } as unknown as MediaStream;
|
|
const props = { stream, owned: false, localSource: false, label: "Screen", theaterMode: false, onToggleTheater: vi.fn(),
|
|
buffering: { target: 725, status: "unsupported" as const, onChange: vi.fn() } };
|
|
const remote = renderToStaticMarkup(createElement(ChannelVideo, props));
|
|
expect(remote).toContain('aria-label="Playback buffering"');
|
|
expect(remote).toContain('name="screen-buffer-target"');
|
|
expect(remote).toContain('max="4000"');
|
|
expect(remote).toContain('value="725"');
|
|
expect(remote).toContain("This browser cannot set the playback buffer target.");
|
|
expect(remote).toContain("viewers downstream");
|
|
const preview = renderToStaticMarkup(createElement(ChannelVideo, { ...props, localSource: true }));
|
|
expect(preview).not.toContain('aria-label="Playback buffering"');
|
|
});
|