891 lines
46 KiB
TypeScript
891 lines
46 KiB
TypeScript
import { AppPage } from "../src/client/channel/app-link";
|
|
import { playbackOutput } from "../src/client/media/playback-output";
|
|
import { HTMLVideoMedia, AppPageMedia, type AppPagePort, type WatchMediaEvent, type WatchMedia, type WatchMediaState } from "../src/client/channel/watch-media";
|
|
import { afterEach, expect, test, vi } from "vitest";
|
|
import { WatchPlayer } from "../src/client/channel/watch-playback";
|
|
|
|
class Video extends EventTarget {
|
|
currentTime = 0; duration = 120; playbackRate = 1; paused = true; ended = false; readyState = 0;
|
|
ranges: [number, number][] = [[0, 120]];
|
|
get buffered() { return { length: this.ranges.length, start: (index: number) => this.ranges[index][0], end: (index: number) => this.ranges[index][1] }; }
|
|
src = ""; error = null; volume = 1; muted = false;
|
|
denied = false;
|
|
play = vi.fn(async () => { if (this.denied) throw new DOMException("Playback denied", "NotAllowedError"); this.paused = false; this.dispatchEvent(new Event("playing")); });
|
|
pause() { this.paused = true; }
|
|
removeAttribute() { this.src = ""; }
|
|
load = vi.fn();
|
|
remove = vi.fn();
|
|
}
|
|
afterEach(() => { vi.restoreAllMocks(); vi.unstubAllGlobals(); vi.useRealTimers(); });
|
|
|
|
test("a displayed frame suffices while paused and running playback needs data for the next frame", () => {
|
|
const element = Object.assign(new Video(), { videoWidth: 640, readyState: 2 });
|
|
const media: WatchMedia = { presentation: { kind: "video", element: element as unknown as HTMLVideoElement },
|
|
read: vi.fn(), patch: vi.fn(), ready: vi.fn(), listen: () => () => {}, close: vi.fn() };
|
|
const player = new WatchPlayer("room", false, media, vi.fn());
|
|
try {
|
|
expect(player.waitingForPicture).toBe(true);
|
|
player.presentFrame();
|
|
expect(player.waitingForPicture).toBe(false);
|
|
element.paused = false;
|
|
expect(player.waitingForPicture).toBe(true);
|
|
element.ended = true;
|
|
expect(player.waitingForPicture).toBe(false);
|
|
element.ended = false;
|
|
element.readyState = 3;
|
|
expect(player.waitingForPicture).toBe(false);
|
|
} finally { player.close(); }
|
|
});
|
|
|
|
test("one watch player retains its first-frame observation across presentation mounts and rejects a late retired callback", () => {
|
|
const media: WatchMedia = { read: vi.fn(), patch: vi.fn(), ready: vi.fn(), listen: () => () => {}, close: vi.fn() };
|
|
const changed = vi.fn(), player = new WatchPlayer('room', false, media, changed);
|
|
expect(player.hasPresentedFrame).toBe(false);
|
|
player.presentFrame(); player.presentFrame();
|
|
expect(player.hasPresentedFrame).toBe(true); expect(changed).toHaveBeenCalledOnce();
|
|
player.close(); player.presentFrame();
|
|
expect(changed).toHaveBeenCalledOnce();
|
|
const replacement = new WatchPlayer('room', false, media, changed);
|
|
replacement.close(); replacement.presentFrame();
|
|
expect(replacement.hasPresentedFrame).toBe(false);
|
|
});
|
|
|
|
test("program anchors use the domain clock and wait for the sharer when a paused point expires", async () => {
|
|
vi.useFakeTimers(); let now = 0; vi.spyOn(performance, "now").mockImplementation(() => now);
|
|
const epoch = 1794744000;
|
|
const state: WatchMediaState = { position: epoch + 8, paused: false, rate: 1, duration: null, readyState: 4, ended: false,
|
|
buffered: [[epoch + 6, epoch + 12]], live: { active: true, seekable: [[epoch + 6, epoch + 18]], syncPosition: epoch + 12 } };
|
|
const patch = vi.fn(async (value: object) => { Object.assign(state, value); });
|
|
const media: WatchMedia = { read: async () => ({ ...state }), patch, ready: async () => {}, listen: () => () => {}, close() {} };
|
|
const player = new WatchPlayer("room", false, media, vi.fn());
|
|
try {
|
|
player.calibrate(5000, 0, 0); player.update({ timeline: "program", position: epoch + 10, paused: true, rate: 1, anchorTime: 5000 });
|
|
await player.ready(); expect(state.position).toBe(epoch + 10); expect(state.paused).toBe(true);
|
|
state.live = { active: true, seekable: [[epoch + 20, epoch + 32]], syncPosition: epoch + 26 }; state.buffered = [];
|
|
await vi.advanceTimersByTimeAsync(1000); expect(player.error).toBeNull(); // A locally retained pause is preserved.
|
|
state.position = epoch + 26; patch.mockClear();
|
|
await vi.advanceTimersByTimeAsync(1000);
|
|
expect(player.error).toBe("watch_live_position_unavailable");
|
|
expect(patch.mock.calls.every(([value]) => !("position" in value))).toBe(true);
|
|
player.update({ timeline: "program", position: epoch + 10, paused: false, rate: 1, anchorTime: 5000 });
|
|
await player.serialize(async () => {}); patch.mockClear(); await player.resume();
|
|
expect(state.paused).toBe(true);
|
|
expect(patch.mock.calls.some(([value]) => (value as { paused?: boolean }).paused === false)).toBe(false);
|
|
now = 5000;
|
|
player.update({ timeline: "program", position: epoch + 25, paused: false, rate: 1, anchorTime: 9000 });
|
|
await player.serialize(async () => {});
|
|
expect(state.position).toBe(epoch + 26); expect(state.paused).toBe(false); expect(player.error).toBeNull();
|
|
state.live.active = false; state.duration = 12;
|
|
await vi.advanceTimersByTimeAsync(1000); expect(state.position).toBeGreaterThan(epoch);
|
|
player.update({ timeline: "program", position: epoch + 32, paused: true, rate: 1, anchorTime: 10000 });
|
|
await player.serialize(async () => {});
|
|
expect(state.position).toBe(epoch + 32); expect(player.error).toBeNull();
|
|
} finally { player.close(); }
|
|
});
|
|
|
|
test("unknown iframe buffering applies explicit updates without inventing periodic safe ranges", async () => {
|
|
vi.useFakeTimers();
|
|
const state: WatchMediaState = { position: 0, paused: true, rate: 1, duration: 120, readyState: 3, ended: false, buffered: null };
|
|
const patch = vi.fn(async (value: object) => { Object.assign(state, value); });
|
|
const media: WatchMedia = { read: async () => ({ ...state }), patch, ready: async () => {}, listen: () => () => {}, close() {}, seekTolerance: 2, playbackRates: () => [1, 1.5] };
|
|
const player = new WatchPlayer("room", false, media, vi.fn());
|
|
try {
|
|
player.calibrate(5000, performance.now()); player.update({ timeline: "media" as const, position: 60, paused: true, rate: 1, anchorTime: 5000 }); await player.ready();
|
|
expect(state.position).toBe(60); patch.mockClear(); state.position = 20;
|
|
await vi.advanceTimersByTimeAsync(3000);
|
|
expect(patch.mock.calls.every(([value]) => !("position" in value))).toBe(true);
|
|
player.update({ timeline: "media" as const, position: 10, paused: true, rate: 1.25, anchorTime: 8000 });
|
|
await player.serialize(async () => {});
|
|
expect(player.error).toBe("watch_media_unsupported");
|
|
expect(patch.mock.calls.every(([value]) => !("rate" in value))).toBe(true);
|
|
} finally { player.close(); }
|
|
});
|
|
|
|
test("a player without seek events publishes an observed paused scrub on the existing tick", async () => {
|
|
vi.useFakeTimers();
|
|
const state: WatchMediaState = { position: 10, paused: true, rate: 1, duration: 120, readyState: 2, ended: false, buffered: null };
|
|
const media: WatchMedia = { read: async () => ({ ...state }), patch: async () => {}, ready: async () => {}, listen: () => () => {}, close() {}, seekEvents: false, seekTolerance: 2 };
|
|
const player = new WatchPlayer("room", true, media, vi.fn()), publish = vi.fn(async () => {});
|
|
try {
|
|
player.calibrate(5000, performance.now()); await player.ready(); player.activate(publish);
|
|
state.position = 40; await vi.advanceTimersByTimeAsync(1000);
|
|
expect(publish).toHaveBeenCalledOnce();
|
|
expect(publish).toHaveBeenCalledWith(expect.objectContaining({ position: 40, paused: true }));
|
|
} finally { player.close(); }
|
|
});
|
|
|
|
test("an owner can publish its first ready sample and measures later movement from the previous rate", async () => {
|
|
vi.useFakeTimers();
|
|
const state: WatchMediaState = { position: 0, paused: false, rate: 2, duration: 60, readyState: 1, ended: false, buffered: null };
|
|
const player = new WatchPlayer("room", true, { read: async () => ({ ...state }), patch: vi.fn(), ready: vi.fn(),
|
|
listen: () => () => {}, close() {}, seekEvents: false }, vi.fn());
|
|
const publish = vi.fn();
|
|
try {
|
|
player.calibrate(0, 0, 0); await player.ready(); player.activate(publish);
|
|
state.position = 10; state.readyState = 3;
|
|
await vi.advanceTimersByTimeAsync(1000);
|
|
expect(publish).toHaveBeenCalledExactlyOnceWith(expect.objectContaining({ position: 10 }));
|
|
state.position = 12.25; state.rate = 1;
|
|
await vi.advanceTimersByTimeAsync(1000);
|
|
expect(publish).toHaveBeenCalledOnce();
|
|
state.position = 13.75;
|
|
await vi.advanceTimersByTimeAsync(1000);
|
|
expect(publish).toHaveBeenCalledTimes(2);
|
|
} finally { player.close(); }
|
|
});
|
|
|
|
test("an embedded viewer resumes at the current host anchor after a hidden interval", async () => {
|
|
vi.useFakeTimers();let now=0;vi.spyOn(performance,'now').mockImplementation(()=>now);
|
|
const state:WatchMediaState={position:10,paused:false,rate:1,duration:120,readyState:3,buffered:null,ended:false};
|
|
let notify:(event:WatchMediaEvent)=>void=()=>{},visible=true;
|
|
const media:WatchMedia={read:async()=>({...state}),patch:async value=>{Object.assign(state,value);},ready:async()=>{},
|
|
listen:fn=>{notify=fn;return()=>{};},close(){},playbackAvailable:()=>visible};
|
|
const player=new WatchPlayer('room',false,media,vi.fn());
|
|
try{
|
|
player.calibrate(5000,0,0);player.update({ timeline: "media" as const,position:10,paused:false,rate:1,anchorTime:5000});await player.ready();
|
|
visible=false;state.paused=true;notify('blocked');now=10000;await vi.advanceTimersByTimeAsync(1000);
|
|
visible=true;await vi.advanceTimersByTimeAsync(1000);expect(state.position).toBe(10);expect(state.paused).toBe(true);
|
|
await player.resume();expect(state.position).toBe(20);expect(state.paused).toBe(false);
|
|
}finally{player.close();}
|
|
});
|
|
|
|
test("a start waits until the media may play, plays once, and a closed player starts nothing", async () => {
|
|
const state: WatchMediaState = { position: 0, paused: true, rate: 1, duration: 120, readyState: 2, buffered: null, ended: false };
|
|
const make = (available: boolean | undefined) => {
|
|
const listeners = new Set<(event: WatchMediaEvent) => void>();
|
|
let may = available;
|
|
const patch = vi.fn(async (value: object) => { Object.assign(state, value); });
|
|
const media: WatchMedia = { read: async () => ({ ...state }), patch, ready: async () => {}, close() {},
|
|
listen: fn => { listeners.add(fn); return () => { listeners.delete(fn); }; },
|
|
...(available === undefined ? {} : { playbackAvailable: () => may! }) };
|
|
return { patch, player: new WatchPlayer("room", true, media, vi.fn()),
|
|
resume: () => { may = true; for (const listener of [...listeners]) listener("visibility-resumed"); } };
|
|
};
|
|
// A file has no such condition: it starts at once.
|
|
const file = make(undefined);
|
|
file.player.start();
|
|
await vi.waitFor(() => expect(file.patch).toHaveBeenCalledWith({ paused: false }));
|
|
file.player.close();
|
|
// An embed that may not play yet starts when it may, once.
|
|
const embed = make(false);
|
|
embed.player.start();
|
|
await Promise.resolve();
|
|
expect(embed.patch).not.toHaveBeenCalled();
|
|
embed.resume();
|
|
await vi.waitFor(() => expect(embed.patch).toHaveBeenCalledWith({ paused: false }));
|
|
embed.resume();
|
|
await Promise.resolve();
|
|
expect(embed.patch.mock.calls.filter(([value]) => (value as { paused?: boolean }).paused === false)).toHaveLength(1);
|
|
embed.player.close();
|
|
// Closed before it may: nothing is started.
|
|
const closed = make(false);
|
|
closed.player.start();
|
|
closed.player.close();
|
|
closed.resume();
|
|
await Promise.resolve();
|
|
expect(closed.patch).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("a local iframe scrub offers explicit resynchronization without periodic seek fights", async () => {
|
|
vi.useFakeTimers();let now=0;vi.spyOn(performance,'now').mockImplementation(()=>now);
|
|
const state:WatchMediaState={position:10,paused:true,rate:1,duration:120,readyState:2,buffered:null,ended:false};
|
|
const media:WatchMedia={read:async()=>({...state}),patch:async value=>{Object.assign(state,value);},ready:async()=>{},listen:()=>()=>{},close(){},seekEvents:false,seekTolerance:2};
|
|
const player=new WatchPlayer('room',false,media,vi.fn());
|
|
try{
|
|
player.calibrate(5000,0,0);player.update({ timeline: "media" as const,position:10,paused:true,rate:1,anchorTime:5000});await player.ready();
|
|
state.position=60;now=1000;await vi.advanceTimersByTimeAsync(1000);
|
|
expect(player.blocked).toBe(true);expect(state.position).toBe(60);
|
|
await player.resume();
|
|
expect(state.position).toBe(10);
|
|
expect(player.blocked).toBe(false);
|
|
player.update({ timeline: "media", position: 10, paused: false, rate: 1, anchorTime: 6000 });
|
|
await player.serialize(async () => {});
|
|
expect(state.paused).toBe(false);
|
|
player.update({ timeline: "media", position: 10, paused: true, rate: 1, anchorTime: 6000 });
|
|
await player.serialize(async () => {});
|
|
state.position = 12; now += 1000;
|
|
await vi.advanceTimersByTimeAsync(1000);
|
|
expect(player.blockedReason).toBeNull();
|
|
state.position = 13; now += 1000;
|
|
await vi.advanceTimersByTimeAsync(1000);
|
|
expect(player.blockedReason).toBe("local-seek");
|
|
}finally{player.close();}
|
|
});
|
|
|
|
test("late metadata and manual autoplay retry use the latest domain anchor; sound remains local", async () => {
|
|
vi.useFakeTimers();
|
|
let now = 100;
|
|
vi.spyOn(performance, "now").mockImplementation(() => now);
|
|
const video = new Video();
|
|
vi.stubGlobal("document", { createElement: () => video });
|
|
const player = new WatchPlayer("room", false, new HTMLVideoMedia("https://media.test/video.mp4", "file"), vi.fn());
|
|
try {
|
|
player.calibrate(5100, 80, 120);
|
|
player.update({ timeline: "media" as const, position: 10, anchorTime: 5100, paused: false, rate: 2 });
|
|
now = 2100; video.readyState = 2; video.denied = true;
|
|
await player.ready();
|
|
video.dispatchEvent(new Event("loadedmetadata"));
|
|
await player.serialize(async () => {});
|
|
expect(video.currentTime).toBe(14);
|
|
expect(player.blocked).toBe(true);
|
|
video.volume = 0.2; now = 4100; video.denied = false;
|
|
await player.resume();
|
|
expect(video.currentTime).toBe(18);
|
|
expect(video.volume).toBe(0.2);
|
|
expect(player.blocked).toBe(false);
|
|
player.update({ timeline: "media" as const, position: 3, anchorTime: 9100, paused: true, rate: 0.5 });
|
|
await player.resume();
|
|
expect(video.currentTime).toBe(3);
|
|
expect(video.paused).toBe(true);
|
|
video.currentTime = 3.2;
|
|
player.update({ timeline: "media" as const, position: 3, anchorTime: 9100, paused: true, rate: 0.5 });
|
|
expect(video.currentTime).toBe(3.2);
|
|
player.update({ timeline: "media", position: 3, anchorTime: 10100, paused: false, rate: 2 });
|
|
await player.serialize(async () => {});
|
|
expect(video.currentTime).toBe(3);
|
|
} finally { player.close(); }
|
|
expect(video.src).toBe("");
|
|
expect(vi.getTimerCount()).toBe(0);
|
|
});
|
|
|
|
test("admitted creator native playback events are serialized and use integer domain milliseconds", async () => {
|
|
vi.useFakeTimers();
|
|
vi.spyOn(performance, "now").mockReturnValue(100.75);
|
|
const video = new Video(); video.readyState = 2;
|
|
vi.stubGlobal("document", { createElement: () => video });
|
|
const player = new WatchPlayer("room", true, new HTMLVideoMedia("https://media.test/video.mp4", "file"), vi.fn());
|
|
const send = vi.fn(async (_playback: unknown) => {});
|
|
try {
|
|
video.dispatchEvent(new Event("pause"));
|
|
player.calibrate(5000, 80.25, 100.75);
|
|
player.activate(send);
|
|
video.currentTime = 20;
|
|
video.dispatchEvent(new Event("seeked"));
|
|
video.dispatchEvent(new Event("pause"));
|
|
await player.serialize(async () => {});
|
|
expect(send).toHaveBeenCalledTimes(1);
|
|
expect(send).toHaveBeenCalledWith({ timeline: "media" as const, position: 20, paused: true, rate: 1, anchorTime: 5010 });
|
|
video.paused = false;
|
|
video.dispatchEvent(new Event("play"));
|
|
await player.serialize(async () => {});
|
|
expect(send).toHaveBeenCalledTimes(2);
|
|
video.paused = true; video.currentTime = 120;
|
|
video.dispatchEvent(new Event("ended"));
|
|
await player.serialize(async () => {});
|
|
expect(send).toHaveBeenLastCalledWith({ timeline: "media" as const, position: 120, paused: true, rate: 1, anchorTime: 5010 });
|
|
} finally { player.close(); }
|
|
});
|
|
|
|
test.each([false, true])("a paused creator retries a rejected update (forced retry: %s)", async force => {
|
|
const video = new Video(); video.readyState = 2; video.currentTime = 25; video.playbackRate = 1.5;
|
|
vi.stubGlobal("document", { createElement: () => video });
|
|
const player = new WatchPlayer("room", true, new HTMLVideoMedia("https://media.test/video.mp4", "file"), vi.fn());
|
|
const send = vi.fn(async () => {}).mockRejectedValueOnce(new Error("offline"));
|
|
try {
|
|
player.calibrate(5000, 0, 0);
|
|
player.activate(send);
|
|
video.dispatchEvent(new Event("pause"));
|
|
await player.serialize(async () => {});
|
|
expect(player.error).toBe("watch_update_failed");
|
|
await Promise.all([player.publish(force), player.publish()]);
|
|
expect(send).toHaveBeenCalledTimes(2);
|
|
expect(send).toHaveBeenLastCalledWith(expect.objectContaining({ paused: true, position: 25, rate: 1.5 }));
|
|
expect(player.error).toBeNull();
|
|
} finally { player.close(); }
|
|
});
|
|
|
|
test("a rejected creator update and its retry preserve an intervening media failure", async () => {
|
|
const video = new Video(); video.readyState = 2;
|
|
vi.stubGlobal("document", { createElement: () => video });
|
|
const player = new WatchPlayer("room", true, new HTMLVideoMedia("https://media.test/video.mp4", "file"), vi.fn());
|
|
let rejectUpdate!: (error: Error) => void;
|
|
const update = new Promise<void>((_resolve, reject) => { rejectUpdate = reject; });
|
|
const send = vi.fn(async () => {}).mockReturnValueOnce(update);
|
|
try {
|
|
player.calibrate(5000, 0, 0);
|
|
player.activate(send);
|
|
video.dispatchEvent(new Event("pause"));
|
|
await vi.waitFor(() => expect(send).toHaveBeenCalledOnce());
|
|
video.dispatchEvent(new Event("error"));
|
|
await vi.waitFor(() => expect(player.error).toBe("watch_media_failed"));
|
|
rejectUpdate(new Error("offline"));
|
|
await player.serialize(async () => {});
|
|
expect(player.error).toBe("watch_media_failed");
|
|
await player.publish(true);
|
|
expect(send).toHaveBeenCalledTimes(2);
|
|
expect(player.error).toBe("watch_media_failed");
|
|
} finally { player.close(); }
|
|
});
|
|
|
|
test("automatic clock publication keeps sampling failures on the media path", async () => {
|
|
const video = new Video(); video.readyState = 2;
|
|
vi.stubGlobal("document", { createElement: () => video });
|
|
const media = new HTMLVideoMedia("https://media.test/video.mp4", "file");
|
|
const player = new WatchPlayer("room", true, media, vi.fn());
|
|
const send = vi.fn(async () => {});
|
|
try {
|
|
player.calibrate(5000, 0, 0);
|
|
player.activate(send);
|
|
vi.spyOn(media, "read").mockRejectedValueOnce(new Error("watch_media_access_denied"));
|
|
video.dispatchEvent(new Event("pause"));
|
|
await player.serialize(async () => {});
|
|
expect(send).not.toHaveBeenCalled();
|
|
expect(player.error).toBe("watch_media_access_denied");
|
|
} finally { player.close(); }
|
|
});
|
|
|
|
test("creator acknowledgements never seek; retiring suppresses queued sends", async () => {
|
|
vi.useFakeTimers();
|
|
const video = new Video(); video.readyState = 2; video.currentTime = 25;
|
|
vi.stubGlobal("document", { createElement: () => video });
|
|
const player = new WatchPlayer("room", true, new HTMLVideoMedia("https://media.test/video.mp4", "file"), vi.fn());
|
|
player.calibrate(5000, 0, 0);
|
|
player.update({ timeline: "media" as const, position: 2, anchorTime: 5000, paused: true, rate: 1 });
|
|
expect(video.currentTime).toBe(25);
|
|
const send = vi.fn(async () => {});
|
|
const pending = player.serialize(send);
|
|
player.close(); await pending;
|
|
expect(send).not.toHaveBeenCalled();
|
|
expect(vi.getTimerCount()).toBe(0);
|
|
});
|
|
|
|
test("candidate cancellation releases its load listeners and deadline", async () => {
|
|
vi.useFakeTimers();
|
|
const video = new Video();
|
|
vi.stubGlobal("document", { createElement: () => video });
|
|
const player = new WatchPlayer("room", true, new HTMLVideoMedia("https://media.test/video.mp4", "file"), vi.fn());
|
|
const cancel = new AbortController();
|
|
const pending = player.ready(cancel.signal);
|
|
cancel.abort();
|
|
await expect(pending).rejects.toMatchObject({ name: "AbortError" });
|
|
player.close();
|
|
expect(vi.getTimerCount()).toBe(0);
|
|
});
|
|
|
|
test("creator playing reanchors initial start and buffer recovery even at the same media position", async () => {
|
|
vi.useFakeTimers();
|
|
const clock = vi.spyOn(performance, "now").mockReturnValue(100);
|
|
const video = new Video(); video.readyState = 2; video.currentTime = 20; video.paused = false;
|
|
vi.stubGlobal("document", { createElement: () => video });
|
|
const player = new WatchPlayer("room", true, new HTMLVideoMedia("https://media.test/video.mp4", "file"), vi.fn());
|
|
const send = vi.fn(async (_playback: unknown) => {});
|
|
try {
|
|
player.calibrate(5000, 100, 100);
|
|
player.activate(send);
|
|
video.dispatchEvent(new Event("play"));
|
|
await player.serialize(async () => {});
|
|
clock.mockReturnValue(1100);
|
|
video.dispatchEvent(new Event("playing"));
|
|
await player.serialize(async () => {});
|
|
expect(send).toHaveBeenLastCalledWith({ timeline: "media" as const, position: 20, paused: false, rate: 1, anchorTime: 6000 });
|
|
video.dispatchEvent(new Event("waiting"));
|
|
video.dispatchEvent(new Event("waiting"));
|
|
await player.serialize(async () => {});
|
|
expect(send).toHaveBeenCalledTimes(3);
|
|
expect(send).toHaveBeenLastCalledWith(expect.objectContaining({ position: 20, paused: true }));
|
|
expect(video.paused).toBe(false);
|
|
clock.mockReturnValue(3100);
|
|
video.dispatchEvent(new Event("playing"));
|
|
await player.serialize(async () => {});
|
|
expect(send).toHaveBeenCalledTimes(4);
|
|
expect(send).toHaveBeenLastCalledWith({ timeline: "media" as const, position: 20, paused: false, rate: 1, anchorTime: 8000 });
|
|
video.dispatchEvent(new Event("waiting"));
|
|
await player.serialize(async () => {});
|
|
video.pause();
|
|
video.dispatchEvent(new Event("pause"));
|
|
video.dispatchEvent(new Event("playing"));
|
|
await player.serialize(async () => {});
|
|
expect(send).toHaveBeenLastCalledWith(expect.objectContaining({ paused: true }));
|
|
} finally { player.close(); }
|
|
});
|
|
|
|
test.each([
|
|
{ event: "waiting", recovery: "playing", paused: false, readyState: 2 },
|
|
{ event: "emptied", recovery: "loadeddata", paused: true, readyState: 0 },
|
|
] as const)("the original-page viewer refreshes readiness on $event and recovery before the next clock tick", async ({ event, recovery, paused, readyState: unavailable }) => {
|
|
vi.useFakeTimers();
|
|
const candidateId = crypto.randomUUID();
|
|
let readyState = 4;
|
|
const state = () => ({ position: 0, paused, rate: 1, duration: 60, readyState, ended: false, buffered: [] });
|
|
const page = new AppPage(crypto.randomUUID(), "https://video.test/", async () => state(), () => {}, () => {});
|
|
const media = new AppPageMedia(page, candidateId);
|
|
const player = new WatchPlayer("room", false, media, vi.fn());
|
|
try {
|
|
await player.ready();
|
|
const now = performance.now();
|
|
player.calibrate(now, now, now);
|
|
player.update({ timeline: "media", position: 0, paused, rate: 1, anchorTime: now });
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
expect(player.waitingForPicture).toBe(false);
|
|
readyState = unavailable;
|
|
const sample = (event: "waiting" | "emptied" | "playing" | "loadeddata", id = candidateId) =>
|
|
page.receive({ type: "page_sample", candidateId: id, event, pageId: page.id });
|
|
sample(event, crypto.randomUUID());
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
expect(player.waitingForPicture).toBe(false);
|
|
sample(event);
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
expect(player.waitingForPicture).toBe(true);
|
|
readyState = 3;
|
|
sample(recovery);
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
expect(player.waitingForPicture).toBe(false);
|
|
} finally { player.close(); }
|
|
});
|
|
|
|
test('App player samples asynchronous reads in the local monotonic domain and retires a pending open', async () => {
|
|
vi.useFakeTimers();
|
|
let now = 100;
|
|
vi.spyOn(performance, 'now').mockImplementation(() => now);
|
|
let complete!: (value: { position: number; paused: boolean; rate: number; duration: number; readyState: number; ended: boolean; buffered: [number, number][] }) => void;
|
|
const port: AppPagePort = { read: () => new Promise(resolve => { complete = resolve; }), patch: vi.fn(async () => {}),
|
|
listen: () => () => {}, focus: vi.fn(async () => {}), close: vi.fn() };
|
|
const media = new AppPageMedia(port);
|
|
const player = new WatchPlayer('room', true, media, vi.fn());
|
|
try {
|
|
player.calibrate(5000, 100, 100);
|
|
await media.opening;
|
|
const sampling = player.sample();
|
|
await Promise.resolve();
|
|
now = 300;
|
|
complete({ position: 12, paused: false, rate: 1, duration: 60, readyState: 4, ended: false, buffered: [[0, 60]] as [number, number][] });
|
|
await expect(sampling).resolves.toMatchObject({ position: 12, anchorTime: 5100 });
|
|
} finally { player.close(); }
|
|
expect(port.close).toHaveBeenCalledTimes(1);
|
|
let opened!: (value: AppPagePort) => void;
|
|
const pendingMedia = new AppPageMedia(() => new Promise(resolve => { opened = resolve; }));
|
|
const pendingPlayer = new WatchPlayer('room', false, pendingMedia, vi.fn());
|
|
await Promise.resolve();
|
|
pendingPlayer.close();
|
|
opened(port);
|
|
await expect(pendingMedia.opening).rejects.toMatchObject({ name: 'AbortError' });
|
|
expect(port.close).toHaveBeenCalledTimes(2);
|
|
expect(vi.getTimerCount()).toBe(0);
|
|
});
|
|
|
|
test('native owner progress reuses the observation tick without publishing playback', async () => {
|
|
vi.useFakeTimers();
|
|
let position = 12, paused = true, bufferEnd = 60;
|
|
const port: AppPagePort = { read: async () => ({ position, paused, rate: 1, duration: 60, readyState: 4, ended: false, buffered: [[0, bufferEnd]] as [number, number][] }),
|
|
patch: vi.fn(async () => {}), listen: () => () => {}, focus: vi.fn(async () => {}), close: vi.fn() };
|
|
const changed = vi.fn();
|
|
const player = new WatchPlayer('room', true, new AppPageMedia(port), changed);
|
|
const mediaChanged = vi.fn();
|
|
const unsubscribe = player.subscribeMedia(mediaChanged);
|
|
const send = vi.fn(async () => {});
|
|
try {
|
|
await player.ready(); player.calibrate(5000, 100, 100); player.activate(send);
|
|
await player.serialize(async () => {});
|
|
expect(player.mediaState?.position).toBe(12);
|
|
changed.mockClear();
|
|
mediaChanged.mockClear();
|
|
await vi.advanceTimersByTimeAsync(2000);
|
|
expect(changed).not.toHaveBeenCalled();
|
|
position = 13; paused = false;
|
|
await vi.advanceTimersByTimeAsync(1000);
|
|
expect(player.mediaState?.position).toBe(13);
|
|
expect(changed).toHaveBeenCalledTimes(1);
|
|
expect(mediaChanged).toHaveBeenCalledTimes(1);
|
|
changed.mockClear();
|
|
bufferEnd = 70;
|
|
await vi.advanceTimersByTimeAsync(1000);
|
|
expect(mediaChanged).toHaveBeenCalledTimes(2);
|
|
expect(changed).not.toHaveBeenCalled();
|
|
unsubscribe();
|
|
position = 14;
|
|
await vi.advanceTimersByTimeAsync(1000);
|
|
expect(mediaChanged).toHaveBeenCalledTimes(2);
|
|
expect(changed).not.toHaveBeenCalled();
|
|
expect(send).not.toHaveBeenCalled();
|
|
} finally { player.close(); }
|
|
});
|
|
|
|
test.each([1, 2])("buffering at readyState %s suspends periodic seeks while explicit controls and recovery still work", async readyState => {
|
|
vi.useFakeTimers();
|
|
let now = 100;
|
|
vi.spyOn(performance, "now").mockImplementation(() => now);
|
|
const video = new Video(); video.readyState = 2;
|
|
vi.stubGlobal("document", { createElement: () => video });
|
|
const player = new WatchPlayer("room", false, new HTMLVideoMedia("https://media.test/video.mp4", "file"), vi.fn());
|
|
try {
|
|
player.calibrate(5100, 100, 100);
|
|
player.update({ timeline: "media" as const, position: 10, paused: false, rate: 1, anchorTime: 5100 });
|
|
await player.ready();
|
|
expect(video.currentTime).toBe(10);
|
|
video.readyState = readyState;
|
|
for (let tick = 0; tick < 3; tick++) {
|
|
now += 1000;
|
|
await vi.advanceTimersByTimeAsync(1000);
|
|
expect(video.currentTime).toBe(10);
|
|
}
|
|
player.update({ timeline: "media" as const, position: 30, paused: true, rate: 0.5, anchorTime: now + 5000 });
|
|
await player.serialize(async () => {});
|
|
expect(video.currentTime).toBe(30);
|
|
expect(video.paused).toBe(true);
|
|
expect(video.playbackRate).toBe(0.5);
|
|
video.currentTime = 29; video.paused = false; video.playbackRate = 1;
|
|
now += 1000;
|
|
await vi.advanceTimersByTimeAsync(1000);
|
|
expect(video.currentTime).toBe(29);
|
|
expect(video.paused).toBe(true);
|
|
expect(video.playbackRate).toBe(0.5);
|
|
video.readyState = 3;
|
|
now += 1000;
|
|
await vi.advanceTimersByTimeAsync(1000);
|
|
expect(video.currentTime).toBe(30);
|
|
} finally { player.close(); }
|
|
expect(vi.getTimerCount()).toBe(0);
|
|
});
|
|
|
|
test("periodic correction waits for its target to be buffered; explicit host seeking can leave that range", async () => {
|
|
vi.useFakeTimers();
|
|
let now = 100;
|
|
vi.spyOn(performance, "now").mockImplementation(() => now);
|
|
const video = new Video(); video.readyState = 4; video.ranges = [[0, 11]];
|
|
vi.stubGlobal("document", { createElement: () => video });
|
|
const player = new WatchPlayer("room", false, new HTMLVideoMedia("https://media.test/video.mp4", "file"), vi.fn());
|
|
try {
|
|
player.calibrate(5100, 100, 100);
|
|
player.update({ timeline: "media" as const, position: 10, paused: false, rate: 1, anchorTime: 5100 });
|
|
await player.ready();
|
|
for (let tick = 0; tick < 3; tick++) {
|
|
now += 1000; await vi.advanceTimersByTimeAsync(1000);
|
|
expect(video.currentTime).toBe(10);
|
|
}
|
|
expect(player.mediaState?.buffered).toEqual([[0, 11]]);
|
|
video.ranges = [[0, 11], [13, 15]];
|
|
now += 1000; await vi.advanceTimersByTimeAsync(1000);
|
|
expect(video.currentTime).toBe(14);
|
|
player.update({ timeline: "media" as const, position: 90, paused: true, rate: 0.5, anchorTime: now + 5000 });
|
|
await player.serialize(async () => {});
|
|
expect(video.currentTime).toBe(90);
|
|
expect(video.paused).toBe(true);
|
|
expect(video.playbackRate).toBe(0.5);
|
|
} finally { player.close(); }
|
|
});
|
|
|
|
test("media parse errors survive readiness and late error events, while retirement ignores late failures", async () => {
|
|
vi.useFakeTimers();
|
|
const video = new Video();
|
|
vi.stubGlobal("document", { createElement: () => video });
|
|
const media = new HTMLVideoMedia(() => Promise.reject(new Error("watch_mp4_parse_failed")), "file");
|
|
const player = new WatchPlayer("room", false, media, vi.fn());
|
|
await expect(player.ready()).rejects.toThrow("watch_mp4_parse_failed");
|
|
expect(player.error).toBe("watch_mp4_parse_failed");
|
|
const changed = vi.fn();
|
|
let emit!: (event: WatchMediaEvent) => void;
|
|
const state = { position: 0, paused: true, rate: 1, duration: 20, readyState: 4, ended: false, buffered: [[0, 20]] as [number, number][] };
|
|
const port = {
|
|
read: vi.fn(async () => state), patch: vi.fn(async () => {}), focus: vi.fn(async () => {}),
|
|
listen: (listener: (event: WatchMediaEvent) => void) => { emit = listener; return () => {}; }, close: vi.fn(),
|
|
};
|
|
const latePlayer = new WatchPlayer("room", false, new AppPageMedia(port), changed);
|
|
await latePlayer.ready();
|
|
emit("error");
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
expect(latePlayer.error).toBe("watch_media_failed");
|
|
port.read.mockRejectedValueOnce(new Error("watch_mp4_parse_failed"));
|
|
emit("error");
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
expect(latePlayer.error).toBe("watch_mp4_parse_failed");
|
|
let rejectRead!: (error: Error) => void;
|
|
port.read.mockImplementationOnce(() => new Promise((_resolve, reject) => { rejectRead = reject; }));
|
|
emit("error");
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
latePlayer.close();
|
|
changed.mockClear();
|
|
rejectRead(new Error("watch_source_failed"));
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
expect(changed).not.toHaveBeenCalled();
|
|
expect(latePlayer.error).toBe("watch_mp4_parse_failed");
|
|
player.close();
|
|
});
|
|
|
|
test("a supported viewer rate restores alignment while a player failure survives the next anchor", async () => {
|
|
vi.useFakeTimers();
|
|
const state = { position: 2, paused: false, rate: 1, duration: 20, readyState: 4, ended: false, buffered: [] };
|
|
const read = vi.fn(async () => ({ ...state })), patch = vi.fn(async value => { Object.assign(state, value); });
|
|
let emit!: (event: WatchMediaEvent) => void;
|
|
const player = new WatchPlayer("room", false, { read, patch, playbackRates: () => [1],
|
|
ready: vi.fn(), listen: listener => { emit = listener; return () => {}; }, close: vi.fn() }, vi.fn());
|
|
const anchor = async (rate: number, position = 0) => {
|
|
player.update({ timeline: "media", position, paused: true, rate, anchorTime: 0 });
|
|
await player.serialize(async () => {});
|
|
};
|
|
try {
|
|
player.calibrate(0, 0, 0);
|
|
await anchor(1.5, 20);
|
|
expect(player.error).toBe("watch_media_unsupported");
|
|
expect(patch).toHaveBeenCalledExactlyOnceWith({ position: 20, paused: true });
|
|
await anchor(1, 4);
|
|
expect(player.error).toBeNull();
|
|
expect(patch).toHaveBeenCalledWith({ position: 4 });
|
|
await anchor(1.5);
|
|
read.mockRejectedValueOnce(new Error("watch_media_unsupported"));
|
|
emit("error");
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
await anchor(1);
|
|
expect(player.error).toBe("watch_media_unsupported");
|
|
} finally { player.close(); }
|
|
});
|
|
|
|
test("a paused aligned viewer observes three idle ticks without writes or UI notifications", async () => {
|
|
vi.useFakeTimers();
|
|
const state = { position: 12, rate: 1, paused: true, duration: 60, ended: false, readyState: 4, buffered: [] };
|
|
const patch = vi.fn(), changed = vi.fn(), read = vi.fn(async () => state);
|
|
const player = new WatchPlayer("room", false, { read, patch, ready: vi.fn(), listen: () => () => {}, close: vi.fn() }, changed);
|
|
try {
|
|
player.calibrate(0, 0, 0);
|
|
player.update({ timeline: "media", position: 12, rate: 1, paused: true, anchorTime: 0 });
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
player.update({ timeline: "media", position: 12.1, rate: 1, paused: true, anchorTime: 1 });
|
|
await player.serialize(async () => {});
|
|
expect(patch).toHaveBeenCalledExactlyOnceWith({ position: 12.1 });
|
|
state.position = 12.1;
|
|
patch.mockClear(); changed.mockClear(); read.mockClear();
|
|
await vi.advanceTimersByTimeAsync(3000);
|
|
expect([read.mock.calls.length, patch.mock.calls.length, changed.mock.calls.length]).toEqual([3, 0, 0]);
|
|
} finally { player.close(); }
|
|
});
|
|
|
|
test("a persistent decoder failure publishes one room change until the player retires", async () => {
|
|
vi.useFakeTimers();
|
|
const video = new Video(), changed = vi.fn();
|
|
vi.stubGlobal("document", { createElement: () => video });
|
|
const player = new WatchPlayer("room", false, new HTMLVideoMedia("https://media.test/file.mp4", "file"), changed);
|
|
try {
|
|
video.readyState = 4;
|
|
player.calibrate(1000, 0, 0);
|
|
player.update({ timeline: "media", position: 0, paused: true, rate: 1, anchorTime: 1000 });
|
|
await player.ready();
|
|
changed.mockClear();
|
|
Object.assign(video, { error: { code: 3 } });
|
|
video.dispatchEvent(new Event("error"));
|
|
await vi.advanceTimersByTimeAsync(3000);
|
|
expect(player.error).toBe("watch_media_decode_failed");
|
|
expect(changed).toHaveBeenCalledTimes(1);
|
|
} finally { player.close(); }
|
|
expect(vi.getTimerCount()).toBe(0);
|
|
});
|
|
|
|
test("HTML viewer drops the blocked play when the host pauses and retries once on a new play", async () => {
|
|
vi.useFakeTimers();
|
|
const video = new Video();
|
|
video.readyState = 2; video.denied = true;
|
|
vi.stubGlobal("document", { createElement: () => video });
|
|
const player = new WatchPlayer("room", false, new HTMLVideoMedia("https://media.test/video.mp4", "file"), vi.fn());
|
|
const anchor = { timeline: "media" as const, position: 0, anchorTime: 0, rate: 1, paused: false };
|
|
try {
|
|
player.calibrate(0, 0, 0);
|
|
player.update(anchor);
|
|
await player.ready();
|
|
expect(player.blocked).toBe(true);
|
|
player.update({ ...anchor, paused: true });
|
|
expect(player.blocked).toBe(false);
|
|
video.dispatchEvent(new Event("blocked"));
|
|
expect(player.blocked).toBe(false);
|
|
await player.serialize(async () => {});
|
|
video.play.mockClear();
|
|
player.update(anchor);
|
|
await player.serialize(async () => {});
|
|
await vi.advanceTimersByTimeAsync(2000);
|
|
expect(video.play).toHaveBeenCalledOnce();
|
|
expect(player.blocked).toBe(true);
|
|
} finally { player.close(); }
|
|
});
|
|
|
|
test.each(["pause", "close", "play"])("waits for the selected output before first play with %s pending", async action => {
|
|
let complete: (() => void) | undefined;
|
|
const video = Object.assign(new Video(), {
|
|
setSinkId: () => complete ? Promise.resolve() : new Promise<void>(resolve => { complete = resolve; }),
|
|
});
|
|
vi.stubGlobal("document", { createElement: () => video });
|
|
let release!: () => void;
|
|
const blocker = playbackOutput.attach({ setSinkId: () => new Promise<void>(resolve => { release = resolve; }) } as unknown as HTMLMediaElement);
|
|
const choosing = playbackOutput.choose("headphones");
|
|
await vi.waitFor(() => expect(release).toBeDefined());
|
|
const media = new HTMLVideoMedia("https://media.test/video.mp4", "file");
|
|
try {
|
|
await media.patch({ paused: false });
|
|
expect(video.play).not.toHaveBeenCalled();
|
|
if (action === "pause") await media.patch({ paused: true });
|
|
release();
|
|
await vi.waitFor(() => expect(complete).toBeDefined());
|
|
if (action === "close") media.close();
|
|
complete!();
|
|
await new Promise(resolve => setTimeout(resolve, 0));
|
|
expect(video.play).toHaveBeenCalledTimes(action === "play" ? 1 : 0);
|
|
} finally { blocker(); release(); complete?.(); media.close(); await choosing; await playbackOutput.choose(""); }
|
|
});
|
|
|
|
test.each([["paused", 10], ["ended", 10], ["waiting", 10], ["playing", 12]] as const)(
|
|
"source loss freezes %s playback within its duration", async (mode, position) => {
|
|
const clock = vi.spyOn(performance, "now").mockReturnValue(0);
|
|
const video = Object.assign(new Video(), { currentTime: 10, duration: 12, readyState: 4, paused: mode === "paused", ended: mode === "ended" });
|
|
vi.stubGlobal("document", { createElement: () => video });
|
|
const player = new WatchPlayer("room", true, new HTMLVideoMedia("https://media.test/video.mp4", "file"), vi.fn());
|
|
try {
|
|
await player.ready();
|
|
if (mode === "waiting") video.dispatchEvent(new Event("waiting"));
|
|
clock.mockReturnValue(5000);
|
|
player.sourceUnavailable();
|
|
expect(player.mediaState).toMatchObject({ position, paused: true });
|
|
} finally { player.close(); }
|
|
});
|
|
|
|
test.each(['resolve', 'reject'] as const)('source retirement freezes the owned clock across a pending page read (%s)', async completion => {
|
|
vi.useFakeTimers();
|
|
let now = 0, changed: (event: WatchMediaEvent) => void = () => {};
|
|
vi.spyOn(performance, 'now').mockImplementation(() => now);
|
|
const state: WatchMediaState = { position: 10, paused: false, rate: 2, duration: 120, readyState: 4, ended: false, buffered: [[0, 120]] };
|
|
const read = vi.fn(async () => ({ ...state })), close = vi.fn();
|
|
const media: WatchMedia = { read, patch: vi.fn(), ready: async () => {}, close, listen: listener => { changed = listener; return () => {}; } };
|
|
const player = new WatchPlayer('room', true, media, vi.fn()), publish = vi.fn(async (_playback: unknown) => {});
|
|
try {
|
|
player.calibrate(5000, 0, 0); await player.ready(); player.activate(publish);
|
|
await player.publish(true); publish.mockClear();
|
|
let resolve!: (state: WatchMediaState) => void, reject!: (error: Error) => void;
|
|
read.mockImplementationOnce(() => new Promise((yes, no) => { resolve = yes; reject = no; }));
|
|
changed('playing');
|
|
await vi.advanceTimersByTimeAsync(0);
|
|
now = 2000; changed('source-unavailable');
|
|
if (completion === 'resolve') resolve({ ...state, position: 80 });
|
|
else reject(new Error('page_player_closed'));
|
|
await player.serialize(async () => {});
|
|
expect(publish.mock.lastCall?.[0]).toEqual({ timeline: 'media', position: 14, paused: true, rate: 2, anchorTime: 7000 });
|
|
expect(player.mediaState).toMatchObject({ position: 14, paused: true, readyState: 0 });
|
|
expect(player.error).toBe('watch_source_unavailable');
|
|
const reads = read.mock.calls.length;
|
|
now = 8000; await vi.advanceTimersByTimeAsync(6000);
|
|
await player.publish(true);
|
|
expect(read).toHaveBeenCalledTimes(reads);
|
|
expect(publish.mock.lastCall?.[0]).toMatchObject({ position: 14, paused: true, anchorTime: 13000 });
|
|
expect(close).not.toHaveBeenCalled();
|
|
} finally { player.close(); }
|
|
expect(close).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
test.each([10, 60])("delayed iframe scrubs converge when the sharer returns to %i", async finalPosition => {
|
|
vi.useFakeTimers();
|
|
const state: WatchMediaState = {
|
|
position: 10, paused: true, rate: 1, readyState: 2, duration: 120, ended: false, buffered: null,
|
|
};
|
|
const issued: number[] = [];
|
|
const media: WatchMedia = {
|
|
seekEvents: false, seekTolerance: 2,
|
|
read: async () => ({ ...state }),
|
|
patch: async ({ position, paused }) => {
|
|
if (position !== undefined) {
|
|
issued.push(position);
|
|
setTimeout(() => { state.position = position; }, issued.length * 1000);
|
|
}
|
|
if (paused !== undefined) state.paused = paused;
|
|
},
|
|
ready: async () => {}, listen: () => () => {}, close() {},
|
|
};
|
|
const player = new WatchPlayer("room", false, media, () => {});
|
|
const host = async (position: number, paused = true) => {
|
|
player.update({ timeline: "media", position, paused, rate: 1, anchorTime: performance.now() });
|
|
await player.serialize(async () => {});
|
|
};
|
|
try {
|
|
player.calibrate(0, 0, 0);
|
|
await host(10); await player.ready();
|
|
await host(60);
|
|
await vi.advanceTimersByTimeAsync(500);
|
|
await host(80);
|
|
await vi.advanceTimersByTimeAsync(400);
|
|
await host(finalPosition);
|
|
expect(issued).toEqual([60, 80, finalPosition]);
|
|
await vi.advanceTimersByTimeAsync(3100);
|
|
expect(player.mediaState?.position).toBe(finalPosition);
|
|
expect(player.blockedReason).toBeNull();
|
|
await host(finalPosition, false);
|
|
expect(state.paused).toBe(false);
|
|
} finally { player.close(); }
|
|
});
|
|
|
|
test("a new shared rate supersedes the rate still pending in the provider", async () => {
|
|
vi.useFakeTimers();
|
|
const state: WatchMediaState = {
|
|
position: 10, paused: true, rate: 1, readyState: 2, duration: 120, ended: false, buffered: null,
|
|
};
|
|
const rates: number[] = [];
|
|
const media: WatchMedia = {
|
|
read: async () => ({ ...state }), playbackRates: () => [1, 2],
|
|
patch: async ({ rate }) => {
|
|
if (rate === undefined) return;
|
|
rates.push(rate);
|
|
setTimeout(() => { state.rate = rate; }, 200);
|
|
},
|
|
ready: async () => {}, listen: () => () => {}, close() {},
|
|
};
|
|
const player = new WatchPlayer("room", false, media, () => {});
|
|
const host = async (rate: number) => {
|
|
player.update({ timeline: "media", position: 10, paused: true, rate, anchorTime: 0 });
|
|
await player.serialize(async () => {});
|
|
};
|
|
try {
|
|
player.calibrate(0, 0, 0);
|
|
await host(1); await player.ready();
|
|
await host(2); await host(1);
|
|
await vi.advanceTimersByTimeAsync(200);
|
|
expect(rates).toEqual([2, 1]);
|
|
expect(state.rate).toBe(1);
|
|
} finally { player.close(); }
|
|
});
|
|
|
|
test("a slower reply that agrees with the kept clock changes nothing, and one that contradicts it replaces it", async () => {
|
|
vi.useFakeTimers();
|
|
let now = 0;
|
|
vi.spyOn(performance, "now").mockImplementation(() => now);
|
|
const state: WatchMediaState = { position: 0, duration: 60, rate: 1, paused: true, readyState: 4, ended: false, buffered: null };
|
|
const media: WatchMedia = { read: async () => ({ ...state }), patch: vi.fn(), ready: async () => {}, listen: () => () => {}, close: vi.fn() };
|
|
const player = new WatchPlayer("room", true, media, vi.fn());
|
|
const anchor = async () => (await player.sample()).anchorTime - now;
|
|
try {
|
|
await player.ready();
|
|
// Twenty milliseconds there and back: the site's clock is 5,000 ahead, give or take ten.
|
|
player.calibrate(5010, 0, 20);
|
|
expect(await anchor()).toBe(5000);
|
|
// Four seconds there and back, stamped ten milliseconds in: by its midpoint the clock would be 1,990 off.
|
|
player.calibrate(5010, 0, 4000);
|
|
expect(await anchor()).toBe(5000);
|
|
// A reply as quick as the first that disagrees with it: the clocks moved, as after a sleep.
|
|
player.calibrate(9010, 0, 20);
|
|
expect(await anchor()).toBe(9000);
|
|
// A slow reply that cannot be reconciled with the kept one replaces it too.
|
|
player.calibrate(20_000, 0, 4000);
|
|
expect(await anchor()).toBe(18_000);
|
|
} finally { player.close(); }
|
|
});
|
|
|
|
test("original-page listening reads the selected element and never publishes a room edit", async () => {
|
|
const candidateId = crypto.randomUUID();
|
|
const state = { position: 9, paused: true, rate: 1, duration: 60, readyState: 4, ended: false, buffered: [], volume: 0, muted: true };
|
|
const request = vi.fn(async (command: { type: string; candidateId?: unknown; patch?: unknown }) => {
|
|
expect(command.candidateId).toBe(candidateId);
|
|
if (command.type === "page_read_element") return state;
|
|
expect(command.type).toBe("page_patch_candidate");
|
|
Object.assign(state, command.patch);
|
|
return null;
|
|
});
|
|
const page = new AppPage(crypto.randomUUID(), "https://video.test/", request, () => {}, () => {});
|
|
const player = new WatchPlayer("room", true, new AppPageMedia(page, candidateId), vi.fn());
|
|
const publish = vi.fn(async () => {});
|
|
try {
|
|
player.activate(publish);
|
|
await player.ready();
|
|
expect(player.localListening).toEqual({ volume: 0, muted: true });
|
|
expect(request.mock.calls.every(([command]) => command.type === "page_read_element")).toBe(true);
|
|
await player.setListening({ volume: 0.35, muted: false });
|
|
expect(request).toHaveBeenCalledWith(expect.objectContaining({ candidateId, patch: { volume: 0.35, muted: false } }), expect.anything());
|
|
state.volume = 0.6;
|
|
page.receive({ type: "page_sample", pageId: page.id, candidateId, event: "volumechange" });
|
|
await player.serialize(async () => {});
|
|
expect(player.localListening).toEqual({ volume: 0.6, muted: false });
|
|
expect(state).toMatchObject({ position: 9, rate: 1, paused: true });
|
|
expect(publish).not.toHaveBeenCalled();
|
|
} finally { player.close(); }
|
|
});
|