import { render, screen } from '@testing-library/react';
import { afterEach, expect, test, vi } from 'vitest';
import { VehicleMap } from './VehicleMap';

class TestMap {
  add = vi.fn();
  addControl = vi.fn();
  setFitView = vi.fn();
  destroy = vi.fn();

  constructor(
    public container: HTMLDivElement,
    public options: Record<string, unknown>
  ) {}
}

class TestOverlay {
  setMap = vi.fn();
  on = vi.fn();
}

class TestScale {}

afterEach(() => {
  delete window.__LINGNIU_APP_CONFIG__;
  delete window.AMapLoader;
  vi.restoreAllMocks();
});

test('loads AMap base map even when realtime vehicle points are empty', async () => {
  const mapLoad = vi.fn().mockResolvedValue({
    Map: TestMap,
    Marker: TestOverlay,
    Polyline: TestOverlay,
    Scale: TestScale
  });
  window.__LINGNIU_APP_CONFIG__ = { amapWebJsKey: 'amap-web-key' };
  window.AMapLoader = { load: mapLoad };

  render(<VehicleMap points={[]} fallbackLabel="等待真实车辆坐标" />);

  expect(await screen.findByText('高德地图已加载')).toBeInTheDocument();
  expect(screen.queryByText('等待真实车辆坐标')).not.toBeInTheDocument();
  expect(mapLoad).toHaveBeenCalledWith({
    key: 'amap-web-key',
    version: '2.0',
    plugins: ['AMap.Scale']
  });
});
