275 lines
19 KiB
TypeScript
275 lines
19 KiB
TypeScript
import { createElement, type ReactNode } from "react";
|
|
import { renderToStaticMarkup } from "react-dom/server";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
import { ChannelWatchVideo, WatchPeerAction } from "../src/client/channel/components/ChannelMedia";
|
|
import { ChannelStage, SourceAudience, type WatchStageSource } from "../src/client/channel/components/ChannelStage";
|
|
import { WatchPlayer } from "../src/client/channel/watch-playback";
|
|
import { setCopy, t, type Lang } from "../src/client/ui/copy";
|
|
import { ChannelSettingsError } from "../src/client/channel/components/ChannelSettingsDialog";
|
|
import { channelErrorPresentation } from "../src/client/channel/error-presentation";
|
|
import { locales } from "../src/client/locales";
|
|
import { WatchSourceDialog } from "../src/client/channel/components/ChannelShareControl";
|
|
import type { ChannelController } from "../src/client/channel/types";
|
|
|
|
vi.mock("../src/client/channel/components/ChannelSettingsDialog", async importOriginal => {
|
|
const actual = await importOriginal<typeof import("../src/client/channel/components/ChannelSettingsDialog")>();
|
|
return { ...actual, ChannelSettingsDialog: ({ children }: { children: ReactNode }) => createElement("div", null, children) };
|
|
});
|
|
|
|
describe("Webpage watch presentation", () => {
|
|
it("keeps shared seek and speed with the sharer while viewers see read-only progress", () => {
|
|
setCopy({ lang: "en" });
|
|
const player = { video: null, presentation: { kind: "external" }, media: {},
|
|
mediaState: { position: 12, duration: 120, rate: 1 }, error: null } as unknown as WatchPlayer;
|
|
const render = (owned: boolean) => renderToStaticMarkup(createElement(ChannelWatchVideo, {
|
|
player, owned, paused: true, label: "Owned source", presentation: "full", disabled: false,
|
|
website: true, originalPageAvailable: true, theaterMode: false, onToggleTheater: vi.fn(),
|
|
onPlayback: vi.fn(), onResume: vi.fn(), onFocusWindow: vi.fn(),
|
|
receiving: { peerAvailable: true, peerSupported: true, mode: "direct", browserSource: true, onPeer: vi.fn(), onDirect: vi.fn() },
|
|
}));
|
|
const owner = render(true), viewer = render(false);
|
|
expect(owner).toContain('aria-label="Playback progress for everyone"');
|
|
expect(owner).toContain('aria-valuetext="0:12 / 2:00"');
|
|
expect(viewer).toContain('>0:12 / 2:00</output>');
|
|
expect(owner.indexOf('aria-label="Playback speed for everyone"')).toBeGreaterThan(owner.indexOf('popover="auto"'));
|
|
expect(viewer).toContain('<progress');
|
|
expect(viewer).toContain("Playback follows the sharer. Volume only affects you.");
|
|
expect(viewer).not.toContain('aria-label="Playback progress for everyone"');
|
|
expect(viewer).not.toContain('aria-label="Playback speed for everyone"');
|
|
});
|
|
it("labels program-time playback by distance from its live edge, not an elapsed epoch", () => {
|
|
setCopy({ lang: "en" });
|
|
const epoch = 1794744000;
|
|
const player = { video: null, presentation: { kind: "external" }, media: {}, error: null,
|
|
mediaState: { position: epoch + 10, duration: null, rate: 1,
|
|
live: { active: true, seekable: [[epoch, epoch + 30]], syncPosition: epoch + 27 } } } as unknown as WatchPlayer;
|
|
const html = renderToStaticMarkup(createElement(ChannelWatchVideo, {
|
|
player, owned: true, paused: true, label: "Live source", presentation: "full", disabled: false,
|
|
website: true, originalPageAvailable: true, theaterMode: false, onToggleTheater: vi.fn(),
|
|
onPlayback: vi.fn(), onResume: vi.fn(), onFocusWindow: vi.fn(),
|
|
}));
|
|
expect(html).toContain(`aria-valuetext="${t("en", "watch.liveDelay", { seconds: "20" })}"`);
|
|
expect(html).not.toContain(' / ');
|
|
});
|
|
it.each([
|
|
{ appConnected: false, pagePlayer: false, connectAdvice: true },
|
|
{ appConnected: true, pagePlayer: false, connectAdvice: false },
|
|
{ appConnected: true, pagePlayer: true, connectAdvice: false },
|
|
])("uses the connected App's webpage capability in source advice: %j", ({ appConnected, pagePlayer, connectAdvice }) => {
|
|
setCopy({ lang: "en" });
|
|
const channel = { appConnected, pagePlayer, pending: new Set(), watchPage: () => undefined,
|
|
watchEmbed: () => undefined, sourceSelectionCommitting: () => false } as unknown as ChannelController;
|
|
const html = renderToStaticMarkup(createElement(WatchSourceDialog, { channel, roomId: "room", onClose: vi.fn() }));
|
|
expect(html.includes(t("en", "watch.connectApp"))).toBe(connectAdvice);
|
|
expect(html.includes('aria-label="Open webpage and select video"')).toBe(pagePlayer);
|
|
expect(html).toContain('aria-label="Play direct video URL"');
|
|
});
|
|
|
|
|
|
it.each([
|
|
[["watch_live_hls_unsupported", "watch_media_encryption_unsupported", "watch_media_encoding_unsupported", "watch_media_range_unsupported", "watch_media_size_unavailable", "watch_manifest_extension_unsupported", "watch_manifest_limit", "watch_resource_limit", "watch_mp4_parse_failed", "watch_indexed_unsupported", "invalid_watch_index"], "watch.forward.unsupported", false],
|
|
[["watch_media_decode_failed"], "watch.decodeFailed", false],
|
|
[["watch_source_inspection_unavailable"], "watch.sourceAccessRequired", false],
|
|
[["watch_media_unsupported", "watch_mp4_mse_unavailable"], "watch.playbackUnsupported", false],
|
|
[["watch_page_unavailable", "watch_page_closed", "page_player_closed"], "watch.forward.sourceUnavailable", false],
|
|
[["watch_invalid_manifest", "watch_media_credentials_unavailable", "watch_media_unavailable", "watch_media_timeout", "watch_media_changed", "watch_media_truncated", "invalid_watch_range_response", "watch_resource_unavailable", "watch_source_unavailable", "watch_source_failed", "watch_source_changed", "watch_source_retired"], "watch.forward.sourceUnavailable", false],
|
|
[["watch_source_busy", "watch_read_busy"], "watch.forward.busy", false],
|
|
[["watch_read_timeout"], "watch.forward.readTimeout", false],
|
|
[["watch_source_already_active"], "watch.forward.active", false],
|
|
[["watch_peer_requires_secure_context", "watch_peer_unavailable"], "watch.forward.secure", false],
|
|
[["watch_path_unavailable"], "channel.error.roomCommunicationFailed", true],
|
|
] as const)("presents %j with actionable copy from %s", (errors, key, connectionHelp) => {
|
|
for (const lang of Object.keys(locales) as Lang[]) {
|
|
for (const error of errors) {
|
|
expect(channelErrorPresentation(error, key => t(lang, key))).toEqual({ message: t(lang, key), connectionHelp });
|
|
}
|
|
}
|
|
});
|
|
|
|
it("shows source reading failures in the existing action error panel", () => {
|
|
setCopy({ lang: "en" });
|
|
const html = renderToStaticMarkup(createElement(ChannelSettingsError, { error: "watch_media_credentials_unavailable" }));
|
|
expect(html).toContain('role="alert"');
|
|
expect(html).toContain("Could not read this video source");
|
|
expect(html).not.toMatch(/sign-in|original webpage|Connect Piik App/);
|
|
expect(html).not.toContain("identity");
|
|
});
|
|
|
|
it("explains a peer decoder failure without suggesting a room connection fault", () => {
|
|
setCopy({ lang: "en" });
|
|
const player = { video: null, presentation: { kind: "external" }, media: {},
|
|
blocked: false, error: "watch_media_decode_failed", mediaState: null } as unknown as WatchPlayer;
|
|
const html = renderToStaticMarkup(createElement(ChannelWatchVideo, {
|
|
player, owned: false, paused: true, label: "Video", presentation: "full", disabled: false, website: true, originalPageAvailable: true,
|
|
theaterMode: false, onToggleTheater: vi.fn(), onPlayback: vi.fn(), onResume: vi.fn(), onFocusWindow: vi.fn(),
|
|
receiving: { peerAvailable: true, peerSupported: true, mode: "peer", browserSource: true, onPeer: vi.fn(), onDirect: vi.fn() },
|
|
}));
|
|
expect(html).toContain("could not decode"); expect(html).not.toContain("Check the room connection");
|
|
expect(channelErrorPresentation("https://media.test/private?grant=secret", key => locales.en.copy[key]).message).not.toContain("secret");
|
|
});
|
|
|
|
it("offers peer reception only for an enabled source and a supported browser", () => {
|
|
setCopy({ lang: "en" });
|
|
const receiving = { peerAvailable: true, peerSupported: true, mode: "direct" as const, browserSource: true, onPeer: vi.fn(), onDirect: vi.fn() };
|
|
const render = (patch = {}) => renderToStaticMarkup(createElement(WatchPeerAction, { receiving: { ...receiving, ...patch } }));
|
|
expect(render()).toContain('aria-label="Receive from sharer"');
|
|
expect(render({ peerAvailable: false })).toBe("");
|
|
expect(render({ mode: "peer" })).toBe("");
|
|
expect(render({ mode: "peer", failed: true })).toContain('aria-label="Receive from sharer"');
|
|
expect(render({ mode: "peer", failed: true, peerAvailable: false })).toMatch(/<button\b[^>]*disabled/);
|
|
expect(render({ peerSupported: false })).toContain("trusted HTTPS address");
|
|
expect(render({ peerSupported: false })).not.toContain("<button");
|
|
});
|
|
|
|
it.each(["watch_source_inspection_unavailable", "watch_media_unavailable", "watch_media_unsupported"])(
|
|
"keeps %s factual when this platform has no original-page capability", error => {
|
|
setCopy({ lang: "en" });
|
|
const player = { video: null, presentation: { kind: "embed" }, media: {}, error } as unknown as WatchPlayer;
|
|
const html = renderToStaticMarkup(createElement(ChannelWatchVideo, {
|
|
player, owned: false, paused: true, label: "Source", presentation: "full", disabled: false, website: true, originalPageAvailable: false,
|
|
theaterMode: false, onToggleTheater: vi.fn(), onPlayback: vi.fn(), onResume: vi.fn(), onFocusWindow: vi.fn(),
|
|
}));
|
|
expect(html).toContain('role="alert"');
|
|
expect(html).not.toMatch(/App|original webpage|sign-in|CORS|authentication/);
|
|
});
|
|
|
|
it("offers an already available peer source without also asking the viewer to connect App", () => {
|
|
setCopy({ lang: "en" });
|
|
const player = { video: null, presentation: { kind: "embed" }, media: {}, error: "watch_media_unavailable" } as unknown as WatchPlayer;
|
|
const receiving = { peerAvailable: true, peerSupported: true, mode: "direct" as const, browserSource: true, failed: true, onPeer: vi.fn(), onDirect: vi.fn() };
|
|
const render = (onAppWatch?: () => void) => renderToStaticMarkup(createElement(ChannelWatchVideo, {
|
|
player, owned: false, paused: true, label: "Source", presentation: "full", disabled: false, website: true, originalPageAvailable: true,
|
|
theaterMode: false, onToggleTheater: vi.fn(), onPlayback: vi.fn(), onResume: vi.fn(), onFocusWindow: vi.fn(), receiving, onAppWatch,
|
|
}));
|
|
expect(render()).toContain('aria-label="Receive from sharer"');
|
|
expect(render()).not.toContain("Connect Piik App");
|
|
expect(render(vi.fn())).toContain('aria-label="Watch in App"');
|
|
});
|
|
|
|
it("an owner's failure returns to its retained provider instead of offering a viewer-only watch", () => {
|
|
setCopy({ lang: "en" });
|
|
const player = { video: null, presentation: { kind: "embed" }, media: { page: {} }, error: "watch_media_unavailable" } as unknown as WatchPlayer;
|
|
const html = renderToStaticMarkup(createElement(ChannelWatchVideo, {
|
|
player, owned: true, paused: true, label: "Source", presentation: "full", disabled: false, website: true, originalPageAvailable: true,
|
|
theaterMode: false, onToggleTheater: vi.fn(), onPlayback: vi.fn(), onResume: vi.fn(), onFocusWindow: vi.fn(), onAppWatch: vi.fn(),
|
|
}));
|
|
expect(html).toContain('aria-label="Return to playback window"');
|
|
expect(html).not.toContain('aria-label="Watch in App"');
|
|
expect(html).not.toContain("Connect Piik App");
|
|
});
|
|
it("does not treat a metadata sample or a decoded but unpainted video as a visible picture", () => {
|
|
setCopy({ lang: "en" });
|
|
vi.stubGlobal("document", { fullscreenEnabled: false });
|
|
try {
|
|
const render = (patch = {}) => {
|
|
const player = { video: { readyState: 0, parentElement: null }, media: {}, mediaState: null, blocked: false, error: null, ...patch };
|
|
return renderToStaticMarkup(createElement(ChannelWatchVideo, {
|
|
player: Object.setPrototypeOf({ ...player, presentation: { kind: "video", element: player.video } }, WatchPlayer.prototype) as WatchPlayer,
|
|
owned: false, paused: true, label: "Preparing", presentation: "full", disabled: false,
|
|
website: false, originalPageAvailable: false, theaterMode: false, onToggleTheater: vi.fn(), onPlayback: vi.fn(), onResume: vi.fn(), onFocusWindow: vi.fn(),
|
|
}));
|
|
};
|
|
expect(render()).toContain('role="status"');
|
|
expect(render()).toContain("Preparing video");
|
|
expect(render({ mediaState: { readyState: 4 } })).toContain("Preparing video");
|
|
expect(render({ video: { readyState: 4, videoWidth: 640, parentElement: null } })).toContain("Preparing video");
|
|
expect(render({ video: { readyState: 4, videoWidth: 640, parentElement: null }, hasPresentedFrame: true })).not.toContain("Preparing video");
|
|
expect(render({ video: { readyState: 4, parentElement: null } })).not.toContain("Preparing video");
|
|
expect(render({ blocked: true })).not.toContain("Preparing video");
|
|
expect(render({ error: "watch_media_failed" })).not.toContain("Preparing video");
|
|
} finally { vi.unstubAllGlobals(); }
|
|
});
|
|
|
|
it.each([true, false])("shows the independent webpage's real controls without a fake video (owner: %s)", owned => {
|
|
setCopy({ lang: "en" });
|
|
const player = { video: null, presentation: { kind: "external" }, media: {}, blocked: false, error: null,
|
|
mediaState: { position: 42, duration: 240, paused: true, rate: 1.5, readyState: 4, ended: false, buffered: [[0, 60]] } } as unknown as WatchPlayer;
|
|
const html = renderToStaticMarkup(createElement(ChannelWatchVideo, {
|
|
player, owned, paused: true, label: "video.example/watch/evening", presentation: "full", disabled: false,
|
|
website: true, originalPageAvailable: true, theaterMode: false, onToggleTheater: vi.fn(), onPlayback: vi.fn(), onResume: vi.fn(), onFocusWindow: vi.fn(),
|
|
}));
|
|
expect(html).toContain("This video uses a separate webpage window");
|
|
expect(html).toContain('aria-label="Return to playback window"');
|
|
expect(html).not.toContain("<video");
|
|
expect(html).not.toContain('aria-label="Picture in picture"');
|
|
expect(html).not.toContain('aria-label="Mute"');
|
|
expect(html.match(/class="c3-mini-controls\b/g) ?? []).toHaveLength(owned ? 1 : 0);
|
|
expect(html.includes('aria-label="Playback for everyone"')).toBe(owned);
|
|
expect(html.includes('aria-label="Playback progress for everyone"')).toBe(owned);
|
|
if (owned) expect(html).toContain('max="240"');
|
|
});
|
|
|
|
it("offers manual App viewing only when the website has no Browser candidate", () => {
|
|
setCopy({ lang: "en" });
|
|
const source: WatchStageSource = { kind: "watch", id: "activity", userId: "friend", name: "Bo", title: "video.example/watch/evening",
|
|
roomName: "Friday", audience: [], owned: false, subscribed: false, paused: true, website: true, originalPageAvailable: true, browserSource: false,
|
|
onRoom: vi.fn(), onJoin: vi.fn(), onLeave: vi.fn(), onStop: vi.fn(), onResume: vi.fn(), onPlayback: vi.fn(), onFocusWindow: vi.fn(), onAppWatch: vi.fn() };
|
|
const render = (value: WatchStageSource) => renderToStaticMarkup(createElement(ChannelStage, { sources: [value], 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() }));
|
|
expect(render(source)).toContain('aria-label="Watch in App"');
|
|
const browser = render({ ...source, browserSource: true });
|
|
expect(browser).toContain('aria-label="Watch"');
|
|
expect(browser).not.toContain('aria-label="Watch in App"');
|
|
const program = render({ ...source, onAppWatch: undefined, originalPageAvailable: false });
|
|
expect(program).not.toContain("Connect Piik App");
|
|
expect(program).not.toContain('aria-label="Watch in App"');
|
|
const disconnected = render({ ...source, onAppWatch: undefined });
|
|
expect(disconnected).toContain("Connect Piik App");
|
|
expect(disconnected).not.toContain('aria-label="Watch in App"');
|
|
const receiving = { peerAvailable: true, peerSupported: true, mode: "direct" as const, browserSource: false, onPeer: vi.fn(), onDirect: vi.fn() };
|
|
const forwarded = render({ ...source, onAppWatch: undefined, receiving });
|
|
expect(forwarded).toContain('aria-label="Receive from sharer"');
|
|
expect(forwarded).not.toContain("Connect Piik App");
|
|
expect(forwarded).not.toContain('aria-label="Watch in App"');
|
|
const directAndForwarded = render({ ...source, browserSource: true, receiving: { ...receiving, browserSource: true } });
|
|
expect(directAndForwarded).toContain('aria-label="Watch"');
|
|
expect(directAndForwarded).toContain('aria-label="Receive from sharer"');
|
|
});
|
|
});
|
|
|
|
it("counts watch joins without claiming screen delivery readiness", () => {
|
|
setCopy({ lang: "en" });
|
|
const person = { userId: "friend", name: "Bo" };
|
|
const joined = renderToStaticMarkup(createElement(SourceAudience, { people: [person], joined: true }));
|
|
expect(joined).toContain('aria-label="Joined this watch: 1"');
|
|
expect(joined).toContain("Joined this watch");
|
|
expect(joined).not.toContain("Connecting");
|
|
const preparing = renderToStaticMarkup(createElement(SourceAudience, { people: [{ ...person, state: "preparing" }] }));
|
|
expect(preparing).toContain('aria-label="Viewers: 0 watching"');
|
|
});
|
|
|
|
|
|
it("keeps return-to-source reachable for a healthy main player only while it retains its Native page", () => {
|
|
setCopy({ lang: "en" });
|
|
vi.stubGlobal("document", { fullscreenEnabled: false });
|
|
try {
|
|
const video = { readyState: 4, parentElement: null };
|
|
const render = (owned: boolean, page?: object) => renderToStaticMarkup(createElement(ChannelWatchVideo, {
|
|
player: { video, presentation: { kind: "video", element: video }, media: { page }, mediaState: {readyState:4}, blocked: false, error: null } as unknown as WatchPlayer,
|
|
owned, paused: true, label: "Prepared source", presentation: "full", disabled: false, website: true, originalPageAvailable: true,
|
|
theaterMode: false, onToggleTheater: vi.fn(), onPlayback: vi.fn(), onResume: vi.fn(), onFocusWindow: vi.fn(),
|
|
}));
|
|
expect(render(true, {})).toContain('aria-label="Return to playback window"');
|
|
expect(render(true)).not.toContain('aria-label="Return to playback window"');
|
|
expect(render(false, {})).not.toContain('aria-label="Return to playback window"');
|
|
} finally { vi.unstubAllGlobals(); }
|
|
});
|
|
|
|
|
|
it("keeps an authority-reported screen delivery failure in the picture with an explicit retry", () => {
|
|
setCopy({ lang: "en" });
|
|
const html = renderToStaticMarkup(createElement(ChannelStage, {
|
|
sources: [{ kind: "screen", id: "failed-picture", userId: "friend", name: "Friend", roomName: "Room",
|
|
source: { kind: "screen", name: "Owned picture" }, title: null, owned: false, localSource: false,
|
|
subscribed: true, paused: false, audience: [], deliveryFailed: true }],
|
|
focusedId: null, canSubscribe: true, onFocus() {}, onWatch() {}, onUnwatch() {}, onRetryPicture() {}, onPause() {}, onStop() {},
|
|
onInputPolicy: async () => {}, onInputDecide: async () => {}, onInputSimultaneous: async () => {}, onInputShortcut: async () => ({ key: 1, available: false }),
|
|
}));
|
|
expect(html).toContain('data-publication-id="failed-picture"');
|
|
expect(html).toContain('data-failed="true"');
|
|
expect(html).toContain(t("en", "screen.deliveryRetry"));
|
|
expect(html).toContain('aria-label="' + t("en", "screen.retryPicture") + '"');
|
|
expect(html).not.toContain(t("en", "channel.previewPage.reconnecting"));
|
|
});
|