## Problem and outcome This phase adds consented remote co-play to normal App/Web rooms and integrates the accepted audio, playback, participant-state and multi-site improvements. It also rebuilds the bilingual README/documentation reading path, refreshes real-component illustrations, and publishes the selected website film. The candidate preserves the established 2.x sharing/viewing contracts. Optional new behavior follows negotiated capabilities; Windows gamepads use an externally installed driver. The separate accompaniment experiment and unselected film studies are outside this phase. ## Release notes **Play together. Your turn!** Approved friends can now play together with shared keyboard and mouse controls or optional gamepads. **远程同乐,轮到你啦!** 获准的朋友现在可以共用键鼠,或使用可选手柄,一起玩本地多人游戏。 ### English Piik 2.4 adds remote co-play, improves shared sound and room controls, and rewrites the bilingual getting-started and help guides around what you want to do. #### Highlights - Let approved friends control together. Each player's held keys and gamepad retire independently; everyone shares the Host's mouse pointer. On Windows x64, optional virtual gamepads use a Host-installed ViGEmBus driver. Piik does not bundle or install it. - Control shared output volume from the Host's playback bar. The optional share microphone adds your voice to the picture, independently of room voice; stopping the share leaves room voice running. Room voice now defaults to 64 kbps and offers a Host bitrate control. - See voice and remote-control participation directly on the couch. Connection details group picture and voice by receive/send direction. Playback actions, chat-overlay settings and dismissible operation feedback have clearer, consistent places. - Approve several Piik sites in App and manage each separately, with one optional startup favorite. - Follow rewritten English and Chinese README and documentation paths: watching, starting a share, room tools, remote control/co-play, and symptom-based help. Illustrated guides use current product components, theme-aware screenshots and shared diagrams. A dedicated network guide covers campus networks, media UDP, SFU sites and optional virtual LANs. - The website has clearer App and co-play FAQs and the selected bilingual 64-second film, with shared playback controls and a held closing card. #### Fixes - Tolerate small macOS capture-crop rounding errors without accepting genuinely invalid geometry. - Keep shared-audio and microphone volume changes independent, preserve commentary through source replacement, and retire each input at its owning lifecycle boundary. - Improve narrow-player controls, touch-held volume menus, tooltip placement and feedback cleanup after reconnection. - Repair the release recovery test on macOS and refresh the build and tunnel runtimes. #### Updating and limits Update the Host's App and the room site, then refresh open pages to use the new features. Established sharing and viewing remain compatible within 2.x; older peers retain the features they support. The wire versions and SQLite schema are unchanged. Restarting App or Server interrupts active sessions. Before downgrading App, remove extra approved sites in the newer launcher so at most one remains. Use the [remote-control guide](https://piik.tv/docs/remote-control.html?lang=en) for the optional Windows driver. Linux gamepads remain experimental; macOS virtual gamepads are not currently available. Windows 10 window-sharing black screens ([#495](https://github.com/TNTcraftHIM/Piik/issues/495)) remain under investigation; use screen sharing if affected. ### 中文 Piik 2.4 新增多人远程同乐,改进分享声音与房间控制,并围绕实际任务重写双语上手教程和帮助文档。 #### 主要更新 - 允许获准的朋友同时控制。各人的按键持有和手柄独立释放,大家共用房主的鼠标指针。Windows x64 可使用房主自行安装的 ViGEmBus 驱动提供虚拟手柄;Piik 不捆绑或安装驱动。 - 房主可通过播控调整分享输出音量。可选的分享麦克风把说话声加入画面,与房间语音独立;停播后房间语音继续。房间语音默认提升至 64 kbps,并提供房主码率设置。 - 沙发人物直接呈现语音和远控参与状态;连接详情按画面/语音、接收/发送分组。播放操作、弹幕设置和可关闭的操作提示各有清楚、一致的位置。 - App 可授权多个 Piik 站点,逐个管理,并选择一个启动时打开的星标站点。 - 重写中英文 README 和文档中心:从观看、开始分享、房间工具、远控/同乐到按症状排障。图文教程使用当前正式组件、明暗主题截图与同源示意图;独立网络指南说明校园网、媒体 UDP、SFU 站点和可选虚拟局域网的用法。 - 官网改进 App 与多人同乐 FAQ,采用选定的双语 64 秒短片,复用产品播控并保留结尾品牌卡。 #### 修复 - 容忍 macOS 采集裁剪中的微小舍入误差,仍拒绝真正无效的画面边界。 - 分享总音量与麦克风音量分别更新;换源保留解说,各项输入按所属生命周期释放。 - 改进窄屏播控、触摸长按音量菜单、提示定位,以及重连后的提示清理。 - 修复 macOS 上发布恢复测试的可移植性,更新构建与公网隧道运行组件。 #### 更新与限制 将房主 App 和房间站点更新后,刷新已打开的页面,即可使用新功能。2.x 内保留已有分享与观看兼容性,旧端继续使用其支持的功能;协议版本和 SQLite schema 不变。重启 App 或 Server 会中断当前会话。 如需降级 App,先在新版启动页删除额外授权,最多保留一个站点。可选的 Windows 驱动安装方法见[远控指南](https://piik.tv/docs/zh/remote-control.html)。Linux 手柄仍属实验能力,macOS 当前暂不提供虚拟手柄。Windows 10 窗口分享黑屏而整屏正常的反馈([#495](https://github.com/TNTcraftHIM/Piik/issues/495))仍在调查,本版不声称已修复。 ## Verification - Integrated Web typecheck, 98 test files / 1,734 tests and production build passed. Later scoped UI repairs passed focused typecheck/tests and preview checks. - Focused Go checks cover input grant/held-state ownership, delayed device retirement, native control/Host, config/launcher/loopback, voice negotiation and source-volume binding. Windows capture compilation/probe and the isolated Linux uinput kernel gate passed. - Normal-page P2P (22 checks) and embedded-SFU (19 checks) gates passed. P2P explicitly exercised source commentary with room voice, independent shutdown, share retirement and the next share's default-off state; SFU also covers live voice bitrate and voice recovery. - Documentation links (108 Markdown files), generated-diagram freshness, the complete bilingual/theme screenshot pipeline and website production build passed. Website/readers were checked at 320, 768 and 1440 px with both languages/themes. - macOS crop logic retains geometric self-tests; this Windows environment did not repeat a macOS SDK/hardware run. Linux desktop/game recognition and macOS virtual-gamepad entitlement/runtime remain explicit limits. - Windows native window-sharing acceptance passed: Host/Viewer decoding, live and paused quality changes, resize/minimize/restore, source replacement, stop and restart. Capturing the Host Chrome window also passed with Auto/H.264 and VP8, including decoded-frame and pixel checks. The Windows 10 case in #495 was not reproduced and remains separate. - Current Go serializers passed the published v2.0.0/v2.3.0 TypeScript readers; existing native input opt-in checks passed. This checks payload compatibility, not an end-to-end run of old published clients. ## Impact Compatible 2.x feature release; Browser/server v24, Native control v10, capture v7 and SQLite schema 2 remain unchanged. New co-play, presence, voice settings and native volume behavior are capability-negotiated. Multi-site App settings retain the older single-site reader path; arbitrary binary downgrade is not promised. No independent room/router, new NAT policy, driver installer or separate accompaniment channel is introduced. ## Related work Includes the macOS crop correction reported in #492 and the macOS deployment-test portability contribution from #496. Supersedes the incomplete phase PR #494. #495 remains open. ## Attribution Co-authored-by: Yuqing Huang <670081066@qq.com> Co-authored-by: luol75569-collab <297886760+luol75569-collab@users.noreply.github.com>
1800 lines
54 KiB
TypeScript
1800 lines
54 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import {
|
|
DEFAULT_QUALITY_SETTINGS,
|
|
DEFAULT_ROUTE_POLICY,
|
|
DEFAULT_VIEWER_DISPLAY_NAME,
|
|
MAX_DISPLAY_NAME_CODE_POINTS,
|
|
MAX_MEDIA_ROUTE_REVISION,
|
|
MAX_VIEWER_QUALITY_EVIDENCE_BYTES,
|
|
MAX_VIEWER_PASSWORD_LENGTH,
|
|
MAX_VIEWERS_PER_ROOM_LIMIT,
|
|
SIGNALING_PROTOCOL,
|
|
clientMessageSchema,
|
|
createRoomRequestSchema,
|
|
decodeClientMessage,
|
|
normalizeDisplayName,
|
|
participantRouteAssignmentSchema,
|
|
preparedRouteCandidateSchema,
|
|
replaceRoomRequestSchema,
|
|
roomAccessUpdateRequestSchema,
|
|
roomAccessUpdateResponseSchema,
|
|
runtimeCapabilitiesSchema,
|
|
serverMessageSchema,
|
|
viewerPasswordSchema,
|
|
} from "../src/shared/protocol.js";
|
|
|
|
const token = "a".repeat(43);
|
|
const roomId = "1234";
|
|
const viewerGrant = `${"b".repeat(21)}g`;
|
|
|
|
it("keeps audio availability nullable for older Hosts and generation-scoped for updates", () => {
|
|
expect(clientMessageSchema.safeParse({ type: "subscribe-audio-presence" }).success).toBe(true);
|
|
for (const available of [false, true, null]) {
|
|
expect(serverMessageSchema.safeParse({ type: "audio-presence", available }).success).toBe(true);
|
|
expect(clientMessageSchema.safeParse({ type: "set-audio-presence", shareGeneration: "audio_share_generation", available }).success)
|
|
.toBe(available !== null);
|
|
}
|
|
expect(serverMessageSchema.safeParse({ type: "audio-presence" }).success).toBe(false);
|
|
expect(clientMessageSchema.safeParse({ type: "set-audio-presence", shareGeneration: "bad", available: true }).success).toBe(false);
|
|
});
|
|
|
|
it("accepts bounded future reaction events while keeping commands and known prop targets strict", () => {
|
|
const event = { type: "room-interaction", id: "event_future_1234", requestId: "request_future_1234", occurredAt: 1,
|
|
sender: { peerId: "sender_peer_1234", role: "viewer", displayName: "Friend" },
|
|
payload: { kind: "reaction", reaction: "future-effect", targetPeerId: "target_peer_1234" } };
|
|
expect(serverMessageSchema.safeParse(event).success).toBe(true);
|
|
for (const role of ["host", "viewer", "", "HOST", "admin"]) {
|
|
expect(serverMessageSchema.safeParse({ ...event, sender: { ...event.sender, role } }).success)
|
|
.toBe(role === "host" || role === "viewer");
|
|
}
|
|
expect(clientMessageSchema.safeParse({ type: "send-room-interaction", requestId: event.requestId, payload: event.payload }).success).toBe(false);
|
|
for (const payload of [
|
|
{ kind: "reaction", reaction: "" }, { kind: "reaction", reaction: "a".repeat(33) },
|
|
{ kind: "reaction", reaction: "future effect" }, { kind: "reaction", reaction: "👋" },
|
|
{ kind: "reaction", reaction: "future-effect", targetPeerId: "bad" },
|
|
{ kind: "reaction", reaction: "future-effect", text: "hidden command" },
|
|
{ kind: "reaction", reaction: "tomato" }, { kind: "future-kind", reaction: "wave" },
|
|
]) expect(serverMessageSchema.safeParse({ ...event, payload }).success).toBe(false);
|
|
expect(serverMessageSchema.safeParse({ ...event, sender: { ...event.sender, peerId: "bad" } }).success).toBe(false);
|
|
});
|
|
const qualitySettings = {
|
|
resolution: { width: 1920, height: 1080 },
|
|
maxFramerate: 60,
|
|
maxBitrate: 8_000_000,
|
|
degradationPreference: "maintain-resolution",
|
|
} as const;
|
|
|
|
describe("runtime capabilities", () => {
|
|
it("defaults missing capabilities off and ignores unknown descriptors", () => {
|
|
expect(runtimeCapabilitiesSchema.parse({})).toEqual({ sfu: false, natPrediction: false, roomInteractions: false, hostRoomSession: false, remoteInput: false, viewerRouteSelection: false, audioPresence: false, coPlayPresence: false, roomVoice: false, roomVoiceListeningPresence: false, relayVolunteer: false });
|
|
expect(runtimeCapabilitiesSchema.parse({ sfu: true, extra: { enabled: true } }))
|
|
.toEqual({ sfu: true, natPrediction: false, roomInteractions: false, hostRoomSession: false, remoteInput: false, viewerRouteSelection: false, audioPresence: false, coPlayPresence: false, roomVoice: false, roomVoiceListeningPresence: false, relayVolunteer: false });
|
|
expect(runtimeCapabilitiesSchema.parse({ natPrediction: true, SFU: true }))
|
|
.toEqual({ sfu: false, natPrediction: true, roomInteractions: false, hostRoomSession: false, remoteInput: false, viewerRouteSelection: false, audioPresence: false, coPlayPresence: false, roomVoice: false, roomVoiceListeningPresence: false, relayVolunteer: false });
|
|
expect(runtimeCapabilitiesSchema.parse({ sfu: true, natPrediction: true }))
|
|
.toEqual({ sfu: true, natPrediction: true, roomInteractions: false, hostRoomSession: false, remoteInput: false, viewerRouteSelection: false, audioPresence: false, coPlayPresence: false, roomVoice: false, roomVoiceListeningPresence: false, relayVolunteer: false });
|
|
expect(runtimeCapabilitiesSchema.parse({ sfu: true, sfuOnly: true }).sfuOnly).toBe(true);
|
|
expect(runtimeCapabilitiesSchema.parse({ roomReactionIds: ["heart", "future-effect"] }).roomReactionIds)
|
|
.toEqual(["heart", "future-effect"]);
|
|
expect(runtimeCapabilitiesSchema.parse({ roomReactionIds: [] }).roomReactionIds).toEqual([]);
|
|
});
|
|
|
|
it("rejects malformed known capabilities and non-object responses", () => {
|
|
for (const value of [null, [], true, { sfu: null }, { sfu: "true" },
|
|
{ natPrediction: null }, { natPrediction: 1 }, { sfuOnly: null }, { sfuOnly: "true" },
|
|
{ roomReactionIds: null }, { roomReactionIds: "heart" }, { roomReactionIds: ["bad id"] },
|
|
{ roomReactionIds: [true] }, { roomReactionIds: [""] },
|
|
{ roomVoiceListeningPresence: null }, { roomVoiceListeningPresence: "true" },
|
|
]) {
|
|
expect(runtimeCapabilitiesSchema.safeParse(value).success).toBe(false);
|
|
}
|
|
});
|
|
});
|
|
|
|
const qualitySettingsWithAudio = {
|
|
...qualitySettings,
|
|
maxAudioBitrate: 128000,
|
|
} as const;
|
|
|
|
const legacyCodecQualitySettings = {
|
|
...qualitySettings,
|
|
videoCodec: "vp8",
|
|
} as const;
|
|
|
|
const qualityEvidence = {
|
|
type: "viewer-quality-evidence",
|
|
guard: {
|
|
connectionId: "connection_12345678",
|
|
routeRevision: 0,
|
|
presentationEpoch: 0,
|
|
},
|
|
sequence: 0,
|
|
windowMs: 2_000,
|
|
metrics: {
|
|
natTraversalPath: "ordinary",
|
|
width: 1_920,
|
|
height: 1_080,
|
|
framesPerSecond: 59.8,
|
|
bitrateKbps: 7_500,
|
|
packetsReceivedDelta: 1_500,
|
|
packetsLostDelta: 2,
|
|
rttMs: 18,
|
|
jitterMs: 3.5,
|
|
framesDecodedDelta: 120,
|
|
framesDroppedDelta: 1,
|
|
decodeMsPerFrame: 2.4,
|
|
freezeCountDelta: 0,
|
|
freezeDurationMsDelta: 0,
|
|
pauseCountDelta: 0,
|
|
pauseDurationMsDelta: 0,
|
|
codec: "video/H264",
|
|
codecProfile: "profile-level-id=42e01f",
|
|
codecParameters:
|
|
"packetization-mode=1; level-asymmetry-allowed=1",
|
|
audioBitrateKbps: 192,
|
|
audioPacketLossPercent: 0.2,
|
|
audioJitterMs: 2.5,
|
|
audioVideoPlayoutDeltaMs: -12.5,
|
|
videoJitterBufferDelayMs: 24,
|
|
audioJitterBufferDelayMs: 18,
|
|
audioConcealedSamplesPercent: 1,
|
|
audioConcealmentEventsDelta: 3,
|
|
audioCodec: "audio/opus",
|
|
},
|
|
} as const;
|
|
|
|
describe("client signaling protocol", () => {
|
|
it("keeps full observations with wide numeric values within the message budget", () => {
|
|
for (const floating of [Number.MAX_SAFE_INTEGER, 1.2345678901234567e-100]) {
|
|
const metrics = Object.fromEntries(Object.entries(qualityEvidence.metrics).map(([key, value]) => [key,
|
|
typeof value !== "number" ? value : key.endsWith("Percent") ? 99.99999999999999 :
|
|
["width", "height", "packetsReceivedDelta", "packetsLostDelta", "framesDecodedDelta",
|
|
"framesDroppedDelta", "freezeCountDelta", "pauseCountDelta", "audioConcealmentEventsDelta"].includes(key)
|
|
? Number.MAX_SAFE_INTEGER : floating,
|
|
]));
|
|
const message = { ...qualityEvidence, sequence: Number.MAX_SAFE_INTEGER,
|
|
guard: { connectionId: "a".repeat(128), routeRevision: MAX_MEDIA_ROUTE_REVISION,
|
|
presentationEpoch: Number.MAX_SAFE_INTEGER },
|
|
metrics: { ...metrics, audioCodec: `audio/${"a".repeat(32)}` } };
|
|
expect(clientMessageSchema.safeParse(message).success).toBe(true);
|
|
expect(Buffer.byteLength(JSON.stringify(message))).toBeLessThanOrEqual(MAX_VIEWER_QUALITY_EVIDENCE_BYTES);
|
|
}
|
|
});
|
|
|
|
it("uses the current strict signaling generation", () => {
|
|
expect(SIGNALING_PROTOCOL).toBe("piik-v24");
|
|
});
|
|
|
|
it("keeps signaling challenges strict and sequence-only", () => {
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "signaling-challenge",
|
|
sequence: Number.MAX_SAFE_INTEGER,
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
type: "signaling-challenge-response",
|
|
sequence: 0,
|
|
}).success,
|
|
).toBe(true);
|
|
for (const invalid of [
|
|
{ type: "signaling-challenge", sequence: -1 },
|
|
{ type: "signaling-challenge", sequence: 1.5 },
|
|
{
|
|
type: "signaling-challenge",
|
|
sequence: Number.MAX_SAFE_INTEGER + 1,
|
|
},
|
|
{ type: "signaling-challenge", sequence: 1, roomId },
|
|
{ type: "signaling-challenge-response", sequence: "1" },
|
|
{ type: "signaling-challenge-response", sequence: 1, sessionId: token },
|
|
]) {
|
|
expect(
|
|
(invalid.type === "signaling-challenge"
|
|
? clientMessageSchema
|
|
: serverMessageSchema
|
|
).safeParse(invalid).success,
|
|
).toBe(false);
|
|
}
|
|
});
|
|
|
|
it("accepts only an empty route diagnostic request", () => {
|
|
expect(
|
|
clientMessageSchema.safeParse({ type: "request-route-diagnostic" })
|
|
.success,
|
|
).toBe(true);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "request-route-diagnostic",
|
|
roomId,
|
|
}).success,
|
|
).toBe(false);
|
|
});
|
|
|
|
it("gates persistent Host room sessions behind an explicit opt-in", () => {
|
|
const host = {
|
|
type: "authenticate" as const,
|
|
protocol: SIGNALING_PROTOCOL,
|
|
roomId,
|
|
role: "host" as const,
|
|
token,
|
|
clientId: "host_12345678",
|
|
roomSession: true as const,
|
|
roomOnly: true as const,
|
|
routePolicy: DEFAULT_ROUTE_POLICY,
|
|
};
|
|
expect(clientMessageSchema.parse(host)).toMatchObject({
|
|
roomSession: true,
|
|
roomOnly: true,
|
|
});
|
|
expect(clientMessageSchema.safeParse({ ...host, roomSession: false }).success).toBe(false);
|
|
expect(clientMessageSchema.safeParse({
|
|
...host,
|
|
roomSession: undefined,
|
|
}).success).toBe(false);
|
|
expect(clientMessageSchema.safeParse({
|
|
type: "start-sharing",
|
|
shareGeneration: "share_12345678",
|
|
routePolicy: DEFAULT_ROUTE_POLICY,
|
|
}).success).toBe(true);
|
|
expect(serverMessageSchema.safeParse({
|
|
type: "sharing-started",
|
|
shareGeneration: "share_12345678",
|
|
routePolicy: DEFAULT_ROUTE_POLICY,
|
|
routeRevision: 0,
|
|
routeAssignment: {
|
|
upstream: { kind: "none" },
|
|
childPeerIds: [],
|
|
sfuPublicationGeneration: null,
|
|
},
|
|
qualitySettings: DEFAULT_QUALITY_SETTINGS,
|
|
paused: false,
|
|
}).success).toBe(true);
|
|
expect(serverMessageSchema.safeParse({
|
|
type: "sharing-start-failed", shareGeneration: "share_12345678", code: "SERVER_ERROR",
|
|
}).success).toBe(true);
|
|
expect(serverMessageSchema.safeParse({
|
|
type: "sharing-start-failed", code: "SERVER_ERROR",
|
|
}).success).toBe(false);
|
|
});
|
|
|
|
it("keeps Viewer route status as a strict crossed union", () => {
|
|
expect(
|
|
serverMessageSchema.parse({
|
|
type: "route-status",
|
|
revision: 3,
|
|
state: "waiting",
|
|
reason: "sfu-admission",
|
|
}),
|
|
).toEqual({
|
|
type: "route-status",
|
|
revision: 3,
|
|
state: "waiting",
|
|
reason: "sfu-admission",
|
|
});
|
|
expect(
|
|
serverMessageSchema.parse({
|
|
type: "route-status",
|
|
revision: 4,
|
|
state: "failed",
|
|
reason: "route-exhausted",
|
|
}),
|
|
).toEqual({
|
|
type: "route-status",
|
|
revision: 4,
|
|
state: "failed",
|
|
reason: "route-exhausted",
|
|
});
|
|
for (const invalid of [
|
|
{
|
|
type: "route-status",
|
|
revision: 3,
|
|
state: "waiting",
|
|
reason: "route-exhausted",
|
|
},
|
|
{
|
|
type: "route-status",
|
|
revision: 3,
|
|
state: "failed",
|
|
reason: "sfu-admission",
|
|
},
|
|
{
|
|
type: "route-status",
|
|
revision: 3,
|
|
state: "waiting",
|
|
reason: "sfu-admission",
|
|
retryAfterMs: 1_000,
|
|
},
|
|
{
|
|
type: "route-status",
|
|
revision: 3,
|
|
state: "waiting",
|
|
reason: "sfu-admission",
|
|
peerId: "private-peer-id",
|
|
},
|
|
]) {
|
|
expect(serverMessageSchema.safeParse(invalid).success).toBe(false);
|
|
}
|
|
});
|
|
|
|
it("accepts an atomic room creation profile", () => {
|
|
expect(
|
|
createRoomRequestSchema.parse({ codeEntryPolicy: "open" }),
|
|
).toEqual({ codeEntryPolicy: "open" });
|
|
expect(
|
|
createRoomRequestSchema.parse({
|
|
codeEntryPolicy: "private",
|
|
roomPassword: "room-password",
|
|
}),
|
|
).toEqual({
|
|
codeEntryPolicy: "private",
|
|
roomPassword: "room-password",
|
|
});
|
|
expect(
|
|
createRoomRequestSchema.parse({ codeEntryPolicy: "private" }),
|
|
).toEqual({ codeEntryPolicy: "private" });
|
|
expect(
|
|
createRoomRequestSchema.parse({
|
|
codeEntryPolicy: "open",
|
|
preferredRoomId: "4321",
|
|
}),
|
|
).toEqual({ codeEntryPolicy: "open", preferredRoomId: "4321" });
|
|
expect(
|
|
createRoomRequestSchema.safeParse({
|
|
codeEntryPolicy: "open",
|
|
preferredRoomId: "0432",
|
|
}).success,
|
|
).toBe(false);
|
|
for (const codeEntryPolicy of [
|
|
"password",
|
|
"private-link",
|
|
"public-watch",
|
|
1,
|
|
]) {
|
|
expect(
|
|
createRoomRequestSchema.safeParse({
|
|
codeEntryPolicy,
|
|
}).success,
|
|
).toBe(false);
|
|
}
|
|
});
|
|
|
|
it("accepts only a replacement creation profile", () => {
|
|
expect(
|
|
replaceRoomRequestSchema.parse({
|
|
codeEntryPolicy: "private",
|
|
roomPassword: "room-password",
|
|
}),
|
|
).toEqual({
|
|
codeEntryPolicy: "private",
|
|
roomPassword: "room-password",
|
|
});
|
|
expect(
|
|
replaceRoomRequestSchema.safeParse({
|
|
codeEntryPolicy: "open",
|
|
preferredRoomId: "4321",
|
|
}).success,
|
|
).toBe(false);
|
|
});
|
|
|
|
it("rejects the removed all-room ICE refresh request", () => {
|
|
expect(clientMessageSchema.safeParse({ type: "refresh-ice" }).success).toBe(
|
|
false,
|
|
);
|
|
});
|
|
|
|
it("accepts a bounded authentication message", () => {
|
|
expect(DEFAULT_ROUTE_POLICY).toEqual({
|
|
peerOnly: false,
|
|
topologyOptimization: true,
|
|
natPrediction: false,
|
|
});
|
|
expect(
|
|
decodeClientMessage(
|
|
JSON.stringify({
|
|
type: "authenticate",
|
|
protocol: SIGNALING_PROTOCOL,
|
|
roomId,
|
|
role: "viewer",
|
|
clientId: "client_12345678",
|
|
}),
|
|
),
|
|
).toMatchObject({ type: "authenticate", role: "viewer" });
|
|
expect(
|
|
decodeClientMessage(
|
|
JSON.stringify({
|
|
type: "authenticate",
|
|
protocol: SIGNALING_PROTOCOL,
|
|
roomId,
|
|
role: "viewer",
|
|
viewerGrant,
|
|
clientId: "client_12345678",
|
|
}),
|
|
),
|
|
).toMatchObject({ viewerGrant });
|
|
expect(
|
|
decodeClientMessage(
|
|
JSON.stringify({
|
|
type: "authenticate",
|
|
protocol: SIGNALING_PROTOCOL,
|
|
roomId,
|
|
role: "host",
|
|
token,
|
|
clientId: "host_client_12345678",
|
|
shareGeneration: "share_generation_12345678",
|
|
qualitySettings: qualitySettingsWithAudio,
|
|
}),
|
|
),
|
|
).toMatchObject({
|
|
role: "host",
|
|
shareGeneration: "share_generation_12345678",
|
|
qualitySettings: qualitySettingsWithAudio,
|
|
routePolicy: {
|
|
peerOnly: false,
|
|
topologyOptimization: true,
|
|
natPrediction: false,
|
|
},
|
|
});
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "authenticate",
|
|
protocol: SIGNALING_PROTOCOL,
|
|
roomId,
|
|
role: "viewer",
|
|
clientId: "client_12345678",
|
|
capabilities: { peerIceTurn: true },
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "authenticate",
|
|
roomId,
|
|
role: "viewer",
|
|
clientId: "client_12345678",
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "authenticate",
|
|
protocol: "invalid-protocol",
|
|
roomId,
|
|
role: "viewer",
|
|
clientId: "client_12345678",
|
|
}).success,
|
|
).toBe(false);
|
|
});
|
|
|
|
it("normalizes display names and rejects misleading Unicode boundaries", () => {
|
|
expect(normalizeDisplayName(" Cafe\u0301\u00a0朋友 ")).toBe("Café 朋友");
|
|
expect(normalizeDisplayName("玩家 👩💻")).toBe("玩家 👩💻");
|
|
expect(normalizeDisplayName("名".repeat(MAX_DISPLAY_NAME_CODE_POINTS))).toBe(
|
|
"名".repeat(MAX_DISPLAY_NAME_CODE_POINTS),
|
|
);
|
|
expect(
|
|
normalizeDisplayName("名".repeat(MAX_DISPLAY_NAME_CODE_POINTS + 1)),
|
|
).toBeNull();
|
|
for (const invalid of ["a\nb", "a\u202eb", "a\ufeffb", "a\ud800b"]) {
|
|
expect(normalizeDisplayName(invalid)).toBeNull();
|
|
}
|
|
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "authenticate",
|
|
protocol: SIGNALING_PROTOCOL,
|
|
roomId,
|
|
role: "viewer",
|
|
clientId: "client_12345678",
|
|
displayName: "小明",
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "authenticate",
|
|
protocol: SIGNALING_PROTOCOL,
|
|
roomId,
|
|
role: "host",
|
|
token,
|
|
clientId: "client_12345678",
|
|
viewerPresence: true,
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "set-display-name",
|
|
displayName: " Cafe\u0301 ",
|
|
}).success,
|
|
).toBe(false);
|
|
expect(DEFAULT_VIEWER_DISPLAY_NAME).toBe("观众");
|
|
});
|
|
|
|
it("accepts only canonical bounded Viewer grants and access actions", () => {
|
|
for (const malformedGrant of [
|
|
"b".repeat(21),
|
|
"b".repeat(23),
|
|
`${"b".repeat(21)}=`,
|
|
`${"b".repeat(21)}b`,
|
|
`g1.${roomId}.1787076000.${"b".repeat(43)}`,
|
|
]) {
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "authenticate",
|
|
protocol: SIGNALING_PROTOCOL,
|
|
roomId,
|
|
role: "viewer",
|
|
viewerGrant: malformedGrant,
|
|
clientId: "client_12345678",
|
|
}).success,
|
|
).toBe(false);
|
|
}
|
|
for (const policy of ["open", "private"] as const) {
|
|
expect(
|
|
roomAccessUpdateRequestSchema.safeParse({
|
|
action: "set-code-entry-policy",
|
|
policy,
|
|
}).success,
|
|
).toBe(true);
|
|
}
|
|
expect(
|
|
roomAccessUpdateRequestSchema.safeParse({
|
|
action: "rotate-viewer-grant",
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
roomAccessUpdateRequestSchema.safeParse({
|
|
action: "revoke-viewer-grant",
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
roomAccessUpdateRequestSchema.safeParse({
|
|
action: "set-code-entry-policy",
|
|
policy: "disabled",
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
roomAccessUpdateRequestSchema.safeParse({
|
|
action: "set-code-entry-policy",
|
|
policy: "password",
|
|
}).success,
|
|
).toBe(false);
|
|
for (const removedMessage of [
|
|
{ type: "set-code-entry-policy", policy: "open" },
|
|
{ type: "rotate-viewer-grant" },
|
|
{ type: "revoke-viewer-grant" },
|
|
{ type: "set-viewer-password", password: "room-password" },
|
|
]) {
|
|
expect(clientMessageSchema.safeParse(removedMessage).success).toBe(false);
|
|
}
|
|
});
|
|
|
|
it("accepts simple bounded Viewer passwords and access responses", () => {
|
|
expect(viewerPasswordSchema.safeParse("x").success).toBe(true);
|
|
expect(viewerPasswordSchema.safeParse("simple-password").success).toBe(true);
|
|
for (const invalid of [
|
|
"",
|
|
"contains space",
|
|
"line\nbreak",
|
|
"x".repeat(MAX_VIEWER_PASSWORD_LENGTH + 1),
|
|
]) {
|
|
expect(viewerPasswordSchema.safeParse(invalid).success).toBe(false);
|
|
}
|
|
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "authenticate",
|
|
protocol: SIGNALING_PROTOCOL,
|
|
roomId,
|
|
role: "viewer",
|
|
clientId: "client_12345678",
|
|
viewerPassword: "easy-password",
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
roomAccessUpdateRequestSchema.safeParse({
|
|
action: "set-viewer-password",
|
|
password: null,
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
roomAccessUpdateResponseSchema.safeParse({
|
|
type: "viewer-password-updated",
|
|
enabled: true,
|
|
}).success,
|
|
).toBe(true);
|
|
});
|
|
|
|
it("rejects unknown fields and malformed tokens", () => {
|
|
const result = clientMessageSchema.safeParse({
|
|
type: "authenticate",
|
|
protocol: SIGNALING_PROTOCOL,
|
|
roomId,
|
|
role: "host",
|
|
token: "short",
|
|
clientId: "client_12345678",
|
|
admin: true,
|
|
});
|
|
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it("requires a host token and rejects viewer tokens or malformed room codes", () => {
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "authenticate",
|
|
protocol: SIGNALING_PROTOCOL,
|
|
roomId,
|
|
role: "host",
|
|
token,
|
|
clientId: "client_12345678",
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "authenticate",
|
|
protocol: SIGNALING_PROTOCOL,
|
|
roomId,
|
|
role: "viewer",
|
|
token,
|
|
clientId: "client_12345678",
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "authenticate",
|
|
protocol: SIGNALING_PROTOCOL,
|
|
roomId: "1234",
|
|
role: "viewer",
|
|
clientId: "client_12345678",
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "authenticate",
|
|
protocol: SIGNALING_PROTOCOL,
|
|
roomId: "0123",
|
|
role: "viewer",
|
|
clientId: "client_12345678",
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "authenticate",
|
|
protocol: SIGNALING_PROTOCOL,
|
|
roomId: "1".repeat(5),
|
|
role: "viewer",
|
|
clientId: "client_12345678",
|
|
}).success,
|
|
).toBe(false);
|
|
});
|
|
|
|
it("bounds SDP before routing it", () => {
|
|
const result = clientMessageSchema.safeParse({
|
|
type: "signal",
|
|
targetPeerId: "viewer_12345678",
|
|
payload: {
|
|
kind: "description",
|
|
connectionId: "connection_12345678",
|
|
description: {
|
|
type: "offer",
|
|
sdp: "v".repeat(48 * 1024 + 1),
|
|
},
|
|
},
|
|
});
|
|
|
|
expect(result.success).toBe(false);
|
|
});
|
|
|
|
it("requires viewers to choose ICE restart or peer rebuild explicitly", () => {
|
|
const restartRequest = {
|
|
type: "restart-request",
|
|
connectionId: "connection_12345678",
|
|
rebuild: true,
|
|
};
|
|
|
|
expect(clientMessageSchema.safeParse(restartRequest).success).toBe(true);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
...restartRequest,
|
|
targetPeerId: "parent_12345678",
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: restartRequest.type,
|
|
connectionId: restartRequest.connectionId,
|
|
}).success,
|
|
).toBe(false);
|
|
});
|
|
|
|
it("accepts explicit sharing-stop and abandonment messages", () => {
|
|
expect(clientMessageSchema.safeParse({ type: "stop-sharing" }).success).toBe(
|
|
true,
|
|
);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "stop-sharing",
|
|
shareGeneration: "share_generation_12345678",
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "authenticate",
|
|
protocol: SIGNALING_PROTOCOL,
|
|
roomId,
|
|
role: "host",
|
|
token,
|
|
clientId: "client_12345678",
|
|
shareGeneration: "share_generation_12345678",
|
|
}).success,
|
|
).toBe(true);
|
|
expect(clientMessageSchema.safeParse({ type: "abandon-room" }).success).toBe(
|
|
true,
|
|
);
|
|
});
|
|
|
|
it("keeps intentional pause updates strict and generation-bound", () => {
|
|
const pause = {
|
|
type: "set-sharing-paused",
|
|
shareGeneration: "share_generation_12345678",
|
|
paused: true,
|
|
};
|
|
expect(clientMessageSchema.safeParse(pause).success).toBe(true);
|
|
expect(
|
|
clientMessageSchema.safeParse({ ...pause, paused: false }).success,
|
|
).toBe(true);
|
|
expect(
|
|
clientMessageSchema.safeParse({ ...pause, shareGeneration: undefined })
|
|
.success,
|
|
).toBe(false);
|
|
expect(
|
|
clientMessageSchema.safeParse({ ...pause, paused: "true" }).success,
|
|
).toBe(false);
|
|
expect(
|
|
clientMessageSchema.safeParse({ ...pause, unexpected: 1 }).success,
|
|
).toBe(false);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
type: "host-status",
|
|
online: true,
|
|
paused: true,
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
serverMessageSchema.safeParse({ type: "host-status", online: true })
|
|
.success,
|
|
).toBe(false);
|
|
const authoritativePause = {
|
|
type: "pause-sharing-source",
|
|
shareGeneration: "share_generation_12345678",
|
|
};
|
|
expect(serverMessageSchema.safeParse(authoritativePause).success).toBe(true);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
...authoritativePause,
|
|
unexpected: null,
|
|
}).success,
|
|
).toBe(false);
|
|
});
|
|
|
|
it("separates numeric requests from capture implementation limits", () => {
|
|
expect(DEFAULT_QUALITY_SETTINGS).not.toHaveProperty("videoCodec");
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "set-quality-settings",
|
|
qualitySettings,
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "set-quality-settings",
|
|
qualitySettings: qualitySettingsWithAudio,
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "set-quality-settings",
|
|
qualitySettings: { ...qualitySettings, resolution: { width: 854, height: 480 } },
|
|
}).success,
|
|
).toBe(true);
|
|
for (const maxAudioBitrate of [
|
|
6_000, 96_000, 256_000, 320_000, 510_000,
|
|
] as const) {
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "set-quality-settings",
|
|
qualitySettings: { ...qualitySettings, maxAudioBitrate },
|
|
}).success,
|
|
).toBe(true);
|
|
}
|
|
for (const next of [
|
|
{ ...qualitySettings, resolution: { width: 16384, height: 8192 } },
|
|
{ ...qualitySettings, resolution: { width: 1921, height: 1081 } },
|
|
{ ...qualitySettings, maxFramerate: 14 },
|
|
{ ...qualitySettings, maxFramerate: 61 },
|
|
{ ...qualitySettings, maxFramerate: 30.5 },
|
|
{ ...qualitySettings, maxBitrate: 1_999_999 },
|
|
{ ...qualitySettings, maxBitrate: 12_000_001 },
|
|
]) expect(clientMessageSchema.safeParse({ type: "set-quality-settings", qualitySettings: next }).success).toBe(true);
|
|
for (const invalid of [
|
|
{ ...qualitySettings, resolution: "2160p" },
|
|
{ ...qualitySettings, resolution: { width: 0x1_0000_0000, height: 1080 } },
|
|
{ ...qualitySettings, maxFramerate: 0 },
|
|
{ ...qualitySettings, maxFramerate: Infinity },
|
|
{ ...qualitySettings, maxFramerate: Number.MAX_SAFE_INTEGER + 1 },
|
|
{ ...qualitySettings, maxBitrate: 0 },
|
|
{ ...qualitySettings, maxBitrate: 0x1_0000_0000 },
|
|
{ ...qualitySettings, maxBitrate: 5_000_000.5 },
|
|
{ ...qualitySettings, degradationPreference: "automatic" },
|
|
legacyCodecQualitySettings,
|
|
{ ...qualitySettings, maxAudioBitrate: "lossless" },
|
|
{ ...qualitySettings, maxAudioBitrate: "music" },
|
|
{ ...qualitySettings, maxAudioBitrate: 0 },
|
|
{ ...qualitySettings, maxAudioBitrate: 5_999 },
|
|
{ ...qualitySettings, maxAudioBitrate: 510_001 },
|
|
{ ...qualitySettings, maxAudioBitrate: 128_000.5 },
|
|
{ ...qualitySettings, maxAudioBitrate: null },
|
|
{ ...qualitySettings, maxAudioBitrate: 960_000 },
|
|
{ ...qualitySettings, codec: "video/VP9" },
|
|
]) {
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "set-quality-settings",
|
|
qualitySettings: invalid,
|
|
}).success,
|
|
).toBe(false);
|
|
}
|
|
const { maxFramerate: _missing, ...missingField } = qualitySettings;
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "set-quality-settings",
|
|
qualitySettings: missingField,
|
|
}).success,
|
|
).toBe(false);
|
|
});
|
|
|
|
it("accepts only a bounded browser relay capacity", () => {
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "relay-capacity",
|
|
downstreamEdges: 0,
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "relay-capacity",
|
|
downstreamEdges: 1,
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "relay-capacity",
|
|
downstreamEdges: 2,
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "relay-capacity",
|
|
downstreamEdges: 3,
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "relay-capacity",
|
|
downstreamEdges: 4,
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "relay-capacity",
|
|
downstreamEdges: 1,
|
|
score: 100,
|
|
}).success,
|
|
).toBe(false);
|
|
});
|
|
|
|
it("accepts only strict, bounded viewer quality evidence", () => {
|
|
expect(clientMessageSchema.safeParse(qualityEvidence).success).toBe(true);
|
|
const { natTraversalPath: _path, ...legacyMetrics } =
|
|
qualityEvidence.metrics;
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
...qualityEvidence,
|
|
metrics: legacyMetrics,
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
...qualityEvidence,
|
|
metrics: {
|
|
...qualityEvidence.metrics,
|
|
freezeDurationMsDelta: 12_000,
|
|
pauseDurationMsDelta: 6_000,
|
|
},
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
...qualityEvidence,
|
|
metrics: {
|
|
...qualityEvidence.metrics,
|
|
freezeCountDelta: null,
|
|
freezeDurationMsDelta: null,
|
|
pauseCountDelta: null,
|
|
pauseDurationMsDelta: null,
|
|
},
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
Buffer.byteLength(JSON.stringify(qualityEvidence), "utf8"),
|
|
).toBeLessThanOrEqual(MAX_VIEWER_QUALITY_EVIDENCE_BYTES);
|
|
|
|
for (const invalid of [
|
|
{ ...qualityEvidence, roomId },
|
|
{
|
|
...qualityEvidence,
|
|
guard: { ...qualityEvidence.guard, peerId: "viewer_12345678" },
|
|
},
|
|
{ ...qualityEvidence, windowMs: 999 },
|
|
{ ...qualityEvidence, sequence: -1 },
|
|
{
|
|
...qualityEvidence,
|
|
metrics: { ...qualityEvidence.metrics, width: null },
|
|
},
|
|
{
|
|
...qualityEvidence,
|
|
metrics: { ...qualityEvidence.metrics, bitrateKbps: Number.POSITIVE_INFINITY },
|
|
},
|
|
{
|
|
...qualityEvidence,
|
|
metrics: { ...qualityEvidence.metrics, freezeCountDelta: -1 },
|
|
},
|
|
{
|
|
...qualityEvidence,
|
|
metrics: {
|
|
...qualityEvidence.metrics,
|
|
freezeCountDelta: Number.MAX_SAFE_INTEGER + 1,
|
|
},
|
|
},
|
|
{
|
|
...qualityEvidence,
|
|
metrics: {
|
|
...qualityEvidence.metrics,
|
|
freezeDurationMsDelta: Number.MAX_SAFE_INTEGER + 1,
|
|
},
|
|
},
|
|
{
|
|
...qualityEvidence,
|
|
metrics: { ...qualityEvidence.metrics, pauseCountDelta: -1 },
|
|
},
|
|
{
|
|
...qualityEvidence,
|
|
metrics: {
|
|
...qualityEvidence.metrics,
|
|
pauseDurationMsDelta: Number.MAX_SAFE_INTEGER + 1,
|
|
},
|
|
},
|
|
{
|
|
...qualityEvidence,
|
|
metrics: {
|
|
...qualityEvidence.metrics,
|
|
codecParameters: "sprop-parameter-sets=deadbeef",
|
|
},
|
|
},
|
|
{
|
|
...qualityEvidence,
|
|
metrics: {
|
|
...qualityEvidence.metrics,
|
|
codecProfile: "device-id=deadbeef",
|
|
},
|
|
},
|
|
{
|
|
...qualityEvidence,
|
|
metrics: {
|
|
...qualityEvidence.metrics,
|
|
scalabilityMode: "DEVICE_ABC123",
|
|
},
|
|
},
|
|
{
|
|
...qualityEvidence,
|
|
metrics: {
|
|
...qualityEvidence.metrics,
|
|
decoderImplementation: "device-specific-decoder",
|
|
},
|
|
},
|
|
]) {
|
|
expect(clientMessageSchema.safeParse(invalid).success).toBe(false);
|
|
}
|
|
});
|
|
|
|
it("accepts only native sender quality state with exact known identity", () => {
|
|
const sender = {
|
|
type: "sender-quality-evidence",
|
|
childPeerId: "viewer_12345678",
|
|
connectionId: "connection_12345678",
|
|
rtpStatsId: "rtp-stats-1",
|
|
trackIdentifier: "track-1",
|
|
sampleTimestampMs: 2_000,
|
|
routeRevision: 3,
|
|
state: "degraded",
|
|
diagnostics: {
|
|
natTraversalPath: "ordinary",
|
|
reason: "bandwidth",
|
|
framesPerSecond: 24,
|
|
bitrateKbps: 1_500,
|
|
},
|
|
} as const;
|
|
expect(clientMessageSchema.safeParse(sender).success).toBe(true);
|
|
const { natTraversalPath: _senderPath, ...legacyDiagnostics } =
|
|
sender.diagnostics;
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
...sender,
|
|
diagnostics: legacyDiagnostics,
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
...sender,
|
|
state: "healthy",
|
|
rtpStatsId: null,
|
|
diagnostics: { ...sender.diagnostics, reason: "none" },
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
...sender,
|
|
sampleTimestampMs: null,
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "sfu-publisher-quality-evidence",
|
|
publicationGeneration: "publication_generation_12345678",
|
|
routeRevision: 4,
|
|
state: "healthy",
|
|
sampleTimestampMs: 2_000,
|
|
diagnostics: {
|
|
natTraversalPath: "ordinary",
|
|
reason: "none",
|
|
framesPerSecond: 30,
|
|
bitrateKbps: 2_000,
|
|
},
|
|
}).success,
|
|
).toBe(true);
|
|
});
|
|
|
|
it.each([
|
|
["video/VP8", null, "max-fr=60; max-fs=8160"],
|
|
["video/VP9", "profile-id=2", "max-fs=8160"],
|
|
["video/AV1", "profile=1", "level-idx=31; tier=0"],
|
|
["video/unknown", null, null],
|
|
])(
|
|
"accepts canonical viewer codec evidence for %s",
|
|
(codec, codecProfile, codecParameters) => {
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
...qualityEvidence,
|
|
metrics: {
|
|
...qualityEvidence.metrics,
|
|
codec,
|
|
codecProfile,
|
|
codecParameters,
|
|
},
|
|
}).success,
|
|
).toBe(true);
|
|
},
|
|
);
|
|
|
|
it("accepts bounded route acknowledgements and failures", () => {
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "route-ready",
|
|
revision: 7,
|
|
phase: "prepare",
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "route-ready",
|
|
revision: 7,
|
|
phase: "prepare",
|
|
qualityApproved: true,
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "route-ready",
|
|
revision: 7,
|
|
phase: "prepare",
|
|
qualityApproved: false,
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "route-transport-connected",
|
|
revision: 7,
|
|
connectionId: "connection_12345678",
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "route-media-unavailable",
|
|
revision: 7,
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "route-failed",
|
|
revision: 7,
|
|
phase: "active",
|
|
connectionId: null,
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "refresh-sfu",
|
|
revision: MAX_MEDIA_ROUTE_REVISION,
|
|
}).success,
|
|
).toBe(true);
|
|
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "route-ready",
|
|
revision: -1,
|
|
phase: "prepare",
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "route-transport-connected",
|
|
revision: 7,
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "route-media-unavailable",
|
|
revision: -1,
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "route-failed",
|
|
revision: 7,
|
|
phase: "active",
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
clientMessageSchema.safeParse({
|
|
type: "refresh-sfu",
|
|
revision: 1.5,
|
|
}).success,
|
|
).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("server signaling protocol", () => {
|
|
it("keeps route status pairings revision-fenced and strict", () => {
|
|
for (const message of [
|
|
{
|
|
type: "route-status",
|
|
revision: 3,
|
|
state: "waiting",
|
|
reason: "sfu-admission",
|
|
},
|
|
{
|
|
type: "route-status",
|
|
revision: 4,
|
|
state: "failed",
|
|
reason: "route-exhausted",
|
|
},
|
|
]) {
|
|
expect(serverMessageSchema.safeParse(message).success).toBe(true);
|
|
}
|
|
for (const message of [
|
|
{
|
|
type: "route-status",
|
|
revision: 3,
|
|
state: "waiting",
|
|
reason: "route-exhausted",
|
|
},
|
|
{
|
|
type: "route-status",
|
|
revision: 3,
|
|
state: "failed",
|
|
reason: "sfu-admission",
|
|
},
|
|
{
|
|
type: "route-status",
|
|
revision: 3,
|
|
state: "failed",
|
|
reason: "route-exhausted",
|
|
retry: true,
|
|
},
|
|
]) {
|
|
expect(serverMessageSchema.safeParse(message).success).toBe(false);
|
|
}
|
|
});
|
|
|
|
it("accepts only the privacy-safe route snapshot payload", () => {
|
|
const message = {
|
|
type: "route-diagnostic-snapshot",
|
|
snapshot: {
|
|
children: [
|
|
{
|
|
ordinal: 1,
|
|
parent: { kind: "host" },
|
|
effectiveCapacity: 2,
|
|
childCount: 1,
|
|
demandAgeMs: 100,
|
|
queueWaitMs: 10,
|
|
candidateStartMs: 20,
|
|
firstDecodedFrameMs: 80,
|
|
finalMs: 80,
|
|
finalRoute: "direct",
|
|
rejectionBucket: "none",
|
|
quality: {
|
|
eligibleWindows: 3,
|
|
eligibleDurationMs: 6_000,
|
|
freezeWindows: 1,
|
|
freezeCount: 1,
|
|
freezeDurationMs: 250,
|
|
pauseCount: 0,
|
|
pauseDurationMs: 0,
|
|
},
|
|
},
|
|
{
|
|
ordinal: 2,
|
|
parent: { kind: "viewer", ordinal: 1 },
|
|
effectiveCapacity: 0,
|
|
childCount: 0,
|
|
demandAgeMs: 90,
|
|
queueWaitMs: null,
|
|
candidateStartMs: null,
|
|
firstDecodedFrameMs: null,
|
|
finalMs: null,
|
|
finalRoute: "waiting",
|
|
rejectionBucket: "sfu-admission",
|
|
quality: null,
|
|
},
|
|
],
|
|
operation: {
|
|
childOrdinal: 2,
|
|
reason: "join",
|
|
stage: "admission",
|
|
cursor: 1,
|
|
candidateCount: 2,
|
|
},
|
|
},
|
|
} as const;
|
|
expect(serverMessageSchema.safeParse(message).success).toBe(true);
|
|
for (const invalid of [
|
|
{
|
|
...message,
|
|
snapshot: { ...message.snapshot, peerId: "viewer_private_12345678" },
|
|
},
|
|
{
|
|
...message,
|
|
snapshot: {
|
|
...message.snapshot,
|
|
children: [
|
|
{
|
|
...message.snapshot.children[0],
|
|
connectionId: "connection_private_12345678",
|
|
},
|
|
],
|
|
},
|
|
},
|
|
{
|
|
...message,
|
|
snapshot: {
|
|
...message.snapshot,
|
|
children: [
|
|
{
|
|
...message.snapshot.children[0],
|
|
quality: {
|
|
...message.snapshot.children[0].quality,
|
|
presentationEpoch: 1,
|
|
},
|
|
},
|
|
],
|
|
},
|
|
},
|
|
{
|
|
...message,
|
|
snapshot: {
|
|
...message.snapshot,
|
|
children: [
|
|
{
|
|
...message.snapshot.children[0],
|
|
quality: {
|
|
...message.snapshot.children[0].quality,
|
|
freezeCount: -1,
|
|
},
|
|
},
|
|
],
|
|
},
|
|
},
|
|
{
|
|
...message,
|
|
snapshot: {
|
|
...message.snapshot,
|
|
children: [
|
|
message.snapshot.children[0],
|
|
{
|
|
...message.snapshot.children[1],
|
|
parent: { kind: "viewer", ordinal: 2 },
|
|
},
|
|
],
|
|
},
|
|
},
|
|
{
|
|
...message,
|
|
snapshot: {
|
|
...message.snapshot,
|
|
operation: {
|
|
...message.snapshot.operation,
|
|
rejectionBucket: "raw-error",
|
|
},
|
|
},
|
|
},
|
|
]) {
|
|
expect(serverMessageSchema.safeParse(invalid).success).toBe(false);
|
|
}
|
|
});
|
|
it("accepts only STUN URLs in the authenticated peer ICE config", () => {
|
|
for (const url of ["stun:stun.test:80/", "stun:stun.test:3478/"]) {
|
|
expect(serverMessageSchema.safeParse({
|
|
...authenticatedMessage(20),
|
|
iceConfig: { iceServers: [{ urls: url }], natPredictionStunUrls: [] },
|
|
}).success).toBe(false);
|
|
}
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
type: "ice-config",
|
|
iceConfig: { iceServers: [] },
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
...authenticatedMessage(8),
|
|
iceConfig: {
|
|
iceServers: [
|
|
{
|
|
urls: "turn:relay.example.test:3478?transport=udp",
|
|
username: `1787076000:${"a".repeat(32)}`,
|
|
credential: "credential",
|
|
},
|
|
],
|
|
},
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
...authenticatedMessage(8),
|
|
iceConfig: {
|
|
iceServers: [{ urls: "turn:relay.example.test:3478?transport=udp" }],
|
|
},
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
...authenticatedMessage(8),
|
|
iceConfig: {
|
|
iceServers: [{ urls: "stuns:stun.example.test:5349" }],
|
|
},
|
|
}).success,
|
|
).toBe(false);
|
|
});
|
|
|
|
function authenticatedMessage(maxViewers: number) {
|
|
return {
|
|
type: "authenticated",
|
|
protocol: SIGNALING_PROTOCOL,
|
|
role: "host",
|
|
peerId: "host_12345678",
|
|
maxViewers,
|
|
endpointMediaCopyCapacity: 2,
|
|
hostOnline: true,
|
|
connectionId: null,
|
|
mediaMode: "peer-assisted",
|
|
shareGeneration: "share_generation_12345678",
|
|
routeRevision: 0,
|
|
routeAssignment: {
|
|
upstream: { kind: "none" },
|
|
childPeerIds: [],
|
|
sfuPublicationGeneration: null,
|
|
},
|
|
qualitySettings,
|
|
routePolicy: DEFAULT_ROUTE_POLICY,
|
|
codeEntryPolicy: "open",
|
|
viewerPasswordEnabled: false,
|
|
viewerAuthorizationGeneration: "viewer_generation_12345678",
|
|
iceConfig: {
|
|
iceServers: [],
|
|
natPredictionStunUrls: [],
|
|
},
|
|
};
|
|
}
|
|
|
|
it("scopes password configuration state to authenticated Hosts", () => {
|
|
const host = authenticatedMessage(8) as Record<string, unknown>;
|
|
expect(serverMessageSchema.safeParse(host).success).toBe(true);
|
|
delete host.viewerPasswordEnabled;
|
|
expect(serverMessageSchema.safeParse(host).success).toBe(false);
|
|
|
|
const viewer = {
|
|
...authenticatedMessage(8),
|
|
role: "viewer",
|
|
peerId: "viewer_12345678",
|
|
} as Record<string, unknown>;
|
|
delete viewer.viewerPasswordEnabled;
|
|
expect(serverMessageSchema.safeParse(viewer).success).toBe(true);
|
|
viewer.viewerPasswordEnabled = true;
|
|
expect(serverMessageSchema.safeParse(viewer).success).toBe(false);
|
|
});
|
|
|
|
it("requires the v20 NAT observation configuration", () => {
|
|
const message = authenticatedMessage(8);
|
|
const { natPredictionStunUrls: _urls, ...legacyIceConfig } =
|
|
message.iceConfig;
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
...message,
|
|
iceConfig: legacyIceConfig,
|
|
}).success,
|
|
).toBe(false);
|
|
});
|
|
|
|
it("accepts dynamic viewer limits within the protocol boundary", () => {
|
|
expect(MAX_VIEWERS_PER_ROOM_LIMIT).toBe(20);
|
|
expect(serverMessageSchema.safeParse(authenticatedMessage(1)).success).toBe(
|
|
true,
|
|
);
|
|
expect(serverMessageSchema.safeParse(authenticatedMessage(8)).success).toBe(
|
|
true,
|
|
);
|
|
expect(
|
|
serverMessageSchema.safeParse(
|
|
authenticatedMessage(MAX_VIEWERS_PER_ROOM_LIMIT),
|
|
).success,
|
|
).toBe(true);
|
|
});
|
|
|
|
it("requires the authenticated endpoint capacity to be 1, 2, or 3", () => {
|
|
for (const endpointMediaCopyCapacity of [1, 2, 3]) {
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
...authenticatedMessage(8),
|
|
endpointMediaCopyCapacity,
|
|
}).success,
|
|
).toBe(true);
|
|
}
|
|
for (const endpointMediaCopyCapacity of [undefined, 0, 4, 1.5]) {
|
|
const message = authenticatedMessage(8) as Record<string, unknown>;
|
|
if (endpointMediaCopyCapacity === undefined) {
|
|
delete message.endpointMediaCopyCapacity;
|
|
} else {
|
|
message.endpointMediaCopyCapacity = endpointMediaCopyCapacity;
|
|
}
|
|
expect(serverMessageSchema.safeParse(message).success).toBe(false);
|
|
}
|
|
});
|
|
|
|
it("accepts a strict, unique and bounded Viewer presence snapshot", () => {
|
|
const viewer = {
|
|
role: "viewer",
|
|
peerId: "viewer_12345678",
|
|
displayName: "小明",
|
|
upstream: { kind: "peer", peerId: "viewer_parent_12345678" },
|
|
} as const;
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
type: "viewer-presence",
|
|
viewers: [
|
|
{
|
|
...viewer,
|
|
upstream: { kind: "sfu" },
|
|
mediaReady: true,
|
|
},
|
|
],
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
type: "viewer-presence",
|
|
viewers: [{ ...viewer, mediaReady: false }],
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
type: "viewer-presence",
|
|
viewers: [viewer, viewer],
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
type: "viewer-presence",
|
|
viewers: [{ ...viewer, mediaTopology: "peer-relay" }],
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
type: "viewer-presence",
|
|
viewers: [{ ...viewer, ip: "203.0.113.1" }],
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
type: "viewer-presence",
|
|
viewers: [
|
|
viewer,
|
|
{
|
|
role: "host",
|
|
peerId: "host_12345678",
|
|
displayName: "分享者",
|
|
upstream: { kind: "none" },
|
|
},
|
|
],
|
|
}).success,
|
|
).toBe(true);
|
|
});
|
|
|
|
it("requires a complete bounded peer-assisted assignment", () => {
|
|
const peerAssisted = {
|
|
...authenticatedMessage(8),
|
|
mediaMode: "peer-assisted",
|
|
shareGeneration: "share_generation_12345678",
|
|
routeRevision: 0,
|
|
routeAssignment: {
|
|
upstream: { kind: "none" },
|
|
childPeerIds: ["viewer_12345678", "viewer_87654321"],
|
|
sfuPublicationGeneration: null,
|
|
},
|
|
qualitySettings,
|
|
routePolicy: DEFAULT_ROUTE_POLICY,
|
|
};
|
|
|
|
expect(serverMessageSchema.safeParse(peerAssisted).success).toBe(true);
|
|
const incomplete = {
|
|
...authenticatedMessage(8),
|
|
} as Record<string, unknown>;
|
|
delete incomplete.routeAssignment;
|
|
expect(serverMessageSchema.safeParse(incomplete).success).toBe(false);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
...peerAssisted,
|
|
routeAssignment: {
|
|
...peerAssisted.routeAssignment,
|
|
childPeerIds: Array.from({ length: MAX_VIEWERS_PER_ROOM_LIMIT + 1 }, (_, index) => `viewer_overflow_${index}`),
|
|
},
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
...peerAssisted,
|
|
qualitySettings: { ...qualitySettings, maxBitrate: 0x1_0000_0000 },
|
|
}).success,
|
|
).toBe(false);
|
|
const missingMode = {
|
|
...authenticatedMessage(8),
|
|
} as Record<string, unknown>;
|
|
delete missingMode.mediaMode;
|
|
expect(serverMessageSchema.safeParse(missingMode).success).toBe(false);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
type: "quality-settings",
|
|
qualitySettings: {
|
|
resolution: { width: 1280, height: 720 },
|
|
maxFramerate: 30,
|
|
maxBitrate: 3_000_000,
|
|
degradationPreference: "balanced",
|
|
},
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
type: "quality-settings",
|
|
qualitySettings: { ...qualitySettings, maxFramerate: 0 },
|
|
}).success,
|
|
).toBe(false);
|
|
});
|
|
|
|
it("accepts only bounded connection-attempt progress on a prepared route", () => {
|
|
const candidate = {
|
|
childPeerId: "child_12345678", connectionId: "connection_12345678",
|
|
transport: "direct", qualityProbe: false,
|
|
};
|
|
for (const total of [1, 3, 4, 5, 20]) {
|
|
for (let current = 1; current <= total; current++) {
|
|
expect(preparedRouteCandidateSchema.safeParse({
|
|
...candidate, connectionAttempt: { current, total },
|
|
}).success).toBe(true);
|
|
}
|
|
}
|
|
for (const connectionAttempt of [
|
|
{ current: 0, total: 3 }, { current: 4, total: 3 },
|
|
{ current: 5, total: 4 }, { current: 1, total: 0 }, { current: 1, total: Number.MAX_SAFE_INTEGER + 1 }, { current: 1.5, total: 3 },
|
|
]) {
|
|
expect(preparedRouteCandidateSchema.safeParse({
|
|
...candidate, connectionAttempt,
|
|
}).success).toBe(false);
|
|
}
|
|
});
|
|
|
|
it("keeps route messages strict alongside the single authenticated mode", () => {
|
|
const assignment = {
|
|
upstream: { kind: "peer", peerId: "parent_12345678" },
|
|
childPeerIds: ["child_12345678", "child_87654321"],
|
|
sfuPublicationGeneration: null,
|
|
};
|
|
|
|
expect(participantRouteAssignmentSchema.safeParse(assignment).success).toBe(
|
|
true,
|
|
);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
type: "route-update",
|
|
revision: 9,
|
|
phase: "prepare",
|
|
assignment,
|
|
candidate: {
|
|
childPeerId: "child_12345678",
|
|
connectionId: "connection_12345678",
|
|
transport: "direct",
|
|
qualityProbe: false,
|
|
},
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
type: "route-update",
|
|
revision: 10,
|
|
phase: "prepare",
|
|
assignment,
|
|
candidate: {
|
|
childPeerId: "child_12345678",
|
|
connectionId: "connection_12345678",
|
|
transport: "sfu",
|
|
qualityProbe: true,
|
|
},
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
type: "route-update",
|
|
revision: 10,
|
|
phase: "prepare",
|
|
assignment,
|
|
candidate: {
|
|
childPeerId: "child_12345678",
|
|
connectionId: "connection_12345678",
|
|
transport: "direct",
|
|
qualityProbe: false,
|
|
unexpected: null,
|
|
},
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
type: "sfu-config",
|
|
revision: 9,
|
|
publicationGeneration: "publication_12345678",
|
|
connectionId: "connection_12345678",
|
|
}).success,
|
|
).toBe(true);
|
|
|
|
expect(
|
|
participantRouteAssignmentSchema.safeParse({
|
|
...assignment,
|
|
childPeerIds: Array.from({ length: MAX_VIEWERS_PER_ROOM_LIMIT + 1 }, (_, index) => `child_${String(index).padStart(8, "0")}`),
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
participantRouteAssignmentSchema.safeParse({
|
|
...assignment,
|
|
childPeerIds: ["child_12345678", "child_12345678"],
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
type: "route-update",
|
|
revision: 9,
|
|
phase: "prepare",
|
|
assignment,
|
|
roomAssignments: [],
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
type: "sfu-config",
|
|
revision: 9,
|
|
publicationGeneration: "publication_12345678",
|
|
connectionId: "",
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
...authenticatedMessage(8),
|
|
routeRevision: MAX_MEDIA_ROUTE_REVISION + 1,
|
|
}).success,
|
|
).toBe(false);
|
|
});
|
|
|
|
it("accepts every bounded hybrid upstream shape", () => {
|
|
for (const upstream of [
|
|
{ kind: "none" },
|
|
{ kind: "peer", peerId: "parent_12345678" },
|
|
{ kind: "sfu" },
|
|
]) {
|
|
expect(
|
|
participantRouteAssignmentSchema.safeParse({
|
|
upstream,
|
|
childPeerIds: [],
|
|
sfuPublicationGeneration:
|
|
upstream.kind === "peer" ? null : "generation_12345678",
|
|
}).success,
|
|
).toBe(true);
|
|
}
|
|
expect(
|
|
participantRouteAssignmentSchema.safeParse({
|
|
upstream: { kind: "sfu" },
|
|
childPeerIds: [],
|
|
sfuPublicationGeneration: null,
|
|
}).success,
|
|
).toBe(false);
|
|
});
|
|
|
|
it("accepts only canonical server-derived viewer evidence envelopes", () => {
|
|
const forwarded = {
|
|
...qualityEvidence,
|
|
viewerPeerId: "viewer_12345678",
|
|
upstream: { kind: "peer", peerId: "host_12345678" },
|
|
};
|
|
expect(serverMessageSchema.safeParse(forwarded).success).toBe(true);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
...forwarded,
|
|
metrics: {
|
|
...forwarded.metrics,
|
|
freezeDurationMsDelta: 12_000,
|
|
pauseDurationMsDelta: 6_000,
|
|
},
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
...forwarded,
|
|
upstream: { kind: "sfu" },
|
|
}).success,
|
|
).toBe(true);
|
|
expect(
|
|
Buffer.byteLength(JSON.stringify(forwarded), "utf8"),
|
|
).toBeLessThanOrEqual(MAX_VIEWER_QUALITY_EVIDENCE_BYTES);
|
|
expect(
|
|
serverMessageSchema.safeParse({ ...forwarded, roomId }).success,
|
|
).toBe(false);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
...qualityEvidence,
|
|
viewerPeerId: "viewer_12345678",
|
|
}).success,
|
|
).toBe(false);
|
|
expect(
|
|
serverMessageSchema.safeParse({
|
|
...forwarded,
|
|
upstream: { kind: "none" },
|
|
}).success,
|
|
).toBe(false);
|
|
});
|
|
|
|
it.each([0, 1.5, MAX_VIEWERS_PER_ROOM_LIMIT + 1])(
|
|
"rejects viewer limit %s outside the protocol boundary",
|
|
(maxViewers) => {
|
|
expect(
|
|
serverMessageSchema.safeParse(authenticatedMessage(maxViewers)).success,
|
|
).toBe(false);
|
|
},
|
|
);
|
|
|
|
});
|