|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { parseHkDetectors, parseHkRawFlow } from "../hk.js"; |
| 3 | +import type { SourceDescriptor } from "../types.js"; |
| 4 | + |
| 5 | +const src = { |
| 6 | + id: "hk-td-flow", |
| 7 | + attribution: "Transport Department, HKSAR", |
| 8 | + country: "HK", |
| 9 | + license: "HK-Gov-Open-Data", |
| 10 | +} as SourceDescriptor; |
| 11 | + |
| 12 | +// Leading mirrors the live file's UTF-8 BOM. |
| 13 | +const CSV = `AID_ID_Number,District,Road_EN,Road_TC,Road_SC,Easting,Northing,Latitude,Longitude,Direction,Rotation |
| 14 | +AID01101,Southern,Aberdeen Praya Road,x,x,833758,812147,22.248091,114.152525,South East,100 |
| 15 | +BADLAT,Southern,Road,x,x,0,0,0,0,North,0 |
| 16 | +`; |
| 17 | + |
| 18 | +const XML = `<?xml version="1.0" encoding="utf-8"?> |
| 19 | +<raw_speed_volume_list><periods> |
| 20 | + <period><period_from>01:50:00</period_from><period_to>01:50:30</period_to><detectors> |
| 21 | + <detector><detector_id>AID01101</detector_id><lanes> |
| 22 | + <lane><lane_id>Fast Lane</lane_id><speed>60</speed><volume>1</volume><valid>Y</valid></lane> |
| 23 | + </lanes></detector> |
| 24 | + </detectors></period> |
| 25 | + <period><period_from>01:55:00</period_from><period_to>01:55:30</period_to><detectors> |
| 26 | + <detector><detector_id>AID01101</detector_id><lanes> |
| 27 | + <lane><lane_id>Fast Lane</lane_id><speed>100</speed><volume>3</volume><valid>Y</valid></lane> |
| 28 | + <lane><lane_id>Slow Lane</lane_id><speed>60</speed><volume>1</volume><valid>Y</valid></lane> |
| 29 | + <lane><lane_id>Broken</lane_id><speed>0</speed><volume>0</volume><valid>N</valid></lane> |
| 30 | + </lanes></detector> |
| 31 | + </detectors></period> |
| 32 | +</periods></raw_speed_volume_list>`; |
| 33 | + |
| 34 | +describe("parseHkDetectors", () => { |
| 35 | + it("maps AID id → WGS84 Point, strips the BOM, and drops out-of-bounds rows", () => { |
| 36 | + const map = parseHkDetectors(CSV); |
| 37 | + expect(map.size).toBe(1); |
| 38 | + expect(map.get("AID01101")).toEqual({ type: "Point", coordinates: [114.152525, 22.248091] }); |
| 39 | + }); |
| 40 | +}); |
| 41 | + |
| 42 | +describe("parseHkRawFlow", () => { |
| 43 | + it("uses the latest period and the volume-weighted mean of valid lanes", () => { |
| 44 | + const siteMap = parseHkDetectors(CSV); |
| 45 | + const { flows } = parseHkRawFlow(XML, src, siteMap); |
| 46 | + expect(flows).toHaveLength(1); |
| 47 | + expect(flows[0]!.id).toBe("hk-td-flow:AID01101"); |
| 48 | + // Latest period: (100·3 + 60·1) / (3+1) = 90; the valid=N lane is ignored. |
| 49 | + expect(flows[0]!.speedKph).toBe(90); |
| 50 | + expect(flows[0]!.geometry).toEqual({ type: "Point", coordinates: [114.152525, 22.248091] }); |
| 51 | + expect(flows[0]!.dataUpdatedAt).toBe("01:55:30"); |
| 52 | + }); |
| 53 | + |
| 54 | + it("skips detectors with no geometry", () => { |
| 55 | + expect(parseHkRawFlow(XML, src, new Map()).flows).toHaveLength(0); |
| 56 | + }); |
| 57 | + |
| 58 | + it("flags a hard parse failure", () => { |
| 59 | + expect(parseHkRawFlow("nope <", src, new Map()).failed).toBe(true); |
| 60 | + }); |
| 61 | +}); |
0 commit comments