Skip to content

A self-hosted UPI & COD payment gateway with zero per-transaction platform fees, universal JavaScript checkout integration, dynamic QR payments, WhatsApp OTP authentication, and automated payment verification.

JavaScript

test(payment-api): keep WhatsApp route tests offline and eliminate 31s delay

ArunGupta152685be8458dfa0
diff --git a/apps/payment-api/src/__tests__/fixtures/offline-whatsapp.js b/apps/payment-api/src/__tests__/fixtures/offline-whatsapp.js
new file mode 100644
index 0000000..3ef8e9e
--- /dev/null
+++ b/apps/payment-api/src/__tests__/fixtures/offline-whatsapp.js
@@ -0,0 +1,37 @@
+/** Route-contract fake. Never imports BerryProtocol, touches a session, or opens a socket. */
+export function createOfflineWhatsApp() {
+ let status = 'DISCONNECTED';
+ let qrCode = null;
+ const calls = [];
+ return {
+ calls,
+ getStatus: () => status,
+ getQrCode: () => qrCode,
+ getClientInfo: () => ({ status, platform: 'BerryProtocol v0.1.10', isConnected: false }),
+ async initialize() {
+ calls.push({ method: 'initialize' });
+ status = 'QR_READY';
+ qrCode = { dataUrl: 'data:image/svg+xml;base64,PHN2Zy8+' };
+ },
+ async reconnect() {
+ calls.push({ method: 'reconnect' });
+ await this.initialize();
+ },
+ async waitForQrCode() { return qrCode; },
+ async requestPairingCode(phoneNumber) {
+ calls.push({ method: 'requestPairingCode', phoneNumber });
+ status = 'PAIRING_CODE_READY';
+ return { pairingCode: '12345678', phoneNumber };
+ },
+ async sendOtp(phoneNumber) {
+ // Do not retain the OTP or message body in test evidence.
+ calls.push({ method: 'sendOtp', phoneNumber });
+ return { success: true, delivered: false, simulated: true };
+ },
+ async disconnect() {
+ calls.push({ method: 'disconnect' });
+ status = 'DISCONNECTED';
+ qrCode = null;
+ }
+ };
+}
diff --git a/apps/payment-api/src/__tests__/whatsapp-berry-auth.test.js b/apps/payment-api/src/__tests__/whatsapp-berry-auth.test.js
index 5d52e52..f2a94ff 100644
--- a/apps/payment-api/src/__tests__/whatsapp-berry-auth.test.js
+++ b/apps/payment-api/src/__tests__/whatsapp-berry-auth.test.js
@@ -6,10 +6,12 @@ process.env.NODE_ENV = process.env.NODE_ENV || 'test';
import { createPaymentApp } from '../index.js';
import { Database } from '@youpi/db';
import { authFetch } from '../../../../scripts/test-support.js';
+import { createOfflineWhatsApp } from './fixtures/offline-whatsapp.js';
test('WhatsApp Berry Protocol OTP Customer Auth & Merchant Configuration Suite', async (t) => {
const customDb = new Database({ inMemory: true });
- const app = createPaymentApp(customDb, { otpCooldownMs: 0 });
+ const whatsapp = createOfflineWhatsApp();
+ const app = createPaymentApp(customDb, { otpCooldownMs: 0, whatsappBerryService: whatsapp });
const testPort = 6288;
const serverInstance = await new Promise(resolve => {
@@ -123,10 +125,6 @@ test('WhatsApp Berry Protocol OTP Customer Auth & Merchant Configuration Suite',
let loginOtp = null;
- // The signup OTP was requested moments earlier.
- // Wait until the 30-second resend cooldown expires.
- await new Promise(resolve => setTimeout(resolve, 31_000));
-
await t.test('7. POST /api/v1/customer/send-otp dispatches WhatsApp OTP for existing user', async () => {
const res = await authFetch(`${apiBase}/api/v1/customer/send-otp`, {
method: 'POST',
@@ -174,18 +172,22 @@ test('WhatsApp Berry Protocol OTP Customer Auth & Merchant Configuration Suite',
const data = await res.json();
assert.equal(data.success, true);
assert.ok(data.qrCode);
+ assert.ok(whatsapp.calls.some(call => call.method === 'initialize'));
});
await t.test('11. Merchant WhatsApp pairing-code API accepts phone and returns 8-digit code', async () => {
const res = await authFetch(`${apiBase}/api/v1/admin/whatsapp/pairing-code`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ phoneNumber: '918796266491' })
+ body: JSON.stringify({ phoneNumber: '919999999999' })
});
assert.equal(res.status, 200);
const data = await res.json();
assert.equal(data.success, true);
- assert.ok(data.pairingCode);
+ assert.equal(data.pairingCode, '12345678');
+ assert.deepEqual(whatsapp.calls.find(call => call.method === 'requestPairingCode'), {
+ method: 'requestPairingCode', phoneNumber: '919999999999'
+ });
});
await t.test('12. Merchant WhatsApp disconnect API safely clears session', async () => {