osint-dashboard/tests/test_geofence_frontend.py
Sirius DevOps 0f91127233
Some checks failed
build-and-deploy / build-push-deploy (push) Failing after 6s
feat(map): geofence inbox shows cameras and the news line
A hit with evidence keeps the meta line and adds a second line: camera
count, the first camera name, and the first headline. A non-empty news
url is an anchor; an empty url stays text. Hits without evidence stay
the one-liner. snapshot_url is never written into the inbox.
2026-11-17 19:56:41 +00:00

207 lines
6.3 KiB
Python

"""Geofence layer panel: draw, watch, inbox, delete (HTML contract)."""
from __future__ import annotations
import json
import subprocess
import tempfile
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
HTML = (ROOT / "app/static/index.html").read_text()
def test_geofence_panel_has_list_and_delete_hook():
assert 'id="gf-draw"' in HTML
assert 'id="gf-list"' in HTML
assert "function deleteGeofence" in HTML
assert "method: 'DELETE'" in HTML or 'method: "DELETE"' in HTML
assert "/api/geofences/" in HTML
def test_load_geofences_renders_delete_controls():
js = HTML.split("async function loadGeofences", 1)[1].split(
"async function loadFireAircraftHits", 1
)[0]
assert "gf-list" in js
assert "deleteGeofence" in js
assert "onEachFeature" in js
assert "bindPopup" in js
def test_finish_cancel_draw_controls():
assert 'id="gf-finish"' in HTML
assert 'id="gf-cancel"' in HTML
assert "function cancelGeofenceDraw" in HTML
assert "function onGfClose" in HTML
def test_watch_geofences_ws_payload():
assert "watch_geofences" in HTML
assert "function sendWatchGeofences" in HTML
def test_geofence_alert_inbox():
assert 'id="gf-inbox"' in HTML
assert "/api/geofence-alerts" in HTML
assert "function loadGfInbox" in HTML
assert "function pushGfInbox" in HTML
def test_delete_geofence_still_present():
assert "function deleteGeofence" in HTML
assert "method: 'DELETE'" in HTML or 'method: "DELETE"' in HTML
def test_fence_dvr_at_endpoint():
assert "/at?timestamp=" in HTML or "/at?timestamp=${" in HTML
assert "function dvrScrubFence" in HTML
assert "gfSelectedId" in HTML
def _inbox_src() -> str:
news = HTML.split("function newsEsc", 1)[1].split("function newsTimeAgo", 1)[0]
inbox = HTML.split("function gfHitTime", 1)[1].split("function onGfHitClick", 1)[0]
return "function newsEsc" + news + "function gfHitTime" + inbox
def _render_inbox(hits: list) -> list[dict]:
harness = r"""
const fs = require('fs');
const src = fs.readFileSync(process.argv[2], 'utf8');
const hits = JSON.parse(fs.readFileSync(process.argv[3], 'utf8'));
var gfNameById = {};
function makeBox() {
const kids = [];
const box = {
children: kids,
prepend(el) { kids.unshift(el); },
removeChild(el) {
const i = kids.indexOf(el);
if (i >= 0) kids.splice(i, 1);
},
};
global.document = {
getElementById(id) { return id === 'gf-inbox' ? box : null; },
createElement() {
return { type: '', className: '', dataset: {}, innerHTML: '', onclick: null };
},
};
return box;
}
eval(src);
const out = [];
for (const hit of hits) {
const box = makeBox();
pushGfInbox(hit);
const el = box.children[0];
out.push({
html: el ? el.innerHTML : '',
hasOnclick: !!(el && typeof el.onclick === 'function'),
});
}
process.stdout.write(JSON.stringify(out));
"""
with tempfile.TemporaryDirectory() as td:
root = Path(td)
src_path = root / "inbox.js"
hits_path = root / "hits.json"
harness_path = root / "harness.js"
src_path.write_text(_inbox_src())
hits_path.write_text(json.dumps(hits))
harness_path.write_text(harness)
proc = subprocess.run(
["node", str(harness_path), str(src_path), str(hits_path)],
check=False,
capture_output=True,
text=True,
)
assert proc.returncode == 0, proc.stderr
return json.loads(proc.stdout)
def test_push_gf_inbox_evidence_pack():
"""Second line from payload.evidence; missing evidence stays the one-liner."""
linked = {
"geofence_name": "Yard",
"source_kind": "news",
"entity_id": "n-1",
"created_at": "2026-11-14T05:00:00Z",
"payload": {
"evidence": {
"cameras": [
{"id": "c1", "name": "North Gate", "snapshot_url": "rtsp://camera/1"},
{"id": "c2", "name": "South Gate", "snapshot_url": "/api/cameras/c2/snapshot"},
],
"news": [{"headline": "Fence brief", "url": "https://ex.com/1", "importance": "high"}],
"snapshot": "/api/geofences/g1/at?ts=2026-11-14T05:00:00Z",
}
},
}
text_news = {
"geofence_name": "Yard",
"source_kind": "earthquake",
"entity_id": "eq-9",
"payload": {
"evidence": {
"cameras": [],
"news": [{"headline": "Silent quake", "url": "", "importance": ""}],
}
},
}
bare = {
"geofence_name": "Old Fence",
"source_kind": "adsb",
"entity_id": "abc123",
"payload": {"icao": "abc123"},
}
top_level = {
"geofence_name": "Top",
"source_kind": "fire_incident",
"entity_id": "fi-1",
"evidence": {
"cameras": [{"id": "c9", "name": "Lot Cam", "snapshot_url": "rtsp://camera/1"}],
"news": [{"headline": "Lot fire", "url": "https://ex.com/1", "importance": ""}],
},
}
as_string = {
"geofence_name": "String Fence",
"source_kind": "news",
"entity_id": "s1",
"payload": json.dumps({
"evidence": {
"cameras": [{"id": "c3", "name": "Cam A", "snapshot_url": "rtsp://camera/1"}],
"news": [{"headline": "From string", "url": "https://ex.com/1", "importance": ""}],
}
}),
}
rendered = _render_inbox([linked, text_news, bare, top_level, as_string])
html, quiet, plain, fallback, parsed = [row["html"] for row in rendered]
assert "2 cams" in html
assert "North Gate" in html
assert 'href="https://ex.com/1"' in html
assert 'target="_blank"' in html
assert 'rel="noopener"' in html
assert "stopPropagation" in html
assert "rtsp://camera/1" not in html
assert rendered[0]["hasOnclick"]
assert "Silent quake" in quiet
assert "<a" not in quiet
assert "Yard" in quiet
assert "Old Fence" in plain
assert "adsb" in plain
assert "abc123" in plain
assert "cams" not in plain
assert "<br>" not in plain
assert rendered[2]["hasOnclick"]
assert "1 cams" in fallback
assert 'href="https://ex.com/1"' in fallback
assert "rtsp://camera/1" not in fallback
assert "1 cams" in parsed
assert "From string" in parsed
assert "rtsp://camera/1" not in parsed