t03-camera-walkthrough.mjs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /**
  2. * T03 真机走查:三键相机契约落在真实 Cesium 上的行为(票 03 / ADR 0008)。
  3. *
  4. * 逻辑脚本(t03-camera-contract.mjs)用假引擎把语义测透了,这里只补浏览器里才成立的部分:
  5. * · Cesium 默认左键旋转 / 滚轮缩放确实被替换(左键只平移、滚轮只改距离);
  6. * · 三种交互在真机上确实按契约改变相机;
  7. * · 未选中时中心就是屏幕中心的地面拾取点(用中心点在画布上的投点位置断言);
  8. * · 选中换中心平滑且保持 heading 与 range,取消选中不回退。
  9. *
  10. * 走查用 page.mouse 派发**真实**输入事件(不是合成 DOM 事件),读的是开发态把手
  11. * `window.__cockpitMap`(见 mapService 的 publishDevHook)与宿主上的 `data-camera`。
  12. *
  13. * 运行(需先起 `yarn dev`,服务在 http://localhost:8000/):
  14. * node .scratch/cockpit-shell-panel-layout/verify/t03-camera-walkthrough.mjs
  15. */
  16. import { join } from 'node:path'
  17. import { tmpdir } from 'node:os'
  18. const BASE = process.env.COCKPIT_BASE || 'http://localhost:8000'
  19. const ENTRY_URL = process.env.COCKPIT_URL || `${BASE}/#/cockpit`
  20. const CHROME = process.env.CHROME_PATH || 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'
  21. const PUPPETEER = 'file:///C:/Users/ASUS/.dsh/profiles/desktop/node_modules/puppeteer-core/lib/puppeteer/puppeteer-core.js'
  22. /** 沅陵县某监测点(只用于「选中换中心」这条,跟真实数据无关)。 */
  23. const XIANGLING = { lon: 110.396, lat: 28.455, height: 0 }
  24. const results = []
  25. /** 页面在走查期间被 HMR 重载时置位:本轮红绿不作数(并发编辑场景)。 */
  26. let runInvalidated = false
  27. function check(id, desc, pass, evidence) {
  28. results.push({ id, desc, pass })
  29. console.log(`${pass ? 'PASS' : 'FAIL'} ${id} ${desc}${evidence ? ` — ${evidence}` : ''}`)
  30. }
  31. const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
  32. const round = (n, digits = 3) => Number(n.toFixed(digits))
  33. /** 角度/经度的最短弧差:heading 是 360 ≡ 0、经度是 ±180 同一点,直接相减会得到假差值。 */
  34. const shortest = (from, to) => ((to - from + 540) % 360) - 180
  35. const puppeteer = (await import(PUPPETEER)).default
  36. const browser = await puppeteer.launch({
  37. executablePath: CHROME,
  38. headless: true,
  39. userDataDir: join(tmpdir(), 'cockpit-t03-profile'),
  40. defaultViewport: { width: 1680, height: 950 },
  41. args: ['--no-sandbox', '--disable-dev-shm-usage', '--no-first-run', '--no-default-browser-check'],
  42. })
  43. console.log(`Chrome: ${await browser.version()}`)
  44. try {
  45. const page = await browser.newPage()
  46. const pageErrors = []
  47. page.on('pageerror', (e) => pageErrors.push(String(e && e.message ? e.message : e)))
  48. await page.goto(ENTRY_URL, { waitUntil: 'networkidle2', timeout: 60000 })
  49. await page.waitForSelector('.cockpit-shell', { timeout: 20000 })
  50. await page
  51. .waitForFunction(
  52. () => document.querySelector('.cockpit-shell__map .map-canvas')?.dataset.mapEngine === 'ready',
  53. { timeout: 60000 },
  54. )
  55. .catch(() => {})
  56. await page.waitForFunction(() => !!window.__cockpitMap, { timeout: 20000 })
  57. await sleep(800)
  58. const readState = () =>
  59. page.evaluate(() => {
  60. const state = window.__cockpitMap.getCameraState()
  61. const canvas = document.querySelector('.cockpit-shell__map .map-canvas canvas')
  62. const rect = canvas ? canvas.getBoundingClientRect() : null
  63. return {
  64. ...state,
  65. dataAttr: document.querySelector('.cockpit-shell__map .map-canvas')?.dataset.camera || '',
  66. canvas: rect ? { x: rect.x, y: rect.y, w: rect.width, h: rect.height } : null,
  67. projection: window.__cockpitMap.projectCameraPivot(),
  68. }
  69. })
  70. const center = async () => {
  71. const st = await readState()
  72. return { x: st.canvas.x + st.canvas.w / 2, y: st.canvas.y + st.canvas.h / 2 }
  73. }
  74. /**
  75. * 等阻尼收敛(实际相机跟上目标)。有了阻尼之后,手势结束不等于画面停稳,
  76. * 量「中心是否落在画布中心」这类**实际态**几何之前必须先等 settled。
  77. */
  78. const waitSettled = async (timeout = 5000) => {
  79. await page.waitForFunction(() => window.__cockpitMap.getCameraState().settled === true, { timeout }).catch(() => {})
  80. }
  81. /**
  82. * 票 30 起有「进入引导」全屏遮罩:点击即进入,而且**引导期间拖动是转星球、滚轮是缩放星球**
  83. * —— 不先关掉它,第一次拖拽会被遮罩吃掉,后续断言测的就成了引导层而不是相机契约。
  84. * 所以进入系统、等镜头在主页视角停稳,再开始走查。
  85. */
  86. const dismissIntro = async () => {
  87. const overlay = await page.$('[data-cockpit-intro]')
  88. if (!overlay) return false
  89. await overlay.click()
  90. await page
  91. .waitForFunction(() => !document.querySelector('[data-cockpit-intro]'), { timeout: 5000 })
  92. .catch(() => {})
  93. return true
  94. }
  95. const hadIntro = await dismissIntro()
  96. console.log(`进入引导:${hadIntro ? '已关闭(点击进入系统)' : '未出现(配置里未启用)'}`)
  97. await waitSettled()
  98. await sleep(300)
  99. // 并发会话在改同仓代码时会触发 Vite HMR 重载页面:一重载,引导层重新出现、dev 把手重建、
  100. // 相机回到初始视角 —— 后面所有断言都会变成假失败。这里打个运行标记,收尾时核验;
  101. // 不一致就把本轮判为「环境重载、结果作废」,而不是报一堆红。
  102. const runTag = await page.evaluate(() => {
  103. const tag = `t03-${Math.random().toString(36).slice(2)}`
  104. window.__t03RunTag = tag
  105. return tag
  106. })
  107. const pageStillSameRun = () => page.evaluate((tag) => window.__t03RunTag === tag, runTag)
  108. // ---------- A1 就绪与状态可读 ----------
  109. const base = await readState()
  110. check(
  111. 'A1-camera-ready',
  112. '引擎就绪后相机状态可读:弹簧臂参数与中心都有值,宿主 data-camera 同步',
  113. !!base &&
  114. !!base.pivot &&
  115. base.pivot.lon > -180 &&
  116. base.hpr.range > 0 &&
  117. base.dataAttr.includes('pivot=') &&
  118. base.dataAttr.includes('h='),
  119. JSON.stringify({ hpr: base.hpr, pivot: base.pivot, data: base.dataAttr }),
  120. )
  121. // ---------- A2 左键拖拽只平移(Cesium 默认左键旋转已被替换) ----------
  122. const a2before = await readState()
  123. const c2 = await center()
  124. await page.mouse.move(c2.x, c2.y)
  125. await page.mouse.down({ button: 'left' })
  126. await page.mouse.move(c2.x + 160, c2.y, { steps: 8 })
  127. await page.mouse.up({ button: 'left' })
  128. await sleep(150)
  129. await waitSettled()
  130. const a2after = await readState()
  131. {
  132. // 经度差按最短弧取:跨 ±180 时直接相减会得到 ±359 的假位移,让方向判断整个反过来。
  133. const dEast = shortest(a2before.pivot.lon, a2after.pivot.lon)
  134. const dNorth = a2after.pivot.lat - a2before.pivot.lat
  135. // 相机右手方向(东北分量):heading 上「前方」=(sin h, cos h),右手 = (cos h, -sin h)。
  136. // 维护者 2026-09-17 裁定:**抓着地图拖** —— 拖右 → 画面右移、相机左移,
  137. // 所以中心沿相机右手的**反方向**移动(alongRight 应为负)。
  138. const h = (a2before.hpr.heading * Math.PI) / 180
  139. const alongRight = dEast * Math.cos(h) + dNorth * -Math.sin(h)
  140. // heading 是圆周量:360 与 0 是同一个角度,必须按最短弧比较(此前直接用减法,360→0 会被误判成变了 360°)。
  141. const headingDrift = Math.abs(shortest(a2before.hpr.heading, a2after.hpr.heading))
  142. check(
  143. 'A2-left-drag-pans-only',
  144. '左键拖拽只平移:heading/pitch/range 不变,中心沿「抓地图」方向移动(默认左键旋转已被替换)',
  145. headingDrift < 0.05 &&
  146. Math.abs(a2after.hpr.pitch - a2before.hpr.pitch) < 0.05 &&
  147. Math.abs(a2after.hpr.range - a2before.hpr.range) < 1 &&
  148. Math.abs(dEast) + Math.abs(dNorth) > 1e-6 &&
  149. alongRight < 0,
  150. JSON.stringify({
  151. hBefore: round(a2before.hpr.heading),
  152. hAfter: round(a2after.hpr.heading),
  153. headingDrift: round(headingDrift),
  154. pivotBefore: a2before.pivot,
  155. pivotAfter: a2after.pivot,
  156. direction: alongRight < 0 ? '沿相机左手方向(画面右移)' : '方向反了',
  157. }),
  158. )
  159. }
  160. // ---------- A3 右键拖拽只做轨道旋转 ----------
  161. // 注意方向:默认全球视角的 pitch 已在下限附近,向下拖会被夹住,所以这里向上拖(pitch 变大)。
  162. const a3before = await readState()
  163. const c3 = await center()
  164. await page.mouse.move(c3.x, c3.y)
  165. await page.mouse.down({ button: 'right' })
  166. await page.mouse.move(c3.x + 120, c3.y - 40, { steps: 8 })
  167. await page.mouse.up({ button: 'right' })
  168. await sleep(120)
  169. await waitSettled()
  170. const a3after = await readState()
  171. {
  172. const dHeading = ((a3after.hpr.heading - a3before.hpr.heading + 540) % 360) - 180
  173. const dPitch = a3after.hpr.pitch - a3before.hpr.pitch
  174. const dPivot = Math.abs(a3after.pivot.lon - a3before.pivot.lon) + Math.abs(a3after.pivot.lat - a3before.pivot.lat)
  175. check(
  176. 'A3-right-drag-orbits-only',
  177. '右键拖拽只做轨道旋转:heading 随 dx、pitch 随 dy 变化,range 与中心不动',
  178. dHeading > 20 &&
  179. dHeading < 45 &&
  180. dPitch > 5 &&
  181. Math.abs(a3after.hpr.range - a3before.hpr.range) < 1 &&
  182. dPivot < 1e-9,
  183. JSON.stringify({
  184. dHeading: round(dHeading),
  185. dPitch: round(dPitch),
  186. pitchBefore: round(a3before.hpr.pitch),
  187. pitchAfter: round(a3after.hpr.pitch),
  188. range: round(a3after.hpr.range),
  189. dPivot,
  190. }),
  191. )
  192. }
  193. // ---------- A4 滚轮只改弹簧臂长度 ----------
  194. const a4before = await readState()
  195. const c4 = await center()
  196. await page.mouse.move(c4.x, c4.y)
  197. await page.mouse.wheel({ deltaY: -240 })
  198. await sleep(150)
  199. const a4after = await readState()
  200. check(
  201. 'A4-wheel-changes-range-only',
  202. '滚轮只改弹簧臂长度:上滚拉近,heading/pitch/中心不动(默认滚轮缩放已被替换)',
  203. a4after.hpr.range < a4before.hpr.range * 0.95 &&
  204. Math.abs(a4after.hpr.heading - a4before.hpr.heading) < 0.05 &&
  205. Math.abs(a4after.hpr.pitch - a4before.hpr.pitch) < 0.05 &&
  206. Math.abs(a4after.pivot.lon - a4before.pivot.lon) < 1e-9 &&
  207. Math.abs(a4after.pivot.lat - a4before.pivot.lat) < 1e-9,
  208. JSON.stringify({ before: round(a4before.hpr.range), after: round(a4after.hpr.range), pivot: a4after.pivot }),
  209. )
  210. // ---------- A5 未选中时中心 = 屏幕中心的地面拾取点 ----------
  211. await waitSettled()
  212. const a5 = await readState()
  213. {
  214. const cx = a5.canvas.x + a5.canvas.w / 2
  215. const cy = a5.canvas.y + a5.canvas.h / 2
  216. const dx = a5.projection ? Math.abs(a5.projection.x - cx) : Infinity
  217. const dy = a5.projection ? Math.abs(a5.projection.y - cy) : Infinity
  218. check(
  219. 'A5-pivot-is-ground-pick',
  220. '未选中时中心 = 屏幕中心的地面拾取点(中心点投回画布即画布中心)',
  221. dx <= 2 && dy <= 2,
  222. JSON.stringify({ projection: a5.projection, canvasCenter: { x: round(cx, 1), y: round(cy, 1) }, dx: round(dx, 2), dy: round(dy, 2) }),
  223. )
  224. }
  225. // ---------- A6 选中换中心:平滑、保持 heading 与 range ----------
  226. const a6before = await readState()
  227. const sampled = await page.evaluate(async (target) => {
  228. const samples = []
  229. window.__cockpitMap.setCameraFocus(target)
  230. const started = performance.now()
  231. while (performance.now() - started < 1400) {
  232. const state = window.__cockpitMap.getCameraState()
  233. samples.push({
  234. t: Math.round(performance.now() - started),
  235. heading: state.hpr.heading,
  236. range: state.hpr.range,
  237. lon: state.pivot.lon,
  238. lat: state.pivot.lat,
  239. flying: state.flying,
  240. })
  241. if (!state.flying) break
  242. await new Promise((r) => setTimeout(r, 40))
  243. }
  244. return samples
  245. }, XIANGLING)
  246. {
  247. const headingFixed = sampled.every((s) => Math.abs(s.heading - a6before.hpr.heading) < 1e-9)
  248. const rangeFixed = sampled.every((s) => Math.abs(s.range - a6before.hpr.range) < 1e-9)
  249. const flyingFrames = sampled.filter((s) => s.flying).length
  250. const last = sampled[sampled.length - 1]
  251. const startLon = a6before.pivot.lon
  252. const dir = Math.sign(XIANGLING.lon - startLon) || 1
  253. const toward = sampled.map((s) => (s.lon - startLon) * dir)
  254. const monotonic = toward.every((v, i) => i === 0 || v >= toward[i - 1] - 1e-12)
  255. // 首帧不许瞬移:它离目标仍差大半个行程(相对起止跨度判断,与尺度无关)。
  256. const span = Math.abs(XIANGLING.lon - startLon)
  257. const firstGap = Math.abs(XIANGLING.lon - sampled[0].lon)
  258. check(
  259. 'A6-focus-flies-to-ground-point',
  260. '选中对象:中心平滑飞到该对象地表点,全程保持 heading 与 range(首帧不瞬移)',
  261. sampled.length >= 3 &&
  262. flyingFrames >= 1 &&
  263. headingFixed &&
  264. rangeFixed &&
  265. monotonic &&
  266. Math.abs(last.lon - XIANGLING.lon) < 1e-6 &&
  267. Math.abs(last.lat - XIANGLING.lat) < 1e-6 &&
  268. firstGap > span * 0.2,
  269. JSON.stringify({
  270. frames: sampled.length,
  271. flyingFrames,
  272. heading: round(last.heading),
  273. range: round(last.range),
  274. pivot: { lon: round(last.lon, 6), lat: round(last.lat, 6) },
  275. spanDeg: round(span),
  276. firstGapDeg: round(firstGap),
  277. }),
  278. )
  279. }
  280. // ---------- A7 取消选中不回退中心 ----------
  281. await waitSettled()
  282. const a7before = await readState()
  283. await page.evaluate(() => window.__cockpitMap.clearCameraFocus())
  284. await sleep(200)
  285. await waitSettled()
  286. const a7after = await readState()
  287. check(
  288. 'A7-clear-focus-keeps-pivot',
  289. '取消选中不立即回退中心(中心仍在刚选中的对象上,focus 标记清空)',
  290. Math.abs(a7after.pivot.lon - a7before.pivot.lon) < 1e-9 &&
  291. Math.abs(a7after.pivot.lat - a7before.pivot.lat) < 1e-9 &&
  292. a7after.focus === null,
  293. JSON.stringify({ pivot: a7after.pivot, focus: a7after.focus }),
  294. )
  295. // ---------- A8 平移把中心交回地面拾取点 ----------
  296. const c8 = await center()
  297. await page.mouse.move(c8.x, c8.y)
  298. await page.mouse.down({ button: 'left' })
  299. await page.mouse.move(c8.x + 40, c8.y + 20, { steps: 5 })
  300. await page.mouse.up({ button: 'left' })
  301. await sleep(150)
  302. await waitSettled()
  303. const a8 = await readState()
  304. check(
  305. 'A8-pan-returns-pivot-to-ground',
  306. '左键平移后中心回到地面拾取点(不再跟随选中对象),且仍落在画布中心',
  307. !!a8.projection &&
  308. Math.abs(a8.projection.x - (a8.canvas.x + a8.canvas.w / 2)) <= 2 &&
  309. Math.abs(a8.projection.y - (a8.canvas.y + a8.canvas.h / 2)) <= 2 &&
  310. Math.abs(a8.pivot.lon - XIANGLING.lon) > 1e-6,
  311. JSON.stringify({ pivot: a8.pivot, projection: a8.projection }),
  312. )
  313. // ---------- A10 阻尼在真机上生效(目标立刻跟手、实际态渐入收敛) ----------
  314. const c10 = await center()
  315. const damping = await page.evaluate(async (point) => {
  316. const read = () => window.__cockpitMap.getCameraState()
  317. window.__cockpitMap.setCameraFocus(point)
  318. await new Promise((r) => setTimeout(r, 40))
  319. const start = read()
  320. const samples = []
  321. const began = performance.now()
  322. // 采样窗要够长:换中心飞行 700ms + 阻尼把缺口从 ~0.02° 收敛到阈值 1e-7° 还要
  323. // τ·ln(0.02/1e-7) ≈ 1.3s,合计约 2s —— 之前的 2000ms 窗口刚好卡在边界上会假失败。
  324. while (performance.now() - began < 6000) {
  325. const s = read()
  326. samples.push({
  327. t: Math.round(performance.now() - began),
  328. settled: s.settled,
  329. flying: s.flying,
  330. lon: s.pivot.lon,
  331. shownLon: s.shownPivot ? s.shownPivot.lon : null,
  332. })
  333. if (s.settled) break
  334. await new Promise((r) => setTimeout(r, 30))
  335. }
  336. return { start, samples }
  337. }, XIANGLING)
  338. {
  339. const s = damping.samples
  340. const last = s[s.length - 1]
  341. // 实际态必须**滞后于**目标态:中途至少采样到一次 shown != target(否则就是瞬移,没有阻尼)。
  342. const lagged = s.some((x) => x.shownLon !== null && Math.abs(x.shownLon - x.lon) > 1e-9)
  343. const flyingFrames = s.filter((x) => x.flying).length
  344. // 单调收敛只对「换中心飞行结束之后」那一段成立:飞行期间目标自己在动(缓动推进),
  345. // 目标-实际缺口会先被拉开再收拢,拿全程做单调判断会误判成「阻尼没在收敛」。
  346. const afterFlight = s.filter((x) => !x.flying && x.shownLon !== null)
  347. const gaps = afterFlight.map((x) => Math.abs(x.lon - x.shownLon))
  348. const shrinking = gaps.every((g, i) => i === 0 || g <= gaps[i - 1] + 1e-12)
  349. check(
  350. 'A10-damping-is-live',
  351. '阻尼在真机上生效:飞行期间实际态滞后、飞行后缺口单调收敛到 0,最终 settled 且吸附到目标',
  352. lagged &&
  353. flyingFrames >= 1 &&
  354. gaps.length >= 2 &&
  355. shrinking &&
  356. last.settled === true &&
  357. Math.abs(last.lon - XIANGLING.lon) < 1e-6,
  358. JSON.stringify({
  359. frames: s.length,
  360. flyingFrames,
  361. afterFlightFrames: gaps.length,
  362. lagged,
  363. firstGap: round(gaps[0] || 0, 6),
  364. lastGap: round(gaps[gaps.length - 1] || 0, 6),
  365. lastLon: round(last.lon, 6),
  366. settled: last.settled,
  367. }),
  368. )
  369. }
  370. check('A9-no-page-errors', '走查期间无页面级报错', pageErrors.length === 0, JSON.stringify(pageErrors.slice(0, 3)))
  371. // 收尾核验:页面若在走查期间被 HMR 重载,上面的红绿都不作数。
  372. const sameRun = await pageStillSameRun()
  373. if (!sameRun) {
  374. runInvalidated = true
  375. }
  376. } finally {
  377. await browser.close()
  378. }
  379. const failed = results.filter((r) => !r.pass)
  380. if (runInvalidated) {
  381. console.log('\n⚠ 页面在走查期间被重载(并发编辑触发 Vite HMR)——本轮结果作废,请在没有其它会话改文件时重跑。')
  382. process.exit(2)
  383. }
  384. console.log(
  385. `\nT03 浏览器走查:${results.length - failed.length}/${results.length} 项通过${failed.length ? `,未通过:${failed.map((r) => r.id).join(', ')}` : ''}`,
  386. )
  387. process.exit(failed.length ? 1 : 0)