diff --git a/scripts/qa-draw-tool.mjs b/scripts/qa-draw-tool.mjs index 5e9620869..43f463a47 100644 --- a/scripts/qa-draw-tool.mjs +++ b/scripts/qa-draw-tool.mjs @@ -65,6 +65,27 @@ const LINE_CLIP = { x: 390, y: 200, width: 510, height: 380 }; const canvasShot = () => page.screenshot({ clip: LINE_CLIP, type: 'png', encoding: 'base64' }); +/** + * `canvasShot` once the globe has no imagery or terrain tiles left to load + * (bounded: a view that never settles is shot as it is, and the comparison + * then reports what it sees). `globe.tilesLoaded` covers the globe only: a + * 3D Tiles tileset on a keyed server can still be refining when it is true. + */ +async function settledCanvasShot() { + await page + .waitForFunction( + () => { + const scene = window.__godsEyeView.viewer.scene; + scene.requestRender(); + return scene.globe.tilesLoaded; + }, + { timeout: 15_000, polling: 100 }, + ) + .catch(() => {}); + await wait(200); + return canvasShot(); +} + /** * Amber pixels in one shot that are not amber in another of the same view: * a mark's own pixels, apart from imagery and panels that both shots share. @@ -505,12 +526,26 @@ try { await page.click(rowControl(lineId, 'swatch')); await page.click(rowControl(lineId, 'color-amber')); await markReaches(lineId, { color: 'amber', alpha: 1 }); + // Only the Line is on the board for its shots. The area's fill pulses, and + // under it borderline roofs drift into and out of amber between two shots + // of an unchanged scene, on the old build as on this one. + const others = await page.evaluate((id) => { + const marks = window.__gevAnnotations.list(); + const ids = marks.filter((m) => m.id !== id && !m.hidden).map((m) => m.id); + for (const other of ids) window.__gevAnnotations.setHidden(other, true); + return ids; + }, lineId); + for (const other of others) + await markReaches(other, { hidden: true, alpha: 0 }); await wait(800); - const shown = await canvasShot(); + // The fly-to above is still refining imagery near the camera at this + // point; a tile that sharpens between the two shots repaints roofs into + // and out of amber, so each shot waits for the globe to finish loading. + const shown = await settledCanvasShot(); await page.click(rowControl(lineId, 'toggle')); await markReaches(lineId, { hidden: true, alpha: 0 }); await wait(800); - const hiddenShot = await canvasShot(); + const hiddenShot = await settledCanvasShot(); // Amber the shown line had and the hidden one lost; amber that appears // only once it is hidden is noise, and must stay small beside it. const gone = await amberOnlyIn(shown, hiddenShot); @@ -522,12 +557,24 @@ try { ); await page.click(rowControl(lineId, 'toggle')); await wait(1200); - const back = await amberOnlyIn(await canvasShot(), hiddenShot); + const back = await amberOnlyIn(await settledCanvasShot(), hiddenShot); check( 'and paints again when shown', back > gone * 0.5, `${back} of ${gone} line pixels back`, ); + await page.evaluate((ids) => { + for (const other of ids) window.__gevAnnotations.setHidden(other, false); + }, others); + const restored = []; + for (const other of others) + if (await markReaches(other, { hidden: false, alpha: 1 })) + restored.push(other); + check( + 'the marks set aside for the Line shots are back on the board', + restored.length === others.length, + `${restored.length} of ${others.length} back`, + ); await shot('03-pins-and-shapes'); // ── Escape cancels the shape, a second Escape leaves draw mode ────────── @@ -575,32 +622,87 @@ try { : 'no CCTV camera or bundled site could be reached — the ownership case cannot be proved', ); if (target) { + // The camera pose the target was found at: a selecting click flies the + // camera to what it picked, so the drawing click has to come back here + // and find the target again, or it would land on plain ground. + await page.evaluate(() => { + const camera = window.__godsEyeView.viewer.camera; + window.__qaTargetView = { + destination: camera.positionWC.clone(), + orientation: { + heading: camera.heading, + pitch: camera.pitch, + roll: camera.roll, + }, + }; + }); + await page.evaluate(clearSelection); + await page.evaluate(startSelectionCount); await clickWorld(target.x, target.y, { settle: 900 }); - const selectedNormally = await page.evaluate(() => - Boolean(window.__godsEyeView.viewer.selectedEntity), - ); + const normal = await page.evaluate(readSelectionCount, target.key); check( `clicking a ${target.layer} selects it when nothing owns the pointer`, - selectedNormally, + // The CCTV branch looks for `CAM-` Cesium entities and reads the + // viewer's selection; neither build draws cameras that way any more, so + // it is not reached and the bundled data centre is the target. + normal.item > 0 || (target.layerId === 'cctv' && normal.entity), + `${normal.item} selection(s) of ${target.key}, ${normal.all} in all, entity ${normal.entity}`, ); + await page.evaluate(clearSelection); await page.evaluate(() => { - window.__godsEyeView.viewer.selectedEntity = undefined; + const viewer = window.__godsEyeView.viewer; + viewer.camera.cancelFlight?.(); + viewer.camera.setView(window.__qaTargetView); + viewer.scene.requestRender(); }); - await page.click('#draw-toggle'); - await page.click('.pp-mode-btn[data-shape="area"]'); - await clickWorld(target.x, target.y, { settle: 900 }); - const whileDrawing = await drawState(); - const selectedWhileDrawing = await page.evaluate(() => - Boolean(window.__godsEyeView.viewer.selectedEntity), + await wait(1500); + const again = await onScreen( + target.belongs, + target.contextPrefix, + target.key, ); check( - `clicking the same ${target.layer} while drawing adds a vertex and selects nothing`, - whileDrawing.vertices === 1 && selectedWhileDrawing === false, - `vertices ${whileDrawing.vertices}, selected ${selectedWhileDrawing}`, + `the ${target.layer} is picked again after the camera returns`, + Boolean(again), + again ? `at ${again.x},${again.y}` : 'not picked on screen any more', ); - await page.keyboard.press('Escape'); - await page.keyboard.press('Escape'); - await wait(150); + if (again) { + await page.click('#draw-toggle'); + await page.click('.pp-mode-btn[data-shape="area"]'); + // Draw mode's own panel must not have covered it: the drawing click has + // to land on the same item, not on plain ground. + const drawingSpot = await onScreen( + target.belongs, + target.contextPrefix, + target.key, + ); + check( + `the ${target.layer} is still picked once draw mode is open`, + Boolean(drawingSpot), + drawingSpot + ? `at ${drawingSpot.x},${drawingSpot.y}` + : 'not picked on screen in draw mode', + ); + if (drawingSpot) { + await page.evaluate(startSelectionCount); + await clickWorld(drawingSpot.x, drawingSpot.y, { settle: 900 }); + const whileDrawing = await drawState(); + const drawingSelection = await page.evaluate( + readSelectionCount, + target.key, + ); + check( + `clicking the same ${target.layer} while drawing adds a vertex and selects nothing`, + whileDrawing.vertices === 1 && + drawingSelection.all === 0 && + !drawingSelection.entity, + `vertices ${whileDrawing.vertices}, ${drawingSelection.all} selection(s), entity ${drawingSelection.entity}`, + ); + } + await page.keyboard.press('Escape'); + await page.keyboard.press('Escape'); + await wait(150); + } } // ── what a reload actually does ───────────────────────────────────────── @@ -877,22 +979,24 @@ async function findClickableTarget() { viewer.scene.requestRender(); }, camera); await wait(4000); - const spot = await onScreen( - (entity) => typeof entity.id === 'string' && entity.id.startsWith('CAM-'), - ); - if (spot) return { ...spot, layer: 'CCTV camera' }; + const spot = await onScreen(cameraEntity); + if (spot) + return { + ...spot, + layer: 'CCTV camera', + layerId: 'cctv', + belongs: cameraEntity, + contextPrefix: null, + }; } // Bundled infrastructure as the keyless fallback: a different guarded handler, // the same shared claim, and always present without a key. const site = await page .evaluate(async () => { - const viewer = window.__godsEyeView.viewer; - // The app registers each local feature's context as `:`. - const isDataCentre = (entity) => - window.__godsEyeView.entityRecords - .read(entity) - ?.contextId?.startsWith('local-datacenters:'); + // The app registers each local feature's context as `:`, + // with its coordinates, whether the layer draws Cesium entities or + // through the draw port. await window.__godsEyeView.dataManager.setEnabled( 'local-datacenters', true, @@ -900,30 +1004,16 @@ async function findClickableTarget() { origin: 'user', }, ); - const centreOf = (entity) => { - const time = viewer.clock.currentTime; - const point = entity.position?.getValue?.(time); - if (point) return point; - const hierarchy = entity.polygon?.hierarchy?.getValue?.(time); - if (hierarchy?.positions?.length) return hierarchy.positions[0]; - const line = entity.polyline?.positions?.getValue?.(time); - return line?.length ? line[0] : null; - }; for (let attempt = 0; attempt < 40; attempt += 1) { - for (let i = 0; i < viewer.dataSources.length; i += 1) { - for (const entity of viewer.dataSources.get(i).entities.values) { - if (!isDataCentre(entity)) continue; - const point = centreOf(entity); - if (!point) continue; - const carto = window.Cesium - ? window.Cesium.Cartographic.fromCartesian(point) - : viewer.scene.globe.ellipsoid.cartesianToCartographic(point); - if (!carto) continue; - return { - lon: (carto.longitude * 180) / Math.PI, - lat: (carto.latitude * 180) / Math.PI, - }; - } + const records = window.__gevContextStore?.entities?.values() ?? []; + for (const record of records) { + if (!String(record?.id).startsWith('local-datacenters:')) continue; + if ( + !Number.isFinite(record.latitude) || + !Number.isFinite(record.longitude) + ) + continue; + return { lon: record.longitude, lat: record.latitude }; } await new Promise((resolve) => setTimeout(resolve, 500)); } @@ -945,23 +1035,97 @@ async function findClickableTarget() { viewer.scene.requestRender(); }, site); await wait(4000); - const spot = await onScreen((entity) => + const spot = await onScreen(dataCentre, 'local-datacenters:'); + return spot + ? { + ...spot, + layer: 'data centre', + layerId: 'local-datacenters', + belongs: dataCentre, + contextPrefix: 'local-datacenters:', + } + : null; +} + +/** A CCTV camera entity (runs in the page through `onScreen`). */ +function cameraEntity(entity) { + return typeof entity.id === 'string' && entity.id.startsWith('CAM-'); +} + +/** + * A bundled data centre: a Cesium entity carrying its context, or a draw-port + * pick id `{ set, id }` from a set named `::` (runs in the + * page through `onScreen`). + */ +function dataCentre(entity) { + return ( window.__godsEyeView.entityRecords .read(entity) - ?.contextId?.startsWith('local-datacenters:'), + ?.contextId?.startsWith('local-datacenters:') || + (typeof entity?.set === 'string' && + entity.set.startsWith('local-datacenters:')) ); - return spot ? { ...spot, layer: 'data centre' } : null; +} + +/** Clear both selection slots (runs in the page). */ +function clearSelection() { + window.__godsEyeView.viewer.selectedEntity = undefined; + const store = window.__gevContextStore; + if (store) store.selectedEntityId = null; +} + +/** + * Count click selections from now on (runs in the page). A layer selects + * through the shared context store, which announces each selection with + * `gev:entity-selected`; one drawn through the draw port has no Cesium entity + * to set as the viewer's selection. Counting the announcements rather than + * reading the slot keeps an earlier selection, or a tracking layer refilling + * the slot without announcing, from deciding the result. + */ +function startSelectionCount() { + const seen = []; + window.removeEventListener('gev:entity-selected', window.__qaOnSelected); + window.__qaSelections = seen; + window.__qaOnSelected = (event) => seen.push(event.detail ?? null); + window.addEventListener('gev:entity-selected', window.__qaOnSelected); +} + +/** + * Selections since `startSelectionCount` (runs in the page): `all`, those of + * the item whose context id is `key`, and whether the viewer holds a selected + * Cesium entity. + * @param {string} key + * @returns {{ all: number, item: number, entity: boolean }} + */ +function readSelectionCount(key) { + const seen = window.__qaSelections || []; + return { + all: seen.length, + item: seen.filter((record) => record?.id === key).length, + entity: Boolean(window.__godsEyeView.viewer.selectedEntity), + }; } /** * Canvas coordinates of the first entity matching `belongs` that is on screen * and clear of the side panels. Annotation marks and the draw tool's own * preview vertices are on screen too, so the predicate has to be specific. + * With `contextPrefix`, the context store's records under that prefix are + * candidates too (at their ground coordinates), for a layer drawn through the + * draw port, which has no entities to walk; `belongs` then also receives the + * draw-port pick id. Returns the spot and `key`, the picked item's context id + * (from the entity's record or the draw-port item's `meta`; without a prefix, + * the entity's own id); with `wantKey`, only that item counts. */ -async function onScreen(belongs, timeout = 20_000) { +async function onScreen( + belongs, + contextPrefix = null, + wantKey = null, + timeout = 20_000, +) { return page .waitForFunction( - (belongsSource) => { + (belongsSource, prefix, want) => { // eslint-disable-next-line no-new-func const belongsTo = new Function(`return (${belongsSource})`)(); const viewer = window.__godsEyeView.viewer; @@ -972,29 +1136,62 @@ async function onScreen(belongs, timeout = 20_000) { if (source?.name === 'gev-draw-preview') continue; collections.push(source.entities); } + const candidates = []; for (const source of collections) { for (const entity of source.values) { if (!belongsTo(entity)) continue; const position = entity.position?.getValue?.( viewer.clock.currentTime, ); - if (!position) continue; - const window2d = scene.cartesianToCanvasCoordinates(position); - if (!window2d) continue; - const { x, y } = window2d; - if (!(x > 300 && x < 900 && y > 180 && y < 600)) continue; - // Projecting into the window is not the same as being clickable: - // the entity may be beyond the horizon or behind a building. Only a - // spot where the scene actually picks it proves the control case. - const hit = scene.pick({ x: Math.round(x), y: Math.round(y) }); - if (!hit?.id || !belongsTo(hit.id)) continue; - return { x: Math.round(x), y: Math.round(y) }; + if (position) candidates.push(position); } } + if (prefix) { + const ellipsoid = scene.globe.ellipsoid; + for (const record of window.__gevContextStore?.entities?.values() ?? + []) { + if (!String(record?.id).startsWith(prefix)) continue; + if ( + !Number.isFinite(record.latitude) || + !Number.isFinite(record.longitude) + ) + continue; + candidates.push( + ellipsoid.cartographicToCartesian({ + longitude: (record.longitude * Math.PI) / 180, + latitude: (record.latitude * Math.PI) / 180, + height: 0, + }), + ); + } + } + for (const position of candidates) { + const window2d = scene.cartesianToCanvasCoordinates(position); + if (!window2d) continue; + const { x, y } = window2d; + if (!(x > 300 && x < 900 && y > 180 && y < 600)) continue; + // Projecting into the window is not the same as being clickable: + // the entity may be beyond the horizon or behind a building. Only a + // spot where the scene actually picks it proves the control case. + const hit = scene.pick({ x: Math.round(x), y: Math.round(y) }); + if (!hit?.id || !belongsTo(hit.id)) continue; + // The picked item's context id: an entity's own record, or that of + // the object a draw-port item carries as its `meta`. + const pickId = hit.id; + const records = window.__godsEyeView.entityRecords; + const key = + records.read(pickId)?.contextId || + records.read(pickId.meta)?.contextId || + (prefix ? null : String(pickId.id)); + if (!key || (want && key !== want)) continue; + return { x: Math.round(x), y: Math.round(y), key }; + } return false; }, { timeout, polling: 500 }, belongs.toString(), + contextPrefix, + wantKey, ) .then( (handle) => handle.jsonValue(),