From c53a6d1c51e301d63543264da790c03acc5fd115 Mon Sep 17 00:00:00 2001 From: Sameh Khamis Date: Wed, 18 Nov 2026 17:23:35 -0800 Subject: [PATCH] Test that a mission label's click never acts on the card or reader beneath it ALPR, News, Wildlife Sightings, Fire Perimeters, GhostMaps and Drone Airspace each skip their own card or overlay hit test when a painted label took the click. Each now has a test that a click the label took does not select the camera or sighting, open the report, or open the card's link, and lets go of the current selection, while the same click off the label still acts. ALPR's presentation gets its first test file. Co-authored-by: Manjunath Paparaju --- scripts/format-scope.json | 1 + src/layers/alpr/presentation.test.mjs | 122 ++++++++++++++++++ src/layers/droneAirspace/ownership.test.mjs | 38 +++++- src/layers/ghostMaps/lifecycle.test.mjs | 47 +++++++ src/layers/news/layer.test.mjs | 45 ++++++- src/layers/perimeters/ownership.test.mjs | 56 +++++++- .../wildlifeSightings/selection.test.mjs | 30 +++++ 7 files changed, 331 insertions(+), 8 deletions(-) create mode 100644 src/layers/alpr/presentation.test.mjs diff --git a/scripts/format-scope.json b/scripts/format-scope.json index 6b0b12a9a8..ccf8f554c6 100644 --- a/scripts/format-scope.json +++ b/scripts/format-scope.json @@ -660,6 +660,7 @@ "src/layers/alpr/model.js", "src/layers/alpr/policy.js", "src/layers/alpr/presentation.js", + "src/layers/alpr/presentation.test.mjs", "src/layers/alpr/visuals.js", "src/layers/alpr/overlay.js", "src/layers/alpr/overlay.test.mjs", diff --git a/src/layers/alpr/presentation.test.mjs b/src/layers/alpr/presentation.test.mjs new file mode 100644 index 0000000000..223083a150 --- /dev/null +++ b/src/layers/alpr/presentation.test.mjs @@ -0,0 +1,122 @@ +import test from 'node:test'; +import assert from 'node:assert/strict'; +import * as Cesium from 'cesium'; +import { fakeInputUnderLabel } from '../../input/testDoubles.mjs'; +import { fakeDrawPort } from '../../testSupport/fakeDrawPort.mjs'; +import { bindFakeCesiumScene } from '../../testSupport/fakeCesiumScene.mjs'; +import { createAlprMarkerSet } from './markerState.js'; +import { createAlprPresentation } from './presentation.js'; + +/** One camera drawn and painted at (100, 100), over a viewer whose clicks a + * label may take. */ +function harness(t) { + const previousImage = globalThis.Image; + const project = Cesium.SceneTransforms.worldToWindowCoordinates; + globalThis.Image = class { + complete = true; + naturalWidth = 128; + }; + Cesium.SceneTransforms.worldToWindowCoordinates = () => ({ x: 100, y: 100 }); + const record = { + id: 'camera:0', + latitude: 30, + longitude: -97, + directionDeg: 90, + }; + const drawPort = fakeDrawPort(); + const markers = createAlprMarkerSet(); + markers.attach(drawPort.icons({ id: 'alpr-cameras:test:markers' })); + const viewer = { + camera: { + positionWC: Cesium.Cartesian3.fromDegrees(-97, 30, 500), + pickEllipsoid: () => Cesium.Cartesian3.fromDegrees(-97, 30), + }, + scene: { + canvas: { clientWidth: 800, clientHeight: 600 }, + globe: { show: true, getHeight: () => 150 }, + pick: () => null, + }, + }; + const { unbind } = bindFakeCesiumScene(viewer, { draw: drawPort }); + const fake = fakeInputUnderLabel(viewer); + const state = { + viewer, + enabled: true, + selectedId: null, + markers, + records: [record], + recordById: new Map([[record.id, record]]), + renderRevision: 0, + lastAnchorSampleAt: 0, + }; + let selected = null; + let paint; + const presentation = createAlprPresentation({ + state, + source: { label: 'Test cameras' }, + services: { + render: { governorRequestRender() {} }, + context: { + clearSelectedEntityContextForLayer() { + selected = null; + }, + getSelectedEntityContext: () => selected, + registerEntityContext() {}, + removeEntityContextsForLayer() {}, + selectEntityContext(carrier) { + selected = { id: carrier.id, layerId: 'alpr-cameras' }; + }, + }, + groundFloor: { cachedGroundFloor: () => 150 }, + overlays: { + registerPaintLane(_lane, painter) { + paint = painter; + return { setActive() {}, requestPaint() {}, unregister() {} }; + }, + keyholeAlpha: () => 1, + }, + }, + }); + presentation.initOverlay(); + presentation.installInteraction(viewer); + t.after(() => { + presentation.destroyOverlay(); + fake.destroy(); + unbind(); + if (previousImage === undefined) delete globalThis.Image; + else globalThis.Image = previousImage; + Cesium.SceneTransforms.worldToWindowCoordinates = project; + }); + presentation.renderRecords(); + const ctx = { + globalAlpha: 1, + save() {}, + restore() {}, + beginPath() {}, + moveTo() {}, + lineTo() {}, + closePath() {}, + stroke() {}, + fill() {}, + createLinearGradient: () => ({ addColorStop() {} }), + drawImage() {}, + }; + paint({ ctx, width: 800, height: 600 }); + return { state, fake, at: { x: 100, y: 100 } }; +} + +test('a click a label painted over a camera takes never selects it, and lets go of the selected camera', (t) => { + const h = harness(t); + h.fake.label.over = true; + h.fake.click(h.at); + assert.equal(h.state.selectedId, null, 'the camera under the label'); + + // Off the label, the same click selects the camera. + h.fake.label.over = false; + h.fake.click(h.at); + assert.equal(h.state.selectedId, 'camera:0'); + + h.fake.label.over = true; + h.fake.click(h.at); + assert.equal(h.state.selectedId, null, 'the selection is let go'); +}); diff --git a/src/layers/droneAirspace/ownership.test.mjs b/src/layers/droneAirspace/ownership.test.mjs index 3dbb16a583..2f52148c40 100644 --- a/src/layers/droneAirspace/ownership.test.mjs +++ b/src/layers/droneAirspace/ownership.test.mjs @@ -2,7 +2,7 @@ import assert from 'node:assert/strict'; import test from 'node:test'; import { createDroneAirspaceLayer } from './index.js'; import { layerFeedState } from '../../data/feedState.js'; -import { fakeInput } from '../../input/testDoubles.mjs'; +import { fakeInputUnderLabel } from '../../input/testDoubles.mjs'; import { bindFakeDraw } from '../../testSupport/fakeDrawPort.mjs'; // The fills set of the layer the last harness made: zone picks name it. @@ -19,11 +19,12 @@ function harness(source, { pick = () => null, cardHit = () => null } = {}) { const owners = new Map(); const viewer = { scene: { pick } }; const { draw } = bindFakeDraw(viewer); - const input = fakeInput(viewer); + const input = fakeInputUnderLabel(viewer); const clicks = { handler: (click) => input.click(click.position), + // Only the label's own overlay handler is left. get destroyed() { - return input.input.subscriberCount() === 0 ? 1 : 0; + return input.input.subscriberCount() === 1 ? 1 : 0; }, }; const layer = createDroneAirspaceLayer({ @@ -61,6 +62,8 @@ function harness(source, { pick = () => null, cardHit = () => null } = {}) { overlay, clicks, owners, + /** A label painted over the map: while `over`, it takes each click. */ + label: input.label, }; } @@ -143,6 +146,35 @@ test('clicking a zone publishes its card; empty space clears it', async () => { assert.equal(h.overlay.entries.get('drone-airspace')?.length ?? 0, 0); }); +test('a click a label painted over the open zone card takes closes the card, as a click beside it does', async () => { + let pickResult = null; + let cardHitResult = null; + const h = harness( + { getSnapshot: async () => [row] }, + { pick: () => pickResult, cardHit: () => cardHitResult }, + ); + await h.layer.update(h.viewer); + pickResult = zonePick('drone-airspace:310:0'); + h.clicks.handler({ position: { x: 10, y: 10 } }); + const [card] = h.overlay.entries.get('drone-airspace'); + + // Off the label, a click on the open card keeps it open. + pickResult = null; + cardHitResult = { entryId: card.id }; + h.clicks.handler({ position: { x: 12, y: 12 } }); + assert.equal(h.overlay.entries.get('drone-airspace')?.length, 1); + + // The open card lies under the label: the click is not on the card. + h.label.over = true; + h.clicks.handler({ position: { x: 12, y: 12 } }); + assert.equal( + h.overlay.entries.get('drone-airspace')?.length ?? 0, + 0, + 'the card is closed', + ); + h.layer.destroy(); +}); + test('late refresh cannot publish after disable or destroy', async () => { for (const action of ['disable', 'destroy']) { let resolve, signal; diff --git a/src/layers/ghostMaps/lifecycle.test.mjs b/src/layers/ghostMaps/lifecycle.test.mjs index 675bdefa24..e1276f963c 100644 --- a/src/layers/ghostMaps/lifecycle.test.mjs +++ b/src/layers/ghostMaps/lifecycle.test.mjs @@ -3,6 +3,7 @@ import test from 'node:test'; import { setLocale } from '../../i18n/index.js'; import { createGhostMapsLayer } from './index.js'; import { bindFakeDraw } from '../../testSupport/fakeDrawPort.mjs'; +import { inputFor } from '../../input/dispatcher.js'; import { GHOSTMAPS_PRODUCTS, GHOSTMAPS_LATEST_DROP } from './catalog.js'; function element(localName, children = [], value = '', attributes = {}) { @@ -672,3 +673,49 @@ test('narrowing to a window the loaded data covers clears a failed wider reload' assert.equal(layer.getStats().error, null); assert.equal(layer.getStats().count, 1); }); + +test("a click a label painted over the open card takes never opens the card's link, and closes the card", async (t) => { + const cards = []; + const opened = []; + let cardUnder = null; + const { layer, viewer, click } = fixture(t, { + now: () => Date.UTC(2026, 10, 3, 12), + reports: [ + { + name: 'Linked', + description: + '' + + '
Date11/2/2026
Sourcereport
', + }, + ], + controls: { + overlayHost: { + setEntries: (_id, entries) => cards.push(entries), + hitTest: () => (cardUnder ? { entryId: cardUnder } : null), + }, + picking: { resolvePickId: (picked) => picked?.id ?? null }, + openExternal: (url) => opened.push(url), + }, + }); + // A label painted over the map, as a Space Missions label is: while it is + // over, it takes each click. + let labelOver = false; + inputFor(viewer).onOverlayClick(() => labelOver); + await layer.update(viewer); + click('ghostmaps:cip:0:0'); + const [card] = cards.at(-1); + assert.equal(card.interactive, true); + + // The open card lies under the label: the click opens nothing. + cardUnder = card.id; + labelOver = true; + click(null); + assert.deepEqual(opened, [], 'the link beneath the label is not opened'); + assert.deepEqual(cards.at(-1), [], 'the card is closed'); + + // Off the label, a click on the open card opens its link. + labelOver = false; + click('ghostmaps:cip:0:0'); + click(null); + assert.deepEqual(opened, ['https://example.com/report']); +}); diff --git a/src/layers/news/layer.test.mjs b/src/layers/news/layer.test.mjs index 9804011d77..824de5a9f9 100644 --- a/src/layers/news/layer.test.mjs +++ b/src/layers/news/layer.test.mjs @@ -6,6 +6,8 @@ import { bindFakeDraw } from '../../testSupport/fakeDrawPort.mjs'; import { fakeCameraPort } from '../../testSupport/fakeCameraPort.mjs'; import { NEWS_KEY_PROVIDERS, createNewsLayer } from './index.js'; import { getSelectedEntityContext } from '../../data/contextStore.js'; +import { fakeInputUnderLabel } from '../../input/testDoubles.mjs'; +import { NEWS_OVERLAY_SOURCE_ID } from './model.js'; const NOW = Date.parse('2026-10-28T10:00:00Z'); const hotspotsPayload = { @@ -51,6 +53,8 @@ function fixture( now = () => NOW, quality, pictureCards, + underLabel = false, + hitTestOverlay = () => null, } = {}, ) { const calls = { feed: [], hotspots: 0, outline: 0 }; @@ -137,11 +141,18 @@ function fixture( for (const fn of [...clickHandlers]) fn({ screenPosition: { x: 10, y: 10 }, topHit }); }; + // Or the scene's own dispatcher, with a label that may be painted over + // the map (`fake.label.over`). + let fake = null; + if (underLabel) { + viewer.scene = { canvas: {}, pick: () => null }; + fake = fakeInputUnderLabel(viewer); + } const layer = createNewsLayer({ source, overlayHost, - inputFor: () => input, - hitTestOverlay: () => null, + inputFor: () => fake?.input ?? input, + hitTestOverlay, now, matchMedia: () => ({ matches: reduced }), getViewCentre: () => centre(), @@ -158,6 +169,7 @@ function fixture( layer.attachShellServices({ runNavigation: (navigate) => navigate() }); t.after(() => { layer.destroy(); + fake?.destroy(); unbind(); if (originalWindow === undefined) delete globalThis.window; else globalThis.window = originalWindow; @@ -172,6 +184,7 @@ function fixture( items, click, clickHandlers, + fake, }; } @@ -1285,6 +1298,34 @@ test('a click on a marker selects its report, on a beacon chooses its hotspot, o assert.equal(clickHandlers.size, 0); }); +test("a click a label painted over a report's card takes never opens the card, and lets go of the report", async (t) => { + let activated = 0; + const { layer, fake } = fixture(t, { + height: 500_000, + underLabel: true, + // A report's card lies under every click. + hitTestOverlay: (_x, _y, { sourceId }) => + sourceId === NEWS_OVERLAY_SOURCE_ID + ? { entry: { activate: () => activated++ } } + : null, + }); + layer.enable(); + await layer.update(); + await flush(); + layer.setParams({ reportId: 'major', pin: 0 }); + assert.equal(layer.getRowControls().feed.selected.id, 'major'); + + fake.label.over = true; + fake.click(); + assert.equal(activated, 0, 'the card beneath the label is not opened'); + assert.equal(layer.getRowControls().feed.selected, null); + + // Off the label, the same click opens the card. + fake.label.over = false; + fake.click(); + assert.equal(activated, 1); +}); + test('zoomed out, a hotspot is one beacon; off, nothing draws; destroyed, the sets go', async (t) => { const { layer, items } = fixture(t); assert.deepEqual(items('points'), []); diff --git a/src/layers/perimeters/ownership.test.mjs b/src/layers/perimeters/ownership.test.mjs index 61cfde2bbf..a996eedeb7 100644 --- a/src/layers/perimeters/ownership.test.mjs +++ b/src/layers/perimeters/ownership.test.mjs @@ -1,7 +1,7 @@ import assert from 'node:assert/strict'; import test from 'node:test'; import { createFirePerimetersLayer } from './index.js'; -import { fakeInput } from '../../input/testDoubles.mjs'; +import { fakeInput, fakeInputUnderLabel } from '../../input/testDoubles.mjs'; import { bindFakeDraw } from '../../testSupport/fakeDrawPort.mjs'; // The fills set of the layer the last harness made: perimeter picks name it. @@ -30,11 +30,12 @@ function harness( const opened = []; const viewer = { scene: { pick } }; const { draw } = bindFakeDraw(viewer); - const input = fakeInput(viewer); + const input = fakeInputUnderLabel(viewer); const clicks = { handler: (click) => input.click(click.position), + // Only the label's own overlay handler is left. get destroyed() { - return input.input.subscriberCount() === 0 ? 1 : 0; + return input.input.subscriberCount() === 1 ? 1 : 0; }, }; const layer = createFirePerimetersLayer({ @@ -78,6 +79,8 @@ function harness( clicks, owners, opened, + /** A label painted over the map: while `over`, it takes each click. */ + label: input.label, }; } @@ -311,6 +314,53 @@ test('a matched incident card carries the InciWeb line and click-through', async ); }); +test('a click a label painted over the incident card takes never opens its InciWeb page, and closes the card', async () => { + let cardHitResult = null; + const h = harness( + { getSnapshot: async () => [row] }, + { + pick: () => perimeterPick('fire-perimeter:2026-NMGNF-000123:0'), + inciwebIndex: [ + { + incident_id: '329300', + tau: 'NMGNF Fixture Fire', + incident_title: 'Fixture Fire', + }, + ], + cardHit: () => cardHitResult, + }, + ); + await h.layer.update(h.viewer); + h.clicks.handler({ position: { x: 10, y: 10 } }); + await new Promise((done) => setTimeout(done, 0)); + const [card] = h.overlay.entries.get('fire-perimeters'); + assert.equal(card.details.at(-1), 'InciWeb ↗ · click card to open'); + + // The open card lies under the label: the click opens nothing. + cardHitResult = { entryId: card.id }; + h.viewer.scene.pick = () => null; + h.label.over = true; + h.clicks.handler({ position: { x: 12, y: 12 } }); + assert.deepEqual(h.opened, [], 'the page beneath the label is not opened'); + assert.equal( + h.overlay.entries.get('fire-perimeters')?.length ?? 0, + 0, + 'the card is closed', + ); + + // Off the label, a click on the open card opens its page. + h.label.over = false; + h.viewer.scene.pick = () => + perimeterPick('fire-perimeter:2026-NMGNF-000123:0'); + h.clicks.handler({ position: { x: 10, y: 10 } }); + await new Promise((done) => setTimeout(done, 0)); + cardHitResult = { entryId: h.overlay.entries.get('fire-perimeters')[0].id }; + h.viewer.scene.pick = () => null; + h.clicks.handler({ position: { x: 12, y: 12 } }); + assert.deepEqual(h.opened, ['https://inciweb.wildfire.gov/node/329300']); + h.layer.destroy(); +}); + test('an unmatched incident renders no InciWeb line and card clicks stay inert', async () => { let cardHitResult = null; const h = harness( diff --git a/src/layers/wildlifeSightings/selection.test.mjs b/src/layers/wildlifeSightings/selection.test.mjs index bf623526bb..e2c3761cfd 100644 --- a/src/layers/wildlifeSightings/selection.test.mjs +++ b/src/layers/wildlifeSightings/selection.test.mjs @@ -2,6 +2,7 @@ import assert from 'node:assert/strict'; import test from 'node:test'; import { createSelection } from './selection.js'; import { bindWildlifeViewer } from '../../testSupport/wildlifeViewer.mjs'; +import { fakeInputUnderLabel } from '../../input/testDoubles.mjs'; const row = { stableId: 'inat:123', @@ -304,6 +305,35 @@ test('empty clicks and layer removal release the selected row and its overlay', assert.equal(f.entries.size, 0); }); +test("a click a label painted over a sighting's card takes never selects the sighting, and lets go of the selected one", (t) => { + let fake = null; + const f = fixture(row, { + input: (viewer) => { + fake ??= fakeInputUnderLabel(viewer); + return fake.input; + }, + }); + f.selection.install(); + t.after(() => { + f.selection.remove(); + fake.destroy(); + }); + f.selection.refreshAmbient(); + // The sighting's name card lies under every click. + f.hit('wildlife-sightings-labels', 'wildlife-sightings-label:inat:123'); + + // Off the label, a click on the card selects and focuses the sighting. + fake.click(); + assert.equal(f.state.selectedId, row.stableId); + assert.equal(f.flights.length, 1); + + fake.label.over = true; + fake.click(); + assert.equal(f.state.selectedId, null, 'the selected sighting is let go'); + assert.equal(f.state.selectedRow, null); + assert.equal(f.flights.length, 1, 'the camera is not sent to the sighting'); +}); + test('a tap selects only a sighting exactly under it, not one the touch search found near it', () => { const f = fixture(); f.selection.install();