up
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import assert from 'node:assert/strict'
|
||||
import { test } from 'node:test'
|
||||
import { Controller, parseCommand } from './controller.js'
|
||||
import { parseRange } from './range.js'
|
||||
|
||||
const command = { id: 'one', action: 'play', media: 'audio', position: 0 }
|
||||
test('валидирует команды на границе IPC / LAN', () => {
|
||||
assert.deepEqual(parseCommand(command), command)
|
||||
for (const value of [
|
||||
null,
|
||||
{},
|
||||
{ ...command, id: '../file' },
|
||||
{ ...command, action: ['play'] },
|
||||
{ ...command, media: ['audio'] },
|
||||
{ ...command, position: NaN },
|
||||
{ ...command, position: -1 },
|
||||
{ ...command, action: 'delete' },
|
||||
{ ...command, media: '/etc/passwd' },
|
||||
]) {
|
||||
assert.throws(() => parseCommand(value))
|
||||
}
|
||||
})
|
||||
test('повтор не применяется; устаревшее подтверждение не меняет новую команду', () => {
|
||||
let broadcasts = 0
|
||||
const c = new Controller(() => broadcasts++)
|
||||
c.command(command)
|
||||
c.command(command)
|
||||
assert.equal(broadcasts, 1)
|
||||
c.command({ ...command, id: 'two', action: 'pause' })
|
||||
c.report({ id: 'one', status: 'playing' })
|
||||
assert.equal(c.state.status, 'pending')
|
||||
c.report({ id: 'two', status: 'paused' })
|
||||
assert.equal(c.state.status, 'paused')
|
||||
assert.equal(c.state.revision, 3)
|
||||
})
|
||||
test('playing подтверждается один раз, ошибки сохраняются', () => {
|
||||
const c = new Controller(() => {})
|
||||
c.command(command)
|
||||
c.report({ id: 'one', status: 'playing' })
|
||||
c.report({ id: 'one', status: 'playing' })
|
||||
assert.equal(c.state.samples.length, 1)
|
||||
c.report({ id: 'one', status: 'error', error: 'Нет файла' })
|
||||
assert.equal(c.state.error, 'Нет файла')
|
||||
assert.throws(() => c.report({ id: 'one', status: 'anything' }))
|
||||
})
|
||||
test('Range: полный файл, границы, суффикс и отказ для недопустимых диапазонов', () => {
|
||||
assert.equal(parseRange(null, 0), null)
|
||||
assert.deepEqual(parseRange(null, 100), { start: 0, end: 99 })
|
||||
assert.deepEqual(parseRange('bytes=20-29', 100), { start: 20, end: 29 })
|
||||
assert.deepEqual(parseRange('bytes=90-', 100), { start: 90, end: 99 })
|
||||
assert.deepEqual(parseRange('bytes=-10', 100), { start: 90, end: 99 })
|
||||
assert.deepEqual(parseRange('bytes=90-999', 100), { start: 90, end: 99 })
|
||||
for (const value of ['bytes=100-', 'bytes=20-10', 'bytes=-0', 'bytes=0-1,3-5', 'bytes=-', 'abc'])
|
||||
assert.equal(parseRange(value, 100), null)
|
||||
})
|
||||
Reference in New Issue
Block a user