forked from longtao.wu/droboticflow
168 lines
4.6 KiB
JavaScript
168 lines
4.6 KiB
JavaScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import { browserMediaSeekTime, createMediaSeekQueue } from "../../src/lib/media-seek-queue.js";
|
|
|
|
test("physical seek survives microsecond truncation without changing the requested boundary", () => {
|
|
for (const time of [4.1, 4 + 2 / 30, 11 / 30, 1 / 30, 10.7, 899.966666666667]) {
|
|
const target = browserMediaSeekTime(time, 900);
|
|
assert(target > time && target - time <= 0.00000201);
|
|
assert(Math.floor(target * 1e6) >= Math.ceil(time * 1e6));
|
|
}
|
|
});
|
|
|
|
test("physical seek stays inside media duration and keeps zero unchanged", () => {
|
|
assert.equal(browserMediaSeekTime(0, 900), 0);
|
|
assert.equal(browserMediaSeekTime(-1, 900), 0);
|
|
assert.equal(browserMediaSeekTime(900, 900), 900);
|
|
assert.equal(browserMediaSeekTime(910, 900), 900);
|
|
assert(browserMediaSeekTime(4.1, NaN) > 4.1);
|
|
});
|
|
|
|
function fixture() {
|
|
let clock = 0;
|
|
let idle = 0;
|
|
const timers = new Map();
|
|
const calls = [];
|
|
const queue = createMediaSeekQueue({
|
|
dispatch: (time, revision) => calls.push({ time, revision, at: clock }),
|
|
onIdle: () => idle++,
|
|
now: () => clock,
|
|
schedule: (fn, delay) => {
|
|
const id = Symbol();
|
|
timers.set(id, { fn, at: clock + delay });
|
|
return id;
|
|
},
|
|
unschedule: (id) => timers.delete(id),
|
|
});
|
|
return {
|
|
queue,
|
|
calls,
|
|
timers,
|
|
idle: () => idle,
|
|
advance(ms) {
|
|
clock += ms;
|
|
for (const [id, job] of timers)
|
|
if (job.at <= clock) {
|
|
timers.delete(id);
|
|
job.fn();
|
|
}
|
|
},
|
|
};
|
|
}
|
|
|
|
test("26 drag samples keep only the in-flight preview and latest target", () => {
|
|
const f = fixture();
|
|
for (let i = 0; i < 26; i++) f.queue.preview(i);
|
|
assert.deepEqual(
|
|
f.calls.map((c) => c.time),
|
|
[0],
|
|
);
|
|
f.advance(400);
|
|
f.queue.complete(f.calls[0].revision);
|
|
assert.deepEqual(
|
|
f.calls.map((c) => c.time),
|
|
[0, 25],
|
|
);
|
|
f.queue.complete(f.calls[1].revision);
|
|
assert.equal(f.queue.isPending(), false);
|
|
assert.equal(f.idle(), 1);
|
|
});
|
|
|
|
test("fast decoding still throttles pointer previews to 120ms", () => {
|
|
const f = fixture();
|
|
f.queue.preview(1);
|
|
f.queue.complete(f.calls[0].revision);
|
|
f.queue.preview(2);
|
|
f.advance(119);
|
|
assert.equal(f.calls.length, 1);
|
|
f.queue.preview(3);
|
|
f.advance(1);
|
|
assert.deepEqual(
|
|
f.calls.map((c) => c.time),
|
|
[1, 3],
|
|
);
|
|
});
|
|
|
|
test("release immediately seeks the exact final target even when preview is slow", () => {
|
|
const f = fixture();
|
|
f.queue.preview(1);
|
|
f.queue.preview(100);
|
|
f.queue.commit(100.004483);
|
|
assert.deepEqual(
|
|
f.calls.map((c) => c.time),
|
|
[1, 100.004483],
|
|
);
|
|
f.queue.complete(f.calls[0].revision);
|
|
assert.equal(f.queue.isPending(), true, "stale preview completion must not settle the commit");
|
|
f.queue.complete(f.calls[1].revision);
|
|
assert.equal(f.idle(), 1);
|
|
f.advance(1000);
|
|
assert.equal(f.calls.length, 2, "old preview cannot replay after release");
|
|
});
|
|
|
|
test("release at the current in-flight target does not restart decoding", () => {
|
|
const f = fixture();
|
|
f.queue.preview(10);
|
|
f.queue.preview(11);
|
|
f.queue.commit(10);
|
|
assert.equal(f.calls.length, 1);
|
|
f.queue.complete(f.calls[0].revision);
|
|
f.advance(1000);
|
|
assert.equal(f.calls.length, 1);
|
|
assert.equal(f.idle(), 1);
|
|
});
|
|
|
|
test("commit removes a scheduled trailing preview", () => {
|
|
const f = fixture();
|
|
f.queue.preview(1);
|
|
f.queue.complete(f.calls[0].revision);
|
|
f.queue.preview(2);
|
|
assert.equal(f.timers.size, 1);
|
|
f.queue.commit(3);
|
|
f.advance(200);
|
|
assert.deepEqual(
|
|
f.calls.map((c) => c.time),
|
|
[1, 3],
|
|
);
|
|
});
|
|
|
|
test("frame steps and exact clicks are never throttled or rounded", () => {
|
|
const f = fixture();
|
|
const boundaries = [0.262, 0.266483, 0.299817, 0.333151];
|
|
boundaries.forEach((t) => f.queue.commit(t));
|
|
assert.deepEqual(
|
|
f.calls.map((c) => c.time),
|
|
boundaries,
|
|
);
|
|
assert.equal(
|
|
f.calls.every((c) => c.at === 0),
|
|
true,
|
|
);
|
|
});
|
|
|
|
test("cancel on source change or unmount discards timers and stale events", () => {
|
|
const f = fixture();
|
|
f.queue.preview(1);
|
|
f.queue.complete(f.calls[0].revision);
|
|
f.queue.preview(2);
|
|
f.queue.cancel();
|
|
f.advance(1000);
|
|
assert.equal(f.calls.length, 1);
|
|
assert.equal(f.queue.isPending(), false);
|
|
f.queue.commit(50);
|
|
f.queue.complete(f.calls[0].revision);
|
|
assert.equal(f.queue.isPending(), true);
|
|
f.queue.complete(f.calls[1].revision);
|
|
assert.equal(f.queue.isPending(), false);
|
|
});
|
|
|
|
test("nonfinite targets never reach a video element", () => {
|
|
const f = fixture();
|
|
for (const t of [NaN, Infinity, -Infinity]) {
|
|
f.queue.commit(t);
|
|
f.queue.preview(t);
|
|
}
|
|
assert.deepEqual(f.calls, []);
|
|
assert.equal(f.queue.isPending(), false);
|
|
});
|