Files
tv/web/test/party.test.mjs
T
4DBug ee0e36d2c7 web: watch-party layer for the Discord Activity
party.js is inert unless a transport attaches. The host publishes route changes
and playback snapshots; viewers mirror the route, load the host's stream without
scraping and follow its clock. Also: forceProxy playback, bug-tv-tmdb/bug-tv-img
meta overrides, relative API bases (/tv), trailer hidden in party mode, and the
web package no longer ships web/test.
2026-10-27 10:00:34 -05:00

65 lines
2.6 KiB
JavaScript

// node --test web/test/ — party.js hub semantics (no DOM needed)
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { party } from '../js/party.js';
const sent = [];
const welcome = (me, host, extra = {}) => ({ t: 'welcome', you: { id: me, username: me }, host, members: [{ id: 'a', username: 'Ann' }, { id: 'b', username: 'Bob' }], now: Date.now(), ...extra });
test('solo until a transport attaches and welcome arrives; publish is a no-op', () => {
assert.equal(party.mode, 'solo');
party.publish('route', { path: '/x' });
party.receive(welcome('a', 'a')); // no transport → still solo
assert.equal(party.mode, 'solo');
});
test('mode derives from welcome/host; publish only as host', () => {
const modes = [];
const off = party.on('mode', (m) => modes.push(m.mode));
const detach = party.attach({ send: (t, d) => sent.push([t, d]) });
assert.equal(sent[0][0], 'ping');
party.receive(welcome('a', 'a'));
assert.equal(party.mode, 'host');
party.publish('route', { path: '/movies' });
assert.deepEqual(sent.at(-1), ['route', { path: '/movies' }]);
party.receive({ t: 'host', host: 'b' });
assert.equal(party.mode, 'viewer');
assert.equal(party.hostName(), 'Bob');
const n = sent.length;
party.publish('route', { path: '/tv' });
assert.equal(sent.length, n);
assert.deepEqual(modes, ['host', 'viewer']);
off();
detach();
assert.equal(party.mode, 'solo');
});
test('viewer gets route/play events; stale welcome snapshot is dropped', () => {
const got = [];
const offs = [party.on('route', (r) => got.push(['route', r.path])), party.on('play', (s) => got.push(['play', s.path]))];
const detach = party.attach({ send() {} });
party.receive(welcome('b', 'a', { route: '/title/movie/550', play: { path: '/watch/movie/550' } }));
assert.equal(party.lastPlay, null);
assert.deepEqual(got, [['route', '/title/movie/550']]);
party.receive({ t: 'play', snapshot: { path: '/watch/movie/550', at: 1 } });
assert.equal(party.lastPlay.path, '/watch/movie/550');
assert.deepEqual(got.at(-1), ['play', '/watch/movie/550']);
offs.forEach((o) => o());
detach();
});
test('clock offset is the median of the last 5 pong samples', () => {
const detach = party.attach({ send() {} });
const realNow = Date.now;
let t = 1_000_000;
Date.now = () => t;
try {
// rtt 0 → sample = now - at; offsets 100,100,5000(outlier),100,100 → median 100
for (const off of [100, 100, 5000, 100, 100]) party.receive({ t: 'pong', at: t, now: t + off });
assert.equal(party.now() - t, 100);
} finally {
Date.now = realNow;
detach();
}
});