117 lines
3.6 KiB
JavaScript
117 lines
3.6 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import { createServer } from 'node:http'
|
|
import { test } from 'node:test'
|
|
import { MpcClient, endpoint } from '../src/client.js'
|
|
import { actions, presets } from '../src/definitions.js'
|
|
import { status, htmlStatus } from './fixture.js'
|
|
|
|
async function server(t, handler) {
|
|
const server = createServer(handler)
|
|
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve))
|
|
t.after(() => {
|
|
server.closeAllConnections()
|
|
server.close()
|
|
})
|
|
return { host: '127.0.0.1', port: server.address().port }
|
|
}
|
|
|
|
test('all original actions and presets dispatch their numeric command IDs', async (t) => {
|
|
const paths = []
|
|
const client = new MpcClient(
|
|
await server(t, (req, res) => {
|
|
paths.push(req.url)
|
|
res.end(JSON.stringify(status))
|
|
}),
|
|
)
|
|
t.after(() => client.close())
|
|
const definitions = actions((id) => client.command(id))
|
|
assert.equal(Object.keys(definitions).length, 138)
|
|
for (const [id, action] of Object.entries(definitions)) {
|
|
await action.callback()
|
|
assert.equal(paths.at(-1), `/command.html?wm_command=${id}`)
|
|
assert.equal(presets()[id].steps[0].down[0].actionId, id)
|
|
}
|
|
await client.probe()
|
|
assert.equal(paths.at(-1), '/status.json')
|
|
})
|
|
|
|
test('HTTP failures and redirects fail rather than reporting success', async (t) => {
|
|
for (const status of [404, 500, 302]) {
|
|
const client = new MpcClient(
|
|
await server(t, (_, res) => {
|
|
res.writeHead(status, { Location: '/other' })
|
|
res.end()
|
|
}),
|
|
)
|
|
t.after(() => client.close())
|
|
await assert.rejects(client.probe())
|
|
}
|
|
})
|
|
|
|
test('timeouts and module destruction cancel pending requests', async (t) => {
|
|
const config = await server(t, () => {})
|
|
const timeout = new MpcClient(config, 30)
|
|
await assert.rejects(timeout.probe(), { name: 'TimeoutError' })
|
|
timeout.close()
|
|
const client = new MpcClient(config)
|
|
const pending = client.probe()
|
|
client.close()
|
|
await assert.rejects(pending, { name: 'AbortError' })
|
|
})
|
|
|
|
test('reject invalid configuration and command injection', async () => {
|
|
for (const config of [
|
|
{ host: '' },
|
|
{ host: 'localhost/path' },
|
|
{ host: 'localhost', port: 0 },
|
|
{ host: 'localhost', port: 65536 },
|
|
]) {
|
|
assert.throws(() => endpoint(config))
|
|
}
|
|
assert.equal(endpoint({ host: '::1' }).hostname, '[::1]')
|
|
const client = new MpcClient({ host: 'localhost' })
|
|
await assert.rejects(client.command('887&extra=1'))
|
|
client.close()
|
|
})
|
|
|
|
test('prefer JSON, fall back once for unavailable endpoints, and decode HTML entities', async (t) => {
|
|
for (const unavailable of [404, 200]) {
|
|
const paths = []
|
|
const client = new MpcClient(
|
|
await server(t, (req, res) => {
|
|
paths.push(req.url)
|
|
if (req.url === '/status.json') {
|
|
res.writeHead(unavailable)
|
|
res.end('<html>Home</html>')
|
|
} else res.end(htmlStatus({ ...status, file: 'A & B 🎬', muted: true }))
|
|
}),
|
|
)
|
|
t.after(() => client.close())
|
|
const first = await client.status()
|
|
assert.equal(first.file, 'A & B 🎬')
|
|
assert.equal(first.muted, true)
|
|
assert.equal(first.state, 'playing')
|
|
await client.status()
|
|
assert.deepEqual(paths, ['/status.json', '/variables.html', '/variables.html'])
|
|
}
|
|
})
|
|
|
|
test('malformed status and server failures are not hidden by HTML fallback', async (t) => {
|
|
for (const body of ['garbage', '{}', JSON.stringify({ ...status, muted: 'false' })]) {
|
|
const client = new MpcClient(await server(t, (_, res) => res.end(body)))
|
|
t.after(() => client.close())
|
|
await assert.rejects(client.status())
|
|
}
|
|
let count = 0
|
|
const client = new MpcClient(
|
|
await server(t, (_, res) => {
|
|
count++
|
|
res.writeHead(503)
|
|
res.end()
|
|
}),
|
|
)
|
|
t.after(() => client.close())
|
|
await assert.rejects(client.status())
|
|
assert.equal(count, 1)
|
|
})
|