切换 - templates/map.html:四个 script(navigation/share/subscribe/map.js) → 一行 /static/dist/app.js;geo.js 保留(bundle 内同一份实现,幂等) - routes/map_api.py 的 MAP_ASSETS 同步换成 dist/app.js,缓存戳跟着产物走 随切换修的 - walk-route:onSearchComplete 里 res.getStatus() 加存在性判断 (起点≈终点时上游返回空壳,原 JS 版与 TS 版都会抛未捕获 TypeError),两版同步修 - compat/globals.ts 末尾挂 window.S = S:原版 91 个顶层 let/var 是全局词法绑定, bundle 里收进 state 对象,至少把 state 暴露出来便于排障 - tests/test_map_assets.py 断言改为 dist/app.js - tests/test_history_30d.py:子进程加 import os + XBCR_DISABLE_BACKGROUND=1 (原来采集线程会往临时库写 bike_history,导致统计断言偶发失败) - app.py 支持 XBCR_DISABLE_BACKGROUND=1:只初始化数据库、不起后台线程(测试/工具用) - tests/test_rounds_and_preview.py:remaining_seconds 允许 2 秒误差(跨秒抖动) 回滚 - 把 map.html 那行换回四个 script、MAP_ASSETS 换回四个文件名即可; static/js/ 一字未改,dist 留着也不会被加载 验证:167 pytest(连跑 3 次)+ 32 node 全绿;浏览器实测(390px,真实数据) 车 498、卡片 #51143 / 步行 488 米、导航条「前方 170 米 右转」、共享/订阅面板可开、 JS 异常 0
52 lines
2.2 KiB
Python
52 lines
2.2 KiB
Python
import re
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from urllib.parse import parse_qs, urlsplit
|
|
|
|
from flask import Flask
|
|
from routes import map_api
|
|
|
|
|
|
class MapAssetTests(unittest.TestCase):
|
|
def setUp(self):
|
|
root = Path(__file__).resolve().parents[1]
|
|
self.app = Flask(__name__, static_folder=str(root / "static"),
|
|
template_folder=str(root / "templates"))
|
|
self.app.register_blueprint(map_api.bp)
|
|
|
|
def test_map_html_versions_the_complete_bundle_and_does_not_block_on_baidu(self):
|
|
response = self.app.test_client().get("/")
|
|
self.assertEqual(200, response.status_code)
|
|
self.assertEqual("no-cache", response.headers["Cache-Control"])
|
|
html = response.get_data(as_text=True)
|
|
urls = re.findall(r'(?:src|href)="(/static/[^\"]+)"', html)
|
|
versions = set()
|
|
for url in urls:
|
|
parsed = urlsplit(url)
|
|
self.assertTrue((Path(self.app.static_folder) / parsed.path.removeprefix("/static/")).is_file())
|
|
versions.add(parse_qs(parsed.query).get("v", [""])[0])
|
|
self.assertEqual(len(map_api.MAP_ASSETS), len(urls))
|
|
self.assertEqual(1, len(versions))
|
|
self.assertNotIn("", versions)
|
|
self.assertRegex(html, r'<script async src="https://api\.map\.baidu\.com/')
|
|
self.assertIn("&callback=onBaiduMapReady", html)
|
|
# TypeScript 重写后:四个脚本合成一个 esbuild IIFE 包
|
|
self.assertRegex(html, r'<script defer src="/static/dist/app\.js\?v=')
|
|
|
|
def test_changing_a_dependency_invalidates_the_whole_bundle_version(self):
|
|
with tempfile.TemporaryDirectory() as folder:
|
|
self.app.static_folder = folder
|
|
dependency = Path(folder) / "js/bike-clusters.js"
|
|
dependency.parent.mkdir()
|
|
dependency.write_text("old", encoding="utf-8")
|
|
before = self.app.test_client().get("/").get_data(as_text=True)
|
|
dependency.write_text("new", encoding="utf-8")
|
|
after = self.app.test_client().get("/").get_data(as_text=True)
|
|
pattern = r'/static/dist/app\.js\?v=([a-f0-9]+)'
|
|
self.assertNotEqual(re.search(pattern, before)[1], re.search(pattern, after)[1])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|