Files
shumai/packages/webui/components/viewers/video/progress-bar.test.tsx
T
90f076f3ba fix(webui): stop duplicate seeks on the video progress bar (#532)
* fix(webui): avoid duplicate seek on progress bar tap

A single tap/click fired onSeek twice: once on pointer-down and again
on pointer-up at the same position. The second seek restarted the
native seek right as data began arriving, which stalled playback for
several seconds on slow connections. Track pointer movement and only
re-seek on release after an actual drag.

Co-Authored-By: Claude <noreply@anthropic.com>

* fix(webui): suppress synthetic mouse seek after touch on progress bar

Mobile browsers dispatch synthetic mousedown/mouseup after touch
interactions. The mouse path re-queued a seek to the same position,
which restarted the native seek ~500ms later and stalled playback on
slow connections. Ignore mouse-down within 700ms of a touch end.

Co-Authored-By: Claude <noreply@anthropic.com>

* fix(webui): reset progress bar drag state on touchcancel

OS gestures (app switch, palm rejection, notification pull) cancel
touches without a touchend. The drag/hover state then stayed stuck
until the next interaction. Reset state without seeking.

Co-Authored-By: Claude <noreply@anthropic.com>

* perf(webui): load seek preview video metadata only

The hidden 180p preview video used preload="auto", downloading the
whole file alongside main playback. On constrained connections it
competed for bandwidth exactly when a seek needed data, lengthening
rebuffering. Metadata is enough until the user actually scrubs.

Co-Authored-By: Claude <noreply@anthropic.com>

* test(webui): cover progress bar seeking on click, tap and touch cancel

Check that a plain click or tap seeks once, a real drag seeks again on release, synthetic mouse events right after a touch are ignored, a cancelled touch resets without seeking, and the hidden preview video preloads metadata only.

Co-Authored-By: Claude <noreply@anthropic.com>

* docs(changelog): note the progress bar seek fixes

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: phj1081 <5908832+phj1081@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>
2026-11-27 10:12:53 +08:00

139 lines
4.0 KiB
TypeScript

// @vitest-environment happy-dom
import { act, cleanup, fireEvent, render, screen } from '@testing-library/react'
import React from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import ProgressBar from './progress-bar'
const TOTAL_FRAMES = 1000
function renderBar(onSeek = vi.fn()) {
render(
<ProgressBar
totalFrames={TOTAL_FRAMES}
currentFrame={0}
fps={25}
previewUrl="https://example.test/preview.mp4"
onSeek={onSeek}
/>,
)
const bar = screen.getByTestId('seekbar')
vi.spyOn(bar, 'getBoundingClientRect').mockReturnValue({
left: 0,
right: 100,
top: 0,
bottom: 10,
width: 100,
height: 10,
x: 0,
y: 0,
toJSON: () => ({}),
} as DOMRect)
return { bar, onSeek }
}
function touchEvent(type: string, clientX: number, list: 'touches' | 'changedTouches') {
const event = new Event(type, { bubbles: true, cancelable: true })
Object.defineProperty(event, 'touches', {
value: list === 'touches' ? [{ clientX, clientY: 5 }] : [],
})
Object.defineProperty(event, 'changedTouches', {
value: list === 'changedTouches' ? [{ clientX, clientY: 5 }] : [],
})
return event
}
describe('ProgressBar seeking', () => {
beforeEach(() => {
vi.useFakeTimers({ toFake: ['Date'] })
vi.setSystemTime(new Date('2026-01-01T00:00:00Z'))
})
afterEach(() => {
cleanup()
vi.useRealTimers()
vi.restoreAllMocks()
})
it('seeks once for a plain click', () => {
const { bar, onSeek } = renderBar()
fireEvent.mouseDown(bar, { button: 0, clientX: 40 })
act(() => {
window.dispatchEvent(new MouseEvent('mouseup', { clientX: 40, clientY: 5 }))
})
expect(onSeek).toHaveBeenCalledTimes(1)
expect(onSeek).toHaveBeenLastCalledWith(400)
})
it('seeks again on release after a real drag', () => {
const { bar, onSeek } = renderBar()
fireEvent.mouseDown(bar, { button: 0, clientX: 40 })
act(() => {
window.dispatchEvent(new MouseEvent('mousemove', { clientX: 60 }))
window.dispatchEvent(new MouseEvent('mouseup', { clientX: 60, clientY: 5 }))
})
expect(onSeek.mock.calls.map(([frame]) => frame)).toEqual([400, 600, 600])
})
it('seeks once for a plain tap and ignores the synthetic mouse events that follow', () => {
const { bar, onSeek } = renderBar()
act(() => {
bar.dispatchEvent(touchEvent('touchstart', 30, 'touches'))
})
act(() => {
window.dispatchEvent(touchEvent('touchend', 30, 'changedTouches'))
})
vi.setSystemTime(new Date('2026-01-01T00:00:00.300Z'))
fireEvent.mouseDown(bar, { button: 0, clientX: 30 })
expect(onSeek).toHaveBeenCalledTimes(1)
expect(onSeek).toHaveBeenLastCalledWith(300)
})
it('accepts a mouse click again once the touch suppression window has passed', () => {
const { bar, onSeek } = renderBar()
act(() => {
bar.dispatchEvent(touchEvent('touchstart', 30, 'touches'))
})
act(() => {
window.dispatchEvent(touchEvent('touchend', 30, 'changedTouches'))
})
vi.setSystemTime(new Date('2026-01-01T00:00:01Z'))
fireEvent.mouseDown(bar, { button: 0, clientX: 70 })
expect(onSeek.mock.calls.map(([frame]) => frame)).toEqual([300, 700])
})
it('resets the drag without seeking when the touch is cancelled', () => {
const { bar, onSeek } = renderBar()
act(() => {
bar.dispatchEvent(touchEvent('touchstart', 30, 'touches'))
})
act(() => {
window.dispatchEvent(touchEvent('touchmove', 50, 'touches'))
})
act(() => {
window.dispatchEvent(new Event('touchcancel'))
})
act(() => {
window.dispatchEvent(touchEvent('touchmove', 80, 'touches'))
})
expect(onSeek.mock.calls.map(([frame]) => frame)).toEqual([300, 500])
})
it('loads only metadata for the hidden seek preview video', () => {
renderBar()
const preview = document.querySelector('video.hidden')
expect(preview).not.toBeNull()
expect(preview?.getAttribute('preload')).toBe('metadata')
})
})