| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- /**
- * T29 真机走查:底图影像确实按配置挂上引擎(票 29)。
- *
- * 逻辑脚本(t29-basemap.mjs)把计划层测透了,这里只测浏览器里才成立的部分:
- * · 默认(source=none)= 不挂底图,宿主 data-map-basemap 为 not-configured(票 27 行为不变);
- * · source=natural-earth = 真的去取 Cesium 自带离线瓦片(断言发生 NaturalEarthII 请求且 200);
- * · source=xyz = 真的按 `{z}/{x}/{y}` 模板逐块取图(用仓库自带那份 TMS 目录当靶子,
- * 只证明通道通、模板被正确替换,不证明该地址的语义——TMS 的 y 轴与 XYZ 相反);
- * · source=tianditu 但没填 tk = 明确失败态(invalid + 浮标写明缺 tk),不是白屏也不是静默无底图。
- *
- * 天地图的**真实取图**需要真 tk,本票不伪造:模板拼装与开发代理/生产子域由逻辑脚本覆盖,
- * 真 key 联调留给维护者(或票 21 的底图校验)。
- *
- * 运行(需先起 `yarn dev`,服务在 http://localhost:8000/):
- * node .scratch/cockpit-shell-panel-layout/verify/t29-basemap-walkthrough.mjs
- */
- import { join } from 'node:path'
- import { tmpdir } from 'node:os'
- const BASE = process.env.COCKPIT_BASE || 'http://localhost:8000'
- const ENTRY_URL = process.env.COCKPIT_URL || `${BASE}/#/cockpit`
- const CHROME = process.env.CHROME_PATH || 'C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe'
- const PUPPETEER = 'file:///C:/Users/ASUS/.dsh/profiles/desktop/node_modules/puppeteer-core/lib/puppeteer/puppeteer-core.js'
- /** 仓库自带的离线瓦片目录(TMS 布局,仅作 xyz 通道的靶子)。 */
- const LOCAL_TILES = 'cesium/Assets/Textures/NaturalEarthII'
- const TILE_HINT = 'Assets/Textures/NaturalEarthII'
- const results = []
- function check(id, desc, pass, evidence) {
- results.push({ id, desc, pass })
- console.log(`${pass ? 'PASS' : 'FAIL'} ${id} ${desc}${evidence ? ` — ${evidence}` : ''}`)
- }
- const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
- const puppeteer = (await import(PUPPETEER)).default
- const browser = await puppeteer.launch({
- executablePath: CHROME,
- headless: true,
- userDataDir: join(tmpdir(), 'cockpit-t29-profile'),
- defaultViewport: { width: 1680, height: 950 },
- args: ['--no-sandbox', '--disable-dev-shm-usage', '--no-first-run', '--no-default-browser-check'],
- })
- console.log(`Chrome: ${await browser.version()}`)
- /** 开一页并记录它发出的瓦片请求(URL + 状态),等引擎到底。 */
- async function openPage(injectConfig) {
- const page = await browser.newPage()
- const tiles = []
- page.on('response', (response) => {
- const url = response.url()
- if (url.includes(TILE_HINT)) tiles.push({ url: url.replace(BASE, ''), status: response.status() })
- })
- if (injectConfig) await page.evaluateOnNewDocument(injectConfig)
- await page.goto(ENTRY_URL, { waitUntil: 'networkidle2', timeout: 60000 })
- await page.waitForSelector('.cockpit-shell', { timeout: 20000 })
- await page
- .waitForFunction(
- () => ['ready', 'failed', 'disabled'].includes(document.querySelector('.cockpit-shell__map .map-canvas')?.dataset.mapEngine),
- { timeout: 60000 },
- )
- .catch(() => {})
- await sleep(1500)
- return { page, tiles }
- }
- const readState = (page) =>
- page.evaluate(() => {
- const host = document.querySelector('.cockpit-shell__map .map-canvas')
- return {
- engine: (host && host.dataset.mapEngine) || '',
- basemap: (host && host.dataset.mapBasemap) || '',
- pill: host?.querySelector('[data-map-engine-status]')?.textContent?.trim() || '',
- }
- })
- try {
- // ---------- A1 默认:不配底图(票 27 行为不变) ----------
- {
- // 本机 .env.local 可能已打开配置文件层(票 21 的本地 GIS 配置);本条测内置默认,显式关掉。
- const { page, tiles } = await openPage(() => {
- window.__COCKPIT_CONFIG_URL__ = ''
- })
- const state = await readState(page)
- check(
- 'A1-none-by-default',
- '默认(map.basemap.source 缺省)不挂底图:data-map-basemap=not-configured,浮标照旧说「底图未配置」',
- state.engine === 'ready' && state.basemap === 'not-configured' && state.pill.includes('底图未配置'),
- JSON.stringify(state),
- )
- check('A1-no-tile-requests', '不配底图时不发任何影像请求', tiles.length === 0, `tiles=${tiles.length}`)
- await page.close()
- }
- // ---------- A2 内置离线影像:真的取到瓦片 ----------
- {
- const { page, tiles } = await openPage(() => {
- window.__COCKPIT_CONFIG__ = { map: { basemap: { source: 'natural-earth' } } }
- })
- const state = await readState(page)
- const ok = tiles.filter((t) => t.status === 200)
- check(
- 'A2-natural-earth-live',
- 'source=natural-earth:宿主标成 natural-earth、浮标显示底图名、且真的取到离线瓦片(HTTP 200)',
- state.engine === 'ready' && state.basemap === 'natural-earth' && state.pill.includes('底图:') && ok.length > 0,
- JSON.stringify({ state, tiles: tiles.slice(0, 3), okCount: ok.length }),
- )
- await page.close()
- }
- // ---------- A3 自定义 XYZ:模板被逐块替换 ----------
- {
- const { page, tiles } = await openPage(() => {
- window.__COCKPIT_CONFIG__ = {
- map: {
- basemap: {
- source: 'xyz',
- url: 'cesium/Assets/Textures/NaturalEarthII/{z}/{x}/{y}.jpg',
- minimumLevel: 0,
- maximumLevel: 2,
- },
- },
- }
- })
- const state = await readState(page)
- const ok = tiles.filter((t) => t.status === 200)
- const zxy = ok.filter((t) => /NaturalEarthII\/\d+\/\d+\/\d+\.jpg$/.test(t.url))
- check(
- 'A3-xyz-template-live',
- 'source=xyz:按 {z}/{x}/{y} 模板真的逐块取图(HTTP 200),模板占位符被替换成数字',
- state.basemap === 'xyz' && zxy.length > 0,
- JSON.stringify({ basemap: state.basemap, sample: zxy.slice(0, 2), okCount: ok.length }),
- )
- await page.close()
- }
- // ---------- A4 天地图缺 tk:明确失败态,不白屏 ----------
- {
- const { page } = await openPage(() => {
- window.__COCKPIT_CONFIG__ = { map: { basemap: { source: 'tianditu' } } }
- })
- const state = await readState(page)
- check(
- 'A4-tianditu-without-token',
- 'source=tianditu 但没有 tk:data-map-basemap=invalid、浮标写明缺 tk 并说明回退,引擎仍就绪',
- state.engine === 'ready' &&
- state.basemap === 'invalid' &&
- state.pill.includes('底图配置无效') &&
- state.pill.includes('tk') &&
- state.pill.includes('回退'),
- JSON.stringify(state),
- )
- await page.close()
- }
- // ---------- A5 配置写错来源:归一化退回 none,不炸 ----------
- {
- const { page } = await openPage(() => {
- window.__COCKPIT_CONFIG__ = { map: { basemap: { source: 'baidu' } } }
- })
- const state = await readState(page)
- check(
- 'A5-unknown-source-falls-back',
- '未知底图来源:退回 none(not-configured)而不是报错或挂出坏底图',
- state.engine === 'ready' && state.basemap === 'not-configured' && state.pill.includes('底图未配置'),
- JSON.stringify(state),
- )
- await page.close()
- }
- } finally {
- await browser.close()
- }
- const failed = results.filter((r) => !r.pass)
- console.log('')
- console.log(`T29 走查:${results.length - failed.length}/${results.length} 项通过。`)
- if (failed.length) {
- console.log('未通过:' + failed.map((f) => f.id).join(', '))
- process.exit(1)
- }
- console.log('T29 走查全部通过。')
|