178 lines
6.0 KiB
JavaScript
178 lines
6.0 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import { createServer } from 'node:http'
|
|
import { setTimeout as delay } from 'node:timers/promises'
|
|
import { test } from 'node:test'
|
|
import { Player } from '../src/player.js'
|
|
import { loadItem } from '../src/load-item.js'
|
|
import { status } from './fixture.js'
|
|
|
|
async function mock(t, handle, initial = {}) {
|
|
const data = { ...status, state: 0, ...initial }
|
|
const commands = []
|
|
const events = []
|
|
const server = createServer((req, res) => {
|
|
const id = new URL(req.url, 'http://localhost').searchParams.get('wm_command')
|
|
if (id) {
|
|
commands.push(id)
|
|
events.push(id)
|
|
if (handle?.(id, data, res) === false) return
|
|
if (id === '909') data.muted = !data.muted
|
|
res.end()
|
|
} else {
|
|
events.push(data.muted ? 'muted-status' : 'unmuted-status')
|
|
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, commands, events }
|
|
}
|
|
const fast = { poll: 10, settle: 40, timeout: 250 }
|
|
|
|
for (const direction of ['next', 'previous']) {
|
|
const navigationId = direction === 'next' ? '920' : '919'
|
|
const loadNext = (player, timing) => loadItem(player, direction, timing)
|
|
test(`Load ${direction}: waits for confirmed mute, immediately stops, retries delayed autoplay, then restores audio`, async (t) => {
|
|
let stops = 0
|
|
const { player, commands, events } = await mock(t, (id, data) => {
|
|
if (id === navigationId)
|
|
setTimeout(() => {
|
|
data.file = 'Next.mp4'
|
|
data.state = 2
|
|
}, 35)
|
|
if (id === '890' && ++stops >= 3) data.state = 0
|
|
})
|
|
assert.equal(await loadNext(player, fast), 'Next.mp4')
|
|
assert.equal(commands.filter((id) => id === navigationId).length, 1)
|
|
assert.equal(commands.filter((id) => id === '909').length, 2)
|
|
const next = events.indexOf(navigationId)
|
|
assert.ok(events.slice(0, next).includes('muted-status'))
|
|
assert.equal(events[next + 1], '890', 'no status request delays the first Stop')
|
|
assert.ok(stops >= 3)
|
|
assert.equal(player.state.state, 'stopped')
|
|
assert.equal(player.state.muted, false)
|
|
})
|
|
|
|
test(`Load ${direction}: preserves existing mute and waits through a false stopped snapshot`, async (t) => {
|
|
const { player, commands } = await mock(
|
|
t,
|
|
(id, data) => {
|
|
if (id === navigationId) {
|
|
data.file = 'Next.mp4'
|
|
setTimeout(() => {
|
|
data.state = 2
|
|
}, 25)
|
|
}
|
|
if (id === '890') data.state = 0
|
|
},
|
|
{ muted: true },
|
|
)
|
|
await loadNext(player, fast)
|
|
assert.ok(commands.filter((id) => id === '890').length >= 2)
|
|
assert.ok(!commands.includes('909'))
|
|
assert.equal(player.state.muted, true)
|
|
})
|
|
|
|
test(`Load ${direction}: unconfirmed mute never advances and toggle is not retried`, async (t) => {
|
|
const { player, commands } = await mock(t, (id, _, res) => {
|
|
if (id === '909') {
|
|
res.end()
|
|
return false
|
|
}
|
|
})
|
|
await assert.rejects(loadNext(player, fast), /Mute was not confirmed/)
|
|
assert.deepEqual(commands, ['909'])
|
|
})
|
|
|
|
test(`Load ${direction}: unchanged filename or failure to stop leaves mute enabled and never skips twice`, async (t) => {
|
|
for (const changeFile of [false, true]) {
|
|
const { player, commands, data } = await mock(t, (id, data) => {
|
|
if (id === navigationId && changeFile) {
|
|
data.file = 'Next.mp4'
|
|
data.state = 2
|
|
}
|
|
})
|
|
await assert.rejects(loadNext(player, fast), /not confirmed/)
|
|
assert.equal(data.muted, true)
|
|
assert.equal(commands.filter((id) => id === navigationId).length, 1)
|
|
assert.equal(commands.filter((id) => id === '909').length, 1)
|
|
}
|
|
})
|
|
|
|
test(`Load ${direction}: lost navigation response triggers best-effort Stop without retrying navigation or unmuting`, async (t) => {
|
|
const { player, commands, data } = await mock(t, (id, data, res) => {
|
|
if (id === navigationId) {
|
|
data.file = 'Next.mp4'
|
|
data.state = 2
|
|
res.writeHead(500)
|
|
res.end()
|
|
return false
|
|
}
|
|
if (id === '890') data.state = 0
|
|
})
|
|
await assert.rejects(loadNext(player, fast), /HTTP 500/)
|
|
assert.deepEqual(commands, ['909', navigationId, '890'])
|
|
assert.equal(data.muted, true)
|
|
assert.equal(data.state, 0)
|
|
})
|
|
|
|
test(`Load ${direction}: Player coalesces repeated loading, excludes normal commands and publishes verified success`, async (t) => {
|
|
const { player, commands } = await mock(t, (id, data) => {
|
|
if (id === navigationId) {
|
|
data.file = 'Next.mp4'
|
|
data.state = 2
|
|
}
|
|
if (id === '890') data.state = 0
|
|
})
|
|
const pending = player.loadItem(direction)
|
|
assert.equal(player.loadItem(direction), pending)
|
|
assert.equal(player.loadItem(direction === 'next' ? 'previous' : 'next'), pending)
|
|
assert.equal(await player.send('887'), false)
|
|
await pending
|
|
assert.equal(player.loadStatus, 'ready')
|
|
assert.deepEqual(commands, ['909', navigationId, '890', '909'])
|
|
})
|
|
|
|
test(`Load ${direction}: closing a player cancels a load sequence without sending navigation or restoring audio`, async (t) => {
|
|
const { player, commands } = await mock(t, (id, _, res) => {
|
|
if (id === '909') {
|
|
res.end()
|
|
return false
|
|
}
|
|
})
|
|
const pending = player.loadItem(direction)
|
|
while (!commands.length) await delay(5)
|
|
player.close()
|
|
await pending
|
|
await delay(80)
|
|
assert.deepEqual(commands, ['909'])
|
|
})
|
|
|
|
test(`Load ${direction}: uncertain final unmute is not toggled again and cannot report ready`, async (t) => {
|
|
let toggles = 0
|
|
const { player, commands } = await mock(t, (id, data, res) => {
|
|
if (id === navigationId) {
|
|
data.file = 'Next.mp4'
|
|
data.state = 2
|
|
}
|
|
if (id === '890') data.state = 0
|
|
if (id === '909' && ++toggles === 2) {
|
|
data.muted = false
|
|
res.writeHead(500)
|
|
res.end()
|
|
return false
|
|
}
|
|
})
|
|
await player.loadItem(direction)
|
|
assert.equal(player.loadStatus, 'failed')
|
|
assert.equal(commands.filter((id) => id === '909').length, 2)
|
|
assert.equal(commands.filter((id) => id === navigationId).length, 1)
|
|
assert.equal(commands.at(-1), '890')
|
|
})
|
|
}
|