import {
  classifySegmentKind,
  decodeGoogleEncodedPolyline,
  downsamplePath,
  pathLengthMeters,
  roundKm,
} from './map-matching.util';

describe('map-matching.util', () => {
  it('downsamples dense points', () => {
    const pts = Array.from({ length: 20 }, (_, i) => ({
      lat: 21.1 + i * 0.00005,
      lng: 72.8,
    }));
    const down = downsamplePath(pts, 20);
    expect(down.length).toBeLessThan(pts.length);
    expect(down[0]).toEqual(pts[0]);
    expect(down[down.length - 1]).toEqual(pts[pts.length - 1]);
  });

  it('classifies segment kinds', () => {
    expect(classifySegmentKind({ meanAccuracy: 10 })).toBe('gps_verified');
    expect(classifySegmentKind({ meanAccuracy: 45 })).toBe('map_matched');
    expect(classifySegmentKind({ gapSeconds: 120 })).toBe('estimated');
  });

  it('decodes a short Google encoded polyline', () => {
    // Encoded polyline for a tiny path near (38.5, -120.2)
    const pts = decodeGoogleEncodedPolyline('_p~iF~ps|U_ulLnnqC_mqNvxq`@');
    expect(pts.length).toBeGreaterThanOrEqual(2);
    expect(pts[0].lat).toBeCloseTo(38.5, 1);
  });

  it('rounds km from meters', () => {
    expect(roundKm(12400)).toBe(12.4);
    expect(
      pathLengthMeters([
        { lat: 0, lng: 0 },
        { lat: 0, lng: 0 },
      ]),
    ).toBe(0);
  });
});
