import { describe, expect, it } from 'vitest';
import { parseVehicleProfileSyncCSV, vehicleProfileSyncCSVHeader } from './profileSync';

describe('parseVehicleProfileSyncCSV', () => {
  it('parses quoted values, BOM, CRLF and normalizes VIN/status', () => {
    const rows = parseVehicleProfileSyncCSV(`\uFEFF${vehicleProfileSyncCSVHeader}\r\nvin001,"车型,一",重卡,示范物流,ACTIVE,车厂平台,2026-07-01T08:30:00+08:00,3600\r\n`);
    expect(rows).toEqual([expect.objectContaining({ vin: 'VIN001', modelName: '车型,一', operationStatus: 'active', runtimeSeconds: 3600 })]);
  });

  it('rejects duplicate VINs and malformed source rows before upload', () => {
    const duplicate = `${vehicleProfileSyncCSVHeader}\nVIN001,,,,unknown,,,\nvin001,,,,unknown,,,`;
    expect(() => parseVehicleProfileSyncCSV(duplicate)).toThrow(/VIN 重复/);
    expect(() => parseVehicleProfileSyncCSV(`${vehicleProfileSyncCSVHeader}\nVIN001,,,,active,,,1.5`)).toThrow(/累计运行秒数/);
    expect(() => parseVehicleProfileSyncCSV('vin,modelName\nVIN001,车型')).toThrow(/表头/);
    expect(() => parseVehicleProfileSyncCSV(`${vehicleProfileSyncCSVHeader}\nVIN001,"车型"x,,,active,,,`)).toThrow(/引号结束/);
  });
});
