Files
vehicle-map-0803/tests/test_app.mjs

75 lines
3.2 KiB
JavaScript

import assert from 'node:assert/strict';
import fs from 'node:fs';
import vm from 'node:vm';
const source = fs.readFileSync(new URL('../app.js', import.meta.url), 'utf8');
const checks = `
dashboard = {
vehicles: [
{ vin: 'VIN-GD-1', plateNumber: '粤A00001', online: true, dailyMileageKm: 10, locationAvailable: true, longitude: 113.2, latitude: 23.1 },
{ vin: 'VIN-GD-2', plateNumber: '粤B00002', online: false, dailyMileageKm: 5, locationAvailable: false },
{ vin: 'VIN-ZJ-1', plateNumber: '浙F00003', online: true, dailyMileageKm: 8, locationAvailable: true, longitude: 120.7, latitude: 30.7 },
{ vin: 'VIN-X-1', plateNumber: '观光车001', online: false, dailyMileageKm: 0, locationAvailable: false }
]
};
const areaNode = {
groupByPosition(points) {
return [
{ subFeatureIndex: 0, subFeature: { properties: { adcode: '440000', name: '广东省', center: [113.2, 23.1] } }, points: points.filter(point => point.vin === 'VIN-GD-1') },
{ subFeatureIndex: 1, subFeature: { properties: { adcode: '330000', name: '浙江省', center: [120.2, 30.3] } }, points: points.filter(point => point.vin === 'VIN-ZJ-1') }
];
}
};
const partition = partitionVehiclesByArea(areaNode, locatedVehicles(), 'province');
assert.equal(partition.groups.length, 2);
assert.equal(partition.unmatched.length, 0);
const provinceNodes = regionNodesFromGroups(partition.groups, 'province');
assert.equal(provinceNodes.length, 2);
const guangdong = provinceNodes.find(node => node.nameZh === '广东');
assert.equal(guangdong.count, 1);
assert.equal(guangdong.online, 1);
assert.equal(guangdong.dist, 10);
assert.equal(hierarchyLevelForZoom(4.8), 'province');
assert.equal(hierarchyLevelForZoom(7), 'city');
assert.equal(hierarchyLevelForZoom(9.5), 'district');
assert.equal(hierarchyLevelForZoom(12), 'vehicle');
vehicleRegionSummary = { level: 'province', nodes: provinceNodes, unassigned: 2, loading: false };
map = { getZoom: () => 4.8 };
assert.equal(vehicleNodes().length, 2);
assert.ok(vehicleNodes().every(node => node.kind === 'vehicleProvince'));
const cityPartition = partitionVehiclesByArea({
groupByPosition(points) {
return [{ subFeatureIndex: 0, subFeature: { properties: { adcode: '440100', name: '广州市', center: [113.3, 23.1] } }, points }];
}
}, [dashboard.vehicles[0]], 'city');
const cityNodes = regionNodesFromGroups(cityPartition.groups, 'city');
assert.equal(cityNodes[0].kind, 'vehicleCity');
assert.equal(cityNodes[0].nameZh, '广州');
map = {
getZoom: () => 12,
getBounds: () => ({
getSouthWest: () => ({ getLng: () => 100, getLat: () => 20 }),
getNorthEast: () => ({ getLng: () => 125, getLat: () => 35 })
})
};
const points = vehiclePointNodes();
assert.equal(points.length, 2);
assert.ok(points.every(node => node.kind === 'vehiclePoint'));
assert.equal(points.find(node => node.nameZh === '粤A00001').online, 1);
`;
const sandbox = {
assert,
console,
document: { addEventListener() {}, querySelector() {}, createElement() { return {}; }, head: { appendChild() {} } },
window: {},
setInterval() {},
clearInterval() {}
};
vm.runInNewContext(`${source}\n${checks}`, sandbox, { filename: 'app.js' });
console.log('vehicle hierarchy aggregation tests: ok');