// Regression checks for channel navigation, single start, controls and cancellation. const fs = require('node:fs'); const vm = require('node:vm'); const assert = require('node:assert/strict'); const listeners = {}, documentListeners = {}, elements = []; let clock = 0, interval, starts = 0, stops = 0, osd = 0, returned = 0; const pending = []; const location = { hash: '#/livetv', pathname: '/web/index.html', search: '', replace(value) { this.hash = value; } }; const document = { fullscreenElement: null, body: { append(element) { elements.push(element); } }, createElement() { return { style: {}, remove() { elements.splice(elements.indexOf(this), 1); } }; }, querySelector() { return { readyState: 0, currentTime: 0 }; }, addEventListener(name, callback) { documentListeners[name] = callback; } }; const window = { ApiClient: { getCurrentUserId: () => 'user', serverId: () => 'server', getItem: async (user, id) => ({ Id: id, Type: 'TvChannel' }) }, addEventListener(name, callback) { listeners[name] = callback; } }; const context = { window, document, location, console, URLSearchParams, history: { replaceState(a, b, url) { location.hash = url.slice(url.indexOf('#')); } }, Date: { now: () => clock }, setTimeout, clearTimeout, setInterval(callback) { interval = callback; return callback; }, clearInterval(callback) { if (interval === callback) interval = null; } }; const manager = { getCurrentPlayer: () => ({ isLocalPlayer: true }), play() { ++starts; return new Promise(resolve => pending.push(resolve)); }, stop: async () => { ++stops; } }; const flush = async () => { for (let i = 0; i < 8; i++) await Promise.resolve(); }; (async () => { vm.runInNewContext(fs.readFileSync(__dirname + '/web/decoder-channel-playback.js', 'utf8'), context); const Plugin = await window.DecoderChannelPlayback(); new Plugin({ playbackManager: manager, toast() {}, loading: { hide() {} }, events: { on() {} }, appRouter: { showVideoOsd: async () => { ++osd; }, show: async () => { ++returned; location.hash = '#/livetv?tab=2'; } } }); assert.equal(location.hash, '#/livetv?tab=2'); location.hash = '#/livetv?tab=0'; listeners.hashchange(); assert.equal(location.hash, '#/livetv?tab=0'); const click = () => documentListeners.click({ button: 0, target: { closest: () => ({ closest: () => ({ dataset: { id: 'channel', serverid: 'server' } }) }) }, preventDefault() {}, stopImmediatePropagation() {} }); click(); click(); await flush(); assert.equal(starts, 1); assert.equal(elements[0].id, 'decoderPlaybackExit'); interval(); await flush(); assert.equal(osd, 1); listeners.keydown({ key: 'Escape', preventDefault() {}, stopImmediatePropagation() {} }); await flush(); assert.equal(stops, 1); assert.equal(returned, 1); assert.equal(elements.length, 0); click(); await flush(); assert.equal(starts, 2); pending[0](); await flush(); assert.equal(stops, 1, 'late old completion must not stop the new channel'); clock = 25001; interval(); await flush(); assert.equal(stops, 2); assert.equal(returned, 2); assert.equal(elements.length, 0); pending[1](); await flush(); console.log('PASS default channels, explicit programmes, one start, native OSD, Escape, late completion and startup timeout'); })().catch(error => { console.error(error); process.exitCode = 1; });