Files
meshcore-analyzer/test-channels-ws-batch-e2e.js
Kpa-clawbot b2f0be994d fix(#1498): channels-ws-batch — wait on packetHash, not length (un-skip explicit-sender) (#1511)
## Summary

The channels-ws-batch E2E tests had a race condition causing flaky
failures on CI, blocking PRs #1490, #1500, #1501.

**Root cause:** Tests waited on `messages.length === prev + 1`, but live
WS traffic from the ingestor could bump `length` independently, causing
timeouts. The earlier #1499 fix attempted to find messages by
`m.hash`/`m.id`, but `processWSBatch` stores `packetHash`/`packetId` on
message objects — so the find never matched.

**Fix:** Replace all length-based waiters with `messages.some(m =>
m.packetHash === '<known-hash>')` which is deterministic regardless of
concurrent WS traffic. Also un-skips the explicit-sender test that was
force-skipped in #1502.

## Tests affected
- "processWSBatch with explicit sender appends to messages" —
un-skipped, now passes
- "GRP_TXT shape with 'Sender: text' parses sender from text" —
race-proof
- "dedup by packetHash" — race-proof
- "new WS message while scrolled up" — race-proof

All 6 tests pass locally (6 passed, 0 failed).

Fixes #1498.

---------

Co-authored-by: mc-bot <bot@meshcore.local>
2026-05-30 12:54:02 -07:00

254 lines
11 KiB
JavaScript

/**
* #1297 B3 — channels.js WebSocket batch processing coverage.
*
* Exercises processWSBatch via the `_channelsHandleWSBatchForTest` and
* `_channelsProcessWSBatchForTest` test hooks. Covers:
* - 'message' shape with explicit sender + text
* - 'message' shape with "Sender: text" parsing (no explicit sender)
* - GRP_TXT packet shape routed via channelKey for user-added rows
* - new-channel append (channel not yet in array)
* - dedup by packetHash (same hash from two observers bumps repeats)
* - unread badge bump on a non-selected channel
* - scroll-button reveal when user is NOT at bottom
*
* Usage: BASE_URL=http://localhost:13581 node test-channels-ws-batch-e2e.js
*/
'use strict';
const { chromium } = require('playwright');
const BASE = process.env.BASE_URL || 'http://localhost:13581';
let passed = 0, failed = 0, skipped = 0;
async function step(name, fn) {
try { await fn(); passed++; console.log(' ✓ ' + name); }
catch (e) { failed++; console.error(' ✗ ' + name + ': ' + e.message); }
}
step.skip = function (name, _fn) {
skipped++;
console.log(' ⊘ ' + name + ' (skipped)');
};
function assert(c, m) { if (!c) throw new Error(m || 'assertion failed'); }
(async () => {
const browser = await chromium.launch({
headless: true,
executablePath: process.env.CHROMIUM_PATH || undefined,
args: ['--no-sandbox', '--disable-gpu', '--disable-dev-shm-usage'],
});
const ctx = await browser.newContext({ viewport: { width: 1280, height: 800 } });
const page = await ctx.newPage();
page.on('dialog', (d) => d.accept());
page.setDefaultTimeout(8000);
page.on('pageerror', (e) => console.error('[pageerror]', e.message));
console.log(`\n=== #1297 B3 channels ws-batch E2E against ${BASE} ===`);
await page.goto(BASE + '/', { waitUntil: 'domcontentloaded' });
await page.evaluate(() => { try { localStorage.clear(); } catch (e) {} });
await page.goto(BASE + '/#/channels', { waitUntil: 'domcontentloaded' });
await page.waitForSelector('#chList .ch-item', { timeout: 10000 });
// Pick the first network channel and select it.
const firstRow = await page.$('.ch-section-network .ch-item');
const selectedHash = await firstRow.getAttribute('data-hash');
await firstRow.click();
await page.waitForFunction(() => {
const t = document.querySelector('#chHeader .ch-header-text');
return t && /—/.test(t.textContent);
}, { timeout: 5000 });
// #1498: this test is genuinely flaky on master CI — closure-over-stale-messages
// hypothesis isn't yet root-caused. Skipping to unblock master while the real
// diagnosis is pending. Re-enable by changing step.skip back to step.
await step('processWSBatch with explicit sender appends to messages', async () => {
await page.evaluate((h) => {
window._channelsProcessWSBatchForTest([{
type: 'message',
data: {
hash: 'wsbatch-explicit-1',
id: 'pkt-wsbatch-1',
decoded: {
payload: {
channel: h,
sender: 'WsAlice',
text: 'hello world from ws',
},
},
},
}], []);
}, selectedHash);
// Find by hash instead of by index — real WS messages from the staging
// ingestor may race in and bump messages.length, making messages[length-1]
// some unrelated XMD packet instead of our injected one.
await page.waitForFunction(() => {
const s = window._channelsGetStateForTest();
return s.messages.some((m) => m.packetHash === 'wsbatch-explicit-1');
}, null, { timeout: 3000 });
const ours = await page.evaluate(() => {
const s = window._channelsGetStateForTest();
return s.messages.find((m) => m.packetHash === 'wsbatch-explicit-1');
});
assert(ours, 'injected message not found in messages by packetHash');
assert(ours.sender === 'WsAlice', 'expected sender WsAlice, got ' + ours.sender);
assert(/hello world/.test(ours.text), 'text mismatch: ' + ours.text);
});
await step('GRP_TXT shape with "Sender: text" parses sender from text', async () => {
await page.evaluate((h) => {
window._channelsProcessWSBatchForTest([{
type: 'packet',
data: {
hash: 'wsbatch-parse-1',
id: 'pkt-parse-1',
decoded: {
header: { payloadTypeName: 'GRP_TXT' },
payload: {
channel: h,
text: 'WsBob: parsed message',
},
},
},
}], []);
}, selectedHash);
await page.waitForFunction(() =>
window._channelsGetStateForTest().messages.some((m) => m.packetHash === 'wsbatch-parse-1'),
null, { timeout: 3000 });
const parsed = await page.evaluate(() => {
const s = window._channelsGetStateForTest();
return s.messages.find((m) => m.packetHash === 'wsbatch-parse-1');
});
assert(parsed.sender === 'WsBob',
'should parse sender from "Sender: text", got: ' + parsed.sender);
assert(parsed.text === 'parsed message',
'displayText should strip sender prefix, got: ' + parsed.text);
});
await step('dedup by packetHash: second observer bumps repeats + observers list', async () => {
await page.evaluate((h) => {
// First observation.
window._channelsProcessWSBatchForTest([{
type: 'message',
data: {
hash: 'wsbatch-dup-1',
id: 'pkt-dup-1',
observer: 'obs-A',
decoded: { payload: { channel: h, sender: 'WsCharlie', text: 'dup' } },
},
}], []);
// Second observation of the SAME packetHash from a different observer.
window._channelsProcessWSBatchForTest([{
type: 'message',
data: {
hash: 'wsbatch-dup-1',
id: 'pkt-dup-1',
observer: 'obs-B',
packet: { observer_name: 'obs-B' },
decoded: { payload: { channel: h, sender: 'WsCharlie', text: 'dup' } },
},
}], []);
}, selectedHash);
await page.waitForFunction(() =>
window._channelsGetStateForTest().messages.some((m) => m.packetHash === 'wsbatch-dup-1'),
null, { timeout: 3000 });
const deduped = await page.evaluate(() => {
const s = window._channelsGetStateForTest();
return s.messages.find((m) => m.packetHash === 'wsbatch-dup-1');
});
assert(deduped.repeats >= 2, 'repeats should be >=2 after dedup, got: ' + deduped.repeats);
assert(Array.isArray(deduped.observers) && deduped.observers.length >= 2,
'observers should accumulate, got: ' + JSON.stringify(deduped.observers));
});
await step('new-channel append: previously-unseen channel adds a sidebar row', async () => {
const newHash = '#wsbatch-new-' + Date.now();
await page.evaluate((h) => {
window._channelsProcessWSBatchForTest([{
type: 'message',
data: {
hash: 'wsbatch-newch-1',
id: 'pkt-newch-1',
decoded: { payload: { channel: h, sender: 'WsDan', text: 'new channel hi' } },
},
}], []);
}, newHash);
await page.waitForFunction((h) => {
const s = window._channelsGetStateForTest();
return s.channels.some((c) => c.hash === h);
}, newHash, { timeout: 3000 });
const ch = await page.evaluate((h) => {
const s = window._channelsGetStateForTest();
return s.channels.find((c) => c.hash === h);
}, newHash);
assert(ch && ch.lastSender === 'WsDan',
'new channel should have lastSender=WsDan, got: ' + JSON.stringify(ch));
});
await step('new WS message while scrolled up appends to state', async () => {
// Force not-at-bottom by scrolling messages container up.
await page.evaluate(() => {
const m = document.getElementById('chMessages');
if (m) m.scrollTop = 0;
});
await page.evaluate(() => {
const s = window._channelsGetStateForTest();
const h = s.selectedHash;
if (!h) return;
window._channelsProcessWSBatchForTest([{
type: 'message',
data: {
hash: 'wsbatch-scroll-1',
id: 'pkt-scroll-1',
decoded: { payload: { channel: h, sender: 'WsEve', text: 'tail' } },
},
}], []);
});
await page.waitForFunction(() =>
window._channelsGetStateForTest().messages.some((m) => m.packetHash === 'wsbatch-scroll-1'),
null, { timeout: 3000 });
const scrollMsg = await page.evaluate(() => {
const s = window._channelsGetStateForTest();
return s.messages.find((m) => m.packetHash === 'wsbatch-scroll-1');
});
assert(scrollMsg.sender === 'WsEve' && /tail/.test(scrollMsg.text),
'tail message should be appended, got: ' + JSON.stringify(scrollMsg));
});
await step('region filter: drops msg from observer outside selected regions', async () => {
// Seed observer regions. obs-name-1 → XYZ region.
await page.evaluate(() => {
if (typeof window._channelsSetObserverRegionsForTest === 'function') {
window._channelsSetObserverRegionsForTest(
{ 'obs-id-1': 'XYZ' }, { 'obs-name-1': 'XYZ' });
}
});
// Direct unit-style test of the exposed predicate — independent of
// any state side effects so we can assert true/false explicitly.
const verdicts = await page.evaluate(() => {
const fn = window._channelsShouldProcessWSMessageForRegion;
const byId = { 'obs-id-1': 'XYZ' };
const byName = { 'obs-name-1': 'XYZ' };
const mkMsg = (name) => ({ data: { observer: name, packet: { observer_name: name } } });
return {
// Selected region matches observer's region → pass.
matchById: fn({ data: { packet: { observer_id: 'obs-id-1' } } }, ['XYZ'], byId, byName),
matchByName: fn(mkMsg('obs-name-1'), ['XYZ'], byId, byName),
// Selected region doesn't match → filtered.
mismatch: fn(mkMsg('obs-name-1'), ['DIFFERENT-REGION'], byId, byName),
// Unknown observer (not in maps), regions set → filtered.
unknown: fn(mkMsg('obs-unknown'), ['XYZ'], byId, byName),
// No regions selected → pass-through.
noRegions: fn(mkMsg('obs-name-1'), [], byId, byName),
};
});
assert(verdicts.matchById === true, 'matching region by id should pass: ' + verdicts.matchById);
assert(verdicts.matchByName === true, 'matching region by name should pass: ' + verdicts.matchByName);
assert(verdicts.mismatch === false, 'mismatched region should be filtered: ' + verdicts.mismatch);
assert(verdicts.unknown === false, 'unknown observer should be filtered: ' + verdicts.unknown);
assert(verdicts.noRegions === true, 'empty regions should pass-through: ' + verdicts.noRegions);
});
await browser.close();
console.log(`\n=== B3 ws-batch: ${passed} passed, ${failed} failed ===\n`);
process.exit(failed === 0 ? 0 : 1);
})().catch((e) => { console.error(e); process.exit(1); });