功能:完善车辆地图合作站信息与缓存刷新

This commit is contained in:
lingniu
2026-09-02 14:05:49 +08:00
parent c6875fbb98
commit e7ee8583aa
5 changed files with 262 additions and 20 deletions
+10 -2
View File
@@ -105,13 +105,15 @@ filterState.district = '';
dashboard = {
stations: [
{ id: 'GD-1', name: '广州合作站', province: '广东省', city: '广州市', address: '广东省广州市黄埔区开源大道1号', longitude: 113.2, latitude: 23.1, cooperative: true, totalHydrogenKg: 20 },
{ id: 'GD-1', name: '广州合作站', province: '广东省', city: '广州市', address: '广东省广州市黄埔区开源大道1号', longitude: 113.2, latitude: 23.1, cooperative: true, contactPerson: '张工', contactPhone: '13800000000', unitPrice: 12.5, totalHydrogenKg: 20 },
{ id: 'GD-2', name: '佛山外部站', province: '广东省', city: '佛山市', longitude: 113.1, latitude: 23.0, cooperative: false, totalHydrogenKg: 10 },
{ id: 'ZJ-1', name: '嘉兴合作站', province: '浙江省', city: '嘉兴市', longitude: 120.7, latitude: 30.7, cooperative: true, totalHydrogenKg: 30 }
]
};
currentMode = 'station';
assert.equal(stationDistrictName(dashboard.stations[0]), '黄埔区');
assert.equal(stationUnitPrice(dashboard.stations[0]), '¥12.50 / kg');
assert.match(detailPhoneField('联系方式', dashboard.stations[0].contactPhone), /tel:13800000000/);
assert.equal(fuzzySearchScore('广州', 'gz'), 3);
assert.equal(fuzzySearchScore('广州市', '广州'), 1);
assert.ok(stationLocationOptions().some(option => option.level === 'district' && option.district === '黄埔区'));
@@ -153,7 +155,9 @@ document.getElementById = id => id === 'navigationLaunchOverlay' ? launchOverlay
window.requestAnimationFrame = callback => callback();
window.setTimeout = (callback, delay) => { launchTimers.push({ callback, delay }); return launchTimers.length; };
window.clearTimeout = () => {};
window.location = { assign() {} };
const navigationTarget = { opener: {}, location: {} };
const openedNavigation = [];
window.open = (...args) => { openedNavigation.push(args); return navigationTarget; };
selectedEntity = { mode: 'station', entity: dashboard.stations[0] };
navigateToSelectedEntity();
assert.equal(launchOverlay.hidden, false);
@@ -161,6 +165,10 @@ assert.ok(launchClassSet.has('is-visible'));
assert.ok(launchClassSet.has('button:is-launching'));
assert.equal(launchButton.disabled, true);
assert.deepEqual(launchTimers.map(timer => timer.delay), [140, 4000]);
assert.deepEqual(openedNavigation[0], ['', '_blank']);
launchTimers[0].callback();
assert.equal(navigationTarget.opener, null);
assert.equal(navigationTarget.location.href, stationNavigationUrl);
const stationView = stationViewportSummary();
assert.equal(stationView.level, 'station');
assert.equal(stationView.visibleNodes.length, 2);
+55
View File
@@ -1,6 +1,8 @@
import importlib.util
from pathlib import Path
import tempfile
import threading
import time
import unittest
from unittest.mock import patch
@@ -52,6 +54,20 @@ class DashboardTest(unittest.TestCase):
self.assertEqual(server.DASHBOARD_CACHE_SECONDS, 120)
self.assertEqual(server.STATION_CACHE_SECONDS, 120)
def test_public_station_directory_limits_partner_contact_and_price_to_cooperative_stations(self):
stations = [
{"id": "partner", "cooperative": True, "contactPerson": "张工", "contactPhone": "13800000000", "unitPrice": 12.5},
{"id": "external", "cooperative": False, "contactPerson": "李工", "contactPhone": "13900000000", "unitPrice": 10.0},
]
with patch.object(server, "_load_stations", return_value=stations):
directory = server._public_station_directory()["stations"]
self.assertEqual(directory[0]["contactPerson"], "张工")
self.assertEqual(directory[0]["contactPhone"], "13800000000")
self.assertEqual(directory[0]["unitPrice"], 12.5)
self.assertNotIn("contactPerson", directory[1])
self.assertNotIn("contactPhone", directory[1])
self.assertNotIn("unitPrice", directory[1])
def test_cache_reuses_dashboard_snapshot_within_window(self):
calls = []
with patch.object(server, "_cache", {}):
@@ -60,6 +76,45 @@ class DashboardTest(unittest.TestCase):
self.assertEqual(calls, ["load"])
self.assertEqual(first, second)
def test_expired_cache_returns_stale_value_while_refreshing_in_background(self):
started = threading.Event()
release = threading.Event()
def loader():
started.set()
release.wait(1)
return {"version": 2}
cache = {"dashboard": (time.time() - 121, {"version": 1})}
with patch.object(server, "_cache", cache), \
patch.object(server, "_cache_refreshing", set()), \
patch.object(server, "_cache_load_locks", {}):
result = server._cached("dashboard", 120, loader)
self.assertEqual(result, {"version": 1})
self.assertTrue(started.wait(1))
release.set()
for _ in range(100):
if cache["dashboard"][1] == {"version": 2}:
break
time.sleep(0.01)
self.assertEqual(cache["dashboard"][1], {"version": 2})
def test_expired_station_region_is_served_while_refresh_is_scheduled(self):
station = {"longitude": 113.2, "latitude": 23.1, "province": "错误省份"}
coordinate = server._station_coordinate_key(station)
stale_region = {"province": "广东省", "city": "广州市", "district": "黄埔区"}
cache = {coordinate: {"updatedAt": time.time() - server.STATION_GEOCODE_CACHE_SECONDS - 1, "region": stale_region}}
with patch.object(server, "_station_geocode_cache", cache), \
patch.object(server, "_station_geocode_cache_dirty", False), \
patch.object(server, "_schedule_station_region_refresh") as schedule, \
patch.object(server, "_reverse_geocode_station") as reverse:
result = server._stations_with_gps_regions([station])
self.assertEqual(result[0]["province"], "广东省")
self.assertEqual(result[0]["city"], "广州市")
reverse.assert_not_called()
schedule.assert_called_once()
self.assertEqual(len(schedule.call_args[0][0]), 1)
def test_station_region_comes_from_gps_reverse_geocode_and_persists_by_coordinate(self):
station = {
"longitude": 113.200001,