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

187 lines
8.2 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, transportBorder, shortFilename } 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), {})
})
test('transport outlines leave the center clear, thicken when active, and turn grey offline', () => {
const image = { width: 72, height: 72 }
const render = (state, control) => Buffer.from(transportBorder(state, image, control).imageBuffer, 'base64')
const waiting = render({ state: 'stopped' }, 'play')
const playing = render({ state: 'playing' }, 'play')
const pixel = (buffer, x, y) => [...buffer.subarray((y * 72 + x) * 4, (y * 72 + x + 1) * 4)]
assert.equal(pixel(waiting, 0, 36)[3], 255)
assert.equal(pixel(waiting, 1, 36)[3], 0)
assert.equal(pixel(playing, 2, 36)[3], 255)
assert.equal(pixel(playing, 3, 36)[3], 0)
assert.equal(pixel(playing, 36, 36)[3], 0)
assert.deepEqual(pixel(render(null, 'play'), 0, 0), [89, 99, 109, 255])
assert.deepEqual(pixel(render({ muted: true }, 'mute'), 0, 0), [186, 41, 54, 255])
assert.deepEqual(transportBorder(null, undefined, 'play'), {})
})
test('split timers share rounded time across boundaries, preserve long hours, and clear unavailable values', () => {
const parts = (values, mode) => ['hh', 'mm', 'ss'].map((part) => values[`${mode}_${part}`])
for (const [milliseconds, expected] of [
[0, ['00', '00', '00']],
[1, ['00', '00', '01']],
[59999, ['00', '01', '00']],
[3599999, ['01', '00', '00']],
[3600001, ['01', '00', '01']],
[90061000, ['25', '01', '01']],
]) {
const data = normalizeStatus({ ...status, duration: milliseconds || 1, position: milliseconds ? 0 : 1 })
assert.deepEqual(parts(variables(data), 'remaining'), expected)
}
assert.deepEqual(parts(variables(normalizeStatus({ ...status, position: 3599999, duration: 4000000 })), 'elapsed'), [
'00',
'59',
'59',
])
assert.deepEqual(parts(variables(null), 'remaining'), ['--', '--', '--'])
assert.deepEqual(parts(variables(normalizeStatus({ ...status, state: -1 })), 'elapsed'), ['--', '--', '--'])
const unknown = variables(normalizeStatus({ ...status, duration: 0 }))
assert.deepEqual(parts(unknown, 'remaining'), ['--', '--', '--'])
assert.deepEqual(parts(unknown, 'elapsed'), ['00', '00', '12'])
})
test('split timer presets are passive and countdown urgency follows remaining time', () => {
const buttons = presets('CLOCK')
for (const mode of ['remaining', 'elapsed'])
for (const part of ['hh', 'mm', 'ss']) {
const button = buttons[`timer_${mode}_${part}`]
assert.equal(button.style.text, `$(CLOCK:${mode}_${part})`)
assert.deepEqual(button.steps[0].down, [])
}
for (const [remaining, color] of [
[30001, 0xffffff],
[30000, 0xe09b20],
[10001, 0xe09b20],
[10000, 0xba2936],
[0, 0xba2936],
]) {
const defs = feedbacks(() => ({ state: 'playing', remaining }))
assert.equal(defs.split_timer_color.callback({ options: { mode: 'remaining' } }).color, color)
assert.equal(defs.split_timer_color.callback({ options: { mode: 'elapsed' } }).color, 0xffffff)
}
assert.equal(feedbacks(() => null).split_timer_color.callback({ options: { mode: 'remaining' } }).color, 0x889099)
})
test('loaded filename preview uses the basename, wraps and truncates without splitting Unicode characters', () => {
assert.equal(shortFilename('C:\\Videos\\Opening film.mp4'), 'Opening \nfilm')
assert.equal(shortFilename('/media/12345678901234567890.mp4'), '12345678\n9012345…')
assert.equal(shortFilename('é👨‍👩‍👧‍👦.mp4'), 'é👨‍👩‍👧‍👦')
assert.equal(shortFilename(''), '')
const fb = feedbacks(
() => null,
() => null,
() => 'idle',
() => ({ status: 'ready', file: '/media/Intro.mp4', direction: 'item_1' }),
)
assert.equal(fb.load_result.callback().text, 'LOADED\nIntro')
})
test('layered directional load feedback follows each direction and clears after its result expires', () => {
let current = 'loading'
const definitions = feedbacks(
() => null,
() => null,
(direction) => (direction === 'next' ? current : 'idle'),
)
const catalog = presets()
for (const state of ['loading', 'ready', 'failed', 'idle']) {
current = state
for (const direction of ['next', 'previous']) {
const preset = catalog[`transport_load_${direction}`]
assert.equal(preset.type, 'layered')
for (const feedback of preset.feedbacks.filter((item) => item.feedbackId === 'load_direction_state')) {
assert.equal(
definitions.load_direction_state.callback(feedback),
direction === 'next' && feedback.options.state === state,
)
}
}
}
})