Files
wehub-resource-sync b4fbd6fe9f
Deploy Site / deploy-vercel (push) Has been skipped
Deploy Site / deploy-docs (push) Has been skipped
Build Skills Index / build-index (push) Has been skipped
CI / Deny unrelated histories (push) Has been skipped
CI / Detect affected areas (push) Successful in 27m35s
CI / OSV scan (push) Failing after 4s
CI / Build&Test Docker image (push) Successful in 9s
CI / Supply-chain scan (push) Has been skipped
CI / Lint Docker scripts (push) Failing after 5m13s
CI / Check contributors (push) Failing after 12m8s
CI / Docs Site (push) Failing after 12m8s
CI / TypeScript (push) Failing after 12m8s
CI / Python lints (push) Failing after 12m9s
CI / Python tests (push) Failing after 12m9s
CI / Check uv.lock (push) Failing after 23m22s
CI / CI timing report (push) Has been cancelled
Build Skills Index / trigger-deploy (push) Has been cancelled
CI / All required checks pass (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:56:03 +08:00

113 lines
3.4 KiB
JavaScript

/**
* Regression tests for the WhatsApp bridge send queue (#33360).
*
* The bridge must serialise all sock.sendMessage() calls through a
* promise-based queue so that concurrent HTTP /send requests never
* produce overlapping Baileys socket writes. Overlapping writes are
* the confirmed root cause of cross-chat contamination.
*
* These tests exercise the queue itself — they do NOT require a live
* WhatsApp socket.
*/
import { strict as assert } from 'node:assert';
// ------------------------------------------------------------------
// 1. Unit test for the queue primitives
// ------------------------------------------------------------------
/**
* Replicate the queue logic from bridge.js so we can test it in
* isolation without importing the full module (which would trigger
* Baileys / express side effects).
*/
function createSendQueue() {
let _sendQueue = Promise.resolve();
function enqueueSend(fn) {
const task = _sendQueue.then(() => fn(), () => fn());
_sendQueue = task.catch(() => {});
return task;
}
return { enqueueSend };
}
// -- serial ordering -------------------------------------------------
{
const { enqueueSend } = createSendQueue();
const order = [];
const a = enqueueSend(async () => {
await new Promise(r => setTimeout(r, 30));
order.push('a');
return 'A';
});
const b = enqueueSend(async () => {
order.push('b');
return 'B';
});
const c = enqueueSend(async () => {
await new Promise(r => setTimeout(r, 10));
order.push('c');
return 'C';
});
const results = await Promise.all([a, b, c]);
assert.deepStrictEqual(results, ['A', 'B', 'C'], 'all tasks resolve');
assert.deepStrictEqual(order, ['a', 'b', 'c'], 'tasks execute in FIFO order');
console.log(' ✓ serial ordering');
}
// -- error isolation (one rejection does not stall the queue) --------
{
const { enqueueSend } = createSendQueue();
const order = [];
const bad = enqueueSend(async () => {
order.push('bad');
throw new Error('boom');
});
const good = enqueueSend(async () => {
order.push('good');
return 'ok';
});
await assert.rejects(() => bad, /boom/, 'bad task rejects');
const g = await good;
assert.strictEqual(g, 'ok', 'good task still resolves');
assert.deepStrictEqual(order, ['bad', 'good'], 'good runs after bad');
console.log(' ✓ error isolation');
}
// -- timeout still fires (wrapped inside enqueueSend) ----------------
{
const { enqueueSend } = createSendQueue();
const timedOut = enqueueSend(async () => {
await new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), 20));
});
await assert.rejects(() => timedOut, /timeout/, 'inner timeout propagates');
console.log(' ✓ timeout propagation');
}
// -- concurrent enqueues maintain single-consumer semantics ----------
{
const { enqueueSend } = createSendQueue();
let concurrent = 0;
let maxConcurrent = 0;
async function tracked() {
concurrent += 1;
if (concurrent > maxConcurrent) maxConcurrent = concurrent;
await new Promise(r => setTimeout(r, 5));
concurrent -= 1;
}
await Promise.all(Array.from({ length: 20 }, () => enqueueSend(tracked)));
assert.strictEqual(maxConcurrent, 1, 'never more than one in-flight');
assert.strictEqual(concurrent, 0, 'all finished');
console.log(' ✓ single-consumer concurrency');
}
console.log('\n✅ All send-queue tests passed.');