import { describe, expect, test } from 'vitest';
import { rawCoordinateAddressEvidence, rawCoordinateEvidence } from './rawEvidence';

describe('rawCoordinateEvidence', () => {
  test('converts Yutong MQTT total_mileage meters to kilometers for RAW map evidence', () => {
    const evidence = rawCoordinateEvidence('YUTONG_MQTT', {
      'yutong_mqtt.data.longitude': '121.075044',
      'yutong_mqtt.data.latitude': '30.590921',
      'yutong_mqtt.data.meter_speed': '27',
      'yutong_mqtt.data.total_mileage': '119925000'
    });

    expect(evidence).toMatchObject({
      longitude: 121.075044,
      latitude: 30.590921,
      speedKmh: 27,
      totalMileageKm: 119925,
      totalMileageKey: 'yutong_mqtt.data.total_mileage',
      totalMileageUnitEvidence: 'm->km'
    });
  });

  test('keeps GB32960 kilometer mileage unchanged', () => {
    const evidence = rawCoordinateEvidence('GB32960', {
      'gb32960.position.longitude': '114.12552',
      'gb32960.position.latitude': '30.452935',
      'gb32960.vehicle.speed_kmh': '91.5',
      'gb32960.vehicle.total_mileage_km': '35370.2'
    });

    expect(evidence).toMatchObject({
      longitude: 114.12552,
      latitude: 30.452935,
      speedKmh: 91.5,
      totalMileageKm: 35370.2,
      totalMileageKey: 'gb32960.vehicle.total_mileage_km',
      totalMileageUnitEvidence: 'km'
    });
  });

  test('rejects out-of-business-range coordinates', () => {
    expect(rawCoordinateEvidence('JT808', {
      'jt808.location.longitude': '0',
      'jt808.location.latitude': '0',
      'jt808.location.total_mileage_km': '10'
    })).toBeUndefined();
  });

  test('summarizes reverse geocode state for RAW coordinate evidence', () => {
    const coordinate = rawCoordinateEvidence('JT808', {
      'jt808.location.longitude': '121.4737',
      'jt808.location.latitude': '31.2304',
      'jt808.location.speed_kmh': '32'
    });

    expect(rawCoordinateAddressEvidence(coordinate, {
      status: 'ready',
      provider: 'AMap Web JS',
      address: '上海市黄浦区人民大道',
      region: '上海市/黄浦区'
    })).toEqual({
      status: 'ready',
      label: 'AMap Web JS 已解析',
      detail: '上海市黄浦区人民大道',
      evidence: '上海市/黄浦区',
      tone: 'ready'
    });

    expect(rawCoordinateAddressEvidence(undefined, { status: 'disabled' })).toMatchObject({
      status: 'unavailable',
      label: '无坐标',
      tone: 'waiting'
    });
  });
});
