diff --git a/CLAUDE.md b/CLAUDE.md index 8def3a0..b84f053 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -189,8 +189,8 @@ Read this first when picking the project back up. `stop_monitoring_blind`, `_slow_drip`, `_spam`) are exempt: 0x96/0x97 mean the same on both series, and the blind tools must reach a unit too wedged to answer. - ⚠ **Before serial mode is used on a unit that calls home to THOR, run the - ring-path Runs A/B** (spec, *Open questions ... for the bench*): a held line + ⚠ **Before this branch merges, run the ring-path Runs A/B** -- the spec's + prerequisite to a webapp sending `serial` (*Open questions ... for the bench*): a held line holds the modem's single TCP session. The page never polls (every request is a click), SFM hangs up on RADIO RING, and **Hang up** releases the line at once; whether a call-home still reaches THOR is unmeasured. diff --git a/sfm/sfm_webapp.html b/sfm/sfm_webapp.html index 3141c17..327d0e9 100644 --- a/sfm/sfm_webapp.html +++ b/sfm/sfm_webapp.html @@ -1613,6 +1613,18 @@ function settingsRows(st) { return rows; } +// The legacy per-index routes (Load / Save / Download, prev/next, the arrow +// keys) address events by position on the MiniMate path: live only for a list +// loaded without a serial that has events. +function perIndexLive({ bySerial, count }) { return !bySerial && count > 0; } + +// A unit's own string (setup name, project, an error naming a serial) placed +// into markup must stay text. +function escapeHtml(v) { + return String(v ?? '').replace(/[&<>"']/g, + c => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c])); +} + function downloadSummary(body) { if (body.inserted === false) return `already stored — ${body.stored_filename}`; return `stored ${body.stored_filename}, ${body.filesize} bytes, check: ${(body.detail || {}).check}`; @@ -1790,6 +1802,9 @@ function switchTab(name) { // ── Connect ──────────────────────────────────────────────────────────────────── async function connectUnit() { if (!devHost()) { setStatus('Enter a device host first.', 'error'); return; } + // Whatever answered last time no longer counts: only this identify confirms. + unitConfirmed = null; + applyControls(); const btn = document.getElementById('connect-btn'); btn.disabled = true; @@ -1852,14 +1867,15 @@ function setEventButtonsEnabled() { // A list loaded by serial is downloaded row by row (by event_id); these // per-index buttons belong to the legacy list. const bySerial = eventsBySerial; + const live = perIndexLive({ bySerial, count: n }); for (const id of ['load-btn', 'save-btn', 'download-btn']) { const el = document.getElementById(id); if (el.dataset.legacyTitle === undefined) el.dataset.legacyTitle = el.title; - el.disabled = n === 0 || bySerial; + el.disabled = !live; el.title = bySerial ? 'For a list loaded without a serial — use a row\'s Download' : el.dataset.legacyTitle; } document.getElementById('prev-btn').disabled = true; - document.getElementById('next-btn').disabled = n <= 1 || bySerial; + document.getElementById('next-btn').disabled = !live || n <= 1; } // ── Event list (opt-in — this is the slow chain walk) ────────────────────────── @@ -2083,7 +2099,9 @@ function populateDeviceTab() { for (const {label, value} of cardData) { const c = document.createElement('div'); c.className = 'dev-card'; - c.innerHTML = `
${label}
${value}
`; + const l = document.createElement('div'); l.className = 'dev-card-label'; l.textContent = label; + const v = document.createElement('div'); v.className = 'dev-card-value'; v.textContent = value; + c.append(l, v); cards.appendChild(c); } @@ -2101,7 +2119,7 @@ function populateDeviceTab() { ['ADC Scale Factor (geo)', cc.geo_adc_scale != null ? `${cc.geo_adc_scale.toFixed(4)} in/s` : '—'], ['Setup Name', cc.setup_name || '—'], ]; - renderTable('compliance-table', complianceRows); + renderTextTable('compliance-table', complianceRows); // Project table const projectRows = [ @@ -2111,7 +2129,7 @@ function populateDeviceTab() { ['Sensor Location', cc.sensor_location || '—'], ['Notes', cc.notes || '—'], ]; - renderTable('project-table', projectRows); + renderTextTable('project-table', projectRows); } function renderTextTable(id, rows) { @@ -2192,7 +2210,7 @@ async function refreshDiagnostics() { if (!store._err) storageInfo = store; - const err = v => `${v}`; + const err = v => `${escapeHtml(v)}`; const rows = []; rows.push(['Monitoring', mon._err ? err(mon._err) @@ -2589,6 +2607,8 @@ function populateSerialEventTable() { } function selectEvent(idx) { + // The arrow keys reach here too: they obey the same rule as the buttons. + if (!perIndexLive({ bySerial: eventsBySerial, count: eventList.length })) return; currentEvent = idx; document.querySelectorAll('.event-chip').forEach((c, i) => c.classList.toggle('active', i === idx)); document.getElementById('prev-btn').disabled = idx <= 0; diff --git a/tests/test_webapp_device_routing.py b/tests/test_webapp_device_routing.py index 7c91bfa..c1f6025 100644 --- a/tests/test_webapp_device_routing.py +++ b/tests/test_webapp_device_routing.py @@ -24,7 +24,7 @@ PAGE = Path(__file__).resolve().parent.parent / "sfm" / "sfm_webapp.html" NODE = shutil.which("node") pytestmark = pytest.mark.skipif(NODE is None, reason="node is not installed; the page-logic tests need it") -EXPORTS = ["seriesFromSerial", "unitQuery", "deviceUrl", "confirmedSeries", "controlsFor", "eventView", "settingsRows", +EXPORTS = ["seriesFromSerial", "unitQuery", "deviceUrl", "confirmedSeries", "controlsFor", "eventView", "settingsRows", "perIndexLive", "escapeHtml", "changeSummary", "downloadSummary"] @@ -220,3 +220,16 @@ def test_settings_rows_show_unit_settings_with_a_dash_for_null(): 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"]