Files
piik/tests/watch-latest-alignment.test.ts
T
AAA 52cf6270e1 fix(watch): preview sources and resume stalled viewers
Show unassociated webpage resources in the existing source dialog before
publishing the same prepared media. Keep preview cancellation and promotion
with the source-selection owner, including canonical embedded-player URLs.

Rejoin the shared position when a viewer resumes after buffering. Retire the
issued seek on playback progress so a later independent stall can recover.

Verified the source dialog, media handoff and cancellation in Chrome, both
TypeScript projects, documentation, and the full 2654-test Web suite.
2026-11-28 22:07:42 -05:00

159 lines
8.1 KiB
TypeScript

import { afterEach, expect, test, vi } from "vitest";
import { WatchPlayer } from "../src/client/channel/watch-playback";
import type { WatchMedia, WatchMediaPatch, WatchMediaState } from "../src/client/channel/watch-media";
afterEach(() => { vi.restoreAllMocks(); vi.useRealTimers(); });
test("a new join clock aligns a running anchor again when the player's buffering is unknown", async () => {
vi.useFakeTimers();
let now = 1000;
vi.spyOn(performance, "now").mockImplementation(() => now);
const state: WatchMediaState = { position: 10, duration: 60, rate: 1, paused: false, readyState: 3, ended: false, buffered: null };
const patch = vi.fn(async (value: WatchMediaPatch) => { Object.assign(state, value); });
const player = new WatchPlayer("room", false, { read: async () => ({ ...state }), patch, ready: vi.fn(), listen: () => () => {}, close: vi.fn() }, vi.fn());
try {
player.calibrate(5000, now, now);
player.update({ timeline: "media", position: 10, paused: false, rate: 1, anchorTime: 5000 });
await player.ready();
now += 10000;
player.calibrate(15000, now, now);
await player.serialize(async () => {});
expect(patch).toHaveBeenCalledExactlyOnceWith({ position: 20 });
} finally { player.close(); }
});
test("one held alignment consumes the latest forced clock and coalesces intermediate paused seeks", async () => {
vi.useFakeTimers();
vi.spyOn(performance, "now").mockReturnValue(1000);
const state: WatchMediaState = { position: 0, duration: 60, rate: 1, paused: true, readyState: 4, ended: false, buffered: null };
let release!: () => void;
const patches: WatchMediaPatch[] = [];
const patch = vi.fn(async (value: WatchMediaPatch) => {
patches.push(value);
if (value.position === 10) await new Promise<void>(resolve => { release = resolve; });
Object.assign(state, value);
});
const read = vi.fn(async () => ({ ...state }));
const media: WatchMedia = { read, patch, ready: async () => {}, listen: () => () => {}, close: vi.fn() };
const player = new WatchPlayer("owned", false, media, () => {});
try {
await player.ready();
player.calibrate(1000, 1000, 1000);
player.update({ timeline: "media", position: 10, paused: true, rate: 1, anchorTime: 1000 });
await vi.advanceTimersByTimeAsync(0);
expect(release).toBeDefined();
for (const position of [12, 18, 24])
player.update({ timeline: "media", position, paused: true, rate: 1, anchorTime: 1000 });
player.calibrate(2000, 1000, 1000);
release();
await player.serialize(async () => {});
expect(state.position).toBe(24);
expect(patches.filter(value => value.position !== undefined).map(value => value.position)).toEqual([10, 24]);
expect(read).toHaveBeenCalledTimes(2);
expect(player.error).toBeNull();
} finally { release?.(); player.close(); }
expect(vi.getTimerCount()).toBe(0);
});
test("a late explicit resume keeps its authority while a normal alignment is in flight", async () => {
vi.useFakeTimers();
vi.spyOn(performance, "now").mockReturnValue(1000);
const state: WatchMediaState = { position: 0, duration: 60, rate: 1, paused: true, readyState: 1, ended: false, buffered: null };
let resolveRead!: (value: WatchMediaState) => void;
const read = vi.fn(async () => ({ ...state }));
read.mockImplementationOnce(() => new Promise(resolve => { resolveRead = resolve; }));
const patch = vi.fn(async (value: WatchMediaPatch) => { Object.assign(state, value); });
const media: WatchMedia = { read, patch, ready: async () => {}, playbackAvailable: () => false, listen: () => () => {}, close: vi.fn() };
const player = new WatchPlayer("owned", false, media, () => {});
try {
await player.ready();
player.calibrate(1000, 1000, 1000);
player.update({ timeline: "media", position: 24, paused: true, rate: 1, anchorTime: 1000 });
await vi.advanceTimersByTimeAsync(0);
const resumed = player.resume();
resolveRead({ ...state });
await resumed;
expect(state.position).toBe(24);
expect(patch).toHaveBeenCalledExactlyOnceWith({ position: 24 });
} finally { player.close(); }
expect(vi.getTimerCount()).toBe(0);
});
test("retirement discards a queued shared update before a held read completes", async () => {
vi.useFakeTimers();
const state: WatchMediaState = { position: 0, duration: 60, rate: 1, paused: true, readyState: 4, ended: false, buffered: null };
let resolveRead!: (value: WatchMediaState) => void;
const read = vi.fn(() => new Promise<WatchMediaState>(resolve => { resolveRead = resolve; }));
const patch = vi.fn(async () => {}), close = vi.fn();
const player = new WatchPlayer("owned", false, { read, patch, ready: async () => {}, listen: () => () => {}, close }, () => {});
await player.ready();
player.calibrate(1000, 1000, 1000);
player.update({ timeline: "media", position: 10, paused: true, rate: 1, anchorTime: 1000 });
await vi.advanceTimersByTimeAsync(0);
player.update({ timeline: "media", position: 24, paused: true, rate: 1, anchorTime: 1000 });
player.close(); resolveRead(state);
await player.serialize(async () => {});
expect(patch).not.toHaveBeenCalled();
expect(read).toHaveBeenCalledOnce();
expect(close).toHaveBeenCalledOnce();
expect(vi.getTimerCount()).toBe(0);
});
test("a player that answers slower than the periodic look still takes its person's pause", async () => {
vi.useFakeTimers();
const state: WatchMediaState = { position: 0, duration: 60, rate: 1, paused: false, readyState: 4, ended: false, buffered: null };
// Each read of the original page takes longer than the second between two looks.
const read = vi.fn(async () => { await new Promise(resolve => setTimeout(resolve, 1100)); return { ...state }; });
const patch = vi.fn(async (value: WatchMediaPatch) => { Object.assign(state, value); });
const media: WatchMedia = { read, patch, ready: async () => {}, listen: () => () => {}, close: vi.fn() };
const player = new WatchPlayer("owned", true, media, () => {});
try {
const ready = player.ready();
await vi.advanceTimersByTimeAsync(1100);
await ready;
await vi.advanceTimersByTimeAsync(1000);
const paused = player.patch({ paused: true });
await vi.advanceTimersByTimeAsync(5000);
expect(patch).toHaveBeenCalledWith({ paused: true });
await paused;
} finally { player.close(); }
});
test.each([false, true])("a viewer rejoins once for each stall (seek waits: %s)", async seekWaits => {
vi.useFakeTimers();
// The player holds thirty seconds from where it plays, and nothing further on.
const state: WatchMediaState = { position: 0, duration: 600, rate: 1, paused: false, readyState: 3, ended: false, buffered: [[0, 30]] };
const moves: number[] = [];
const patch = vi.fn(async (value: WatchMediaPatch) => { if (value.position !== undefined) { moves.push(value.position); state.position = value.position; } });
const media: WatchMedia = { read: async () => ({ ...state }), patch, ready: async () => {}, listen: () => () => {}, close: vi.fn() };
const player = new WatchPlayer("room", false, media, () => {});
try {
await player.ready();
const now = performance.now();
player.calibrate(now, now, now);
player.update({ timeline: "media", position: 0, paused: false, rate: 1, anchorTime: now });
await vi.advanceTimersByTimeAsync(0);
// A minute without data: the room is at sixty, far outside what this player holds.
state.readyState = 2;
await vi.advanceTimersByTimeAsync(60_000);
expect(moves).toEqual([]);
state.readyState = 3;
await vi.advanceTimersByTimeAsync(1000);
expect(moves).toEqual([61]);
// The move cost three seconds of waiting. That lag is its own and is left alone.
state.readyState = seekWaits ? 2 : 3;
state.buffered = [[61, 62]];
await vi.advanceTimersByTimeAsync(3000);
state.readyState = 3;
await vi.advanceTimersByTimeAsync(1000);
expect(moves).toEqual([61]);
// A second stall is rejoined from again.
state.position = 62;
state.readyState = 2;
await vi.advanceTimersByTimeAsync(20_000);
state.readyState = 3;
await vi.advanceTimersByTimeAsync(1000);
expect(moves).toEqual([61, 86]);
} finally { player.close(); }
});