"""The SFM page's device-routing rules, run through Node (phase 1c). sfm/sfm_webapp.html keeps every rule that decides where a request goes, or whether a series-specific control is live, in one block of pure functions between `/* */` and `/* */`. This file extracts that block and runs it in Node -- no browser, no DOM. The safety rules it pins (agreed with Brian, 2026-10-28): * a series-specific control follows the unit's own /device/info answer, never a typed serial alone, and stops counting the moment the serial is edited; * config and call-home writes stay off for both series until phases 4-5. """ from __future__ import annotations import json import re import shutil import subprocess from pathlib import Path import pytest PAGE = Path(__file__).resolve().parent.parent / "sfm" / "sfm_webapp.html" STATIC = PAGE.parent / "static" ROUTING_JS = STATIC / "device_routing.js" # the page's routing rules, loaded by ', PAGE.read_text()) assert srcs == ["static/device_routing.js", "static/sfm_webapp.js"] js = tmp_path / "page.js" js.write_text("\n;\n".join((PAGE.parent / s).read_text() for s in srcs)) r = subprocess.run([NODE, "--check", str(js)], capture_output=True, text=True, timeout=30) assert r.returncode == 0, r.stderr def test_every_device_url_is_built_by_deviceUrl(): """One place builds /device/ URLs, so the series rules cannot be bypassed: outside the device-routing block the page holds no hand-built device path.""" outside = PAGE.read_text() + PAGE_JS.read_text() hand_built = [m.group(0) for m in re.finditer(r"(`\$\{api\(\)\}/device/[^`]*`|'/device/[^']*'|\"/device/[^\"]*\")", outside)] assert hand_built == [], hand_built def test_device_url_puts_an_event_index_in_the_path(): # the legacy per-event routes carry the index in the path, not the query r = run(["deviceUrl", ["event_waveform", BLANK, {"index": 3}]], ["deviceUrl", ["event_file", S3, {"index": 0}]], ["deviceUrl", ["event_file", S3]]) assert r[0] == {"method": "GET", "path": "/device/event/3/waveform?host=h&tcp_port=9034"} assert r[1] == {"method": "GET", "path": "/device/event/0/blastware_file?serial=BE9441&host=h&tcp_port=9034"} assert "throws" in r[2] def test_event_view_reads_both_event_shapes(): shared = {"event_id": "BE9441:0111f210:20261026170036", "index": 2, "timestamp": "2026-10-26T17:00:36", "record_type": "histogram", "peak_vector_sum_ips": 1.083, "peaks_ips": {"tran": 0.25, "vert": 1.08, "long": 0.195}} legacy = {"index": 0, "timestamp": {"display": "2026-10-26 16:06:37"}, "record_type": "Waveform", "peak_values": {"tran_in_s": 0.03, "vert_in_s": 0.095, "long_in_s": 0.02, "peak_vector_sum": 0.1, "micl_psi": 0.0001}} bare = {"index": 5} a, b, c = run(["eventView", [shared]], ["eventView", [legacy]], ["eventView", [bare]]) assert a == {"label": "2026-10-26 17:00:36", "title": "histogram", "peaks": {"tran": 0.25, "vert": 1.08, "long": 0.195, "pvs": 1.083, "micPsi": None}} assert b == {"label": "2026-10-26 16:06:37", "title": "Waveform", "peaks": {"tran": 0.03, "vert": 0.095, "long": 0.02, "pvs": 0.1, "micPsi": 0.0001}} assert c == {"label": "Event 5", "title": "", "peaks": None} def test_settings_rows_show_unit_settings_with_a_dash_for_null(): full = {"serial": "UM20147", "series": 4, "setup_name": "TMI default", "record_mode": "Waveform", "sample_rate": 1024, "record_time_s": 2.0, "histogram_interval_s": 900, "geo_trigger_ips": 0.02, "notes": {"project": "Bench", "client": ""}} empty = {"serial": "BE9441", "series": 3, "setup_name": None, "record_mode": None, "sample_rate": None, "record_time_s": None, "histogram_interval_s": None, "geo_trigger_ips": None, "notes": {}} a, b, c = run(["settingsRows", [full]], ["settingsRows", [empty]], ["settingsRows", [{**empty, "histogram_interval_s": 15}]]) assert a == [["Setup Name", "TMI default"], ["Record Mode", "Waveform"], ["Sample Rate", "1024 sps"], ["Record Time", "2.00 s"], ["Histogram Interval", "15 min"], ["Geo Trigger", "0.0200 in/s"], ["Note: project", "Bench"], ["Note: client", "—"]] assert b == [["Setup Name", "—"], ["Record Mode", "—"], ["Sample Rate", "—"], ["Record Time", "—"], ["Histogram Interval", "—"], ["Geo Trigger", "—"], ["Notes", "—"]] assert c[4] == ["Histogram Interval", "15 s"] def test_the_per_index_routes_follow_the_list_not_the_keyboard(): # the legacy per-index routes (Load / Save / Download / arrow keys) are live # only for a list loaded without a serial that has events r = run(["perIndexLive", [{"bySerial": False, "count": 2}]], ["perIndexLive", [{"bySerial": True, "count": 2}]], ["perIndexLive", [{"bySerial": False, "count": 0}]], ["perIndexLive", [{"bySerial": True, "count": 0}]]) assert r == [True, False, False, False] def test_escape_html_makes_a_unit_string_inert(): r = run(["escapeHtml", ['Bench &\'']], ["escapeHtml", [None]], ["escapeHtml", [42]]) assert r == ["Bench <b id="x">&'", "", "42"]