diff --git a/vehicle-data-platform/apps/web/src/App.tsx b/vehicle-data-platform/apps/web/src/App.tsx index 4d30db30..6d62f295 100644 --- a/vehicle-data-platform/apps/web/src/App.tsx +++ b/vehicle-data-platform/apps/web/src/App.tsx @@ -103,24 +103,28 @@ export default function App() { } }; - const openHistoryForVehicle = (vin: string) => { + const openHistoryForVehicle = (vin: string, protocol?: string) => { const nextVin = vin.trim(); + const nextProtocol = protocol?.trim() ?? activeProtocol; if (!nextVin) { return; } setAnalysisVin(nextVin); + setActiveProtocol(nextProtocol); setActivePage('history'); - replaceHash('history', nextVin, activeProtocol); + replaceHash('history', nextVin, nextProtocol); }; - const openMileageForVehicle = (vin: string) => { + const openMileageForVehicle = (vin: string, protocol?: string) => { const nextVin = vin.trim(); + const nextProtocol = protocol?.trim() ?? activeProtocol; if (!nextVin) { return; } setAnalysisVin(nextVin); + setActiveProtocol(nextProtocol); setActivePage('mileage'); - replaceHash('mileage', nextVin, activeProtocol); + replaceHash('mileage', nextVin, nextProtocol); }; const updateVehicleDetailQuery = (keyword: string, protocol?: string) => { diff --git a/vehicle-data-platform/apps/web/src/pages/VehicleDetail.tsx b/vehicle-data-platform/apps/web/src/pages/VehicleDetail.tsx index aeba2d60..89d44444 100644 --- a/vehicle-data-platform/apps/web/src/pages/VehicleDetail.tsx +++ b/vehicle-data-platform/apps/web/src/pages/VehicleDetail.tsx @@ -28,8 +28,8 @@ export function VehicleDetail({ }: { vin: string; protocol?: string; - onOpenHistory: (vin: string) => void; - onOpenMileage: (vin: string) => void; + onOpenHistory: (vin: string, protocol?: string) => void; + onOpenMileage: (vin: string, protocol?: string) => void; onQueryChange?: (keyword: string, protocol?: string) => void; }) { const [query, setQuery] = useState({ keyword: vin, protocol }); @@ -130,8 +130,8 @@ export function VehicleDetail({ - - + + diff --git a/vehicle-data-platform/apps/web/src/test/App.test.tsx b/vehicle-data-platform/apps/web/src/test/App.test.tsx index 470cf988..f259f563 100644 --- a/vehicle-data-platform/apps/web/src/test/App.test.tsx +++ b/vehicle-data-platform/apps/web/src/test/App.test.tsx @@ -1,8 +1,9 @@ -import { fireEvent, render, screen, waitFor } from '@testing-library/react'; +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(); }); @@ -321,3 +322,79 @@ test('switches vehicle detail to a source from the coverage cards', async () => }); expect(fetchMock).toHaveBeenCalledWith(expect.stringContaining('/api/vehicles/detail?keyword=VIN001&protocol=JT808'), undefined); }); + +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/vehicles/detail')) { + 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'); + }); +});