Files
tv-discord/test/sync.test.js
T

74 lines
3.2 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const path = require('path');
const { pathToFileURL } = require('url');
const load = () => import(pathToFileURL(path.join(__dirname, '..', 'activity', 'js', 'sync.js')).href);
test('targetTime extrapolates while playing, clamps to duration', async () => {
const S = await load();
const st = { position: 100, playing: true, updatedAt: 1_000_000, duration: 0 };
assert.equal(S.targetTime(st, 0, 1_005_000), 105);
// local clock 2s behind the server → offset +2000
assert.equal(S.targetTime(st, 2000, 1_003_000), 105);
assert.equal(S.targetTime({ ...st, playing: false }, 0, 1_005_000), 100);
assert.equal(S.targetTime({ ...st, duration: 103 }, 0, 1_005_000), 103);
// updatedAt in the future (clock skew) never rewinds
assert.equal(S.targetTime(st, 0, 999_000), 100);
assert.equal(S.targetTime(null), 0);
});
test('driftAction thresholds', async () => {
const S = await load();
assert.equal(S.driftAction(10, 10).action, 'none');
assert.equal(S.driftAction(10.2, 10).action, 'none');
let a = S.driftAction(10.5, 10);
assert.equal(a.action, 'rate');
assert.equal(a.rate, S.RATE_SLOW);
a = S.driftAction(9, 10);
assert.equal(a.action, 'rate');
assert.equal(a.rate, S.RATE_FAST);
assert.equal(S.driftAction(12, 10).action, 'seek');
assert.equal(S.driftAction(7, 10).action, 'seek');
// cooldown: no seek, nudge instead
assert.equal(S.driftAction(7, 10, { canSeek: false }).action, 'rate');
// paused: seek once noticeably off, never rate
assert.equal(S.driftAction(10.5, 10, { playing: false }).action, 'seek');
assert.equal(S.driftAction(10.1, 10, { playing: false }).action, 'none');
assert.equal(S.driftAction(10.5, 10, { playing: false, canSeek: false }).action, 'none');
});
test('isJump detects explicit seeks but not ticks', async () => {
const S = await load();
const now = 2_000_000;
const a = { position: 50, playing: true, updatedAt: now - 10_000 };
const tickState = { position: 60, playing: true, updatedAt: now };
assert.equal(S.isJump(a, tickState, 0, now), false);
assert.equal(S.isJump(a, { position: 200, playing: true, updatedAt: now }, 0, now), true);
assert.equal(S.isJump(a, { position: 60, playing: false, updatedAt: now }, 0, now), false);
assert.equal(S.isJump(null, a, 0, now), true);
});
test('clock offset: sample math and rolling median', async () => {
const S = await load();
// sent at 1000 (local), server stamped 5100, received at 1200 → rtt 200, offset 5100 - 1100
assert.deepEqual(S.offsetSample(1000, 5100, 1200), { offset: 4000, rtt: 200 });
assert.equal(S.median([3, 1, 2]), 2);
assert.equal(S.median([4, 1, 3, 2]), 2.5);
assert.equal(S.median([]), 0);
const c = S.createClock(5);
assert.equal(c.offset(), 0);
for (const o of [100, 105, 9999, 98, 102, 101]) c.add({ offset: o });
assert.equal(c.count, 5);
assert.equal(c.offset(), 102); // outlier ignored, oldest (100) dropped
c.add(NaN);
assert.equal(c.count, 5);
});
test('formatTime', async () => {
const S = await load();
assert.equal(S.formatTime(0), '0:00');
assert.equal(S.formatTime(65.9), '1:05');
assert.equal(S.formatTime(3725), '1:02:05');
});