beffroi-tools/companion-modules/media-player-classic/test/state.test.js

87 lines
3.9 KiB
JavaScript

import assert from 'node:assert/strict'
import { test } from 'node:test'
import { normalizeStatus, parseVariables, variables, clock } from '../src/state.js'
import { feedbacks, progressBar } from '../src/feedbacks.js'
import { presets } from '../src/definitions.js'
import { status, htmlStatus } from './fixture.js'
test('playback feedback follows numeric player state, not localized strings or button presses', () => {
for (const [code, name] of [
[-1, 'idle'],
[0, 'stopped'],
[1, 'paused'],
[2, 'playing'],
]) {
const state = normalizeStatus({ ...status, state: code, stateString: 'Localized text' })
const definitions = feedbacks(() => state)
for (const id of ['playing', 'stopped', 'paused', 'idle']) assert.equal(definitions[id].callback(), id === name)
assert.equal(definitions.muted.callback(), false)
}
assert.equal(feedbacks(() => normalizeStatus({ ...status, volume: 0 })).muted.callback(), false)
assert.equal(feedbacks(() => normalizeStatus({ ...status, muted: true })).muted.callback(), true)
assert.equal(feedbacks(() => null).offline.callback(), true)
assert.equal(feedbacks(() => null).stopped.callback(), false)
})
test('timing uses milliseconds, clamps end/seek overshoot, and handles unknown duration', () => {
const state = normalizeStatus(status)
assert.equal(state.remaining, 47500)
assert.equal(variables(state).remaining, '00:48')
assert.equal(variables(state).elapsed, '00:12')
assert.equal(variables(state).progress, 20.8)
assert.equal(clock(3661000), '1:01:01')
assert.equal(clock(0), '00:00')
const ended = normalizeStatus({ ...status, position: 61000 })
assert.equal(ended.remaining, 0)
assert.equal(ended.progress, 100)
const unknown = normalizeStatus({ ...status, duration: 0 })
assert.equal(variables(unknown).remaining, '--:--')
assert.equal(unknown.progress, null)
assert.equal(variables(null).remaining_seconds, null)
assert.equal(variables(null).connected, false)
assert.equal(variables(null).muted, null)
})
test('HTML parsing rejects incomplete/error pages and does not evaluate scripts', () => {
assert.deepEqual(parseVariables(htmlStatus()), normalizeStatus(status))
assert.throws(() => parseVariables('<html>Authentication required</html>'))
assert.throws(() => parseVariables(htmlStatus().replace('<p id="state">2</p>', '')))
assert.throws(() => normalizeStatus({ ...status, state: 42 }))
assert.throws(() => normalizeStatus({ ...status, duration: 'NaN' }))
})
test('transport presets use playlist commands and state feedback; time displays are passive', () => {
const buttons = presets('MY_PLAYER')
for (const [preset, command] of [
['play', '887'],
['stop', '890'],
['previous', '919'],
['next', '920'],
['mute', '909'],
]) {
assert.equal(buttons[`transport_${preset}`].steps[0].down[0].actionId, command)
assert.equal(buttons[`transport_${preset}`].feedbacks.at(-1).feedbackId, 'offline')
}
assert.equal(buttons.transport_play.feedbacks[0].feedbackId, 'playing')
assert.equal(buttons.transport_stop.feedbacks[0].feedbackId, 'stopped')
assert.equal(buttons.transport_mute.feedbacks[0].feedbackId, 'muted')
assert.equal(buttons.transport_remaining.style.text, 'LEFT\n$(MY_PLAYER:remaining)')
assert.deepEqual(buttons.transport_remaining.steps[0].down, [])
})
test('progress graphic fills the bottom proportionally and clears when state is unavailable', () => {
const bar = progressBar(normalizeStatus({ ...status, position: 30000 }), { width: 72, height: 72 })
const pixels = Buffer.from(bar.imageBuffer, 'base64')
assert.equal(pixels.length, 72 * 72 * 4)
assert.equal(pixels[3], 0)
assert.equal(pixels[71 * 72 * 4 + 3], 255)
assert.notDeepEqual(
pixels.subarray((71 * 72 + 35) * 4, (71 * 72 + 36) * 4),
pixels.subarray((71 * 72 + 36) * 4, (71 * 72 + 37) * 4),
)
assert.ok(
Buffer.from(progressBar(null, { width: 72, height: 72 }).imageBuffer, 'base64').every((value) => value === 0),
)
assert.deepEqual(progressBar(null, undefined), {})
})