@@ -669,4 +669,52 @@ describe("WhatsAppConnectionController", () => {
|
669 | 669 | vi.useRealTimers(); |
670 | 670 | } |
671 | 671 | }); |
| 672 | + |
| 673 | +it("uses messageTimeoutMs * 4 as the app-silence window for fresh connections with no inbound", async () => { |
| 674 | +// Verifies the watchdog respects appSilenceTimeoutMs = messageTimeoutMs * 4 on first open. |
| 675 | +// Transport is kept well within its own timeout so only app-silence fires. |
| 676 | +vi.useFakeTimers(); |
| 677 | +const msgTimeoutMs = 100; |
| 678 | +const controllerLocal = new WhatsAppConnectionController({ |
| 679 | +accountId: "work", |
| 680 | +authDir: "/tmp/wa-auth", |
| 681 | +verbose: false, |
| 682 | +keepAlive: true, |
| 683 | +heartbeatSeconds: 1, |
| 684 | +transportTimeoutMs: 10_000, |
| 685 | +messageTimeoutMs: msgTimeoutMs, |
| 686 | +watchdogCheckMs: 10, |
| 687 | +reconnectPolicy: { |
| 688 | +initialMs: 250, |
| 689 | +maxMs: 1_000, |
| 690 | +factor: 2, |
| 691 | +jitter: 0, |
| 692 | +maxAttempts: 5, |
| 693 | +}, |
| 694 | +}); |
| 695 | + |
| 696 | +try { |
| 697 | +const sock = createSocketWithTransportEmitter(); |
| 698 | +createWaSocketMock.mockResolvedValueOnce(sock as never); |
| 699 | +waitForWaConnectionMock.mockResolvedValueOnce(undefined); |
| 700 | + |
| 701 | +const timeouts: string[] = []; |
| 702 | +await controllerLocal.openConnection({ |
| 703 | +connectionId: "conn-app-silence", |
| 704 | +createListener: async () => createListenerStub() as never, |
| 705 | +onWatchdogTimeout: () => timeouts.push("timeout"), |
| 706 | +}); |
| 707 | + |
| 708 | +// Just before messageTimeoutMs * 4 — no force-close expected |
| 709 | +await vi.advanceTimersByTimeAsync(msgTimeoutMs * 4 - 20); |
| 710 | +expect(timeouts).toHaveLength(0); |
| 711 | + |
| 712 | +// Past messageTimeoutMs * 4 — force-close must fire |
| 713 | +await vi.advanceTimersByTimeAsync(40); |
| 714 | +expect(timeouts.length).toBeGreaterThanOrEqual(1); |
| 715 | +} finally { |
| 716 | +await controllerLocal.shutdown(); |
| 717 | +vi.useRealTimers(); |
| 718 | +} |
| 719 | +}); |
672 | 720 | }); |