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 { defaultNativeCapturePath, nativeCaptureTargetKey, } from "../src/client/native/capture-selection"; import type { NativeAdapter, NativeCaptureTarget, } from "../src/client/native/wire"; function target( title: string, sourceId: string, ): Extract { return { kind: "window", title, sourceId, pid: 10, creationTime: "123456", }; } describe("native capture source selection", () => { const game = target("My Game", "1"); afterEach(() => setCopy({ lang: "en", vis: true })); it("shows one system chooser for picker-only Apps instead of duplicate desktop tabs", () => { setCopy({ lang: "en", vis: false }); 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", vis: false }); 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", vis: false }); 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", "vis"] as const)("keeps Browser capture reachable with an actionable App mismatch in %s", (mode) => { const lang = mode === "zh" ? "zh" : "en"; setCopy({ lang, vis: mode === "vis" }); 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", vis: false }); const restarted = { ...game, creationTime: "654321" }; const html = renderToStaticMarkup(createElement(AudioExclusionSelect, { sources: [restarted, { ...restarted, sourceId: "2" }], value: game, onChange: () => {}, })); expect(html).toMatch(/