fix(platform): keep dashboard metrics on preview failures

This commit is contained in:
lingniu
2026-07-04 08:40:35 +08:00
parent 7c1ad16018
commit d671184c09
3 changed files with 88 additions and 15 deletions

View File

@@ -135,6 +135,79 @@ test('dashboard renders vehicle service summary metrics', async () => {
expect(fetchMock).toHaveBeenCalledWith('/api/vehicle-service/summary', undefined);
});
test('dashboard keeps core vehicle service metrics when preview requests fail', 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/vehicle-service/summary')) {
return {
ok: true,
json: async () => ({
data: {
totalVehicles: 1033,
boundVehicles: 1024,
onlineVehicles: 208,
singleSourceVehicles: 391,
multiSourceVehicles: 181,
noDataVehicles: 461,
identityRequiredVehicles: 9,
serviceStatuses: [],
protocols: [],
missingSources: []
},
traceId: 'trace-test',
timestamp: 1783094400000
})
} as Response;
}
if (path.includes('/api/quality/issues')) {
return {
ok: false,
status: 500,
json: async () => ({
error: { message: '质量预览失败', detail: 'TDengine timeout' },
traceId: 'trace-quality',
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(<App />);
expect(await screen.findByText('总车辆')).toBeInTheDocument();
expect(screen.getAllByText('1,033').length).toBeGreaterThanOrEqual(1);
expect(screen.getByText('单源车辆')).toBeInTheDocument();
expect(screen.getByText('391')).toBeInTheDocument();
});
test('opens dashboard coverage from missing source distribution', async () => {
window.history.replaceState(null, '', '/#/dashboard');
const fetchMock = vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => {