Download code/frontend/apps/web/src/components/timeline-panel.test.tsx from Jiali9/Affect-Atlas: direct link, hf CLI and curl.
- Browser
- Download file 5.78 kB
-
https://huggingface.co/Jiali9/Affect-Atlas/resolve/main/code/frontend/apps/web/src/components/timeline-panel.test.tsx
- Command line
-
hf download hf://Jiali9/Affect-Atlas/code/frontend/apps/web/src/components/timeline-panel.test.tsx
-
curl -L -o timeline-panel.test.tsx https://huggingface.co/Jiali9/Affect-Atlas/resolve/main/code/frontend/apps/web/src/components/timeline-panel.test.tsx
5.78 kB
| import { act, cleanup, fireEvent, render, screen } from '@testing-library/react' | |
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' | |
| import i18n from '@/i18n' | |
| import { TimelinePanel } from './timeline-panel' | |
| import { predictionFixture } from '@/lib/timeline.fixtures' | |
| import { useTimelinePlayer } from '@/lib/use-timeline-player' | |
| import type { Prediction } from '@/lib/api' | |
| beforeEach(async () => { await i18n.changeLanguage('zh') }) | |
| afterEach(() => { cleanup(); vi.restoreAllMocks() }) | |
| function Harness({ result = predictionFixture }: { result?: Prediction | null }) { | |
| const player = useTimelinePlayer() | |
| return <><audio ref={player.audioRef} onTimeUpdate={player.onTimeUpdate} onSeeked={player.onTimeUpdate} aria-label="test player"/><button onClick={player.reset}>Reset file</button><TimelinePanel result={result} currentTime={player.currentTime} selectedIndex={player.selectedIndex} onSelect={player.selectWindow}/></> | |
| } | |
| describe('window selection and local playback', () => { | |
| it('shows the complete window note in the selected language without changing the raw result', async () => { | |
| const disclaimer = '探索性窗口评分:静态整体评分模型作用于固定10秒窗口、5秒步长的时间特征;复用整段MERT上下文,窗口短于45秒训练片段,局部准确性尚未验证;非逐秒情绪真值或已验证动态模型。曲线未平滑、未校正。' | |
| const result = { ...predictionFixture, timeline: { ...predictionFixture.timeline!, disclaimer } } | |
| const { container } = render(<Harness result={result}/>) | |
| expect(screen.getByText('滑动窗口评分')).toBeTruthy() | |
| expect(screen.getByText(/局部准确性尚未验证/)).toBeTruthy() | |
| await act(async () => { await i18n.changeLanguage('en') }) | |
| expect(screen.getByText('Sliding Window Scoring')).toBeTruthy() | |
| expect(screen.getByText(/accuracy on individual windows has not been validated/)).toBeTruthy() | |
| expect(container.querySelector('.timeline-panel')?.textContent).not.toMatch(/\p{Script=Han}/u) | |
| expect(result.timeline.disclaimer).toBe(disclaimer) | |
| }) | |
| it('selects a button window, seeks to start without calling play and displays raw details', () => { | |
| const play = vi.spyOn(HTMLMediaElement.prototype, 'play').mockResolvedValue() | |
| const { container } = render(<Harness/>) | |
| const buttons = screen.getAllByRole('button', { name: /窗口/ }) | |
| fireEvent.click(buttons[1]) | |
| expect(buttons[1].getAttribute('aria-pressed')).toBe('true') | |
| expect(container.querySelector('audio')?.currentTime).toBe(5) | |
| expect(play).not.toHaveBeenCalled() | |
| expect(screen.getByText('已选窗口 2')).toBeTruthy() | |
| expect(screen.getByTitle('模型原始值:-0.00001234')).toBeTruthy() | |
| expect(screen.getByText('相对整体 -0.1235')).toBeTruthy() | |
| }) | |
| it('synchronizes playhead and overlap highlight while retaining pause position', () => { | |
| const { container } = render(<Harness/>) | |
| const audio = container.querySelector('audio')! | |
| const initialX = screen.getByTestId('playhead').getAttribute('x1') | |
| audio.currentTime = 11.9 | |
| fireEvent.timeUpdate(audio) | |
| const movedX = screen.getByTestId('playhead').getAttribute('x1') | |
| expect(movedX).not.toBe(initialX) | |
| expect(screen.getAllByRole('button', { name: /窗口/ })[2].className).toContain('playing-window') | |
| fireEvent.pause(audio) | |
| expect(screen.getByTestId('playhead').getAttribute('x1')).toBe(movedX) | |
| expect(screen.getByText('播放位置 11.90 s')).toBeTruthy() | |
| }) | |
| it('selects an actual SVG point and resets selection and playback for a cleared file', () => { | |
| const pause = vi.spyOn(HTMLMediaElement.prototype, 'pause').mockImplementation(() => {}) | |
| const { container } = render(<Harness/>) | |
| fireEvent.click(container.querySelectorAll('.curve-point')[2]) | |
| expect(container.querySelector('audio')?.currentTime).toBe(7) | |
| expect(screen.getByText('已选窗口 3')).toBeTruthy() | |
| fireEvent.click(screen.getByText('Reset file')) | |
| expect(pause).toHaveBeenCalledOnce() | |
| expect(container.querySelector('audio')?.currentTime).toBe(0) | |
| expect(screen.queryByText('已选窗口 3')).toBeNull() | |
| expect(screen.getAllByRole('button', { name: /窗口/ }).every(button => button.getAttribute('aria-pressed') === 'false')).toBe(true) | |
| }) | |
| it('shows one raw point per score without interpolating a short clip', () => { | |
| const point = { ...predictionFixture.timeline!.points[0], start_seconds: 0, end_seconds: 3, center_seconds: 1.5 } | |
| const { container } = render(<Harness result={{ ...predictionFixture, duration_seconds: 3, timeline: { ...predictionFixture.timeline!, points: [point] } }}/>) | |
| expect(container.querySelector('polyline')).toBeNull() | |
| expect(container.querySelectorAll('.curve-point')).toHaveLength(2) | |
| expect(screen.getByText(/仅一个整段窗口/)).toBeTruthy() | |
| fireEvent.click(screen.getByRole('button', { name: /窗口 1/ })) | |
| expect(screen.getByTitle('模型原始值:1.23456789')).toBeTruthy() | |
| }) | |
| it('keeps legacy and empty tasks free of fabricated points and disables CSV', () => { | |
| const legacy = { ...predictionFixture } | |
| delete legacy.timeline | |
| const { container, rerender } = render(<Harness result={legacy}/>) | |
| expect(screen.getByText(/此旧任务只有整体结果/)).toBeTruthy() | |
| expect(container.querySelector('.timeline-chart')).toBeNull() | |
| expect(screen.getByRole('button', { name: '导出时间窗 CSV' }).hasAttribute('disabled')).toBe(true) | |
| expect(screen.getByRole('button', { name: '导出完整 JSON' }).hasAttribute('disabled')).toBe(false) | |
| rerender(<Harness result={null}/>) | |
| expect(screen.getByText(/当前没有曲线数据/)).toBeTruthy() | |
| expect(screen.queryByTestId('playhead')).toBeNull() | |
| }) | |
| }) | |