Files
tv-discord/test/helpers.js
T

92 lines
2.6 KiB
JavaScript

// Fake bug-tv client + controllable clock for room tests.
function fakeClock(start = Date.parse('2026-01-01T00:00:00Z')) {
let now = start;
let seq = 0;
const timers = new Map();
return {
now: () => now,
setTimeout(fn, ms) {
const id = ++seq;
timers.set(id, { at: now + ms, fn });
return id;
},
clearTimeout(id) {
timers.delete(id);
},
advance(ms) {
const end = now + ms;
for (;;) {
let next = null;
for (const [id, t] of timers) if (t.at <= end && (!next || t.at < next[1].at)) next = [id, t];
if (!next) break;
timers.delete(next[0]);
now = next[1].at;
next[1].fn();
}
now = end;
},
pending: () => timers.size,
};
}
function stream(id, o = {}) {
return {
id,
provider: o.provider || 'p',
name: o.name || `${id} name`,
title: o.title || '',
quality: o.pixels ? `${o.pixels}p` : '',
pixels: o.pixels == null ? 1080 : o.pixels,
type: o.type || 'm3u8',
size: o.size || 0,
seeds: o.seeds == null ? null : o.seeds,
subtitles: o.subtitles || [],
playback: { kind: o.kind || 'hls', url: `/v1/p/${id}/x.m3u8`, ...(o.fallback ? { fallback: `/v1/p/${id}/f` } : {}) },
};
}
const SHOW = {
id: 1399,
name: 'Show',
first_air_date: '2011-04-17',
poster_path: '/p.jpg',
backdrop_path: '/b.jpg',
overview: 'A show.',
seasons: [
{ season_number: 0, episode_count: 5, name: 'Specials' },
{ season_number: 1, episode_count: 3, air_date: '2011-04-17' },
{ season_number: 2, episode_count: 2, air_date: '2012-04-01' },
],
};
const MOVIE = { id: 550, title: 'Fight Club', release_date: '1999-10-15', poster_path: '/fc.jpg', backdrop_path: '/fcb.jpg', overview: 'Soap.' };
const ep = (n, air = '2011-01-01') => ({ episode_number: n, name: `Ep ${n}`, air_date: air, still_path: `/s${n}.jpg`, overview: `ep ${n}` });
const SEASONS = { 0: [ep(1)], 1: [ep(1), ep(2), ep(3)], 2: [ep(1), ep(2)] };
function fakeBugtv() {
const calls = { streams: [], prefetch: [] };
return {
calls,
last: () => calls.streams[calls.streams.length - 1],
streams(params, h) {
calls.streams.push({ params, h });
return new Promise(() => {}); // the test drives the handlers
},
async prefetch(params) {
calls.prefetch.push(params);
return { queued: [] };
},
async searchMulti() {
return [];
},
async details(type) {
return type === 'tv' ? SHOW : MOVIE;
},
async season(id, n) {
return { season_number: n, episodes: SEASONS[n] || [] };
},
};
}
module.exports = { fakeClock, fakeBugtv, stream, SHOW, MOVIE };