Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(engine): baileys — suppress onDisconnected on transient reconnect; test pairing-code guard

rmyndharise90f5d61ff3b
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index c4f113c17..1ea9db05c 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -90,14 +90,16 @@ describe('BaileysAdapter lifecycle & status', () => {
expect(makeWASocket).not.toHaveBeenCalled(); // no reconnect
});
- it('on a recoverable close: reconnects (re-creates the socket)', async () => {
+ it('on a recoverable close: reconnects (re-creates the socket) and does NOT fire onDisconnected', async () => {
+ const onDisconnected = jest.fn();
const adapter = newAdapter();
- await adapter.initialize(noopCallbacks({}));
+ await adapter.initialize(noopCallbacks({ onDisconnected }));
const makeWASocket = jest.requireMock('@whiskeysockets/baileys').default as jest.Mock;
makeWASocket.mockClear();
fakeSock.fire('connection.update', { connection: 'close', lastDisconnect: { error: { output: { statusCode: 515 } } } });
await new Promise(r => setImmediate(r)); // let the async connect() run
expect(makeWASocket).toHaveBeenCalledTimes(1);
+ expect(onDisconnected).not.toHaveBeenCalled();
});
it('disconnect() ends the socket and does not reconnect', async () => {
@@ -108,6 +110,11 @@ describe('BaileysAdapter lifecycle & status', () => {
expect(adapter.getStatus()).toBe(EngineStatus.DISCONNECTED);
});
+ it('requestPairingCode throws EngineNotReadyError before initialize()', async () => {
+ const adapter = newAdapter();
+ await expect(adapter.requestPairingCode('628999')).rejects.toBeInstanceOf(EngineNotReadyError);
+ });
+
it('requestPairingCode delegates to the socket', async () => {
const adapter = newAdapter();
await adapter.initialize(noopCallbacks({}));
@@ -134,6 +141,3 @@ describe('BaileysAdapter capability gating', () => {
});
});
-// Suppress unused import lint warning — EngineNotReadyError is the correct class to import
-// even though its direct use in tests is through the toBeInstanceOf check on thrown errors.
-void EngineNotReadyError;
diff --git a/src/engine/adapters/baileys.adapter.ts b/src/engine/adapters/baileys.adapter.ts
index fb3e19dba..b69df4903 100644
--- a/src/engine/adapters/baileys.adapter.ts
+++ b/src/engine/adapters/baileys.adapter.ts
@@ -149,7 +149,9 @@ export class BaileysAdapter implements IWhatsAppEngine {
}
// Recoverable (e.g. restartRequired right after pairing, transient drop) — reconnect.
- this.callbacks.onDisconnected?.(`connection closed (${statusCode ?? 'unknown'})`);
+ // Do NOT fire onDisconnected here; this is a transient drop, not a terminal disconnect.
+ // connect() calls setStatus(INITIALIZING) which fires onStateChanged — that is the correct signal.
+ this.logger.log('Baileys connection dropped; reconnecting', { statusCode });
this.connect().catch(err => {
this.setStatus(EngineStatus.FAILED);
this.callbacks.onError?.(err instanceof Error ? err.message : String(err));