import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'; import { afterEach, expect, test, vi } from 'vitest'; import App from '../App'; afterEach(() => { cleanup(); window.history.replaceState(null, '', '/'); vi.restoreAllMocks(); }); test('renders vehicle platform shell', () => { render(); expect(screen.getByText('车辆服务中台')).toBeInTheDocument(); expect(screen.getAllByText('总览工作台').length).toBeGreaterThanOrEqual(1); }); test('shows vehicle service status distribution on dashboard', async () => { window.history.replaceState(null, '', '/#/dashboard'); vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => { const path = String(input); if (path.includes('/api/dashboard/summary')) { return { ok: true, json: async () => ({ data: { onlineVehicles: 3, activeToday: 4, frameToday: 1286320, issueVehicles: 7, kafkaLag: 0, protocols: [], serviceStatuses: [ { status: 'healthy', title: '服务正常', count: 12 }, { status: 'degraded', title: '部分来源离线', count: 39 } ], linkHealth: [] }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } return { ok: true, json: async () => ({ data: { items: [], total: 0, limit: 10, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; }); render(); expect(await screen.findByText('车辆服务状态')).toBeInTheDocument(); expect(screen.getByText('部分来源离线')).toBeInTheDocument(); expect(screen.getByText('39')).toBeInTheDocument(); }); test('shows resolved vehicle service status after topbar search', async () => { window.history.replaceState(null, '', '/#/dashboard'); const fetchMock = vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => { const path = String(input); if (path.includes('/api/ops/health')) { return { ok: true, json: async () => ({ data: { linkHealth: [], kafkaLag: 0, redisOnlineKeys: 0, tdengineWritable: true, mysqlWritable: true, runtime: { requestTimeoutMs: 5000 } }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } if (path.includes('/api/dashboard/summary')) { return { ok: true, json: async () => ({ data: { onlineVehicles: 3, activeToday: 4, frameToday: 1286320, issueVehicles: 7, kafkaLag: 0, protocols: [], serviceStatuses: [], linkHealth: [] }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } if (path.includes('/api/vehicle-service/overview')) { return { ok: true, json: async () => ({ data: { vin: 'LB9A32A24R0LS1426', plate: '粤AG18312', phone: '13307795425', oem: 'G7s', protocols: ['GB32960', 'JT808'], primaryProtocol: 'JT808', coverageStatus: 'partial', sourceCount: 2, onlineSourceCount: 1, lastSeen: '2026-07-03 20:12:10', realtimeCount: 2, historyCount: 0, rawCount: 0, mileageCount: 0, qualityIssueCount: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } if (path.includes('/api/vehicle-service')) { return { ok: true, json: async () => ({ data: { vin: 'LB9A32A24R0LS1426', lookupKey: 'LB9A32A24R0LS1426', lookupResolved: true, sources: ['GB32960', 'JT808'], sourceStatus: [], realtime: [], history: { items: [], total: 0, limit: 20, offset: 0 }, raw: { items: [], total: 0, limit: 10, offset: 0 }, mileage: { items: [], total: 0, limit: 20, offset: 0 }, quality: { items: [], total: 0, limit: 20, offset: 0 } }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } return { ok: true, json: async () => ({ data: { items: [], total: 0, limit: 10, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; }); render(); fireEvent.change(screen.getByPlaceholderText('搜索 VIN / 车牌 / 手机号'), { target: { value: '粤AG18312' } }); fireEvent.click(screen.getByRole('button', { name: '查询车辆' })); expect(await screen.findByText('当前车辆:部分来源离线')).toBeInTheDocument(); expect(screen.getByText('粤AG18312 / LB9A32A24R0LS1426')).toBeInTheDocument(); expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining('/api/vehicle-service/overview?keyword=%E7%B2%A4AG18312'), undefined); expect(fetchMock).not.toHaveBeenCalledWith(expect.stringContaining('/api/vehicles/resolve'), undefined); }); test('shows row service status in dashboard vehicle previews', async () => { window.history.replaceState(null, '', '/#/dashboard'); vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => { const path = String(input); if (path.includes('/api/dashboard/summary')) { return { ok: true, json: async () => ({ data: { onlineVehicles: 3, activeToday: 4, frameToday: 1286320, issueVehicles: 7, kafkaLag: 0, protocols: [], serviceStatuses: [], linkHealth: [] }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } if (path.includes('/api/vehicles/coverage')) { return { ok: true, json: async () => ({ data: { items: [{ vin: 'VIN-COVERAGE-WARN', plate: '粤A预警1', phone: '', oem: 'G7s', protocols: ['GB32960', 'JT808'], sourceCount: 2, onlineSourceCount: 1, online: true, lastSeen: '2026-07-03 20:12:10', bindingStatus: 'bound', serviceStatus: { status: 'degraded', severity: 'warning', title: '覆盖部分离线', detail: '覆盖预览由 API 判定', sourceCount: 2, onlineSourceCount: 1 } }], total: 1, limit: 8, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } if (path.includes('/api/realtime/vehicles')) { return { ok: true, json: async () => ({ data: { items: [{ vin: 'VIN-REALTIME-OFFLINE', plate: '粤A离线1', phone: '', oem: 'G7s', protocols: ['JT808'], sourceCount: 1, onlineSourceCount: 0, online: false, bindingStatus: 'bound', serviceStatus: { status: 'offline', severity: 'error', title: '实时车辆离线', detail: '实时预览由 API 判定', sourceCount: 1, onlineSourceCount: 0 }, primaryProtocol: 'JT808', longitude: 113.2, latitude: 23.1, speedKmh: 0, socPercent: 0, totalMileageKm: 100, lastSeen: '2026-07-03 20:12:10' }], total: 1, limit: 8, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } return { ok: true, json: async () => ({ data: { items: [], total: 0, limit: 10, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; }); render(); expect(await screen.findByText('VIN-COVERAGE-WARN')).toBeInTheDocument(); expect(screen.getByText('覆盖部分离线')).toBeInTheDocument(); expect(screen.getByText('VIN-REALTIME-OFFLINE')).toBeInTheDocument(); expect(screen.getByText('实时车辆离线')).toBeInTheDocument(); }); test('filters dashboard coverage by service status', async () => { window.history.replaceState(null, '', '/#/dashboard'); const fetchMock = vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => { const path = String(input); if (path.includes('/api/dashboard/summary')) { return { ok: true, json: async () => ({ data: { onlineVehicles: 3, activeToday: 4, frameToday: 1286320, issueVehicles: 7, kafkaLag: 0, protocols: [], serviceStatuses: [], linkHealth: [] }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } return { ok: true, json: async () => ({ data: { items: [], total: 0, limit: 8, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; }); render(); expect(await screen.findByText('车辆服务覆盖')).toBeInTheDocument(); fireEvent.click(screen.getByTestId('dashboard-service-status-filter')); fireEvent.click(await screen.findByText('部分来源离线')); fireEvent.click(screen.getByRole('button', { name: '筛选' })); await waitFor(() => { expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining('serviceStatus=degraded'), undefined); }); expect(screen.getByText('当前筛选:部分来源离线')).toBeInTheDocument(); }); test('opens dashboard coverage from service status distribution', async () => { window.history.replaceState(null, '', '/#/dashboard'); const fetchMock = vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => { const path = String(input); if (path.includes('/api/dashboard/summary')) { return { ok: true, json: async () => ({ data: { onlineVehicles: 3, activeToday: 4, frameToday: 1286320, issueVehicles: 7, kafkaLag: 0, protocols: [], serviceStatuses: [ { status: 'degraded', title: '部分来源离线', count: 39 } ], linkHealth: [] }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } return { ok: true, json: async () => ({ data: { items: [], total: 0, limit: 8, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; }); render(); fireEvent.click(await screen.findByRole('button', { name: '查看部分来源离线' })); await waitFor(() => { expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining('serviceStatus=degraded'), undefined); }); }); test('opens vehicle detail from shareable hash', async () => { window.history.replaceState(null, '', '/#/detail?keyword=%E7%B2%A4AG18312'); vi.spyOn(globalThis, 'fetch').mockResolvedValue({ ok: true, json: async () => ({ data: { vin: 'LB9A32A24R0LS1426', lookupKey: '粤AG18312', lookupResolved: true, resolution: { lookupKey: '粤AG18312', resolved: true, vin: 'LB9A32A24R0LS1426', plate: '粤AG18312', phone: '13307795425', oem: 'G7s', protocols: ['GB32960', 'JT808'], online: true, lastSeen: '2026-07-03 20:12:10' }, sources: ['GB32960', 'JT808'], sourceStatus: [], realtime: [], history: { items: [], total: 0, limit: 20, offset: 0 }, raw: { items: [], total: 0, limit: 10, offset: 0 }, mileage: { items: [], total: 0, limit: 20, offset: 0 }, quality: { items: [], total: 0, limit: 20, offset: 0 } }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response); render(); expect(await screen.findByDisplayValue('粤AG18312')).toBeInTheDocument(); expect(screen.getAllByText('车辆服务').length).toBeGreaterThanOrEqual(1); }); test('shows vehicle context when opening detail from shareable hash', async () => { window.history.replaceState(null, '', '/#/detail?keyword=VIN001'); vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => { const path = String(input); if (path.includes('/api/vehicle-service/overview')) { return { ok: true, json: async () => ({ data: { vin: 'VIN001', plate: '粤AG18312', phone: '13307795425', oem: 'G7s', protocols: ['GB32960', 'JT808'], primaryProtocol: 'JT808', coverageStatus: 'partial', sourceCount: 2, onlineSourceCount: 1, lastSeen: '2026-07-03 20:12:10', realtimeCount: 2, historyCount: 0, rawCount: 0, mileageCount: 0, qualityIssueCount: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } if (path.includes('/api/vehicle-service')) { return { ok: true, json: async () => ({ data: { vin: 'VIN001', lookupKey: 'VIN001', lookupResolved: true, sources: ['GB32960', 'JT808'], sourceStatus: [], realtime: [], history: { items: [], total: 0, limit: 20, offset: 0 }, raw: { items: [], total: 0, limit: 10, offset: 0 }, mileage: { items: [], total: 0, limit: 20, offset: 0 }, quality: { items: [], total: 0, limit: 20, offset: 0 } }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } return { ok: true, json: async () => ({ data: path.includes('/api/ops/health') ? { linkHealth: [], kafkaLag: 0, redisOnlineKeys: 0, tdengineWritable: true, mysqlWritable: true, runtime: { requestTimeoutMs: 5000 } } : { items: [], total: 0, limit: 10, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; }); render(); expect(await screen.findByText('粤AG18312 / VIN001')).toBeInTheDocument(); expect(screen.getByText('当前车辆:部分来源离线')).toBeInTheDocument(); }); test('refreshes vehicle context when hash changes to another detail vehicle', async () => { window.history.replaceState(null, '', '/#/dashboard'); vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => { const path = String(input); if (path.includes('/api/vehicle-service/overview')) { return { ok: true, json: async () => ({ data: { vin: 'VIN002', plate: '粤B20002', phone: '', oem: 'G7s', protocols: ['JT808'], primaryProtocol: 'JT808', coverageStatus: 'offline', sourceCount: 1, onlineSourceCount: 0, lastSeen: '2026-07-03 20:10:10', realtimeCount: 1, historyCount: 0, rawCount: 0, mileageCount: 0, qualityIssueCount: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } if (path.includes('/api/dashboard/summary')) { return { ok: true, json: async () => ({ data: { onlineVehicles: 3, activeToday: 4, frameToday: 1286320, issueVehicles: 7, kafkaLag: 0, protocols: [], serviceStatuses: [], linkHealth: [] }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } if (path.includes('/api/vehicle-service')) { return { ok: true, json: async () => ({ data: { vin: 'VIN002', lookupKey: 'VIN002', lookupResolved: true, sources: ['JT808'], sourceStatus: [], realtime: [], history: { items: [], total: 0, limit: 20, offset: 0 }, raw: { items: [], total: 0, limit: 10, offset: 0 }, mileage: { items: [], total: 0, limit: 20, offset: 0 }, quality: { items: [], total: 0, limit: 20, offset: 0 } }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } return { ok: true, json: async () => ({ data: path.includes('/api/ops/health') ? { linkHealth: [], kafkaLag: 0, redisOnlineKeys: 0, tdengineWritable: true, mysqlWritable: true, runtime: { requestTimeoutMs: 5000 } } : { items: [], total: 0, limit: 10, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; }); render(); window.history.pushState(null, '', '/#/detail?keyword=VIN002'); window.dispatchEvent(new HashChangeEvent('hashchange')); expect(await screen.findByText('粤B20002 / VIN002')).toBeInTheDocument(); expect(screen.getByText('当前车辆:车辆离线')).toBeInTheDocument(); }); test('shows vehicle context when opening detail from sidebar navigation', async () => { window.history.replaceState(null, '', '/#/dashboard'); vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => { const path = String(input); if (path.includes('/api/vehicle-service/overview')) { return { ok: true, json: async () => ({ data: { vin: 'LB9A32A24R0LS1426', plate: '粤AGQ8398', phone: '', oem: 'G7s', protocols: ['JT808'], primaryProtocol: 'JT808', coverageStatus: 'offline', sourceCount: 1, onlineSourceCount: 0, lastSeen: '2026-07-03 20:10:10', realtimeCount: 1, historyCount: 0, rawCount: 0, mileageCount: 0, qualityIssueCount: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } if (path.includes('/api/dashboard/summary')) { return { ok: true, json: async () => ({ data: { onlineVehicles: 3, activeToday: 4, frameToday: 1286320, issueVehicles: 7, kafkaLag: 0, protocols: [], serviceStatuses: [], linkHealth: [] }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } if (path.includes('/api/vehicle-service')) { return { ok: true, json: async () => ({ data: { vin: 'LB9A32A24R0LS1426', lookupKey: 'LB9A32A24R0LS1426', lookupResolved: true, sources: ['JT808'], sourceStatus: [], realtime: [], history: { items: [], total: 0, limit: 20, offset: 0 }, raw: { items: [], total: 0, limit: 10, offset: 0 }, mileage: { items: [], total: 0, limit: 20, offset: 0 }, quality: { items: [], total: 0, limit: 20, offset: 0 } }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } return { ok: true, json: async () => ({ data: path.includes('/api/ops/health') ? { linkHealth: [], kafkaLag: 0, redisOnlineKeys: 0, tdengineWritable: true, mysqlWritable: true, runtime: { requestTimeoutMs: 5000 } } : { items: [], total: 0, limit: 10, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; }); render(); fireEvent.click(screen.getAllByText('车辆详情')[0]); expect(await screen.findByText('粤AGQ8398 / LB9A32A24R0LS1426')).toBeInTheDocument(); expect(screen.getByText('当前车辆:车辆离线')).toBeInTheDocument(); }); test('applies protocol from shareable history hash to API requests', async () => { window.history.replaceState(null, '', '/#/history?keyword=%E7%B2%A4AG18312&protocol=JT808'); const fetchMock = vi.spyOn(globalThis, 'fetch').mockImplementation(async (input, init) => { const path = String(input); if (path.includes('/api/ops/health')) { return { ok: true, json: async () => ({ data: { linkHealth: [], kafkaLag: 0, redisOnlineKeys: 0, tdengineWritable: true, mysqlWritable: true, runtime: { requestTimeoutMs: 5000 } }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } return { ok: true, json: async () => ({ data: { items: [], total: 0, limit: 10, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; }); render(); await waitFor(() => { expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining('/api/history/locations?'), undefined); }); expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining('protocol=JT808'), undefined); expect(fetchMock).toHaveBeenCalledWith('/api/history/raw-frames/query', expect.objectContaining({ method: 'POST', body: expect.stringContaining('"protocol":"JT808"') })); }); test('keeps row protocol when opening vehicle service from history results', async () => { window.history.replaceState(null, '', '/#/history?keyword=%E7%B2%A4AG18312&protocol=JT808'); vi.spyOn(globalThis, 'fetch').mockImplementation(async (input, init) => { const path = String(input); if (path.includes('/api/ops/health')) { return { ok: true, json: async () => ({ data: { linkHealth: [], kafkaLag: 0, redisOnlineKeys: 0, tdengineWritable: true, mysqlWritable: true, runtime: { requestTimeoutMs: 5000 } }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } if (path.includes('/api/history/locations')) { return { ok: true, json: async () => ({ data: { items: [{ vin: 'VIN-JT808-001', plate: '粤AG18312', protocol: 'JT808', longitude: 113.2, latitude: 23.1, speedKmh: 30, socPercent: 0, totalMileageKm: 100, lastSeen: '2026-07-03 20:12:10', deviceTime: '2026-07-03 20:12:10', serverTime: '2026-07-03 20:12:11' }], total: 1, limit: 10, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } if (path.includes('/api/vehicles/resolve')) { return { ok: true, json: async () => ({ data: { lookupKey: 'VIN-JT808-001', resolved: true, vin: 'VIN-JT808-001', plate: '粤AG18312', phone: '13307795425', oem: 'G7s', protocols: ['JT808'], online: true, lastSeen: '2026-07-03 20:12:10' }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } return { ok: true, json: async () => ({ data: { items: [], total: 0, limit: 10, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; }); render(); expect(await screen.findByText('VIN-JT808-001')).toBeInTheDocument(); fireEvent.click(screen.getAllByRole('button', { name: '车辆服务' })[0]); await waitFor(() => { expect(window.location.hash).toBe('#/detail?keyword=VIN-JT808-001&protocol=JT808'); }); }); test('keeps primary protocol when opening vehicle service from realtime vehicles', async () => { window.history.replaceState(null, '', '/#/realtime'); vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => { const path = String(input); if (path.includes('/api/realtime/vehicles')) { return { ok: true, json: async () => ({ data: { items: [{ vin: 'VIN-MQTT-001', plate: '川AHTWO1', phone: '', oem: '宇通', protocols: ['GB32960', 'YUTONG_MQTT'], sourceCount: 2, onlineSourceCount: 1, online: true, primaryProtocol: 'YUTONG_MQTT', longitude: 113.2, latitude: 23.1, speedKmh: 30, socPercent: 78, totalMileageKm: 119925, lastSeen: '2026-07-03 20:12:10' }], total: 1, limit: 50, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } if (path.includes('/api/vehicles/resolve')) { return { ok: true, json: async () => ({ data: { lookupKey: 'VIN-MQTT-001', resolved: true, vin: 'VIN-MQTT-001', plate: '川AHTWO1', phone: '', oem: '宇通', protocols: ['GB32960', 'YUTONG_MQTT'], online: true, lastSeen: '2026-07-03 20:12:10' }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } return { ok: true, json: async () => ({ data: { items: [], total: 0, limit: 10, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; }); render(); expect(await screen.findByText('VIN-MQTT-001')).toBeInTheDocument(); fireEvent.click(screen.getByRole('button', { name: '车辆服务' })); await waitFor(() => { expect(window.location.hash).toBe('#/detail?keyword=VIN-MQTT-001&protocol=YUTONG_MQTT'); }); }); test('shows canonical service status in realtime vehicles', async () => { window.history.replaceState(null, '', '/#/realtime'); vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => { const path = String(input); if (path.includes('/api/realtime/vehicles')) { return { ok: true, json: async () => ({ data: { items: [{ vin: 'VIN-RT-DEGRADED', plate: '粤ART001', phone: '', oem: 'G7s', protocols: ['GB32960'], sourceCount: 1, onlineSourceCount: 1, online: true, primaryProtocol: 'GB32960', longitude: 113.2, latitude: 23.1, speedKmh: 30, socPercent: 78, totalMileageKm: 119925, lastSeen: '2026-07-03 20:12:10', bindingStatus: 'bound', serviceStatus: { status: 'degraded', severity: 'warning', title: '部分来源离线', detail: '由实时车辆 API 统一判定', sourceCount: 2, onlineSourceCount: 1 } }], total: 1, limit: 50, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } return { ok: true, json: async () => ({ data: { items: [], total: 0, limit: 10, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; }); render(); expect(await screen.findByText('VIN-RT-DEGRADED')).toBeInTheDocument(); expect(screen.getByText('部分来源离线')).toBeInTheDocument(); }); test('shows vehicle service status in vehicle list', async () => { window.history.replaceState(null, '', '/#/vehicles'); vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => { const path = String(input); if (path.includes('/api/vehicles/coverage')) { return { ok: true, json: async () => ({ data: { items: [{ vin: 'VIN-DEGRADED-001', plate: '粤AG18312', phone: '13307795425', oem: 'G7s', protocols: ['GB32960', 'JT808'], sourceCount: 1, onlineSourceCount: 1, online: true, lastSeen: '2026-07-03 20:12:10', bindingStatus: 'bound', serviceStatus: { status: 'degraded', severity: 'warning', title: '部分来源离线', detail: '由车辆服务 API 统一判定', sourceCount: 2, onlineSourceCount: 1 } }], total: 1, limit: 20, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } return { ok: true, json: async () => ({ data: path.includes('/api/ops/health') ? { linkHealth: [], kafkaLag: 0, redisOnlineKeys: 0, tdengineWritable: true, mysqlWritable: true, runtime: { requestTimeoutMs: 5000 } } : { items: [], total: 0, limit: 10, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; }); render(); expect(await screen.findByText('VIN-DEGRADED-001')).toBeInTheDocument(); expect(screen.getByText('车辆服务状态')).toBeInTheDocument(); expect(screen.getByText('部分来源离线')).toBeInTheDocument(); }); test('filters vehicle list by service status', async () => { window.history.replaceState(null, '', '/#/vehicles'); const fetchMock = vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => { const path = String(input); if (path.includes('/api/vehicles/coverage')) { return { ok: true, json: async () => ({ data: { items: [], total: 0, limit: 20, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } return { ok: true, json: async () => ({ data: path.includes('/api/ops/health') ? { linkHealth: [], kafkaLag: 0, redisOnlineKeys: 0, tdengineWritable: true, mysqlWritable: true, runtime: { requestTimeoutMs: 5000 } } : { items: [], total: 0, limit: 10, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; }); render(); await screen.findByRole('heading', { name: '车辆服务' }); fireEvent.click(screen.getByTestId('service-status-filter')); fireEvent.click(await screen.findByText('部分来源离线')); fireEvent.click(screen.getByRole('button', { name: '查询' })); await waitFor(() => { expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining('serviceStatus=degraded'), undefined); }); }); test('switches vehicle detail to a source from the coverage cards', async () => { window.history.replaceState(null, '', '/#/detail?keyword=VIN001'); const fetchMock = vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => { const path = String(input); if (path.includes('/api/vehicle-service')) { return { ok: true, json: async () => ({ data: { vin: 'VIN001', lookupKey: 'VIN001', lookupResolved: true, resolution: { lookupKey: 'VIN001', resolved: true, vin: 'VIN001', plate: '粤AG18312', phone: '13307795425', oem: 'G7s', protocols: ['GB32960', 'JT808', 'YUTONG_MQTT'], online: true, lastSeen: '2026-07-03 20:12:10' }, realtimeSummary: { vin: 'VIN001', plate: '粤AG18312', phone: '13307795425', oem: 'G7s', protocols: ['GB32960', 'JT808', 'YUTONG_MQTT'], sourceCount: 3, onlineSourceCount: 2, online: true, primaryProtocol: 'GB32960', longitude: 113.2, latitude: 23.1, speedKmh: 30, socPercent: 78, totalMileageKm: 100, lastSeen: '2026-07-03 20:12:10' }, sources: ['GB32960', 'JT808', 'YUTONG_MQTT'], sourceStatus: [ { protocol: 'GB32960', online: true, lastSeen: '2026-07-03 20:12:10', hasRealtime: true, hasHistory: true, hasRaw: true, hasMileage: true }, { protocol: 'JT808', online: true, lastSeen: '2026-07-03 20:12:08', hasRealtime: true, hasHistory: true, hasRaw: true, hasMileage: true }, { protocol: 'YUTONG_MQTT', online: false, lastSeen: '2026-07-03 20:10:00', hasRealtime: true, hasHistory: false, hasRaw: true, hasMileage: false } ], realtime: [], history: { items: [], total: 0, limit: 20, offset: 0 }, raw: { items: [], total: 0, limit: 10, offset: 0 }, mileage: { items: [], total: 0, limit: 20, offset: 0 }, quality: { items: [], total: 0, limit: 20, offset: 0 } }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } return { ok: true, json: async () => ({ data: { items: [], total: 0, limit: 10, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; }); render(); expect(await screen.findByText('VIN001')).toBeInTheDocument(); fireEvent.click(screen.getByRole('button', { name: '仅看 JT808' })); await waitFor(() => { expect(window.location.hash).toBe('#/detail?keyword=VIN001&protocol=JT808'); }); expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining('/api/vehicle-service?keyword=VIN001&protocol=JT808'), undefined); }); test('shows unified service overview on vehicle detail', async () => { window.history.replaceState(null, '', '/#/detail?keyword=VIN001'); vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => { const path = String(input); if (path.includes('/api/vehicle-service')) { return { ok: true, json: async () => ({ data: { vin: 'VIN001', lookupKey: 'VIN001', lookupResolved: true, serviceOverview: { vin: 'VIN001', plate: '粤AG18312', phone: '13307795425', oem: 'G7s', protocols: ['GB32960', 'JT808', 'YUTONG_MQTT'], primaryProtocol: 'GB32960', coverageStatus: 'partial', sourceCount: 3, onlineSourceCount: 2, lastSeen: '2026-07-03 20:12:10', realtimeCount: 3, historyCount: 12, rawCount: 34, mileageCount: 7, qualityIssueCount: 1 }, serviceStatus: { status: 'degraded', severity: 'warning', title: '部分来源离线', detail: '2/3 个来源在线', sourceCount: 3, onlineSourceCount: 2 }, sources: ['GB32960', 'JT808', 'YUTONG_MQTT'], sourceStatus: [], realtime: [], history: { items: [], total: 12, limit: 20, offset: 0 }, raw: { items: [], total: 34, limit: 10, offset: 0 }, mileage: { items: [], total: 7, limit: 20, offset: 0 }, quality: { items: [], total: 1, limit: 20, offset: 0 } }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } return { ok: true, json: async () => ({ data: path.includes('/api/ops/health') ? { linkHealth: [], kafkaLag: 0, redisOnlineKeys: 0, tdengineWritable: true, mysqlWritable: true, runtime: { requestTimeoutMs: 5000 } } : { items: [], total: 0, limit: 10, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; }); render(); expect(await screen.findByText('车辆服务概览')).toBeInTheDocument(); expect(screen.getByText('2/3')).toBeInTheDocument(); expect(screen.getByText('历史 12 / RAW 34 / 里程 7')).toBeInTheDocument(); }); test('opens history with the currently selected vehicle detail source', async () => { window.history.replaceState(null, '', '/#/detail?keyword=VIN001'); vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => { const path = String(input); if (path.includes('/api/vehicle-service')) { return { ok: true, json: async () => ({ data: { vin: 'VIN001', lookupKey: 'VIN001', lookupResolved: true, resolution: { lookupKey: 'VIN001', resolved: true, vin: 'VIN001', plate: '粤AG18312', phone: '13307795425', oem: 'G7s', protocols: ['GB32960', 'JT808'], online: true, lastSeen: '2026-07-03 20:12:10' }, realtimeSummary: { vin: 'VIN001', plate: '粤AG18312', phone: '13307795425', oem: 'G7s', protocols: ['GB32960', 'JT808'], sourceCount: 2, onlineSourceCount: 2, online: true, primaryProtocol: 'GB32960', longitude: 113.2, latitude: 23.1, speedKmh: 30, socPercent: 78, totalMileageKm: 100, lastSeen: '2026-07-03 20:12:10' }, sources: ['GB32960', 'JT808'], sourceStatus: [ { protocol: 'GB32960', online: true, lastSeen: '2026-07-03 20:12:10', hasRealtime: true, hasHistory: true, hasRaw: true, hasMileage: true }, { protocol: 'JT808', online: true, lastSeen: '2026-07-03 20:12:08', hasRealtime: true, hasHistory: true, hasRaw: true, hasMileage: true } ], realtime: [], history: { items: [], total: 0, limit: 20, offset: 0 }, raw: { items: [], total: 0, limit: 10, offset: 0 }, mileage: { items: [], total: 0, limit: 20, offset: 0 }, quality: { items: [], total: 0, limit: 20, offset: 0 } }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } return { ok: true, json: async () => ({ data: { items: [], total: 0, limit: 10, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; }); render(); fireEvent.click(await screen.findByRole('button', { name: '仅看 JT808' })); fireEvent.click(screen.getByRole('button', { name: '查看历史' })); await waitFor(() => { expect(window.location.hash).toBe('#/history?keyword=VIN001&protocol=JT808'); }); }); test('opens vehicle service from an empty history query with the current filters', async () => { window.history.replaceState(null, '', '/#/history?keyword=VIN404&protocol=JT808'); vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => { const path = String(input); if (path.includes('/api/vehicles/resolve')) { return { ok: true, json: async () => ({ data: { lookupKey: 'VIN404', resolved: true, vin: 'VIN404', plate: '', phone: '', oem: '', protocols: ['JT808'], online: false, lastSeen: '' }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } return { ok: true, json: async () => ({ data: path.includes('/api/ops/health') ? { linkHealth: [], kafkaLag: 0, redisOnlineKeys: 0, tdengineWritable: true, mysqlWritable: true, runtime: { requestTimeoutMs: 5000 } } : { items: [], total: 0, limit: 10, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; }); render(); fireEvent.click(await screen.findByRole('button', { name: '当前车辆服务' })); await waitFor(() => { expect(window.location.hash).toBe('#/detail?keyword=VIN404&protocol=JT808'); }); }); test('shows selected source scope on vehicle detail', async () => { window.history.replaceState(null, '', '/#/detail?keyword=VIN001&protocol=JT808'); const fetchMock = vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => { const path = String(input); if (path.includes('/api/vehicle-service')) { return { ok: true, json: async () => ({ data: { vin: 'VIN001', lookupKey: 'VIN001', lookupResolved: true, resolution: { lookupKey: 'VIN001', resolved: true, vin: 'VIN001', plate: '粤AG18312', phone: '13307795425', oem: 'G7s', protocols: ['GB32960', 'JT808'], online: true, lastSeen: '2026-07-03 20:12:10' }, realtimeSummary: { vin: 'VIN001', plate: '粤AG18312', phone: '13307795425', oem: 'G7s', protocols: ['GB32960', 'JT808'], sourceCount: 2, onlineSourceCount: 1, online: true, primaryProtocol: 'JT808', longitude: 113.2, latitude: 23.1, speedKmh: 30, socPercent: 78, totalMileageKm: 100, lastSeen: '2026-07-03 20:12:10' }, sources: ['GB32960', 'JT808'], sourceStatus: [ { protocol: 'GB32960', online: false, lastSeen: '2026-07-03 20:11:10', hasRealtime: true, hasHistory: true, hasRaw: true, hasMileage: true }, { protocol: 'JT808', online: true, lastSeen: '2026-07-03 20:12:10', hasRealtime: true, hasHistory: true, hasRaw: true, hasMileage: true } ], serviceStatus: { status: 'degraded', severity: 'warning', title: '部分来源离线', detail: '1/2 个来源在线,车辆服务可用但需要关注离线来源。', sourceCount: 2, onlineSourceCount: 1 }, realtime: [], history: { items: [], total: 0, limit: 20, offset: 0 }, raw: { items: [], total: 0, limit: 10, offset: 0 }, mileage: { items: [], total: 0, limit: 20, offset: 0 }, quality: { items: [], total: 0, limit: 20, offset: 0 } }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; } return { ok: true, json: async () => ({ data: path.includes('/api/ops/health') ? { linkHealth: [], kafkaLag: 0, redisOnlineKeys: 0, tdengineWritable: true, mysqlWritable: true, runtime: { requestTimeoutMs: 5000 } } : { items: [], total: 0, limit: 10, offset: 0 }, traceId: 'trace-test', timestamp: 1783094400000 }) } as Response; }); render(); expect(await screen.findByText('当前查看范围')).toBeInTheDocument(); expect(screen.getByText('单一来源:JT808')).toBeInTheDocument(); expect(screen.getByText('车辆服务状态')).toBeInTheDocument(); expect(screen.getByText('部分来源离线')).toBeInTheDocument(); expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining('/api/vehicle-service?keyword=VIN001&protocol=JT808'), undefined); });