mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-12 23:36:15 -08:00
refactor(chapter): PKG-CHAPTER-03 split runtime into typed lifecycle and view modules
This commit is contained in:
1 parent
a0286c4e95
commit
48660c4e12
23 files changed
+1207
-497
No files matched your search
@@ -0,0 +1,49 @@
|
||||
import assert from 'node:assert/strict'
|
||||
// eslint-disable-next-line test/no-import-node-test -- Pure chapter logic uses the repository Node runner.
|
||||
import { test } from 'node:test'
|
||||
import { loadModules } from './helpers/load.js'
|
||||
|
||||
const { normalize } = await loadModules({ normalize: { file: 'packages/artplayer-plugin-chapter/src/chapters', name: 'normalizeChapters' } })
|
||||
|
||||
test('Chapter normalization preserves caller array and objects while filling every gap', () => {
|
||||
const first = { start: 1, end: 2, title: ' First ' }
|
||||
const last = { start: 4, end: 6, title: 'Last' }
|
||||
const input = [last, first]
|
||||
assert.equal(normalize(input, 8), input)
|
||||
assert.equal(input[1], first)
|
||||
assert.equal(input[3], last)
|
||||
assert.deepEqual(input, [
|
||||
{ start: 0, end: 1, title: '' },
|
||||
first,
|
||||
{ start: 2, end: 4, title: '' },
|
||||
last,
|
||||
{ start: 6, end: 8, title: '' },
|
||||
])
|
||||
assert.equal(normalize(input, 8), input)
|
||||
assert.equal(input.length, 5, 'Repeated updates must not duplicate gaps')
|
||||
})
|
||||
|
||||
test('Chapter endpoints, adjacency and Infinity normalization retain historical behavior', () => {
|
||||
const input = [{ start: 0, end: 2, title: 'A' }, { start: 2, end: Infinity, title: 'B' }]
|
||||
assert.equal(normalize(input, 8), input)
|
||||
assert.equal(input[1].end, 8)
|
||||
assert.equal(input.length, 2)
|
||||
})
|
||||
|
||||
test('No usable timeline leaves input untouched and produces no chapters', () => {
|
||||
for (const duration of [0, -1, Infinity, Number.NaN]) {
|
||||
const input = [{ start: 3, end: Infinity, title: 'Pending' }]
|
||||
assert.deepEqual(normalize(input, duration), [])
|
||||
assert.equal(input[0].end, Infinity)
|
||||
}
|
||||
for (const input of [undefined, null, false, {}, []])
|
||||
assert.deepEqual(normalize(input, 8), [])
|
||||
})
|
||||
|
||||
test('Invalid chapter types and times are rejected including non-finite values', () => {
|
||||
for (const input of [[null], [{ start: '0', end: 8, title: 'Bad' }], [{ start: 0, end: 8, title: 42 }]])
|
||||
assert.throws(() => normalize(input, 8), TypeError)
|
||||
for (const [start, end] of [[Number.NaN, 3], [0, Number.NaN], [-Infinity, 2], [Infinity, Infinity], [0, -Infinity], [-1, 2], [2, 2], [3, 2], [0, 9]])
|
||||
assert.throws(() => normalize([{ start, end, title: 'Bad' }], 8), { name: 'Error', message: 'Illegal chapter time point' })
|
||||
assert.throws(() => normalize([{ start: 0, end: 4, title: 'A' }, { start: 3, end: 6, title: 'B' }], 8), /Illegal chapter time point/)
|
||||
})
|
||||
Reference in new issue
Block a user