Files
piik/tests/native-capture-selection.test.ts
T
AAA 64e6281eae feat(i18n): replace visual mode with more languages
Use one text catalog across the 3.0 launcher, rooms and shared controls. Add Traditional Chinese, Japanese and Korean, expose them through the third language control, and remove the separate visual-mode state and rendering paths. Preserve ordinary icons, illustrations and motion.
2026-11-14 10:40:23 -04:00

320 lines
16 KiB
TypeScript

import { afterEach, describe, expect, it } from "vitest";
import { createElement } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { CaptureSourcePicker } from "../src/client/components/living/CaptureSourcePicker";
import { AudioExclusionSelect } from "../src/client/components/living/AudioExclusionSelect";
import { CameraSources } from "../src/client/components/living/CameraSources";
import { setCopy, t } from "../src/client/ui/copy";
import {
nativeCaptureTargetKey,
} from "../src/client/native/capture-selection";
import type {
NativeCaptureTarget,
} from "../src/client/native/wire";
function target(
title: string,
sourceId: string,
): Extract<NativeCaptureTarget, { kind: "window" }> {
return {
kind: "window",
title,
sourceId,
pid: 10,
creationTime: "123456",
};
}
describe("native capture source selection", () => {
const game = target("My Game", "1");
afterEach(() => setCopy({ lang: "en" }));
it("offers OBS only with input capability and waits for its explicit connect action", () => {
setCopy({ lang: "en" });
let preparations = 0;
const props = {
nativeSources: { kind: "browser" } as const,
onBrowser() {}, onNative() {}, onPreview: async () => null, onRefresh() {}, onCancel() {},
};
const unavailable = renderToStaticMarkup(createElement(CaptureSourcePicker, props));
expect(unavailable).not.toContain('data-source-tab="external"');
const supported = { ...props, onExternalInput: () => { preparations++; } };
const browser = renderToStaticMarkup(createElement(CaptureSourcePicker, supported));
expect(browser).toContain('data-source-tab="external"');
expect(browser).not.toContain(t("en", "host.sourcePicker.externalAction"));
const external = renderToStaticMarkup(createElement(CaptureSourcePicker, { ...supported, initialTab: "external" }));
expect(external).toMatch(/data-source-tab="external"[^>]*aria-selected="true"/);
expect(external).toContain(t("en", "host.sourcePicker.externalAction"));
expect(external).not.toContain(t("en", "host.sourcePicker.empty"));
expect(preparations).toBe(0);
});
it("shows one system chooser for picker-only Apps instead of duplicate desktop tabs", () => {
setCopy({ lang: "en" });
const html = renderToStaticMarkup(createElement(CaptureSourcePicker, {
nativeSources: { kind: "ready", sources: [{ kind: "picker", sourceId: "1", title: "Choose on device" }], processAudio: false, systemAudio: true },
onBrowser() {}, onNative() {}, onCamera() {}, onPreview: async () => null, onRefresh() {}, onCancel() {},
}));
expect(html).toMatch(/data-source-tab="picker"[^>]*aria-selected="true"/);
expect(html).not.toContain('data-source-tab="window"');
expect(html).not.toContain('data-source-tab="display"');
expect(html).toContain('data-source-tab="browser"');
expect(html).toContain('data-source-tab="camera"');
expect(html).toContain(t("en", "host.sourcePicker.systemAction"));
expect(html).toContain('class="lr-source-option is-action"');
expect(html).not.toContain('class="lr-source-option-preview"');
});
it("omits unsupported Browser capture while keeping the system chooser and Camera", () => {
const html = renderToStaticMarkup(createElement(CaptureSourcePicker, {
nativeSources: { kind: "ready", sources: [{ kind: "picker", sourceId: "1", title: "Choose on device" }], processAudio: false, systemAudio: true },
browserSupported: false,
onBrowser() {}, onNative() {}, onCamera() {}, onPreview: async () => null, onRefresh() {}, onCancel() {},
}));
expect([...html.matchAll(/data-source-tab="([^"]+)"/g)].map(match => match[1]))
.toEqual(["camera", "picker"]);
expect(html).toMatch(/data-source-tab="picker"[^>]*aria-selected="true"/);
});
it("keeps an explicitly selected system chooser while refreshing without offering stale sources", () => {
setCopy({ lang: "en" });
const html = renderToStaticMarkup(createElement(CaptureSourcePicker, {
nativeSources: { kind: "loading", appDetected: true,
sources: [{ kind: "picker", sourceId: "1", title: "Choose on device" }] },
initialTab: "picker", browserSupported: false,
onBrowser() {}, onNative() {}, onCamera() {}, onPreview: async () => null, onRefresh() {}, onCancel() {},
}));
expect([...html.matchAll(/data-source-tab="([^"]+)"/g)].map(match => match[1]))
.toEqual(["camera", "picker"]);
expect(html).toMatch(/data-source-tab="picker"[^>]*aria-selected="true"/);
expect(html).toContain(`role="status" aria-label="${t("en", "host.sourcePicker.loading")}"`);
expect(html).not.toContain('class="lr-source-option is-action"');
expect(html).not.toContain('data-camera-source=');
});
it("shows the existing no-source result when neither Browser API nor App capture is available", () => {
setCopy({ lang: "en" });
const html = renderToStaticMarkup(createElement(CaptureSourcePicker, {
nativeSources: { kind: "browser" }, browserSupported: false, cameraSupported: false,
onBrowser() {}, onNative() {}, onPreview: async () => null, onRefresh() {}, onCancel() {},
}));
expect(html).not.toContain('role="tab"');
expect(html).not.toContain("is-action");
expect(html).toContain(t("en", "host.capture.noSource"));
});
it("keeps enumerated cameras selectable while optional thumbnails load", () => {
const props = {
sources: [{ id: "front", label: "Front camera", preview: null }],
busy: true, error: null, selected: "", onSelect: () => {},
};
const html = renderToStaticMarkup(createElement(CameraSources, { ...props, disabled: false }));
expect(html).toContain('data-camera-source="front"');
expect(html).not.toContain('disabled=""');
expect(renderToStaticMarkup(createElement(CameraSources, { ...props, disabled: true })))
.toContain('disabled=""');
});
it.each(["en", "zh"] as const)("keeps Browser capture reachable with an actionable App mismatch in %s", (lang) => {
setCopy({ lang });
const props = {
nativeSources: { kind: "incompatible" },
onBrowser: () => {}, onNative: () => {}, onPreview: async () => null,
onRefresh: () => {}, onCancel: () => {},
} as const;
const html = renderToStaticMarkup(createElement(CaptureSourcePicker, { ...props, initialTab: "browser" }));
expect(html).not.toContain(t(lang, "native.incompatible"));
expect(html).toContain(`aria-label="${t(lang, "host.sourcePicker.browser")}"`);
expect(html).not.toMatch(/data-source-tab="browser"[^>]*disabled=""/);
expect(html).not.toContain(t(lang, "host.sourcePicker.empty"));
expect(html).not.toContain('role="status"');
const nativeTab = renderToStaticMarkup(createElement(CaptureSourcePicker, { ...props, initialTab: "window" }));
expect(nativeTab).toContain(t(lang, "native.incompatible"));
expect(nativeTab).toContain('role="status"');
});
it("keeps pending native selection from implicitly opening a mobile camera", () => {
const props = { onBrowser: () => {}, onNative: () => {}, onCamera: () => {},
onPreview: async () => null, onRefresh: () => {}, onCancel: () => {},
browserSupported: false };
const pending = renderToStaticMarkup(createElement(CaptureSourcePicker, {
...props, nativeSources: { kind: "loading" }, initialTab: "window",
}));
expect(pending).toMatch(/aria-selected="true"[^>]*data-source-tab="window"|data-source-tab="window"[^>]*aria-selected="true"/);
const explicit = renderToStaticMarkup(createElement(CaptureSourcePicker, {
...props, nativeSources: { kind: "loading" }, initialTab: "camera",
}));
expect(explicit).toMatch(/aria-selected="true"[^>]*data-source-tab="camera"|data-source-tab="camera"[^>]*aria-selected="true"/);
const browser = renderToStaticMarkup(createElement(CaptureSourcePicker, {
...props, nativeSources: { kind: "browser" }, initialTab: "window",
}));
expect(browser).toMatch(/aria-selected="true"[^>]*data-source-tab="camera"|data-source-tab="camera"[^>]*aria-selected="true"/);
});
it("uses the complete window identity as the UI key", () => {
expect(nativeCaptureTargetKey(game)).toBe("window:1:10:123456");
expect(
nativeCaptureTargetKey({ ...game, creationTime: "654321" }),
).not.toBe(nativeCaptureTargetKey(game));
expect(
nativeCaptureTargetKey({
kind: "display",
sourceId: "2",
title: "Display 1",
}),
).toBe("display:2");
});
it("keeps a missing exclusion selected and distinguishes a restarted process", () => {
setCopy({ lang: "en" });
const restarted = { ...game, creationTime: "654321" };
const html = renderToStaticMarkup(createElement(AudioExclusionSelect, {
sources: [restarted, { ...restarted, sourceId: "2" }],
value: game, onChange: () => {},
}));
expect(html).toMatch(/<option value="10:123456" disabled="" selected="">/);
expect(html.match(/<option value="10:654321"/g)).toHaveLength(1);
expect(html).not.toMatch(/<option value="" selected="">/);
});
it.each(["unavailable", "unsupported", "failed"] as const)("does not describe %s as an empty list or block Browser capture", (kind) => {
for (const lang of ["en", "zh"] as const) {
setCopy({ lang });
const props = {
nativeSources: { kind }, onBrowser: () => {}, onNative: () => {},
onPreview: async () => null, onRefresh: () => {}, onCancel: () => {},
};
const nativeTab = renderToStaticMarkup(createElement(CaptureSourcePicker, props));
if (kind === "unavailable") {
expect(nativeTab).not.toContain('data-source-tab="window"');
expect(nativeTab).not.toContain('data-source-tab="display"');
expect(nativeTab).toMatch(/data-source-tab="browser"[^>]*aria-selected="true"/);
}
if (kind === "unavailable") expect(nativeTab).not.toContain('role="status"');
else expect(nativeTab.replaceAll("&#x27;", "'")).toContain(t(lang, `host.sourcePicker.${kind}`));
expect(nativeTab).not.toContain(t(lang, "host.sourcePicker.empty"));
expect(nativeTab).not.toMatch(/class="lr-source-picker-refresh"[^>]*disabled=""/);
const browserTab = renderToStaticMarkup(createElement(CaptureSourcePicker, { ...props, initialTab: "browser" }));
expect(browserTab).toContain(`aria-label="${t(lang, "host.sourcePicker.browser")}"`);
const browserCopy = browserTab.replaceAll("&#x27;", "'");
expect(browserCopy).not.toContain('role="status"');
expect(browserTab).not.toMatch(/class="lr-source-option is-action"[^>]*disabled=""/);
}
});
it.each(["browser", "window"] as const)("keeps Browser and Camera reachable during discovery with initial %s preference", initialTab => {
setCopy({ lang: "en" });
const html = renderToStaticMarkup(createElement(CaptureSourcePicker, {
nativeSources: { kind: "loading" }, initialTab,
onBrowser: () => {}, onNative: () => {}, onPreview: async () => null,
onRefresh: () => {}, onCancel: () => {},
}));
expect(html).toMatch(new RegExp(`data-source-tab="${initialTab}"[^>]*aria-selected="true"`));
expect(html).not.toMatch(/data-source-tab="browser"[^>]*disabled=""/);
expect(html).not.toContain("lr-brand-loader");
expect(html).not.toMatch(/class="lr-source-picker-close"[^>]*disabled=""/);
});
it("separates windows from displays while keeping Browser and Camera switching available", () => {
const html = renderToStaticMarkup(createElement(CaptureSourcePicker, {
nativeSources: {
kind: "ready",
sources: [game, { kind: "display", sourceId: "2", title: "Display 1" }],
processAudio: false,
systemAudio: true,
},
audioLocked: true,
onBrowser: () => {},
onCamera: () => {},
onNative: () => {},
onPreview: async () => null,
onRefresh: () => {},
onCancel: () => {},
}));
expect(html).toContain('data-source-tab="browser"');
expect(html).not.toMatch(/data-source-tab="browser"[^>]*disabled=""/);
expect(html).not.toMatch(/data-source-tab="camera"[^>]*disabled=""/);
expect(html).toMatch(/data-native-source="window:1:10:123456"[^>]*disabled=""/);
expect(html).not.toContain('data-native-source="display:2"');
expect(html.match(/role="tab"/g)).toHaveLength(4);
expect([...html.matchAll(/data-source-tab="([^"]+)"/g)].map((match) => match[1]))
.toEqual(["browser", "camera", "window", "display"]);
expect(html).toMatch(/data-source-tab="window"[^>]*aria-selected="true"/);
});
it("offers only Browser and Camera without an App, defaulting to Browser", () => {
setCopy({ lang: "en" });
const html = renderToStaticMarkup(createElement(CaptureSourcePicker, {
nativeSources: { kind: "browser" }, onBrowser: () => {}, onCamera: () => {},
onNative: () => {}, onPreview: async () => null, onRefresh: () => {}, onCancel: () => {},
}));
expect([...html.matchAll(/data-source-tab="([^"]+)"/g)].map((match) => match[1]))
.toEqual(["browser", "camera"]);
expect(html).toMatch(/data-source-tab="browser"[^>]*aria-selected="true"/);
expect(html).not.toMatch(/data-source-tab="camera"[^>]*disabled=""/);
expect(html).not.toContain('role="status"');
});
it("selects a focusable Camera tab when Browser capture is unsupported", () => {
const html = renderToStaticMarkup(createElement(CaptureSourcePicker, {
nativeSources: { kind: "browser" }, browserSupported: false,
onBrowser: () => {}, onCamera: () => {}, onNative: () => {},
onPreview: async () => null, onRefresh: () => {}, onCancel: () => {},
}));
expect(html).toMatch(/data-source-tab="camera"[^>]*aria-selected="true"[^>]*tabindex="0"/);
expect(html).not.toContain('data-source-tab="browser"');
});
it("retains the platform-owned combined source picker", () => {
const html = renderToStaticMarkup(createElement(CaptureSourcePicker, {
nativeSources: {
kind: "ready",
sources: [{ kind: "picker", sourceId: "1", title: "System picker" }],
processAudio: false,
systemAudio: true,
},
onBrowser: () => {},
onNative: () => {},
onPreview: async () => null,
onRefresh: () => {},
onCancel: () => {},
}));
expect(html).toContain('data-native-source="picker:1"');
expect(html).toContain('aria-checked="true"');
});
it.each(["browser", "window"] as const)("blocks %s confirmation during owning page work while retaining cancel", (initialTab) => {
setCopy({ lang: "en" });
const html = renderToStaticMarkup(createElement(CaptureSourcePicker, {
nativeSources: { kind: "ready", sources: [game], processAudio: false, systemAudio: false },
initialTab, selectionDisabled: true,
onBrowser: () => {}, onNative: () => {}, onPreview: async () => null,
onRefresh: () => {}, onCancel: () => {},
}));
expect(html).toMatch(/class="lr-source-option(?: is-action)?"[^>]*disabled=""/);
expect(html).not.toMatch(/class="lr-source-picker-close"[^>]*disabled=""/);
});
it.each(["en", "zh"] as const)("offers the requested border preference only for supported native capture in %s", (lang) => {
setCopy({ lang });
const render = (supported: boolean | undefined, initialTab: "window" | "display" | "browser", initialShowCaptureBorder?: boolean) =>
renderToStaticMarkup(createElement(CaptureSourcePicker, {
nativeSources: { kind: "ready", sources: [game], processAudio: true, systemAudio: true, captureBorderControl: supported },
initialTab, initialShowCaptureBorder,
onBrowser: () => {}, onNative: () => {}, onPreview: async () => null,
onRefresh: () => {}, onCancel: () => {},
}));
const label = t(lang, "host.sourcePicker.showCaptureBorder");
for (const tab of ["window", "display"] as const) {
expect(render(true, tab)).toContain(`aria-checked="false" aria-label="${label}"`);
expect(render(true, tab, true)).toContain(`aria-checked="true" aria-label="${label}"`);
expect(render(true, tab)).toContain(`aria-description="${t(lang, "host.sourcePicker.showCaptureBorderHint")}"`);
expect(render(false, tab, true)).not.toContain(label);
expect(render(undefined, tab, true)).not.toContain(label);
}
expect(render(true, "browser", true)).not.toContain(label);
});
});