mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-26 10:55:27 +00:00
* fix(search): preview matched video chunks on hover * fix(search): show chunk covers before hover * fix(search): reuse sprites for whole-video matches
305 lines
9.3 KiB
TypeScript
305 lines
9.3 KiB
TypeScript
// @vitest-environment happy-dom
|
|
import { act, cleanup, fireEvent, render } from '@testing-library/react'
|
|
import React from 'react'
|
|
import {
|
|
mockIsIntersecting,
|
|
resetIntersectionMocking,
|
|
setupIntersectionMocking,
|
|
} from 'react-intersection-observer/test-utils'
|
|
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import { SpriteScrubber } from './sprite-scrubber'
|
|
import { FilePreview } from './file-browser/file-preview'
|
|
|
|
class MockResizeObserver {
|
|
observe = vi.fn()
|
|
unobserve = vi.fn()
|
|
disconnect = vi.fn()
|
|
}
|
|
globalThis.ResizeObserver = MockResizeObserver as unknown as typeof ResizeObserver
|
|
|
|
describe('SpriteScrubber', () => {
|
|
afterEach(() => {
|
|
cleanup()
|
|
resetIntersectionMocking()
|
|
vi.unstubAllGlobals()
|
|
})
|
|
|
|
it.each(['chunk', 'full video', 'thumbnail'])('uses the %s preview on hover', (expected) => {
|
|
setupIntersectionMocking(vi.fn)
|
|
const chunkUrl = 'https://example.com/match.webp'
|
|
const fullUrl = 'https://example.com/full.webp'
|
|
const requests: string[] = []
|
|
const image = {
|
|
naturalWidth: 500,
|
|
naturalHeight: 200,
|
|
onload: null as (() => void) | null,
|
|
onerror: null as (() => void) | null,
|
|
set src(url: string) {
|
|
requests.push(url)
|
|
},
|
|
}
|
|
vi.stubGlobal(
|
|
'Image',
|
|
class {
|
|
constructor() {
|
|
return image
|
|
}
|
|
},
|
|
)
|
|
const { getByTestId, getByAltText, queryByAltText, container } = render(
|
|
<FilePreview
|
|
item={{
|
|
preview: { proxyType: 'video', spriteUrl: fullUrl, thumbnailUrl: '/poster.webp' },
|
|
}}
|
|
matchDuration={{ startTime: 0, endTime: 7 }}
|
|
matchedSprite={{ url: chunkUrl, columns: 5, rows: 2, frameCount: 7 }}
|
|
/>,
|
|
)
|
|
const scrubber = getByTestId('sprite-scrubber')
|
|
vi.spyOn(scrubber, 'getBoundingClientRect').mockReturnValue(new DOMRect(0, 0, 100, 100))
|
|
expect(requests).toEqual([])
|
|
act(() => mockIsIntersecting(scrubber, true))
|
|
expect(requests).toEqual([chunkUrl])
|
|
|
|
if (expected === 'chunk') {
|
|
act(() => image.onload?.())
|
|
expect(queryByAltText('Thumbnail')).toBeNull()
|
|
expect(scrubber).toHaveProperty('dataset.frame', '0')
|
|
} else {
|
|
act(() => image.onerror?.())
|
|
expect(requests).toEqual([chunkUrl])
|
|
expect(getByAltText('Thumbnail').className).toContain('opacity-100')
|
|
}
|
|
|
|
fireEvent.mouseEnter(scrubber)
|
|
fireEvent.mouseMove(scrubber, { clientX: 100 })
|
|
expect(scrubber).toHaveProperty('dataset.frame', '6')
|
|
|
|
if (expected === 'chunk') {
|
|
expect(requests).toEqual([chunkUrl])
|
|
} else {
|
|
expect(requests).toEqual([chunkUrl, fullUrl])
|
|
}
|
|
|
|
if (expected === 'thumbnail') {
|
|
act(() => image.onerror?.())
|
|
expect(getByAltText('Thumbnail').className).toContain('opacity-100')
|
|
expect(container.querySelector('.animate-spin')).toBeNull()
|
|
} else if (expected === 'full video') {
|
|
act(() => image.onload?.())
|
|
expect(getByAltText('Thumbnail').className).toContain('opacity-0')
|
|
expect(scrubber).toHaveProperty('dataset.frame', '99')
|
|
}
|
|
|
|
fireEvent.mouseLeave(scrubber)
|
|
expect(scrubber).toHaveProperty('dataset.frame', '0')
|
|
if (expected === 'chunk') {
|
|
expect(queryByAltText('Thumbnail')).toBeNull()
|
|
} else {
|
|
expect(getByAltText('Thumbnail').className).toContain('opacity-100')
|
|
}
|
|
})
|
|
|
|
it('keeps the regular cover and waits for hover when a match has no chunk sprite', () => {
|
|
const load = vi.fn()
|
|
vi.stubGlobal('Image', load)
|
|
const { getByAltText } = render(
|
|
<FilePreview
|
|
item={{
|
|
preview: { proxyType: 'video', spriteUrl: '/full.webp', thumbnailUrl: '/poster.webp' },
|
|
}}
|
|
matchDuration={{ startTime: 0, endTime: 10 }}
|
|
/>,
|
|
)
|
|
|
|
expect(getByAltText('Thumbnail').getAttribute('src')).toBe('/poster.webp')
|
|
expect(load).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it.each(['video', undefined] as const)(
|
|
'uses the full sprite for a legacy video match with proxy type %s and no poster or dimensions',
|
|
(proxyType) => {
|
|
const { getByTestId } = render(
|
|
<FilePreview
|
|
item={{ preview: { proxyType, spriteUrl: '/full.webp' } }}
|
|
matchDuration={{ startTime: 0, endTime: 10 }}
|
|
/>,
|
|
)
|
|
|
|
expect(getByTestId('sprite-scrubber')).toBeTruthy()
|
|
},
|
|
)
|
|
|
|
it('scrubs forward by default when hovering from left to right', () => {
|
|
const { getByTestId } = render(
|
|
<SpriteScrubber
|
|
spriteUrl="https://example.com/sprite.webp"
|
|
thumbnailUrl="https://example.com/thumb.webp"
|
|
videoWidth={1080}
|
|
videoHeight={1080}
|
|
/>,
|
|
)
|
|
|
|
const scrubber = getByTestId('sprite-scrubber')
|
|
vi.spyOn(scrubber, 'getBoundingClientRect').mockReturnValue({
|
|
left: 0,
|
|
top: 0,
|
|
width: 100,
|
|
height: 100,
|
|
right: 100,
|
|
bottom: 100,
|
|
x: 0,
|
|
y: 0,
|
|
toJSON: () => ({}),
|
|
})
|
|
|
|
// At x = 0 (left edge): frame 0
|
|
fireEvent.mouseMove(scrubber, { clientX: 0 })
|
|
expect(scrubber.getAttribute('data-frame')).toBe('0')
|
|
|
|
// At x = 50 (middle): frame 50
|
|
fireEvent.mouseMove(scrubber, { clientX: 50 })
|
|
expect(scrubber.getAttribute('data-frame')).toBe('50')
|
|
|
|
// At x = 99 (near right edge): frame 99
|
|
fireEvent.mouseMove(scrubber, { clientX: 99 })
|
|
expect(scrubber.getAttribute('data-frame')).toBe('99')
|
|
})
|
|
|
|
it('scrubs in reverse when reverse={true} (moving right decreases frame index)', () => {
|
|
const { getByTestId } = render(
|
|
<SpriteScrubber
|
|
spriteUrl="https://example.com/sprite.webp"
|
|
thumbnailUrl="https://example.com/thumb.webp"
|
|
videoWidth={1080}
|
|
videoHeight={1080}
|
|
reverse={true}
|
|
/>,
|
|
)
|
|
|
|
const scrubber = getByTestId('sprite-scrubber')
|
|
vi.spyOn(scrubber, 'getBoundingClientRect').mockReturnValue({
|
|
left: 0,
|
|
top: 0,
|
|
width: 100,
|
|
height: 100,
|
|
right: 100,
|
|
bottom: 100,
|
|
x: 0,
|
|
y: 0,
|
|
toJSON: () => ({}),
|
|
})
|
|
|
|
// At x = 0 (left edge): frame 99
|
|
fireEvent.mouseMove(scrubber, { clientX: 0 })
|
|
expect(scrubber.getAttribute('data-frame')).toBe('99')
|
|
|
|
// At x = 25: frame 74 (rotated right / CCW)
|
|
fireEvent.mouseMove(scrubber, { clientX: 25 })
|
|
expect(scrubber.getAttribute('data-frame')).toBe('74')
|
|
|
|
// At x = 50: frame 49
|
|
fireEvent.mouseMove(scrubber, { clientX: 50 })
|
|
expect(scrubber.getAttribute('data-frame')).toBe('49')
|
|
|
|
// At x = 99 (right edge): frame 0
|
|
fireEvent.mouseMove(scrubber, { clientX: 99 })
|
|
expect(scrubber.getAttribute('data-frame')).toBe('0')
|
|
})
|
|
|
|
it('FilePreview passes reverse={true} for 3D assets and reverse={false} for video assets', () => {
|
|
const asset3D = {
|
|
proxyType: '3d' as const,
|
|
preview: {
|
|
proxyType: '3d' as const,
|
|
spriteUrl: 'https://example.com/3d-sprite.webp',
|
|
thumbnailUrl: 'https://example.com/3d-poster.webp',
|
|
originalWidth: 1080,
|
|
originalHeight: 1080,
|
|
},
|
|
}
|
|
|
|
const { getByTestId, unmount } = render(<FilePreview item={asset3D} />)
|
|
const scrubber3D = getByTestId('sprite-scrubber')
|
|
vi.spyOn(scrubber3D, 'getBoundingClientRect').mockReturnValue({
|
|
left: 0,
|
|
top: 0,
|
|
width: 100,
|
|
height: 100,
|
|
right: 100,
|
|
bottom: 100,
|
|
x: 0,
|
|
y: 0,
|
|
toJSON: () => ({}),
|
|
})
|
|
|
|
// Moving right decreases frame index for 3D assets
|
|
fireEvent.mouseMove(scrubber3D, { clientX: 25 })
|
|
expect(scrubber3D.getAttribute('data-frame')).toBe('74')
|
|
|
|
unmount()
|
|
|
|
const assetVideo = {
|
|
proxyType: 'video' as const,
|
|
preview: {
|
|
proxyType: 'video' as const,
|
|
spriteUrl: 'https://example.com/video-sprite.webp',
|
|
thumbnailUrl: 'https://example.com/video-poster.webp',
|
|
originalWidth: 1920,
|
|
originalHeight: 1080,
|
|
},
|
|
}
|
|
|
|
const { getByTestId: getVideoScrubber } = render(<FilePreview item={assetVideo} />)
|
|
const scrubberVideo = getVideoScrubber('sprite-scrubber')
|
|
vi.spyOn(scrubberVideo, 'getBoundingClientRect').mockReturnValue({
|
|
left: 0,
|
|
top: 0,
|
|
width: 100,
|
|
height: 100,
|
|
right: 100,
|
|
bottom: 100,
|
|
x: 0,
|
|
y: 0,
|
|
toJSON: () => ({}),
|
|
})
|
|
|
|
// Moving right increases frame index for video assets
|
|
fireEvent.mouseMove(scrubberVideo, { clientX: 25 })
|
|
expect(scrubberVideo.getAttribute('data-frame')).toBe('25')
|
|
})
|
|
|
|
it('renders with cursor-pointer instead of cursor-crosshair for cards', () => {
|
|
const { getByTestId } = render(
|
|
<SpriteScrubber
|
|
spriteUrl="https://example.com/sprite.webp"
|
|
thumbnailUrl="https://example.com/thumb.webp"
|
|
videoWidth={1920}
|
|
videoHeight={1080}
|
|
/>,
|
|
)
|
|
|
|
const scrubber = getByTestId('sprite-scrubber')
|
|
expect(scrubber.className).toContain('cursor-pointer')
|
|
expect(scrubber.className).not.toContain('cursor-crosshair')
|
|
})
|
|
|
|
it('renders loading spinner overlay with pointer-events-none when hovering before sprite loads', () => {
|
|
const { getByTestId, container } = render(
|
|
<SpriteScrubber
|
|
spriteUrl="https://example.com/sprite.webp"
|
|
thumbnailUrl="https://example.com/thumb.webp"
|
|
videoWidth={1920}
|
|
videoHeight={1080}
|
|
/>,
|
|
)
|
|
|
|
const scrubber = getByTestId('sprite-scrubber')
|
|
fireEvent.mouseEnter(scrubber)
|
|
|
|
const spinnerOverlay = container.querySelector('.animate-spin')?.parentElement
|
|
expect(spinnerOverlay).toBeTruthy()
|
|
expect(spinnerOverlay?.className).toContain('pointer-events-none')
|
|
})
|
|
})
|