From d8fc58e0f49823720e2d92ea70da42809e2d14cb Mon Sep 17 00:00:00 2001 From: n30nex <6686206+n30nex@users.noreply.github.com> Date: Sun, 29 Nov 2026 12:35:47 -0500 Subject: [PATCH 1/2] test(nodes): require direct SNR interval counts on Analytics --- tests/e2e/test-node-analytics-heard-by-e2e.js | 65 ++++++++++++++++++- tests/unit/test-node-analytics-heard-by.js | 28 ++++++++ 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/tests/e2e/test-node-analytics-heard-by-e2e.js b/tests/e2e/test-node-analytics-heard-by-e2e.js index 5db9c869f..534fe465d 100644 --- a/tests/e2e/test-node-analytics-heard-by-e2e.js +++ b/tests/e2e/test-node-analytics-heard-by-e2e.js @@ -23,6 +23,30 @@ function assert(cond, msg) { if (!cond) throw new Error(msg || 'Assertion failed'); } +async function checkSnr(page, trend) { + const card = page.getByRole('region', { name: 'Direct SNR distribution' }); + assert(await card.count() === 1, 'missing direct SNR distribution'); + const readings = trend.map(sample => sample.snr).filter(Number.isFinite); + const text = await card.innerText(); + assert(text.includes('500') && /retained/i.test(text), 'identify the bounded sample'); + const table = card.getByRole('table', { name: 'Direct SNR distribution' }); + if (!readings.length) { + assert(text.includes('No direct SNR samples in this window') && !await table.count(), 'empty sample'); + return; + } + assert(text.includes(readings.length + (readings.length === 1 ? ' sample' : ' samples') + ' in this window'), + 'sample count must match the response'); + const edges = [-Infinity, -30, -25, -20, -15, -10, -5, 0, 5, 10, 15, 20, 25, 30, Infinity]; + const counts = await table.locator('tbody tr').evaluateAll(rows => rows.map(r => Number(r.cells[1].textContent.trim()))); + assert(counts.length === edges.length - 1, 'expected fourteen fixed SNR intervals'); + for (let i = 0; i < counts.length; i++) { + const expected = readings.filter(v => v >= edges[i] && v < edges[i + 1]).length; + assert(counts[i] === expected, 'interval ' + edges[i] + ' to ' + edges[i + 1] + ': expected ' + expected + ', got ' + counts[i]); + } + assert(await table.getByRole('rowheader', { name: '-5 to under 0', exact: true }).count() === 1, + 'interval limits are exposed as row headers'); +} + async function run() { const browser = await chromium.launch({ headless: true, @@ -37,11 +61,19 @@ async function run() { const nodes = (await (await page.request.get(`${BASE}/api/nodes?limit=50`)).json()).nodes || []; assert(nodes.length > 0, 'fixture has no nodes'); const pk = nodes[0].public_key; - const real = await (await page.request.get(`${BASE}/api/nodes/${pk}/analytics?days=7`)).json(); - assert(Array.isArray(real.heardBy), 'analytics response has no heardBy array'); + const path = '/api/nodes/' + pk + '/'; + const nodeRequests = []; + page.on('request', req => { + const url = new URL(req.url()); + if (url.pathname.startsWith(path)) nodeRequests.push(url.pathname.slice(path.length) + url.search); + }); + let real; await test('the Observers stat counts the heardBy rows; no SNR Trend or Observer Coverage chart', async () => { + const response = page.waitForResponse(r => new URL(r.url()).pathname === path + 'analytics'); await page.goto(`${BASE}/#/nodes/${encodeURIComponent(pk)}/analytics`, { waitUntil: 'domcontentloaded' }); + real = await (await response).json(); + assert(Array.isArray(real.heardBy), 'analytics response has no heardBy array'); await page.waitForSelector('#heardByCard'); assert(!(await page.$('#snrChart')), '#snrChart still on the page'); assert(!(await page.$('#observerChart')), '#observerChart still on the page'); @@ -52,6 +84,15 @@ async function run() { assert(stat === String(real.heardBy.length), `Observers stat ${stat}, heardBy rows ${real.heardBy.length}`); }); + await test('direct SNR intervals match the unmodified analytics response', async () => { + await checkSnr(page, real.snrTrend); + if (process.env.SCREENSHOT_DIR) { + const file = require('path').join(process.env.SCREENSHOT_DIR, 'direct-snr-distribution.png'); + await page.getByRole('region', { name: 'Direct SNR distribution' }).screenshot({ path: file }); + } + }); + + const changedTrend = [-31, -25, -0.25, 0, 0, 30, null].map(snr => ({ snr })); await test('12 observers: 10 rows, then "Show all 12" shows all', async () => { const stub = JSON.parse(JSON.stringify(real)); stub.heardBy = Array.from({ length: 12 }, (_, i) => ({ @@ -59,6 +100,7 @@ async function run() { medianSnr: 8 - i, p10Snr: 5 - i, p90Snr: 10 - i, recentMedianSnr: null, earlierMedianSnr: null, })); stub.computedStats.uniqueObservers = 12; + stub.snrTrend = changedTrend; await page.route('**/api/nodes/*/analytics?days=30', (route) => route.fulfill({ contentType: 'application/json', body: JSON.stringify(stub) })); await page.click('#timeRangeBtns button[data-days="30"]'); await page.waitForSelector('[data-action="heard-show-all"]'); @@ -72,6 +114,25 @@ async function run() { assert(first === '#/observers/e2eobs0', `first row link: ${first}`); }); + await test('a saved window keeps its SNR counts and reuses the analytics request', async () => { + const saved = new URL(page.url()).hash; + assert(new URLSearchParams(saved.split('?')[1]).get('days') === '30', 'time window missing from the link'); + await checkSnr(page, changedTrend); + const table = page.getByRole('table', { name: 'Direct SNR distribution' }); + await table.focus(); + await page.keyboard.press('Shift+Tab'); + await page.keyboard.press('Tab'); + assert(await table.evaluate(el => el === document.activeElement), 'keyboard can reach the interval counts'); + + await page.goto(BASE + '/#/home'); + await page.goBack(); + await page.waitForSelector('#timeRangeBtns button.active[data-days="30"]'); + await checkSnr(page, changedTrend); + assert(nodeRequests.filter(r => r.startsWith('analytics?')).join(',') === 'analytics?days=7,analytics?days=30', + 'only one analytics request per window'); + assert(nodeRequests.every(r => /^(analytics|battery|hop_analytics)\?/.test(r)), 'unexpected per-node request'); + }); + await browser.close(); console.log('\nAll node analytics heard-by E2E tests passed.'); } diff --git a/tests/unit/test-node-analytics-heard-by.js b/tests/unit/test-node-analytics-heard-by.js index 825bca3c6..6915e4f32 100644 --- a/tests/unit/test-node-analytics-heard-by.js +++ b/tests/unit/test-node-analytics-heard-by.js @@ -92,6 +92,34 @@ t('a row without a median shows its count and no strip', () => { assert.ok(!html.includes('na-heard-dot')); }); +t('direct SNR bins include exact five-dB boundaries, zero and both tails', () => { + const readings = [-31, -30, -29.75, -25, -20, -15, -10, -5, -0.25, 0, '0', 4.75, 5, + 10, 15, 20, 25, 29.75, 30, 31, null, '', ' ', false, 'bad', Infinity, NaN]; + const snrTrend = readings.map(snr => ({ snr })).concat([null, {}, { rssi: -90 }]); + const html = HB.html({ heardBy: [row(1, 4)], snrTrend }, { pubkey: PK }); + assert.match(html, /20 samples in this window/, 'show the number of usable direct readings'); + const rows = Array.from(html.matchAll(/
${summary} CoreScope keeps recent direct receptions per fixed observer. The chart shows up to the 500 newest retained SNR readings in the selected window.
+ ${rows ? `| SNR interval (dB) | Samples |
|---|