aboutsummaryrefslogtreecommitdiffstats
path: root/packages/meshbay-hub/tests
diff options
context:
space:
mode:
Diffstat (limited to 'packages/meshbay-hub/tests')
-rw-r--r--packages/meshbay-hub/tests/harness/reconnect_race_harness.mjs230
-rw-r--r--packages/meshbay-hub/tests/test_hover_scoped.py62
-rw-r--r--packages/meshbay-hub/tests/test_reconnect_race.py93
3 files changed, 385 insertions, 0 deletions
diff --git a/packages/meshbay-hub/tests/harness/reconnect_race_harness.mjs b/packages/meshbay-hub/tests/harness/reconnect_race_harness.mjs
new file mode 100644
index 0000000..0f1cd97
--- /dev/null
+++ b/packages/meshbay-hub/tests/harness/reconnect_race_harness.mjs
@@ -0,0 +1,230 @@
+// Run the SHIPPED download over the SHIPPED reconnect, against a node that
+// refuses what comes before the handshake, and see whether the download lives.
+//
+// transport.js is executed whole — connect(), _reconnectLoop, _sendAndWait,
+// _dispatch and the framing are the shipped code — and the pipeline is lifted
+// out of file-utils.js as text. What is modelled is the environment: a clock,
+// a peer connection that opens its channel some time after the answer, a hub
+// that answers the offer and the token, and a node that answers anything but
+// the opening messages with "Handshake required" until the proof is in, as
+// webrtc/dispatch.py does. Cryptography is not under test here: the checks
+// that would refuse a peer are stubbed to accept.
+//
+// The scenario is the one that failed live: the node stops answering, the
+// chunks in flight time out, and their retries come due while the reconnect's
+// new channel is open and its handshake is not finished.
+//
+// Usage: node reconnect_race_harness.mjs <file-utils.js> <transport sources> <json>
+import { readFileSync } from 'fs';
+import { delimiter } from 'path';
+
+const utils = readFileSync(process.argv[2], 'utf8');
+const transportSrc = process.argv[3].split(delimiter)
+ .map((p) => readFileSync(p, 'utf8')).join('\n');
+const {
+ chunks = 400,
+ chunkMs = 100,
+ hangAt = 5000, // the node stops answering the first connection
+ failAfter = 30200, // ...and the peer connection reports `failed` this much later
+ openMs = 100, // answer → channel open
+ rttMs = 300, // one request → its reply, during the handshake
+ lateCloseMs = 0, // >0: the replaced channel's close event fires this late
+ runUntil = 300000,
+} = JSON.parse(process.argv[4] || '{}');
+
+for (const level of ['log', 'warn', 'error', 'info', 'debug']) {
+ console[level] = (...args) => process.stderr.write(args.join(' ') + '\n');
+}
+process.on('unhandledRejection', () => {});
+
+// ── The clock ────────────────────────────────────────────────────────────────
+
+let now = 0;
+let nextId = 1;
+let timers = [];
+globalThis.setTimeout = (fn, ms) => {
+ const t = { at: now + (ms || 0), fn, id: nextId++ };
+ timers.push(t);
+ return t.id;
+};
+globalThis.setInterval = (fn, ms) => {
+ const t = { at: now + ms, fn, id: nextId++, every: ms };
+ timers.push(t);
+ return t.id;
+};
+globalThis.clearTimeout = (id) => { timers = timers.filter((t) => t.id !== id); };
+globalThis.clearInterval = globalThis.clearTimeout;
+Date.now = () => now;
+const flush = async () => { for (let i = 0; i < 50; i++) await Promise.resolve(); };
+async function run(until) {
+ await flush();
+ while (timers.length) {
+ const due = timers.reduce((a, b) => (b.at < a.at ? b : a));
+ if (due.at > until) break;
+ if (due.every) due.at += due.every;
+ else timers = timers.filter((t) => t !== due);
+ now = Math.max(now, due.every ? due.at - due.every : due.at);
+ due.fn();
+ await flush();
+ }
+ now = until;
+}
+
+// ── The page around the transport ────────────────────────────────────────────
+
+globalThis.window = globalThis;
+globalThis.addEventListener = () => {};
+globalThis.removeEventListener = () => {};
+globalThis.location = { hash: '' };
+globalThis.document = {
+ addEventListener() {}, removeEventListener() {}, visibilityState: 'visible',
+};
+globalThis.localStorage = { getItem: () => null, setItem() {}, removeItem() {} };
+
+const b64 = (u8) => Buffer.from(u8).toString('base64');
+window.MeshBayCrypto = {
+ b64encode: (u8) => b64(u8),
+ b64decode: (s) => new Uint8Array(Buffer.from(String(s), 'base64')),
+ verifyNodeSignature: async () => true,
+ challengeTranscript: () => new Uint8Array(1),
+ webrtcBinding: () => new Uint8Array(1),
+ handshakeProof: async () => new Uint8Array([7]),
+ handshakeTranscript: () => new Uint8Array(1),
+ constantTimeEqual: () => true,
+ openGroup: async () => window.__codec.msgpack_encode({ chat_epoch: 1 }),
+ decryptChunkBin: async (_k, _f, index) => new Uint8Array([index & 255]),
+};
+
+const SDP = 'v=0\r\na=fingerprint:sha-256 AA:BB\r\n';
+globalThis.fetch = async (url) => ({
+ ok: true, status: 200, headers: { get: () => null },
+ json: async () => (url.endsWith('/mnp-token') ? { mnp_token: 't' } : { sdp: SDP }),
+});
+
+// ── The node ─────────────────────────────────────────────────────────────────
+
+const OPENING = new Set(['handshake', 'handshake_response', 'keypair_bundle_fetch',
+ 'gek_bundle_fetch', 'join_request', 'device_add_request', 'device_hello']);
+let refusedBeforeHandshake = 0;
+let served = 0;
+const pcs = [];
+
+class NodeSession {
+ constructor(channel) { this.channel = channel; this.authed = false; this.hung = false; }
+ reply(obj, ms) {
+ setTimeout(() => {
+ if (this.hung || this.channel.readyState !== 'open') return;
+ const body = window.__codec.msgpack_encode(obj);
+ const frame = new Uint8Array(4 + body.byteLength);
+ new DataView(frame.buffer).setUint32(0, body.byteLength, false);
+ frame.set(body, 4);
+ this.channel.onmessage({ data: frame.buffer });
+ }, ms);
+ }
+ receive(frame) {
+ if (this.hung) return;
+ const msg = window.__codec.msgpack_decode(frame.slice(4));
+ const req_id = msg.req_id;
+ if (!this.authed && !OPENING.has(msg.type)) {
+ refusedBeforeHandshake += 1;
+ return this.reply({ type: 'error', detail: 'Handshake required', req_id }, 1);
+ }
+ switch (msg.type) {
+ case 'handshake':
+ return this.reply({ type: 'handshake_challenge', v: '99.0', v_min: '0.0',
+ nonce: b64(new Uint8Array(32)), node_pk: 'pk', sig: 's', req_id }, rttMs);
+ case 'handshake_response':
+ setTimeout(() => { this.authed = true; }, rttMs);
+ return this.reply({ type: 'handshake_ack', proof: 'AA==', node_pk: 'pk', sig: 's',
+ nonce: 'n', ct: 'c', req_id }, rttMs);
+ case 'device_hello':
+ return this.reply({ type: 'device_hello_ack', req_id }, rttMs);
+ case 'file_req':
+ served += 1;
+ return this.reply({ type: 'file_chunk', file_id: msg.file_id,
+ chunk_index: msg.chunk_index, nonce: 1, ct: new Uint8Array([1]), req_id }, chunkMs);
+ default:
+ return undefined;
+ }
+ }
+}
+
+class FakeChannel {
+ constructor() { this.readyState = 'connecting'; this.binaryType = 'arraybuffer'; }
+ send(frame) {
+ if (this.readyState !== 'open') throw new Error('InvalidStateError');
+ this.session.receive(frame);
+ }
+ close() {
+ if (this.readyState === 'closed') return;
+ this.readyState = 'closed';
+ if (this.onclose) setTimeout(() => this.onclose({}), lateCloseMs);
+ }
+}
+
+class FakePC {
+ constructor() {
+ this.connectionState = 'new';
+ this.iceConnectionState = 'new';
+ this.iceGatheringState = 'complete';
+ pcs.push(this);
+ }
+ createDataChannel() { this.channel = new FakeChannel(); return this.channel; }
+ async createOffer() { return { type: 'offer', sdp: SDP }; }
+ async setLocalDescription(d) { this.localDescription = d; }
+ async setRemoteDescription() {
+ setTimeout(() => {
+ if (this.connectionState === 'closed') return;
+ this.channel.session = new NodeSession(this.channel);
+ this.channel.readyState = 'open';
+ this.connectionState = 'connected';
+ if (this.channel.onopen) this.channel.onopen();
+ }, openMs);
+ }
+ close() {
+ this.connectionState = 'closed';
+ if (this.channel) this.channel.close();
+ }
+}
+globalThis.RTCPeerConnection = FakePC;
+
+// ── The shipped transport and pipeline ───────────────────────────────────────
+
+new Function(`${transportSrc}\nwindow.__codec = { msgpack_encode, msgpack_decode };`)();
+const Transport = window.MeshBayTransport;
+
+const constant = (name) => utils.match(new RegExp(`^const ${name} = [^;]+;`, 'm'))[0];
+const from = utils.indexOf('const CHUNK_RETRY_ATTEMPTS');
+const to = utils.indexOf('async function downloadEntry');
+const { pipelinedDownload } = new Function('window', 't', `
+ ${constant('PIPELINE_WINDOW')}
+ ${constant('LANE_MIN_CHUNKS')}
+ ${utils.slice(from, utils.lastIndexOf('/**', to))}
+ return { pipelinedDownload };`)(window, (key) => key);
+
+const identity = { pkEdB64: 'ed', signAs: async () => 'sig', native: false };
+const main = new Transport('https://hub.invalid', 'token');
+let outcome = 'pending';
+let reconnected = 0;
+main.addReconnectListener(() => { reconnected += 1; });
+const written = [];
+const writable = { write: async (bytes) => { written.push(bytes[0]); } };
+
+main.connect('node', 'token', 'group', new Uint8Array(32), identity, null, 'u', 'uid',
+ undefined, undefined, '', 'pk')
+ .then(() => pipelinedDownload(main, 'gek', 'f'.repeat(64), chunks, null, writable, null, ''))
+ .then(() => { outcome = 'done'; }, (err) => { outcome = `failed: ${err.message}`; });
+
+await run(hangAt);
+pcs[0].channel.session.hung = true;
+await run(hangAt + failAfter);
+pcs[0].connectionState = 'failed';
+pcs[0].onconnectionstatechange();
+await run(runUntil);
+
+process.stdout.write(JSON.stringify({
+ outcome, reconnected, refusedBeforeHandshake, served,
+ written: written.length,
+ inOrder: written.every((b, i) => b === (i & 255)),
+ connections: pcs.length,
+}));
diff --git a/packages/meshbay-hub/tests/test_hover_scoped.py b/packages/meshbay-hub/tests/test_hover_scoped.py
new file mode 100644
index 0000000..f28dacd
--- /dev/null
+++ b/packages/meshbay-hub/tests/test_hover_scoped.py
@@ -0,0 +1,62 @@
+"""
+Every `:hover` rule in style.css is inside `@media (hover: hover)`.
+
+A tap on a touchscreen puts the element in `:hover` and nothing takes it out
+until another element is tapped, so a button tapped once kept its hover colour
+as if still pressed. It was fixed for the music player's buttons alone, then
+reported again on the pager arrows, the nav hamburger and the rest: a rule per
+button only moves the bug to the next button. This holds the whole file.
+"""
+
+from pathlib import Path
+
+import pytest
+
+STYLE = (Path(__file__).resolve().parents[1] / "src" / "meshbay_hub"
+ / "static" / "style.css")
+
+pytestmark = pytest.mark.skipif(not STYLE.exists(),
+ reason="the stylesheet is not in this checkout")
+
+
+def _hover_rules(source: str) -> list[tuple[int, list[str], str]]:
+ """(line, enclosing at-rule preludes, selector) for each rule naming :hover."""
+ found = []
+ stack: list[str] = []
+ start = 0
+ i = 0
+ while i < len(source):
+ if source.startswith("/*", i):
+ i = source.index("*/", i) + 2
+ start = i
+ continue
+ c = source[i]
+ if c == "{":
+ prelude = source[start:i].strip()
+ if ":hover" in prelude and not prelude.startswith("@"):
+ found.append((source.count("\n", 0, i) + 1, list(stack), prelude))
+ stack.append(prelude)
+ start = i + 1
+ elif c == "}":
+ stack.pop()
+ start = i + 1
+ elif c == ";":
+ start = i + 1
+ i += 1
+ return found
+
+
+def test_every_hover_rule_is_scoped_to_devices_that_hover():
+ rules = _hover_rules(STYLE.read_text(encoding="utf-8"))
+ assert rules, "no :hover rule found, did the parser break?"
+ loose = [f"line {line}: {sel}" for line, stack, sel in rules
+ if not any("(hover: hover)" in at for at in stack)]
+ assert not loose, ("wrap these in @media (hover: hover), a tap leaves them "
+ "stuck on a touchscreen:\n" + "\n".join(loose))
+
+
+def test_the_parser_catches_a_loose_rule():
+ rules = _hover_rules("/* a { } */ .a:hover { color: red; }\n"
+ "@media (hover: hover) { .b:hover { color: red; } }")
+ assert [(sel, bool(stack)) for _, stack, sel in rules] == [
+ (".a:hover", False), (".b:hover", True)]
diff --git a/packages/meshbay-hub/tests/test_reconnect_race.py b/packages/meshbay-hub/tests/test_reconnect_race.py
new file mode 100644
index 0000000..01c5182
--- /dev/null
+++ b/packages/meshbay-hub/tests/test_reconnect_race.py
@@ -0,0 +1,93 @@
+"""
+A reconnect must not fail the download it is reconnecting for.
+
+Reported live on 0.20: a large download from a node under load stopped with
+"Handshake required" in the transfers panel. `_sendAndWait` let every message
+through while the reconnect's own connect() ran, because the handshake it sends
+has to go through; so a chunk whose retry came due in that window went out on a
+channel that had opened and not finished its handshake. The node answered
+"Handshake required" (webrtc/dispatch.py), which no retry treats as a dropped
+connection. Now only the opening messages pass, and anything else waits for the
+session or fails as a dropped connection, which the download waits out.
+
+The replaced channel's close event is the second half: it reached the new
+connection, marked it closed and failed what it had in flight.
+
+These run the shipped transport and pipeline against a node that refuses what
+comes before the handshake: harness/reconnect_race_harness.mjs.
+"""
+
+import json
+import os
+import re
+import shutil
+import subprocess
+from pathlib import Path
+
+import pytest
+from spa_source import classic_scripts, transport_source
+
+STATIC = Path(__file__).resolve().parents[1] / "src" / "meshbay_hub" / "static"
+FILE_UTILS = STATIC / "file-utils.js"
+HARNESS = Path(__file__).parent / "harness" / "reconnect_race_harness.mjs"
+DISPATCH = (Path(__file__).resolve().parents[2] / "meshbay-node" / "src" / "meshbay_node"
+ / "transport" / "webrtc" / "dispatch.py")
+
+pytestmark = pytest.mark.skipif(
+ shutil.which("node") is None or not FILE_UTILS.exists(),
+ reason="node or the SPA sources are not available")
+
+
+def _run(**cfg) -> dict:
+ transport = os.pathsep.join(
+ str(STATIC / name) for name in classic_scripts() if name.startswith("transport"))
+ proc = subprocess.run(
+ ["node", str(HARNESS), str(FILE_UTILS), transport, json.dumps(cfg)],
+ capture_output=True, text=True, encoding="utf-8", timeout=120)
+ assert proc.returncode == 0, proc.stderr
+ return json.loads(proc.stdout)
+
+
+def test_retries_that_come_due_during_the_handshake_wait_for_it():
+ # The chunks in flight time out just before the connection is declared
+ # failed, and their retries land while the new channel is handshaking.
+ out = _run()
+ assert out["reconnected"] == 1 and out["connections"] == 2
+ assert out["refusedBeforeHandshake"] == 0, "a chunk was asked for before the handshake"
+ assert out["outcome"] == "done"
+ assert out["written"] == 400 and out["inOrder"]
+
+
+@pytest.mark.parametrize("cfg", [
+ {"failAfter": 20000}, # failure declared before anything times out
+ {"failAfter": 31000, "rttMs": 800}, # a slow handshake
+ {"openMs": 2500}, # a slow channel
+])
+def test_the_download_survives_other_timings(cfg):
+ out = _run(**cfg)
+ assert out["refusedBeforeHandshake"] == 0
+ assert out["outcome"] == "done" and out["written"] == 400 and out["inOrder"]
+
+
+@pytest.mark.parametrize("late", [800, 1500, 3000])
+def test_the_replaced_channels_close_event_is_ignored(late):
+ out = _run(lateCloseMs=late)
+ assert out["outcome"] == "done" and out["written"] == 400
+
+
+def _node_opening_types() -> set[str]:
+ """What the node answers before the handshake, read from its dispatch."""
+ from meshbay_common.protocol import MNP
+ src = DISPATCH.read_text(encoding="utf-8")
+ before = src[src.index("def _dispatch_message"):src.index("elif self._user_id is None")]
+ names = set(re.findall(r"MNP\.([A-Z_]+)", before))
+ return {getattr(MNP, n) for n in names}
+
+
+def test_the_opening_messages_are_the_ones_the_node_takes_before_the_handshake():
+ m = re.search(r"const SESSION_OPENING = new Set\(\[(.*?)\]\);", transport_source(), re.S)
+ client = set(re.findall(r"'([a-z_]+)'", m.group(1)))
+ # device_hello is the one connect() sends after the proof and before it
+ # returns: the node takes it then, and the session is not up until it has.
+ assert client - _node_opening_types() == {"device_hello"}
+ assert _node_opening_types() <= client