fix(qa-matrix): cap substrate request timeouts

This commit is contained in:
Peter Steinberger
2026-05-29 16:22:26 -04:00
parent 69c3b56bde
commit 0983e763fe
2 changed files with 30 additions and 1 deletions

View File

@@ -0,0 +1,27 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { requestMatrixJson, type MatrixQaFetchLike } from "./request.js";
const MAX_TIMER_TIMEOUT_MS = 2_147_000_000;
describe("requestMatrixJson", () => {
afterEach(() => {
vi.restoreAllMocks();
});
it("caps oversized request timeouts before creating the abort signal", async () => {
const signal = AbortSignal.abort();
const timeoutSpy = vi.spyOn(AbortSignal, "timeout").mockReturnValue(signal);
const fetchImpl = vi.fn<MatrixQaFetchLike>(async () => Response.json({ ok: true }));
await requestMatrixJson({
baseUrl: "https://matrix.example.test",
endpoint: "/_matrix/client/v3/account/whoami",
fetchImpl,
method: "GET",
timeoutMs: MAX_TIMER_TIMEOUT_MS + 1_000_000,
});
expect(timeoutSpy).toHaveBeenCalledWith(MAX_TIMER_TIMEOUT_MS);
expect(fetchImpl).toHaveBeenCalledWith(expect.any(URL), expect.objectContaining({ signal }));
});
});

View File

@@ -1,3 +1,5 @@
import { resolveTimerTimeoutMs } from "openclaw/plugin-sdk/number-runtime";
export type MatrixQaFetchLike = typeof fetch;
type MatrixQaRequestResult<T> = {
@@ -30,7 +32,7 @@ export async function requestMatrixJson<T>(params: {
...(params.accessToken ? { authorization: `Bearer ${params.accessToken}` } : {}),
},
...(params.body !== undefined ? { body: JSON.stringify(params.body) } : {}),
signal: AbortSignal.timeout(params.timeoutMs ?? 20_000),
signal: AbortSignal.timeout(resolveTimerTimeoutMs(params.timeoutMs, 20_000)),
});
let body: unknown = {};
try {