feat(platform): export history query results

This commit is contained in:
lingniu
2026-07-04 10:59:47 +08:00
parent 312382c95d
commit cd9d160f3e
6 changed files with 261 additions and 0 deletions

View File

@@ -3037,6 +3037,143 @@ test('shows trajectory playback workspace from history locations', async () => {
expect(screen.getByText('终点')).toBeInTheDocument();
});
test('exports current history location page as csv', async () => {
window.history.replaceState(null, '', '/#/history?keyword=VIN-EXPORT-LOC&protocol=JT808');
const createObjectURL = vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:history-location');
const revokeObjectURL = vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => undefined);
vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => undefined);
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-EXPORT-LOC',
plate: '粤A导出1',
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/history/raw-frames/query')) {
expect(init?.method).toBe('POST');
}
return {
ok: true,
json: async () => ({
data: { items: [], total: 0, limit: 10, offset: 0 },
traceId: 'trace-test',
timestamp: 1783094400000
})
} as Response;
});
render(<App />);
expect(await screen.findByText('VIN-EXPORT-LOC')).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: '导出位置当前页 CSV' }));
expect(createObjectURL).toHaveBeenCalled();
expect(revokeObjectURL).toHaveBeenCalledWith('blob:history-location');
});
test('exports current raw frame page as csv with parsed fields', async () => {
window.history.replaceState(null, '', '/#/history?keyword=VIN-EXPORT-RAW&protocol=GB32960&includeFields=true&tab=raw');
const createObjectURL = vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:history-raw');
const revokeObjectURL = vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => undefined);
vi.spyOn(HTMLAnchorElement.prototype, 'click').mockImplementation(() => undefined);
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/history/locations')) {
return {
ok: true,
json: async () => ({
data: { items: [], total: 0, limit: 10, offset: 0 },
traceId: 'trace-test',
timestamp: 1783094400000
})
} as Response;
}
if (path.includes('/api/history/raw-frames/query')) {
return {
ok: true,
json: async () => ({
data: {
items: [{
id: 'raw-export-001',
vin: 'VIN-EXPORT-RAW',
plate: '粤A导出2',
protocol: 'GB32960',
frameType: 'REALTIME',
deviceTime: '2026-07-03 20:12:10',
serverTime: '2026-07-03 20:12:11',
rawSizeBytes: 256,
parsedFields: { 'gb32960.vehicle.speed_kmh': 42 }
}],
total: 1,
limit: 10,
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(<App />);
expect(await screen.findByText('raw-export-001')).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: '导出 RAW 当前页 CSV' }));
expect(createObjectURL).toHaveBeenCalled();
expect(revokeObjectURL).toHaveBeenCalledWith('blob:history-raw');
});
test('shows vehicle and source scope on mileage hash', async () => {
window.history.replaceState(null, '', '/#/mileage?keyword=VIN-MILEAGE-001&protocol=GB32960');
vi.spyOn(globalThis, 'fetch').mockImplementation(async (input) => {

View File

@@ -65,6 +65,16 @@ Object.defineProperty(window, 'matchMedia', {
})
});
Object.defineProperty(URL, 'createObjectURL', {
configurable: true,
value: () => 'blob:test'
});
Object.defineProperty(URL, 'revokeObjectURL', {
configurable: true,
value: () => undefined
});
document.createRange = () => ({
setStart: () => undefined,
setEnd: () => undefined,