mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-03 17:34:07 +00:00
fix(whatsapp): cap credential flush timeout
This commit is contained in:
25
extensions/whatsapp/src/creds-persistence.test.ts
Normal file
25
extensions/whatsapp/src/creds-persistence.test.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { MAX_TIMER_TIMEOUT_MS } from "openclaw/plugin-sdk/number-runtime";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { enqueueCredsSave, waitForCredsSaveQueueWithTimeout } from "./creds-persistence.js";
|
||||
|
||||
describe("creds-persistence", () => {
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it("caps oversized credential flush timeouts before scheduling", async () => {
|
||||
vi.useFakeTimers();
|
||||
const timeoutSpy = vi.spyOn(globalThis, "setTimeout");
|
||||
const authDir = "oversized-timeout";
|
||||
enqueueCredsSave(
|
||||
authDir,
|
||||
() => undefined,
|
||||
() => undefined,
|
||||
);
|
||||
|
||||
await waitForCredsSaveQueueWithTimeout(authDir, Number.MAX_SAFE_INTEGER);
|
||||
|
||||
expect(timeoutSpy).toHaveBeenCalledWith(expect.any(Function), MAX_TIMER_TIMEOUT_MS);
|
||||
});
|
||||
});
|
||||
@@ -1,3 +1,4 @@
|
||||
import { resolveTimerTimeoutMs } from "openclaw/plugin-sdk/number-runtime";
|
||||
import { replaceFileAtomic } from "openclaw/plugin-sdk/security-runtime";
|
||||
import { assertWebCredsPathRegularFileOrMissing, resolveWebCredsPath } from "./creds-files.js";
|
||||
|
||||
@@ -71,11 +72,12 @@ export async function waitForCredsSaveQueueWithTimeout(
|
||||
authDir: string,
|
||||
timeoutMs = CREDS_SAVE_FLUSH_TIMEOUT_MS,
|
||||
): Promise<CredsQueueWaitResult> {
|
||||
const boundedTimeoutMs = resolveTimerTimeoutMs(timeoutMs, CREDS_SAVE_FLUSH_TIMEOUT_MS, 0);
|
||||
let flushTimeout: ReturnType<typeof setTimeout> | undefined;
|
||||
return await Promise.race([
|
||||
waitForCredsSaveQueue(authDir).then(() => "drained" as const),
|
||||
new Promise<CredsQueueWaitResult>((resolve) => {
|
||||
flushTimeout = setTimeout(() => resolve("timed_out"), timeoutMs);
|
||||
flushTimeout = setTimeout(() => resolve("timed_out"), boundedTimeoutMs);
|
||||
}),
|
||||
]).finally(() => {
|
||||
if (flushTimeout) {
|
||||
|
||||
Reference in New Issue
Block a user