Files
piik/tests/channel-external-input.test.ts
T
AAA 156a67aa68 fix(client): preserve App control for unfamiliar codec diagnostics
An unfamiliar codec in optional format-error detail now uses the existing
generic message. Keep the known external-input lifecycle, current permit and
unrelated capture or request. Known H.264 detail still validates its fields.

Actual App socket regressions failed in Chinese and English before this
change. Both App control test files pass all 65 tests after it, along with
the source TypeScript check. The malformed-known case still fails validation.
2026-11-28 04:12:50 -05:00

212 lines
12 KiB
TypeScript

import { PRODUCT_VERSION } from "../src/client/channel/product-version";
import { afterEach, expect, test, vi } from "vitest";
import { ChannelApp } from "../src/client/channel/app-link";
import { DEFAULT_QUALITY_SETTINGS } from "../src/shared/protocol";
import { createElement, type ReactNode } from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { ChannelExternalInput } from "../src/client/channel/components/ChannelExternalInput";
import { channelErrorPresentation } from "../src/client/components/error-presentation";
import { setCopy, t } from "../src/client/ui/copy";
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) };
});
class Socket {
static OPEN = 1;
static current: Socket;
readyState = 1;
sent: Array<Record<string, unknown>> = [];
onopen?: () => void;
onmessage?: (event: { data: string }) => void;
onclose?: (() => void) | null;
constructor() { Socket.current = this; }
send(value: string) { this.sent.push(JSON.parse(value)); }
receive(value: unknown) { this.onmessage?.({ data: JSON.stringify(value) }); }
reply(result: unknown) { this.receive({ id: this.sent.at(-1)!.id, result }); }
close() { this.readyState = 3; this.onclose?.(); }
}
afterEach(() => { vi.unstubAllGlobals(); setCopy({ lang: "en" }); });
async function setup() {
vi.stubGlobal("WebSocket", Socket);
const app = new ChannelApp({ service: "piik-channel-app", ...PRODUCT_VERSION,
token: "test-token", port: 8793 }, "site", "user", "credential", "Me", () => {}, () => {});
const socket = Socket.current;
socket.onopen?.(); socket.reply(null); await app.ready;
return { app, socket };
}
async function arm(app: ChannelApp, socket: Socket, publication: string) {
const preparing = app.prepareExternalInput(publication, "room");
socket.reply({ endpoint: "http://127.0.0.1:8793/whip/test", token: "test-input" });
await preparing;
}
function prepared(socket: Socket, publicationId: string) {
const captureId = crypto.randomUUID();
socket.receive({ type: "external_input_state", publicationId, state: "prepared", captureId,
sessionId: crypto.randomUUID(), codec: "h264" });
return captureId;
}
test.each(["waiting", "closed"] as const)("OBS %s retires its preparation without inventing capture audio controls", async state => {
const { app, socket } = await setup();
try {
const publicationId = crypto.randomUUID();
const arming = app.prepareExternalInput(publicationId, "room");
const audioWhileArming = app.hasSourceAudioControl(publicationId);
socket.reply({ endpoint: "http://127.0.0.1:8793/whip/test", token: "test-input" });
await arming;
expect(audioWhileArming).toBe(false);
prepared(socket, publicationId);
expect(app.hasSourceAudioControl(publicationId)).toBe(false);
socket.receive({ type: "external_input_state", publicationId, state });
expect(app.hasCapture(publicationId)).toBe(false);
expect(app.hasSourceAudioControl(publicationId)).toBe(false);
const commands = socket.sent.length;
await app.cancelCapture(publicationId);
expect(socket.sent).toHaveLength(commands);
expect(app.externalInput(publicationId)?.state).toBe(state === "waiting" ? "waiting" : undefined);
} finally { app.close(); }
});
test("an unknown commit outcome leaves the capture unconfirmed", async () => {
const { app, socket } = await setup();
try {
const publicationId = crypto.randomUUID();
await arm(app, socket, publicationId);
const captureId = prepared(socket, publicationId);
const commit = app.commitCapture(publicationId, captureId);
socket.reply({ type: "deferred" });
await expect(commit).rejects.toThrow("invalid_app_response");
expect(app.hasCapture(publicationId)).toBe(false);
const cancel = app.cancelCapture(publicationId);
expect(socket.sent.at(-1)?.type).toBe("cancel_external_input");
socket.reply(null);
await cancel;
} finally { app.close(); }
});
test("retiring OBS permission preserves a replacement Native capture preparation", async () => {
const { app, socket } = await setup();
try {
const publicationId = crypto.randomUUID();
await arm(app, socket, publicationId);
const preparing = app.prepareCapture(publicationId, "room", { target: { kind: "display", sourceId: "1", title: "Display" },
audio: false, showCaptureBorder: false, profile: DEFAULT_QUALITY_SETTINGS, codecMode: "h264", encodingExecution: "auto" });
const captureId = crypto.randomUUID();
socket.reply({ captureId, sessionId: crypto.randomUUID(), codec: "h264" });
await preparing;
expect(app.hasSourceAudioControl(publicationId)).toBe(true);
const committing = app.commitCapture(publicationId, captureId);
socket.receive({ type: "external_input_state", publicationId, state: "closed" });
socket.reply(null); await committing;
expect(app.hasCapture(publicationId)).toBe(true);
expect(app.hasSourceAudioControl(publicationId)).toBe(true);
} finally { app.close(); }
});
test.each(["zh", "en"] as const)("OBS actual format reaches one %s alert and clears on corrected input", async lang => {
const { app, socket } = await setup();
try {
const independent = crypto.randomUUID();
const preparing = app.prepareCapture(independent, "other", { target: { kind: "display", sourceId: "1", title: "Display" },
audio: false, showCaptureBorder: false, profile: DEFAULT_QUALITY_SETTINGS, codecMode: "h264", encodingExecution: "auto" });
const captureId = crypto.randomUUID();
socket.reply({ captureId, sessionId: crypto.randomUUID(), codec: "h264" }); await preparing;
const committing = app.commitCapture(independent, captureId); socket.reply(null); await committing;
const publicationId = crypto.randomUUID();
await arm(app, socket, publicationId);
const permit = app.externalInput(publicationId)!;
const unsupportedVideoFormat = { codec: "h264", profileIdc: 110, constraintFlags: 0, levelIdc: 52 };
setCopy({ lang });
const capacity = app.relayCapacity();
for (const format of [unsupportedVideoFormat, { ...unsupportedVideoFormat, bitDepth: 10 }, unsupportedVideoFormat]) {
socket.receive({ type: "external_input_state", publicationId, state: "error", error: "external_video_format_unsupported", unsupportedVideoFormat: format });
const input = app.externalInput(publicationId)!;
expect(input).toMatchObject({ endpoint: permit.endpoint, token: permit.token });
expect(input.unsupportedVideoFormat).toEqual(unsupportedVideoFormat);
const html = renderToStaticMarkup(createElement(ChannelExternalInput, { input, busy: false, onStop() {}, onClose() {} }));
expect(html).toContain("High 10"); expect(html).toContain("6e0034"); expect(html).toContain("5.2");
expect(html).toContain("NV12");
expect(html).toContain(lang === "zh" ? "sharing.zh-CN.md#用-obs-分享画面和声音" : "sharing.md#share-video-and-audio-from-obs");
expect(html.match(/role="alert"/g)).toHaveLength(1);
expect(html).not.toContain("external_video_format_unsupported");
expect(app.connected).toBe(true); expect(app.hasCapture(independent)).toBe(true);
}
socket.receive({ type: "external_input_state", publicationId, state: "error", error: "external_video_format_unsupported",
unsupportedVideoFormat: { codec: "future", bitDepth: 12 } });
const input = app.externalInput(publicationId);
expect(input).toMatchObject({ endpoint: permit.endpoint, token: permit.token, state: "error", error: "external_video_format_unsupported" });
expect(input?.unsupportedVideoFormat).toBeUndefined();
expect(app.hasCapture(publicationId)).toBe(false);
expect(app.captureId(independent)).toBe(captureId);
const html = renderToStaticMarkup(createElement(ChannelExternalInput, { input: input!, busy: false, onStop() {}, onClose() {} }));
expect(html).toContain(t(lang, "channel.error.externalVideoFormatUnsupported"));
const result = { sessionId: crypto.randomUUID(), relayOverride: 2 };
socket.reply(result);
await expect(capacity).resolves.toEqual(result);
prepared(socket, publicationId);
expect(app.externalInput(publicationId)?.state).toBe("prepared");
expect(app.externalInput(publicationId)?.unsupportedVideoFormat).toBeUndefined();
expect(app.externalInput(publicationId)?.error).toBeUndefined();
socket.receive({ type: "external_input_state", publicationId, state: "waiting" });
expect(app.externalInput(publicationId)?.unsupportedVideoFormat).toBeUndefined();
} finally { app.close(); }
});
test("malformed H.264 diagnostic still retires App control", async () => {
const { app, socket } = await setup();
try {
const publicationId = crypto.randomUUID();
await arm(app, socket, publicationId);
socket.receive({ type: "external_input_state", publicationId, state: "error", error: "external_video_format_unsupported",
unsupportedVideoFormat: { codec: "h264", profileIdc: 256, constraintFlags: 0, levelIdc: 52 } });
expect(app.connected).toBe(false);
} finally { app.close(); }
});
test("unknown profile remains numeric and a generic error ignores unrelated format detail", () => {
const format = { codec: "h264" as const, profileIdc: 153, constraintFlags: 0, levelIdc: 52 };
const copy = (key: Parameters<typeof t>[1], vars?: Record<string, string>) => t("en", key, vars);
const message = channelErrorPresentation("external_video_format_unsupported", copy, undefined, format).message!;
expect(message).toContain("profile_idc=153");
expect(message).not.toContain("Received H.264 High 10");
const genericMessage = channelErrorPresentation("external_input_failed", copy, undefined, format).message;
expect(genericMessage).toContain("streaming connection and log in OBS");
expect(genericMessage).not.toMatch(/H\.264|NV12|profile_idc/);
const level = channelErrorPresentation("external_video_format_unsupported", copy, undefined,
{ ...format, profileIdc: 100, levelIdc: 60 }).message;
expect(level).toMatch(/High, Level 6\.0.*64003c.*5\.2.*frame rate/);
expect(level).not.toContain("NV12");
const offer = channelErrorPresentation("external_video_offer_unsupported", copy, undefined, format).message;
expect(offer).toMatch(/offer.*H\.264/);
expect(offer).not.toMatch(/Received|SPS/);
expect(channelErrorPresentation("external_video_format_unsupported", copy, undefined,
{ ...format, levelIdc: 0 }).message).toContain("level_idc=0");
});
test("capture controls distinguish replaced, current and disconnected requests", async () => {
const { app, socket } = await setup();
try {
const publicationId = crypto.randomUUID();
await arm(app, socket, publicationId);
const live = () => socket.receive({ type: "external_input_state", publicationId, state: "live",
captureId: crypto.randomUUID(), sessionId: crypto.randomUUID(), codec: "h264" });
live();
const controls = [app.setCapturePaused(publicationId, true), app.setCaptureTitle(publicationId, "title"),
app.setCaptureQuality(publicationId, DEFAULT_QUALITY_SETTINGS)];
const requests = socket.sent.slice(-3);
live();
for (const { id } of requests) socket.receive({ id, error: "capture_failed" });
const results = await Promise.allSettled(controls);
expect(results).toEqual(controls.map(() => ({ status: "rejected", reason: expect.objectContaining({ name: "AbortError" }) })));
const current = app.setCapturePaused(publicationId, true);
socket.receive({ id: socket.sent.at(-1)!.id, error: "capture_failed" });
await expect(current).rejects.toThrow("capture_failed");
const disconnected = app.setCapturePaused(publicationId, true);
socket.close();
await expect(disconnected).rejects.toThrow("app_disconnected");
} finally { app.close(); }
});