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

137 lines
4.6 KiB
JavaScript

import assert from 'node:assert/strict'
import { createServer } from 'node:http'
import { test } from 'node:test'
import { InstanceStatus } from '@companion-module/base'
import MpcInstance from '../src/main.js'
import { status as sample } from './fixture.js'
function instance() {
const state = { statuses: [] }
const module = new MpcInstance({
_isInstanceContext: true,
id: 'test-mpc',
label: 'MPC_TEST',
setActionDefinitions: (actions) => {
state.actions = actions
},
setPresetDefinitions: (sections, presets) => {
state.sections = sections
state.presets = presets
},
setFeedbackDefinitions: (feedbacks) => {
state.feedbacks = feedbacks
},
setVariableDefinitions: () => {},
setVariableValues: (variables) => {
state.variables = variables
},
checkAllFeedbacks: () => {},
updateStatus: (status) => state.statuses.push(status),
})
return { module, state }
}
test('initialization registers API 2 presets; successful actions recover failed requests', async (t) => {
let status = 200
const server = createServer((_, response) => {
response.writeHead(status)
response.end(JSON.stringify(sample))
})
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve))
const { module, state } = instance()
t.after(async () => {
await module.destroy()
server.closeAllConnections()
server.close()
})
await module.init({ host: '127.0.0.1', port: server.address().port })
await module.send('887')
assert.equal(state.statuses.at(-1), InstanceStatus.Ok)
assert.equal(Object.keys(state.actions).length, 138)
assert.deepEqual(state.sections.flatMap((section) => section.definitions).sort(), Object.keys(state.presets).sort())
assert.equal(state.variables.remaining, '00:48')
assert.equal(state.feedbacks.playing.callback(), true)
assert.ok(Object.values(state.presets).every((preset) => preset.type === 'simple'))
status = 503
await module.send('887')
assert.equal(state.statuses.at(-1), InstanceStatus.ConnectionFailure)
assert.equal(state.variables.state, 'offline')
assert.equal(state.variables.muted, null)
assert.equal(state.feedbacks.playing.callback(), false)
status = 200
await module.send('888')
assert.equal(state.statuses.at(-1), InstanceStatus.Ok)
})
test('configuration changes and destroy cancel stale operations without overwriting status', async (t) => {
const server = createServer(() => {})
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve))
t.after(() => {
server.closeAllConnections()
server.close()
})
const { module, state } = instance()
await module.init({ host: '127.0.0.1', port: server.address().port })
const oldRequest = module.send('887')
await module.configUpdated({ host: '' })
await oldRequest
assert.equal(state.statuses.at(-1), InstanceStatus.BadConfig)
await module.configUpdated({ host: '127.0.0.1', port: server.address().port })
const pending = module.send('888')
await module.destroy()
const count = state.statuses.length
await pending
assert.equal(state.statuses.length, count)
assert.equal(module.client, undefined)
})
test('polling discovers external changes, retries failures, and stops on destroy', async (t) => {
let data = { ...sample }
let code = 200
let requests = 0
let active = 0
let maximum = 0
const server = createServer((_, response) => {
requests++
active++
maximum = Math.max(maximum, active)
setTimeout(() => {
active--
response.writeHead(code)
response.end(JSON.stringify(data))
}, 15)
})
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve))
const { module, state } = instance()
t.after(async () => {
await module.destroy()
server.closeAllConnections()
server.close()
})
const until = async (condition) => {
const deadline = Date.now() + 3000
while (!condition()) {
assert.ok(Date.now() < deadline, 'polling did not reach expected state')
await new Promise((resolve) => setTimeout(resolve, 10))
}
}
await module.init({ host: '127.0.0.1', port: server.address().port, poll_interval: 250 })
await Promise.all(Array.from({ length: 10 }, () => module.refresh()))
assert.equal(maximum, 1)
assert.equal(requests, 1)
data = { ...sample, state: 1, muted: true, position: 20000 }
await until(() => state.variables.paused && state.variables.muted)
assert.equal(state.variables.remaining, '00:40')
code = 503
await until(() => state.variables.state === 'offline')
assert.equal(state.variables.remaining, '--:--')
code = 200
data = { ...sample, state: 0, position: 0 }
await until(() => state.variables.stopped)
assert.equal(state.feedbacks.stopped.callback(), true)
await module.destroy()
const lastCount = requests
await new Promise((resolve) => setTimeout(resolve, 350))
assert.equal(requests, lastCount)
})