Read feeds.vessels.configured and feeds.fires.configured from /api/stats. A missing feeds object leaves both rows visible. WFIGS and trains stay.
152 lines
5.1 KiB
Python
152 lines
5.1 KiB
Python
"""Layer rail hides Vessels and FIRMS only when /api/stats says the key is unset."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import subprocess
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
HTML = (ROOT / "app/static/index.html").read_text()
|
|
|
|
|
|
def _between(start: str, end: str) -> str:
|
|
return HTML.split(start, 1)[1].split(end, 1)[0]
|
|
|
|
|
|
def _feed_fns() -> str:
|
|
return _between("function applyFeedConfig", "async function pollLayerStats")
|
|
|
|
|
|
def _layer_chunk(elem_id: str) -> str:
|
|
idx = HTML.index(f'id="{elem_id}"')
|
|
start = HTML.rfind('<div class="lp-layer"', 0, idx)
|
|
assert start != -1, elem_id
|
|
end = HTML.find('<div class="lp-layer"', idx)
|
|
if end == -1:
|
|
end = idx + 400
|
|
return HTML[start:end]
|
|
|
|
|
|
def test_hide_keyed_on_feeds_configured_not_counts():
|
|
js = _feed_fns()
|
|
assert "function applyFeedConfig" not in js # sliced after the name
|
|
assert "feeds.vessels" in js
|
|
assert "feeds.fires" in js
|
|
assert "configured === false" in js
|
|
assert "lp-vessels-on" in js
|
|
assert "lp-fires-on" in js
|
|
for stay in ("lp-incidents-on", "lp-perim-on", "lp-trains-on", "lp-ac-on"):
|
|
assert stay not in js, stay
|
|
assert "=== 0" not in js
|
|
assert ".closest('.lp-layer')" in js
|
|
assert "row.hidden = hide" in js
|
|
assert "display = hide ? 'none'" in js
|
|
poll = _between("async function pollLayerStats", "function hideSwpcChip")
|
|
assert "applyFeedConfig(s)" in poll
|
|
assert 'class="lp-layer"' in _layer_chunk("lp-vessels-on")
|
|
assert 'class="lp-layer"' in _layer_chunk("lp-fires-on")
|
|
assert "FIRMS Fires" in _layer_chunk("lp-fires-on")
|
|
assert "Vessels" in _layer_chunk("lp-vessels-on")
|
|
assert "WFIGS" not in _layer_chunk("lp-fires-on")
|
|
|
|
|
|
def test_payloads_hide_only_unconfigured_vessel_and_firms_rows():
|
|
"""Run the real functions against stats payloads. Count 0 is not a hide signal."""
|
|
src = "function applyFeedConfig" + _feed_fns()
|
|
harness = r"""
|
|
const fs = require('fs');
|
|
const src = fs.readFileSync(process.argv[2], 'utf8');
|
|
const cases = JSON.parse(fs.readFileSync(process.argv[3], 'utf8'));
|
|
const IDS = ['lp-vessels-on','lp-fires-on','lp-incidents-on','lp-perim-on','lp-trains-on'];
|
|
function makeDoc() {
|
|
const byId = {};
|
|
function node(className, id) {
|
|
const el = {
|
|
id: id || '',
|
|
className,
|
|
hidden: false,
|
|
style: { display: '' },
|
|
parentElement: null,
|
|
closest(sel) {
|
|
const want = sel.replace(/^\./, '');
|
|
let n = this;
|
|
while (n) {
|
|
if ((n.className || '').split(/\s+/).includes(want)) return n;
|
|
n = n.parentElement;
|
|
}
|
|
return null;
|
|
},
|
|
};
|
|
if (id) byId[id] = el;
|
|
return el;
|
|
}
|
|
for (const id of IDS) {
|
|
const row = node('lp-layer');
|
|
const input = node('', id);
|
|
input.parentElement = row;
|
|
}
|
|
global.document = { getElementById(id) { return byId[id] || null; } };
|
|
}
|
|
eval(src);
|
|
const out = {};
|
|
for (const [name, payload] of Object.entries(cases)) {
|
|
makeDoc();
|
|
applyFeedConfig(payload);
|
|
out[name] = {};
|
|
for (const id of IDS) {
|
|
const row = document.getElementById(id).closest('.lp-layer');
|
|
out[name][id] = { hidden: row.hidden, display: row.style.display };
|
|
}
|
|
}
|
|
process.stdout.write(JSON.stringify(out));
|
|
"""
|
|
cases = {
|
|
"both_false": {
|
|
"vessels": 0,
|
|
"fires": 0,
|
|
"feeds": {"vessels": {"configured": False}, "fires": {"configured": False}},
|
|
},
|
|
"both_true": {
|
|
"vessels": 0,
|
|
"fires": 0,
|
|
"feeds": {"vessels": {"configured": True}, "fires": {"configured": True}},
|
|
},
|
|
"no_feeds": {"vessels": 0, "fires": 0, "trains": 0},
|
|
"counts_only_zero": {"vessels": 0, "fires": 0},
|
|
"vessels_only_unset": {
|
|
"feeds": {"vessels": {"configured": False}, "fires": {"configured": True}},
|
|
},
|
|
}
|
|
scratch = Path("/home/lancelot/.hermes/profiles/frontend/cache/scratch")
|
|
scratch.mkdir(parents=True, exist_ok=True)
|
|
src_path = scratch / "t_617c8a08-feed-fns.js"
|
|
cases_path = scratch / "t_617c8a08-feed-cases.json"
|
|
harness_path = scratch / "t_617c8a08-feed-harness.js"
|
|
src_path.write_text(src)
|
|
cases_path.write_text(json.dumps(cases))
|
|
harness_path.write_text(harness)
|
|
proc = subprocess.run(
|
|
["node", str(harness_path), str(src_path), str(cases_path)],
|
|
check=True,
|
|
capture_output=True,
|
|
text=True,
|
|
)
|
|
got = json.loads(proc.stdout)
|
|
hidden = {"hidden": True, "display": "none"}
|
|
shown = {"hidden": False, "display": ""}
|
|
stay = ("lp-incidents-on", "lp-perim-on", "lp-trains-on")
|
|
|
|
assert got["both_false"]["lp-vessels-on"] == hidden
|
|
assert got["both_false"]["lp-fires-on"] == hidden
|
|
for eid in stay:
|
|
assert got["both_false"][eid] == shown, eid
|
|
|
|
for name in ("both_true", "no_feeds", "counts_only_zero"):
|
|
for eid in ("lp-vessels-on", "lp-fires-on", *stay):
|
|
assert got[name][eid] == shown, (name, eid)
|
|
|
|
assert got["vessels_only_unset"]["lp-vessels-on"] == hidden
|
|
assert got["vessels_only_unset"]["lp-fires-on"] == shown
|
|
for eid in stay:
|
|
assert got["vessels_only_unset"][eid] == shown, eid
|