import assert from 'node:assert/strict' import { createServer } from 'node:http' import { test } from 'node:test' import { Player } from '../src/player.js' import { loadItem } from '../src/load-item.js' import { actions, presets } from '../src/definitions.js' import { feedbacks } from '../src/feedbacks.js' import { status } from './fixture.js' const fast = { poll: 10, settle: 40, timeout: 220 } async function mock(t, onSelect) { const data = { ...status, file: 'Clip 1.mp4', state: 0 } const playlist = { active: 0, items: Array.from({ length: 12 }, (_, index) => ({ index, label: `Clip ${index + 1}.mp4`, duration: 60000 })), } const commands = [] const server = createServer((req, res) => { const url = new URL(req.url, 'http://localhost') if (url.pathname === '/playlist.json') return res.end(JSON.stringify(playlist)) const command = url.searchParams.get('wm_command') if (command) { commands.push(req.url) if (command === '909') data.muted = !data.muted if (command === '890') data.state = 0 if (command === '-3') { const index = Number(url.searchParams.get('index')) if (onSelect) onSelect({ index, data, playlist, res }) else { playlist.active = index data.file = playlist.items[index].label data.state = 2 } } if (!res.writableEnded) res.end() } else res.end(JSON.stringify(data)) }) await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)) const player = new Player({ host: '127.0.0.1', port: server.address().port }, 5000, () => {}) t.after(() => { player.close() server.closeAllConnections() server.close() }) return { player, data, playlist, commands } } test('numbered loading converts 1-based numbers, supports items beyond the presets, and restores mute', async (t) => { const { player, commands, data, playlist } = await mock(t) assert.equal(await loadItem(player, 'item_12', fast), 'Clip 12.mp4') assert.deepEqual(commands, [ '/command.html?wm_command=909', '/command.html?wm_command=-3&index=11', '/command.html?wm_command=890', '/command.html?wm_command=909', ]) assert.equal(playlist.active, 11) assert.equal(data.state, 0) assert.equal(data.muted, false) }) test('invalid or missing playlist items fail before muting or navigation', async (t) => { const { player, commands, playlist } = await mock(t) for (const direction of ['item_0', 'item_-1', 'item_1.5', 'item_13', 'item_10001', 'item_1&index=2']) { await assert.rejects(loadItem(player, direction, fast)) } playlist.items = [] playlist.active = -1 await assert.rejects(loadItem(player, 'item_1', fast), /does not exist/) assert.deepEqual(commands, []) }) test('loading the already selected item stops without reopening and preserves existing mute', async (t) => { const { player, commands, data } = await mock(t) data.state = 2 data.muted = true assert.equal(await loadItem(player, 'item_1', fast), 'Clip 1.mp4') assert.deepEqual(commands, ['/command.html?wm_command=890']) assert.equal(data.muted, true) }) test('indexed selection does not mistake an early active-index update for completed loading', async (t) => { const { player, data, commands } = await mock(t, ({ index, data, playlist }) => { playlist.active = index setTimeout(() => { data.file = playlist.items[index].label data.state = 2 }, 70) }) assert.equal(await loadItem(player, 'item_3', { ...fast, timeout: 500 }), 'Clip 3.mp4') assert.equal(data.state, 0) assert.equal(data.muted, false) assert.ok(commands.filter((path) => path.includes('wm_command=890')).length >= 2) assert.equal(commands.filter((path) => path.includes('wm_command=-3')).length, 1) }) test('wrong active index, unchanged filename, or playlist changes cannot restore audio', async (t) => { for (const problem of ['index', 'filename', 'reorder']) { const { player, commands, data } = await mock(t, ({ index, data, playlist }) => { if (problem !== 'index') playlist.active = index if (problem !== 'filename') data.file = 'Clip 2.mp4' if (problem === 'reorder') playlist.items[1].label = 'Replacement.mp4' }) await assert.rejects(loadItem(player, 'item_2', fast)) assert.equal(data.muted, true) assert.equal(commands.filter((path) => path.includes('wm_command=909')).length, 1) assert.equal(commands.filter((path) => path.includes('wm_command=-3')).length, 1) } }) test('uncertain indexed-selection response stops without selecting again or restoring audio', async (t) => { const { player, commands, data } = await mock(t, ({ index, data, playlist, res }) => { playlist.active = index data.file = 'Clip 2.mp4' data.state = 2 res.writeHead(500) res.end() }) await assert.rejects(loadItem(player, 'item_2', fast), /HTTP 500/) assert.equal(commands.filter((path) => path.includes('wm_command=-3')).length, 1) assert.equal(commands.filter((path) => path.includes('wm_command=909')).length, 1) assert.equal(data.state, 0) assert.equal(data.muted, true) }) test('modular action, first ten presets and load-result feedback agree on item numbers', async () => { let sent const definitions = actions( () => {}, null, null, (item, target) => { sent = { item, target } }, ) await definitions.load_playlist_item.callback({ options: { item: 12, target: 'backup' } }) assert.deepEqual(sent, { item: 12, target: 'backup' }) for (let item = 1; item <= 10; item++) { const preset = presets()[`transport_load_item_${item}`] assert.equal(preset.steps[0].down[0].options.item, item) assert.equal(preset.feedbacks[0].options.item, item) } const fb = feedbacks( () => null, () => null, (key) => (key === 'item_12' ? 'ready' : 'idle'), () => ({ status: 'ready', direction: 'item_12' }), ) assert.equal(fb.load_playlist_item_status.callback({ options: { item: 12 } }).text, '↓\nLOADED\n12') assert.equal(fb.load_playlist_item_status.callback({ options: { item: 1 } }).text, '↓\nLOAD\n1') assert.equal(fb.load_result.callback().text, 'LOADED\n#12') }) test('direct playlist play sends only selection, leaves mute alone, and does not record an experimental load', async (t) => { for (const muted of [true, false]) { const { player, data, commands } = await mock(t) data.muted = muted await player.playPlaylistItem(3) assert.deepEqual(commands, ['/command.html?wm_command=-3&index=2']) assert.equal(data.state, 2) assert.equal(data.muted, muted) assert.equal(player.loadResult.status, 'idle') assert.equal(player.state.file, 'Clip 3.mp4') } }) test('direct playlist play rejects missing items without a playback command', async (t) => { const { player, commands } = await mock(t) await player.playPlaylistItem(13) assert.deepEqual(commands, []) assert.match(player.error, /does not exist/) }) test('direct playlist action and presets expose selection independently of experimental loads', async () => { let sent const definitions = actions( () => {}, null, null, null, (item, target) => { sent = { item, target } }, ) await definitions.play_playlist_item.callback({ options: { item: 7, target: 'main' } }) assert.deepEqual(sent, { item: 7, target: 'main' }) for (let item = 1; item <= 10; item++) { const button = presets()[`playlist_play_item_${item}`] assert.deepEqual(button.steps[0].down, [{ actionId: 'play_playlist_item', options: { item, target: 'both' } }]) assert.deepEqual( button.feedbacks.map((f) => f.feedbackId), ['offline'], ) } })