import {
  describe,
  expect,
  it
} from 'vitest';
import {
  MEDIA_SEEK_SLIDER_STEPS,
  mediaSeekSliderValue,
  mediaSeekTimeFromSliderValue
} from './chat-video-player-seek.rules';

describe('chat-video-player seek slider rules', () => {
  it('maps playback time to a fixed high-resolution slider range', () => {
    expect(mediaSeekSliderValue(30, 120)).toBe(2500);
    expect(mediaSeekSliderValue(60, 120)).toBe(5000);
    expect(mediaSeekSliderValue(120, 120)).toBe(MEDIA_SEEK_SLIDER_STEPS);
  });

  it('returns zero when duration is unknown', () => {
    expect(mediaSeekSliderValue(12, 0)).toBe(0);
    expect(mediaSeekSliderValue(12, Number.NaN)).toBe(0);
  });

  it('converts slider values back to playback time', () => {
    expect(mediaSeekTimeFromSliderValue(2500, 120)).toBe(30);
    expect(mediaSeekTimeFromSliderValue(MEDIA_SEEK_SLIDER_STEPS, 120)).toBe(120);
  });

  it('round-trips sub-second positions without snapping to whole seconds', () => {
    const duration = 367.891;
    const currentTime = 123.456;
    const sliderValue = mediaSeekSliderValue(currentTime, duration);
    const restoredTime = mediaSeekTimeFromSliderValue(sliderValue, duration);

    expect(Math.abs(restoredTime - currentTime)).toBeLessThan(0.05);
  });
});
