Edit: /opt/mawid/scripts/load-sanity.mjs (2836B)
#!/usr/bin/env node
/**
* Load sanity (PROJECT_PLAN Phase 7 task 6): fire N concurrent WhatsApp
* webhook conversations at the running API and verify none are lost.
* Usage: node scripts/load-sanity.mjs [count] (default 50)
*/
import { createHmac } from 'node:crypto';
import { execFileSync } from 'node:child_process';
const API = process.env.API_URL ?? 'http://localhost:3001';
const SECRET = process.env.WA_APP_SECRET ?? 'dev-app-secret';
const PHONE_NUMBER_ID = process.env.WA_PHONE_NUMBER_ID_TEST ?? 'test-phone-number-id';
const COUNT = Number(process.argv[2] ?? 50);
const runId = `${Date.now()}`;
function payload(i) {
return {
object: 'whatsapp_business_account',
entry: [
{
id: 'waba-load',
changes: [
{
field: 'messages',
value: {
messaging_product: 'whatsapp',
metadata: { display_phone_number: '905550000001', phone_number_id: PHONE_NUMBER_ID },
contacts: [{ profile: { name: `Load Patient ${i}` }, wa_id: `90570${String(i).padStart(7, '0')}` }],
messages: [
{
from: `90570${String(i).padStart(7, '0')}`,
id: `wamid.load.${runId}.${i}`,
timestamp: String(Math.floor(Date.now() / 1000)),
type: 'text',
text: { body: `load test message ${i}` },
},
],
},
},
],
},
],
};
}
const started = Date.now();
const results = await Promise.all(
Array.from({ length: COUNT }, async (_, i) => {
const body = JSON.stringify(payload(i));
const signature = `sha256=${createHmac('sha256', SECRET).update(body).digest('hex')}`;
try {
const res = await fetch(`${API}/webhooks/whatsapp`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'X-Hub-Signature-256': signature },
body,
});
return res.status;
} catch {
return 0;
}
}),
);
const elapsed = Date.now() - started;
const ok = results.filter((s) => s >= 200 && s < 300).length;
console.log(`sent ${COUNT} concurrent webhooks in ${elapsed}ms — ${ok} accepted`);
// Verify persistence: every wamid from this run must be stored exactly once.
const stored = Number(
execFileSync('docker', [
'compose',
'exec',
'-T',
'postgres',
'psql',
'-U',
'mawid',
'-d',
'mawid',
'-tAc',
`SELECT count(*) FROM "Message" WHERE "waMessageId" LIKE 'wamid.load.${runId}.%'`,
])
.toString()
.trim(),
);
console.log(`stored inbound messages for this run: ${stored}/${COUNT}`);
if (ok !== COUNT || stored !== COUNT) {
console.error('LOAD SANITY FAILED — message loss detected');
process.exit(1);
}
console.log('LOAD SANITY PASSED — no message loss');