Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(wwebjs): keep ACTION_REQUIRED through a page re-inject

Yudhi Armyndharis4ecf301e6fb8
diff --git a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
index b5c5dee55..dfdc80025 100644
--- a/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
+++ b/src/engine/adapters/whatsapp-web-js.adapter.spec.ts
@@ -7533,6 +7533,34 @@ describe('WhatsAppWebJsAdapter honest outcomes (no phantom success)', () => {
}
});
+ // A page navigation re-runs the inject, which fires 'authenticated' and then 'ready' again.
+ it('stays ACTION_REQUIRED through a re-inject and arms no readiness deadline', async () => {
+ jest.useFakeTimers();
+ try {
+ const { adapter, evaluate, client } = promoteToReady();
+
+ (client as EventEmitter).emit('authenticated');
+ await jest.advanceTimersByTimeAsync(2100);
+ for (let i = 0; i < 5; i++) {
+ evaluate.mockResolvedValueOnce({ modalPresent: true, dismissed: true } satisfies ModalProbe);
+ await jest.advanceTimersByTimeAsync(5100);
+ }
+ expect(adapter.getStatus()).toBe(EngineStatus.ACTION_REQUIRED);
+ const { reconcile: deadline } = adapter as unknown as { reconcile: { scheduleReadyReconcile: () => void } };
+ const reconcile = jest.spyOn(deadline, 'scheduleReadyReconcile');
+
+ (client as EventEmitter).emit('authenticated');
+ expect(adapter.getStatus()).toBe(EngineStatus.ACTION_REQUIRED);
+ (client as EventEmitter).emit('ready');
+ await jest.advanceTimersByTimeAsync(0);
+
+ expect(adapter.getStatus()).toBe(EngineStatus.ACTION_REQUIRED);
+ expect(reconcile).not.toHaveBeenCalled();
+ } finally {
+ jest.useRealTimers();
+ }
+ });
+
// ── Dialog diagnostics (#1072 follow-up) ─────────────────────────────────────
// When the probe finds nothing to click, the watcher asks the page what dialogs ARE visible,
// so a modal whose title or button label the detector does not recognise lands in the logs
diff --git a/src/engine/adapters/wwebjs-lifecycle.ts b/src/engine/adapters/wwebjs-lifecycle.ts
index c1a1dbcfc..b1844330d 100644
--- a/src/engine/adapters/wwebjs-lifecycle.ts
+++ b/src/engine/adapters/wwebjs-lifecycle.ts
@@ -614,7 +614,8 @@ export class WwebjsLifecycle {
this.client.on('authenticated', () => {
// Only the first authentication starts the reconcile window. Ignore a re-fired 'authenticated'
- // while already AUTHENTICATING (so it can't restart the 90s deadline), once READY/FAILED, or any
+ // while already AUTHENTICATING (so it can't restart the 90s deadline), once READY/FAILED, at
+ // ACTION_REQUIRED (a re-inject must not clear it, which only stop then start does), or any
// time after the adapter is finished — teardown, or a reported disconnect the lifecycle has not
// replaced the engine for yet (#982). The initial status is DISCONNECTED too, so "finished" is
// carried by the flags, never by the status alone.
@@ -624,6 +625,7 @@ export class WwebjsLifecycle {
this.disconnectReported ||
this.status === EngineStatus.AUTHENTICATING ||
this.status === EngineStatus.READY ||
+ this.status === EngineStatus.ACTION_REQUIRED ||
this.status === EngineStatus.FAILED
) {
return;