|
1 | | -import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 1 | +import { MAX_TIMER_TIMEOUT_MS } from "openclaw/plugin-sdk/number-runtime"; |
| 2 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
2 | 3 | import { startWebLoginWithQr, waitForWebLogin } from "./login-qr.js"; |
3 | 4 | import { renderQrPngDataUrl } from "./qr-image.js"; |
4 | 5 | import { |
@@ -101,6 +102,10 @@ describe("login-qr", () => { |
101 | 102 | .mockImplementation(async (input) => `data:image/png;base64,encoded:${input}`); |
102 | 103 | }); |
103 | 104 |
|
| 105 | + afterEach(() => { |
| 106 | + vi.useRealTimers(); |
| 107 | + }); |
| 108 | + |
104 | 109 | it("restarts login once on status 515 and completes", async () => { |
105 | 110 | waitForWaConnectionMock |
106 | 111 | // Baileys v7 wraps the error: { error: BoomError(515) } |
@@ -181,6 +186,27 @@ describe("login-qr", () => { |
181 | 186 | expect(logoutWebMock).toHaveBeenCalledOnce(); |
182 | 187 | }); |
183 | 188 |
|
| 189 | + it("caps oversized wait timeouts to a timer-safe delay", async () => { |
| 190 | + const accountId = "oversized-wait-timeout"; |
| 191 | + waitForWaConnectionMock.mockImplementation(() => new Promise(() => {})); |
| 192 | + |
| 193 | + const start = await startWebLoginWithQr({ timeoutMs: 5000, accountId }); |
| 194 | + expect(start.qrDataUrl).toBe("data:image/png;base64,encoded:qr-data"); |
| 195 | + |
| 196 | + vi.useFakeTimers(); |
| 197 | + const resultPromise = waitForWebLogin({ |
| 198 | + timeoutMs: Number.MAX_SAFE_INTEGER, |
| 199 | + currentQrDataUrl: start.qrDataUrl, |
| 200 | + accountId, |
| 201 | + }); |
| 202 | + |
| 203 | + await vi.advanceTimersByTimeAsync(MAX_TIMER_TIMEOUT_MS); |
| 204 | + await expect(resultPromise).resolves.toEqual({ |
| 205 | + connected: false, |
| 206 | + message: "Still waiting for the QR scan. Let me know when you’ve scanned it.", |
| 207 | + }); |
| 208 | + }); |
| 209 | + |
184 | 210 | it("turns unexpected login cleanup failures into a normal login error", async () => { |
185 | 211 | waitForWaConnectionMock.mockRejectedValueOnce({ |
186 | 212 | output: { statusCode: 401 }, |
|
0 commit comments