Files
meshcore-analyzer/test-channels-ws-batch-e2e.js
T
Kpa-clawbot d837166158 test(coverage): add Playwright E2E for channels page (#1297 B3) (#1300)
## #1297 B3 — Playwright E2E coverage for `public/channels.js`

Pure-coverage PR. Adds five Playwright suites targeting the largest
under-tested branches of `public/channels.js` (1950 LOC, was **19.9%
statements** per the live coverage refinement in #1297 — the single
biggest delta opportunity in the umbrella). No production code changes.

### Coverage exemption

Per repo `AGENTS.md` TDD rule: this is the **net-new test coverage**
case — there is no production change to gate, so a failing-then-passing
red commit isn't applicable. All five suites exercise existing channels
init() code paths that ship today.

### New test files

| File | Scenarios exercised |
| --- | --- |
| `test-channels-list-render-e2e.js` | Sectioned sidebar (My Channels /
Network / Encrypted) headers, encrypted collapse toggle + localStorage
persistence, row badges + previews, color dot + color clear control,
sidebar resize handle width persist |
| `test-channels-selection-flow-e2e.js` | `selectChannel()` header
update + URL replaceState, message row rendering (avatars, sender
colors, packet links), node detail panel open via mouse + keyboard +
close-with-focus-restore, deep-link route restoration, scroll button
initial state |
| `test-channels-add-modal-e2e.js` | Generate PSK Channel (key + QR +
status banner + localStorage persist), Add PSK invalid hex error path,
Add PSK valid hex success + close + My Channels row, Monitor Hashtag
with and without leading `#`, empty-hashtag no-op, Scan QR unavailable
fallback, Escape close, Remove ✕ flow |
| `test-channels-share-color-e2e.js` | Share modal normal mode
(dedicated `#chShareModal` with QR + Hex Key + Copy success label),
Share modal error mode (`openShareModalError` when no stored key — field
groups hidden), Escape close, `ChannelColorPicker.show` invocation on
color-dot click, keyboard Enter on a `[data-share-channel]` span |
| `test-channels-ws-batch-e2e.js` | `processWSBatch` via
`_channelsProcessWSBatchForTest`: explicit-sender append, `"Sender:
text"` parsing branch, packetHash dedup + observer accumulation,
new-channel append (channel previously unseen), scroll-button branch
when user not at bottom, region-filter exclusion code path |

All five tests wired into `.github/workflows/deploy.yml` after the
existing `test-channel-fluid-e2e.js` step.

### Preflight

`bash ~/.openclaw/skills/pr-preflight/scripts/run-all.sh origin/master`
→
exit 0, all gates pass (PII, CSS vars, branch scope, etc.).

Refs #1297

---------

Co-authored-by: openclaw-bot <openclaw-bot@users.noreply.github.com>
Co-authored-by: openclaw-bot <bot@openclaw.local>
Co-authored-by: mc-bot <bot@meshcore.local>
2026-05-29 11:46:51 +00:00

253 lines
10 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;
async function step(name, fn) {
try { await fn(); passed++; console.log(' ✓ ' + name); }
catch (e) { failed++; console.error(' ✗ ' + name + ': ' + e.message); }
}
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 });
await step('processWSBatch with explicit sender appends to messages', async () => {
const before = await page.evaluate(() => {
const s = window._channelsGetStateForTest();
return s.messages.length;
});
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);
await page.waitForFunction((prev) => {
const s = window._channelsGetStateForTest();
return s.messages.length === prev + 1;
}, before, { timeout: 3000 });
const last = await page.evaluate(() => {
const s = window._channelsGetStateForTest();
return s.messages[s.messages.length - 1];
});
assert(last.sender === 'WsAlice', 'expected sender WsAlice, got ' + last.sender);
assert(/hello world/.test(last.text), 'text mismatch: ' + last.text);
});
await step('GRP_TXT shape with "Sender: text" parses sender from text', async () => {
const before = await page.evaluate(
() => window._channelsGetStateForTest().messages.length);
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((prev) =>
window._channelsGetStateForTest().messages.length === prev + 1,
before, { timeout: 3000 });
const last = await page.evaluate(() => {
const s = window._channelsGetStateForTest();
return s.messages[s.messages.length - 1];
});
assert(last.sender === 'WsBob',
'should parse sender from "Sender: text", got: ' + last.sender);
assert(last.text === 'parsed message',
'displayText should strip sender prefix, got: ' + last.text);
});
await step('dedup by packetHash: second observer bumps repeats + observers list', async () => {
const before = await page.evaluate(
() => window._channelsGetStateForTest().messages.length);
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((prev) =>
window._channelsGetStateForTest().messages.length === prev + 1,
before, { timeout: 3000 });
const last = await page.evaluate(() => {
const s = window._channelsGetStateForTest();
return s.messages[s.messages.length - 1];
});
assert(last.repeats >= 2, 'repeats should be >=2 after dedup, got: ' + last.repeats);
assert(Array.isArray(last.observers) && last.observers.length >= 2,
'observers should accumulate, got: ' + JSON.stringify(last.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;
});
const before = await page.evaluate(
() => window._channelsGetStateForTest().messages.length);
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(
(prev) => window._channelsGetStateForTest().messages.length === prev + 1,
before, { timeout: 3000 });
const last = await page.evaluate(() => {
const s = window._channelsGetStateForTest();
return s.messages[s.messages.length - 1];
});
assert(last.sender === 'WsEve' && /tail/.test(last.text),
'tail message should be appended, got: ' + JSON.stringify(last));
});
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); });