t03-camera-walkthrough.mjs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. function check(id, desc, pass, evidence) {
  26. results.push({ id, desc, pass })
  27. console.log(`${pass ? 'PASS' : 'FAIL'} ${id} ${desc}${evidence ? ` — ${evidence}` : ''}`)
  28. }
  29. const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
  30. const round = (n, digits = 3) => Number(n.toFixed(digits))
  31. const puppeteer = (await import(PUPPETEER)).default
  32. const browser = await puppeteer.launch({
  33. executablePath: CHROME,
  34. headless: true,
  35. userDataDir: join(tmpdir(), 'cockpit-t03-profile'),
  36. defaultViewport: { width: 1680, height: 950 },
  37. args: ['--no-sandbox', '--disable-dev-shm-usage', '--no-first-run', '--no-default-browser-check'],
  38. })
  39. console.log(`Chrome: ${await browser.version()}`)
  40. try {
  41. const page = await browser.newPage()
  42. const pageErrors = []
  43. page.on('pageerror', (e) => pageErrors.push(String(e && e.message ? e.message : e)))
  44. await page.goto(ENTRY_URL, { waitUntil: 'networkidle2', timeout: 60000 })
  45. await page.waitForSelector('.cockpit-shell', { timeout: 20000 })
  46. await page
  47. .waitForFunction(
  48. () => document.querySelector('.cockpit-shell__map .map-canvas')?.dataset.mapEngine === 'ready',
  49. { timeout: 60000 },
  50. )
  51. .catch(() => {})
  52. await page.waitForFunction(() => !!window.__cockpitMap, { timeout: 20000 })
  53. await sleep(800)
  54. const readState = () =>
  55. page.evaluate(() => {
  56. const state = window.__cockpitMap.getCameraState()
  57. const canvas = document.querySelector('.cockpit-shell__map .map-canvas canvas')
  58. const rect = canvas ? canvas.getBoundingClientRect() : null
  59. return {
  60. ...state,
  61. dataAttr: document.querySelector('.cockpit-shell__map .map-canvas')?.dataset.camera || '',
  62. canvas: rect ? { x: rect.x, y: rect.y, w: rect.width, h: rect.height } : null,
  63. projection: window.__cockpitMap.projectCameraPivot(),
  64. }
  65. })
  66. const center = async () => {
  67. const st = await readState()
  68. return { x: st.canvas.x + st.canvas.w / 2, y: st.canvas.y + st.canvas.h / 2 }
  69. }
  70. // ---------- A1 就绪与状态可读 ----------
  71. const base = await readState()
  72. check(
  73. 'A1-camera-ready',
  74. '引擎就绪后相机状态可读:弹簧臂参数与中心都有值,宿主 data-camera 同步',
  75. !!base &&
  76. !!base.pivot &&
  77. base.pivot.lon > -180 &&
  78. base.hpr.range > 0 &&
  79. base.dataAttr.includes('pivot=') &&
  80. base.dataAttr.includes('h='),
  81. JSON.stringify({ hpr: base.hpr, pivot: base.pivot, data: base.dataAttr }),
  82. )
  83. // ---------- A2 左键拖拽只平移(Cesium 默认左键旋转已被替换) ----------
  84. const a2before = await readState()
  85. const c2 = await center()
  86. await page.mouse.move(c2.x, c2.y)
  87. await page.mouse.down({ button: 'left' })
  88. await page.mouse.move(c2.x + 160, c2.y, { steps: 8 })
  89. await page.mouse.up({ button: 'left' })
  90. await sleep(120)
  91. const a2after = await readState()
  92. {
  93. const dEast = a2after.pivot.lon - a2before.pivot.lon
  94. const dNorth = a2after.pivot.lat - a2before.pivot.lat
  95. // 相机右手方向(东北分量):heading 上「前方」=(sin h, cos h),右手 = (cos h, -sin h)。
  96. const h = (a2before.hpr.heading * Math.PI) / 180
  97. const alongRight = dEast * Math.cos(h) + dNorth * -Math.sin(h)
  98. check(
  99. 'A2-left-drag-pans-only',
  100. '左键拖拽只平移:heading/pitch/range 不变,中心沿相机右手方向移动(默认左键旋转已被替换)',
  101. Math.abs(a2after.hpr.heading - a2before.hpr.heading) < 0.05 &&
  102. Math.abs(a2after.hpr.pitch - a2before.hpr.pitch) < 0.05 &&
  103. Math.abs(a2after.hpr.range - a2before.hpr.range) < 1 &&
  104. Math.abs(dEast) + Math.abs(dNorth) > 1e-6 &&
  105. alongRight > 0,
  106. JSON.stringify({
  107. hBefore: round(a2before.hpr.heading),
  108. hAfter: round(a2after.hpr.heading),
  109. pivotBefore: a2before.pivot,
  110. pivotAfter: a2after.pivot,
  111. rightSign: alongRight > 0 ? '沿右手方向' : '反向',
  112. }),
  113. )
  114. }
  115. // ---------- A3 右键拖拽只做轨道旋转 ----------
  116. // 注意方向:默认全球视角的 pitch 已在下限附近,向下拖会被夹住,所以这里向上拖(pitch 变大)。
  117. const a3before = await readState()
  118. const c3 = await center()
  119. await page.mouse.move(c3.x, c3.y)
  120. await page.mouse.down({ button: 'right' })
  121. await page.mouse.move(c3.x + 120, c3.y - 40, { steps: 8 })
  122. await page.mouse.up({ button: 'right' })
  123. await sleep(120)
  124. const a3after = await readState()
  125. {
  126. const dHeading = ((a3after.hpr.heading - a3before.hpr.heading + 540) % 360) - 180
  127. const dPitch = a3after.hpr.pitch - a3before.hpr.pitch
  128. const dPivot = Math.abs(a3after.pivot.lon - a3before.pivot.lon) + Math.abs(a3after.pivot.lat - a3before.pivot.lat)
  129. check(
  130. 'A3-right-drag-orbits-only',
  131. '右键拖拽只做轨道旋转:heading 随 dx、pitch 随 dy 变化,range 与中心不动',
  132. dHeading > 20 &&
  133. dHeading < 45 &&
  134. dPitch > 5 &&
  135. Math.abs(a3after.hpr.range - a3before.hpr.range) < 1 &&
  136. dPivot < 1e-9,
  137. JSON.stringify({
  138. dHeading: round(dHeading),
  139. dPitch: round(dPitch),
  140. pitchBefore: round(a3before.hpr.pitch),
  141. pitchAfter: round(a3after.hpr.pitch),
  142. range: round(a3after.hpr.range),
  143. dPivot,
  144. }),
  145. )
  146. }
  147. // ---------- A4 滚轮只改弹簧臂长度 ----------
  148. const a4before = await readState()
  149. const c4 = await center()
  150. await page.mouse.move(c4.x, c4.y)
  151. await page.mouse.wheel({ deltaY: -240 })
  152. await sleep(150)
  153. const a4after = await readState()
  154. check(
  155. 'A4-wheel-changes-range-only',
  156. '滚轮只改弹簧臂长度:上滚拉近,heading/pitch/中心不动(默认滚轮缩放已被替换)',
  157. a4after.hpr.range < a4before.hpr.range * 0.95 &&
  158. Math.abs(a4after.hpr.heading - a4before.hpr.heading) < 0.05 &&
  159. Math.abs(a4after.hpr.pitch - a4before.hpr.pitch) < 0.05 &&
  160. Math.abs(a4after.pivot.lon - a4before.pivot.lon) < 1e-9 &&
  161. Math.abs(a4after.pivot.lat - a4before.pivot.lat) < 1e-9,
  162. JSON.stringify({ before: round(a4before.hpr.range), after: round(a4after.hpr.range), pivot: a4after.pivot }),
  163. )
  164. // ---------- A5 未选中时中心 = 屏幕中心的地面拾取点 ----------
  165. const a5 = await readState()
  166. {
  167. const cx = a5.canvas.x + a5.canvas.w / 2
  168. const cy = a5.canvas.y + a5.canvas.h / 2
  169. const dx = a5.projection ? Math.abs(a5.projection.x - cx) : Infinity
  170. const dy = a5.projection ? Math.abs(a5.projection.y - cy) : Infinity
  171. check(
  172. 'A5-pivot-is-ground-pick',
  173. '未选中时中心 = 屏幕中心的地面拾取点(中心点投回画布即画布中心)',
  174. dx <= 2 && dy <= 2,
  175. JSON.stringify({ projection: a5.projection, canvasCenter: { x: round(cx, 1), y: round(cy, 1) }, dx: round(dx, 2), dy: round(dy, 2) }),
  176. )
  177. }
  178. // ---------- A6 选中换中心:平滑、保持 heading 与 range ----------
  179. const a6before = await readState()
  180. const sampled = await page.evaluate(async (target) => {
  181. const samples = []
  182. window.__cockpitMap.setCameraFocus(target)
  183. const started = performance.now()
  184. while (performance.now() - started < 1400) {
  185. const state = window.__cockpitMap.getCameraState()
  186. samples.push({
  187. t: Math.round(performance.now() - started),
  188. heading: state.hpr.heading,
  189. range: state.hpr.range,
  190. lon: state.pivot.lon,
  191. lat: state.pivot.lat,
  192. flying: state.flying,
  193. })
  194. if (!state.flying) break
  195. await new Promise((r) => setTimeout(r, 40))
  196. }
  197. return samples
  198. }, XIANGLING)
  199. {
  200. const headingFixed = sampled.every((s) => Math.abs(s.heading - a6before.hpr.heading) < 1e-9)
  201. const rangeFixed = sampled.every((s) => Math.abs(s.range - a6before.hpr.range) < 1e-9)
  202. const flyingFrames = sampled.filter((s) => s.flying).length
  203. const last = sampled[sampled.length - 1]
  204. const startLon = a6before.pivot.lon
  205. const dir = Math.sign(XIANGLING.lon - startLon) || 1
  206. const toward = sampled.map((s) => (s.lon - startLon) * dir)
  207. const monotonic = toward.every((v, i) => i === 0 || v >= toward[i - 1] - 1e-12)
  208. // 首帧不许瞬移:它离目标仍差大半个行程(相对起止跨度判断,与尺度无关)。
  209. const span = Math.abs(XIANGLING.lon - startLon)
  210. const firstGap = Math.abs(XIANGLING.lon - sampled[0].lon)
  211. check(
  212. 'A6-focus-flies-to-ground-point',
  213. '选中对象:中心平滑飞到该对象地表点,全程保持 heading 与 range(首帧不瞬移)',
  214. sampled.length >= 3 &&
  215. flyingFrames >= 1 &&
  216. headingFixed &&
  217. rangeFixed &&
  218. monotonic &&
  219. Math.abs(last.lon - XIANGLING.lon) < 1e-6 &&
  220. Math.abs(last.lat - XIANGLING.lat) < 1e-6 &&
  221. firstGap > span * 0.2,
  222. JSON.stringify({
  223. frames: sampled.length,
  224. flyingFrames,
  225. heading: round(last.heading),
  226. range: round(last.range),
  227. pivot: { lon: round(last.lon, 6), lat: round(last.lat, 6) },
  228. spanDeg: round(span),
  229. firstGapDeg: round(firstGap),
  230. }),
  231. )
  232. }
  233. // ---------- A7 取消选中不回退中心 ----------
  234. const a7before = await readState()
  235. await page.evaluate(() => window.__cockpitMap.clearCameraFocus())
  236. await sleep(200)
  237. const a7after = await readState()
  238. check(
  239. 'A7-clear-focus-keeps-pivot',
  240. '取消选中不立即回退中心(中心仍在刚选中的对象上,focus 标记清空)',
  241. Math.abs(a7after.pivot.lon - a7before.pivot.lon) < 1e-9 &&
  242. Math.abs(a7after.pivot.lat - a7before.pivot.lat) < 1e-9 &&
  243. a7after.focus === null,
  244. JSON.stringify({ pivot: a7after.pivot, focus: a7after.focus }),
  245. )
  246. // ---------- A8 平移把中心交回地面拾取点 ----------
  247. const c8 = await center()
  248. await page.mouse.move(c8.x, c8.y)
  249. await page.mouse.down({ button: 'left' })
  250. await page.mouse.move(c8.x + 40, c8.y + 20, { steps: 5 })
  251. await page.mouse.up({ button: 'left' })
  252. await sleep(150)
  253. const a8 = await readState()
  254. check(
  255. 'A8-pan-returns-pivot-to-ground',
  256. '左键平移后中心回到地面拾取点(不再跟随选中对象),且仍落在画布中心',
  257. !!a8.projection &&
  258. Math.abs(a8.projection.x - (a8.canvas.x + a8.canvas.w / 2)) <= 2 &&
  259. Math.abs(a8.projection.y - (a8.canvas.y + a8.canvas.h / 2)) <= 2 &&
  260. Math.abs(a8.pivot.lon - XIANGLING.lon) > 1e-6,
  261. JSON.stringify({ pivot: a8.pivot, projection: a8.projection }),
  262. )
  263. check('A9-no-page-errors', '走查期间无页面级报错', pageErrors.length === 0, JSON.stringify(pageErrors.slice(0, 3)))
  264. } finally {
  265. await browser.close()
  266. }
  267. const failed = results.filter((r) => !r.pass)
  268. console.log(
  269. `\nT03 浏览器走查:${results.length - failed.length}/${results.length} 项通过${failed.length ? `,未通过:${failed.map((r) => r.id).join(', ')}` : ''}`,
  270. )
  271. process.exit(failed.length ? 1 : 0)