File size: 5,776 Bytes
891fa77
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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()
  })
})