223 lines
9.2 KiB
JavaScript
223 lines
9.2 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 { normalizeStatus } from '../src/state.js'
|
|
import { comparePlayers } from '../src/redundancy.js'
|
|
import { colors, redundancyStyle } from '../src/feedbacks.js'
|
|
import { status as sample } from './fixture.js'
|
|
|
|
async function player(t) {
|
|
const mock = { data: { ...sample }, commands: [], statusCode: 200, commandCode: 200 }
|
|
const server = createServer((request, response) => {
|
|
if (request.url.startsWith('/command.html')) {
|
|
mock.commands.push(new URL(request.url, 'http://localhost').searchParams.get('wm_command'))
|
|
if (mock.holdCommands) {
|
|
mock.held = response
|
|
return
|
|
}
|
|
response.writeHead(mock.commandCode)
|
|
} else response.writeHead(mock.statusCode)
|
|
response.end(JSON.stringify(mock.data))
|
|
})
|
|
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve))
|
|
t.after(() => {
|
|
server.closeAllConnections()
|
|
server.close()
|
|
})
|
|
mock.config = { host: '127.0.0.1', port: server.address().port }
|
|
return mock
|
|
}
|
|
|
|
async function moduleFor(t, main, backup, extra = {}) {
|
|
const seen = { statuses: [] }
|
|
const module = new MpcInstance({
|
|
_isInstanceContext: true,
|
|
id: 'test',
|
|
label: 'MPC_TEST',
|
|
setActionDefinitions: (value) => {
|
|
seen.actions = value
|
|
},
|
|
setFeedbackDefinitions: () => {},
|
|
setVariableDefinitions: () => {},
|
|
setPresetDefinitions: () => {},
|
|
setVariableValues: (value) => {
|
|
seen.variables = value
|
|
},
|
|
checkAllFeedbacks: () => {},
|
|
updateStatus: (status) => seen.statuses.push(status),
|
|
})
|
|
t.after(() => module.destroy())
|
|
const config = {
|
|
...main.config,
|
|
...(backup ? { backup_host: backup.config.host, backup_port: backup.config.port } : {}),
|
|
...extra,
|
|
}
|
|
await module.init(config)
|
|
await module.refresh()
|
|
return { module, seen, config }
|
|
}
|
|
|
|
const until = async (condition) => {
|
|
const end = Date.now() + 3000
|
|
while (!condition()) {
|
|
assert.ok(Date.now() < end, 'Expected player update did not arrive')
|
|
await new Promise((resolve) => setTimeout(resolve, 10))
|
|
}
|
|
}
|
|
|
|
test('optional unconfigured backup stays neutral and existing actions default to both', async (t) => {
|
|
const main = await player(t)
|
|
const { module, seen } = await moduleFor(t, main, null, { backup_host: ' ', backup_port: 0 })
|
|
await seen.actions['887'].callback({ options: {} })
|
|
assert.deepEqual(main.commands, ['887'])
|
|
assert.equal(module.backup, undefined)
|
|
assert.equal(seen.variables.sync_status, 'no_backup')
|
|
assert.equal(seen.variables.backup_status, 'not_configured')
|
|
assert.equal(seen.statuses.at(-1), InstanceStatus.Ok)
|
|
})
|
|
|
|
test('main is authoritative and actions can target both, main, or backup', async (t) => {
|
|
const main = await player(t)
|
|
const backup = await player(t)
|
|
backup.data = { ...sample, state: 1, muted: true, position: 5000 }
|
|
const { module, seen } = await moduleFor(t, main, backup)
|
|
assert.equal(seen.variables.state, 'playing')
|
|
assert.equal(seen.variables.muted, false)
|
|
assert.equal(seen.variables.backup_state, 'paused')
|
|
assert.equal(seen.variables.backup_muted, true)
|
|
assert.equal(seen.variables.sync_status, 'out_of_sync')
|
|
await module.send('887')
|
|
await seen.actions['888'].callback({ options: { target: 'main' } })
|
|
await seen.actions['890'].callback({ options: { target: 'backup' } })
|
|
assert.deepEqual(main.commands, ['887', '888'])
|
|
assert.deepEqual(backup.commands, ['887', '890'])
|
|
assert.equal(seen.statuses.at(-1), InstanceStatus.Ok)
|
|
})
|
|
|
|
test('main command failure does not suppress backup delivery or replace authoritative feedback', async (t) => {
|
|
const main = await player(t)
|
|
const backup = await player(t)
|
|
const { module, seen } = await moduleFor(t, main, backup)
|
|
main.commandCode = 503
|
|
await module.send('887')
|
|
assert.deepEqual(backup.commands, ['887'])
|
|
assert.equal(seen.variables.state, 'offline')
|
|
assert.equal(seen.variables.backup_state, 'playing')
|
|
assert.equal(seen.statuses.at(-1), InstanceStatus.ConnectionFailure)
|
|
})
|
|
|
|
test('backup failure and empty media do not mark the connection as failed, and recovery resumes comparison', async (t) => {
|
|
const main = await player(t)
|
|
const backup = await player(t)
|
|
backup.statusCode = 503
|
|
const { module, seen } = await moduleFor(t, main, backup, { poll_interval: 250 })
|
|
assert.equal(seen.variables.sync_status, 'backup_offline')
|
|
assert.equal(seen.variables.backup_muted, null)
|
|
assert.equal(seen.statuses.at(-1), InstanceStatus.Ok)
|
|
backup.commandCode = 503
|
|
await module.send('909', 'backup')
|
|
assert.equal(seen.statuses.at(-1), InstanceStatus.Ok)
|
|
backup.statusCode = 200
|
|
backup.data = { ...sample, state: -1 }
|
|
await until(() => seen.variables.sync_status === 'backup_empty')
|
|
backup.data = { ...sample }
|
|
await until(() => seen.variables.sync_status === 'in_sync')
|
|
assert.equal(seen.statuses.at(-1), InstanceStatus.Ok)
|
|
})
|
|
|
|
test('slow backup requests do not block main commands or status polling', async (t) => {
|
|
const main = await player(t)
|
|
const backup = await player(t)
|
|
const { module, seen } = await moduleFor(t, main, backup, { poll_interval: 250 })
|
|
backup.holdCommands = true
|
|
main.data = { ...sample, state: 1 }
|
|
const sending = module.send('888')
|
|
await until(() => backup.held && seen.variables.state === 'paused')
|
|
assert.deepEqual(main.commands, ['888'])
|
|
main.data = { ...sample, state: 0 }
|
|
await until(() => seen.variables.state === 'stopped')
|
|
backup.held.end('ok')
|
|
await sending
|
|
assert.equal(seen.statuses.at(-1), InstanceStatus.Ok)
|
|
})
|
|
|
|
test('invalid or duplicate backup configuration leaves the main operational', async (t) => {
|
|
const main = await player(t)
|
|
for (const config of [
|
|
{ backup_host: 'localhost', backup_port: 0 },
|
|
{ backup_host: main.config.host, backup_port: main.config.port },
|
|
]) {
|
|
const { module, seen } = await moduleFor(t, main, null, config)
|
|
assert.equal(seen.variables.sync_status, 'backup_config')
|
|
assert.equal(seen.statuses.at(-1), InstanceStatus.Ok)
|
|
await module.send('887')
|
|
assert.equal(module.backup, undefined)
|
|
await module.destroy()
|
|
}
|
|
assert.deepEqual(main.commands, ['887', '887'])
|
|
})
|
|
|
|
test('reconfiguration cancels both old players and removes stale backup state', async (t) => {
|
|
const main = await player(t)
|
|
const backup = await player(t)
|
|
const { module, seen, config } = await moduleFor(t, main, backup)
|
|
const oldMain = module.main
|
|
const oldBackup = module.backup
|
|
backup.holdCommands = true
|
|
const pending = module.send('887', 'backup')
|
|
await until(() => backup.held)
|
|
await module.configUpdated({ ...config, backup_host: '' })
|
|
await pending
|
|
await module.refresh()
|
|
assert.equal(oldMain.active, false)
|
|
assert.equal(oldBackup.active, false)
|
|
assert.equal(seen.variables.sync_status, 'no_backup')
|
|
assert.equal(seen.variables.backup_file, '')
|
|
assert.equal(seen.variables.backup_remaining_seconds, null)
|
|
await module.destroy()
|
|
assert.equal(module.main, undefined)
|
|
assert.equal(module.backup, undefined)
|
|
})
|
|
|
|
const snapshot = (patch = {}, sampledAt = 10000) => ({ state: normalizeStatus({ ...sample, ...patch }), sampledAt })
|
|
const compare = (main, backup, options = {}) => comparePlayers(main, backup, { configured: true, ...options }, 10000)
|
|
|
|
test('comparison handles optional, offline, idle, and stale players without claiming synchronization', () => {
|
|
assert.equal(comparePlayers(null, null).status, 'no_backup')
|
|
assert.equal(compare(snapshot(), null).status, 'backup_offline')
|
|
assert.equal(compare(null, snapshot()).status, 'main_offline')
|
|
assert.equal(compare(snapshot(), snapshot({ state: -1 })).status, 'backup_empty')
|
|
assert.equal(compare(snapshot({ state: -1 }), snapshot()).status, 'main_empty')
|
|
assert.equal(compare(snapshot(), snapshot({ file: '' })).status, 'backup_empty')
|
|
assert.equal(compare(snapshot({}, 1000), snapshot()).status, 'checking')
|
|
for (const status of ['no_backup', 'backup_offline', 'backup_empty'])
|
|
assert.notEqual(redundancyStyle({ status }).bgcolor, colors.red)
|
|
})
|
|
|
|
test('comparison accounts for sample time and rate, with configurable position tolerance', () => {
|
|
assert.equal(compare(snapshot({ position: 1000 }, 9000), snapshot({ position: 2000 })).status, 'in_sync')
|
|
assert.equal(compare(snapshot({ position: 1000, rate: 2 }, 9000), snapshot({ position: 3000, rate: 2 })).offset, 0)
|
|
assert.equal(compare(snapshot(), snapshot({ position: sample.position + 1001 })).status, 'out_of_sync')
|
|
assert.equal(
|
|
compare(snapshot(), snapshot({ position: sample.position + 1001 }), { tolerance: 1500 }).status,
|
|
'in_sync',
|
|
)
|
|
assert.equal(compare(snapshot(), snapshot({ position: sample.position - 2000 })).offset, -2000)
|
|
assert.equal(compare(snapshot({ state: 1 }, 9000), snapshot({ state: 1 })).offset, 0)
|
|
})
|
|
|
|
test('comparison checks media/state/rate; audio is optional and paths can differ', () => {
|
|
assert.equal(
|
|
compare(snapshot({ file: 'C:\\media\\Clip.mp4' }), snapshot({ file: 'D:\\backup\\clip.mp4' })).status,
|
|
'in_sync',
|
|
)
|
|
for (const patch of [{ file: 'other.mp4' }, { state: 1 }, { rate: 2 }, { duration: sample.duration + 2000 }]) {
|
|
assert.equal(compare(snapshot(), snapshot(patch)).status, 'out_of_sync')
|
|
}
|
|
assert.equal(compare(snapshot(), snapshot({ muted: true, volume: 0 })).status, 'in_sync')
|
|
assert.equal(compare(snapshot(), snapshot({ muted: true }), { compareAudio: true }).status, 'out_of_sync')
|
|
assert.equal(compare(snapshot(), snapshot({ volume: 0 }), { compareAudio: true }).status, 'out_of_sync')
|
|
})
|