import { describe, expect, it } from 'vitest';

import fixWarp from './fixWarp';

describe('fixWarp', () => {
  it('should return default warp settings for invalid input', () => {
    const result = fixWarp(null);

    expect(result.awaitingAnalysis).toBe(false);
    expect(result.speed).toBe(1.0);
    expect(result.enabled).toBe(true);
    expect(result.markers).toEqual({});
  });

  it('should fix a valid warp object in place', () => {
    const input = {
      awaitingAnalysis: true,
      speed: 2.0,
      enabled: false,
      markers: { 0: 0, 1: 1 },
    };

    const result = fixWarp(input);

    expect(result).toBe(input); // Referential equality
    expect(result.awaitingAnalysis).toBe(true);
    expect(result.speed).toBe(2.0);
    expect(result.enabled).toBe(false);
    expect(result.markers).toEqual({ 0: 0, 1: 1 });
  });

  it('should clamp speed to valid range', () => {
    const tooFast = fixWarp({ speed: 200 });
    expect(tooFast.speed).toBe(128);

    const tooSlow = fixWarp({ speed: 0.001 });
    expect(tooSlow.speed).toBe(1 / 128);
  });

  it('should remove invalid marker entries', () => {
    const input = {
      markers: {
        0: 0,
        1: 1,
        invalidKey: 2, // String key should be removed if not a valid number
        2: Infinity, // Invalid value should be removed
        3: NaN, // Invalid value should be removed
      },
    };

    const result = fixWarp(input);

    expect(result.markers).toEqual({ 0: 0, 1: 1 });
    expect(result.markers).toBe(input.markers); // Referential stability
  });
});
