Skip to content

Commit bd2a91d

Browse files
authored
test(minimax): clear OAuth observation deadlines (#134569)
* test(minimax): clear OAuth observation deadlines * test(providers): pin promotional pricing fixture dates
1 parent 4fd2529 commit bd2a91d

1 file changed

Lines changed: 62 additions & 94 deletions

File tree

‎extensions/minimax/oauth.test.ts‎

Lines changed: 62 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { createServer } from "node:http";
33
import type { Socket } from "node:net";
44
import { MAX_TIMER_TIMEOUT_MS } from "openclaw/plugin-sdk/number-runtime";
5+
import { withTimeout } from "openclaw/plugin-sdk/text-utility-runtime";
56
import { afterEach, describe, expect, it, vi } from "vitest";
67
import { loginMiniMaxPortalOAuth } from "./oauth.js";
78

@@ -189,24 +190,15 @@ async function expectFetchWithoutDeadlineToStayPending(params: {
189190
expect(result).toBe("pending");
190191
}
191192

192-
async function loginOutcomeWithin(
193-
promise: Promise<unknown>,
194-
timeoutMs: number,
195-
): Promise<
196-
| { status: "pending" }
197-
| { status: "resolved" }
198-
| {
199-
status: "rejected";
200-
error: unknown;
201-
}
202-
> {
203-
return await Promise.race([
193+
async function loginOutcomeWithin(promise: Promise<unknown>, timeoutMs: number) {
194+
return await withTimeout(
204195
promise.then(
205196
() => ({ status: "resolved" as const }),
206197
(error: unknown) => ({ status: "rejected" as const, error }),
207198
),
208-
timeoutResult({ status: "pending" as const }, timeoutMs),
209-
]);
199+
timeoutMs,
200+
{ message: "MiniMax OAuth did not settle within the test deadline" },
201+
);
210202
}
211203

212204
function expectAbortOrTimeoutError(error: unknown) {
@@ -305,88 +297,64 @@ describe("loginMiniMaxPortalOAuth", () => {
305297
await expect(loginMiniMax()).rejects.toThrow("invalid token expiry");
306298
});
307299

308-
it("times out authorization code HTTP requests against a hanging loopback server", async () => {
309-
const realFetch = fetch;
310-
const server = await startHangingLoopbackServer();
311-
const oauthTimeout = captureMiniMaxOAuthFetchTimeout();
312-
let loginPromise: Promise<unknown> | undefined;
313-
314-
try {
315-
await expectFetchWithoutDeadlineToStayPending({
316-
url: `${server.origin}/control`,
317-
init: { method: "POST", body: "response_type=code" },
318-
waitForRequest: () => server.waitForRequestCount(1),
319-
});
320-
321-
const fetchMock = vi.fn(
322-
async (_input: RequestInfo | URL, init?: RequestInit) =>
323-
await realFetch(`${server.origin}/device-code`, init),
324-
);
325-
vi.stubGlobal("fetch", fetchMock);
326-
327-
loginPromise = loginMiniMax();
328-
loginPromise.catch(() => undefined);
329-
330-
await server.waitForRequestCount(2);
331-
oauthTimeout.fire();
332-
const result = await loginOutcomeWithin(loginPromise, 2_000);
333-
if (result.status !== "rejected") {
334-
throw new Error(`expected authorization code request to reject, got ${result.status}`);
335-
}
336-
expectAbortOrTimeoutError(result.error);
337-
expect(server.requests).toContain("/device-code");
338-
expect(fetchMock.mock.calls[0]?.[1]?.signal).toBeInstanceOf(AbortSignal);
339-
expect(
340-
oauthTimeout.setTimeoutSpy.mock.calls.some(
341-
([, timeout]) => timeout === MINIMAX_OAUTH_FETCH_TIMEOUT_MS,
342-
),
343-
).toBe(true);
344-
} finally {
345-
await server.close();
346-
await loginPromise?.catch(() => undefined);
347-
}
348-
});
349-
350-
it("times out token polling HTTP requests against a hanging loopback server", async () => {
351-
const realFetch = fetch;
352-
const server = await startHangingLoopbackServer();
353-
const oauthTimeout = captureMiniMaxOAuthFetchTimeout();
354-
let loginPromise: Promise<unknown> | undefined;
355-
356-
try {
357-
await expectFetchWithoutDeadlineToStayPending({
358-
url: `${server.origin}/control`,
359-
init: { method: "POST", body: "grant_type=user_code" },
360-
waitForRequest: () => server.waitForRequestCount(1),
361-
});
362-
363-
const fetchMock = stubOAuthFetch(
364-
(_input, init) => authorizationResponse(init),
365-
async (_input, init) => await realFetch(`${server.origin}/token`, init),
366-
);
367-
368-
loginPromise = loginMiniMax();
369-
loginPromise.catch(() => undefined);
300+
it.each([
301+
{
302+
phase: "authorization code",
303+
path: "/device-code",
304+
controlBody: "response_type=code",
305+
fetchIndex: 0,
306+
},
307+
{
308+
phase: "token polling",
309+
path: "/token",
310+
controlBody: "grant_type=user_code",
311+
fetchIndex: 1,
312+
},
313+
])(
314+
"times out $phase HTTP requests against a hanging loopback server",
315+
async ({ phase, path, controlBody, fetchIndex }) => {
316+
const realFetch = fetch;
317+
const server = await startHangingLoopbackServer();
318+
const oauthTimeout = captureMiniMaxOAuthFetchTimeout();
319+
let loginPromise: Promise<unknown> | undefined;
320+
321+
try {
322+
await expectFetchWithoutDeadlineToStayPending({
323+
url: `${server.origin}/control`,
324+
init: { method: "POST", body: controlBody },
325+
waitForRequest: () => server.waitForRequestCount(1),
326+
});
370327

371-
await server.waitForRequestCount(2);
372-
oauthTimeout.fire();
373-
const result = await loginOutcomeWithin(loginPromise, 2_000);
374-
if (result.status !== "rejected") {
375-
throw new Error(`expected token polling request to reject, got ${result.status}`);
328+
const hangingResponse: FetchResponder = async (_input, init) =>
329+
await realFetch(`${server.origin}${path}`, init);
330+
const fetchMock =
331+
fetchIndex === 0
332+
? stubOAuthFetch(hangingResponse)
333+
: stubOAuthFetch((_input, init) => authorizationResponse(init), hangingResponse);
334+
335+
loginPromise = loginMiniMax();
336+
loginPromise.catch(() => undefined);
337+
338+
await server.waitForRequestCount(2);
339+
oauthTimeout.fire();
340+
const result = await loginOutcomeWithin(loginPromise, 2_000);
341+
if (result.status !== "rejected") {
342+
throw new Error(`expected ${phase} request to reject, got ${result.status}`);
343+
}
344+
expectAbortOrTimeoutError(result.error);
345+
expect(server.requests).toContain(path);
346+
expect(fetchMock.mock.calls[fetchIndex]?.[1]?.signal).toBeInstanceOf(AbortSignal);
347+
expect(
348+
oauthTimeout.setTimeoutSpy.mock.calls.some(
349+
([, timeout]) => timeout === MINIMAX_OAUTH_FETCH_TIMEOUT_MS,
350+
),
351+
).toBe(true);
352+
} finally {
353+
await server.close();
354+
await loginPromise?.catch(() => undefined);
376355
}
377-
expectAbortOrTimeoutError(result.error);
378-
expect(server.requests).toContain("/token");
379-
expect(fetchMock.mock.calls[1]?.[1]?.signal).toBeInstanceOf(AbortSignal);
380-
expect(
381-
oauthTimeout.setTimeoutSpy.mock.calls.some(
382-
([, timeout]) => timeout === MINIMAX_OAUTH_FETCH_TIMEOUT_MS,
383-
),
384-
).toBe(true);
385-
} finally {
386-
await server.close();
387-
await loginPromise?.catch(() => undefined);
388-
}
389-
});
356+
},
357+
);
390358

391359
it("bounds authorization error bodies without using response.text()", async () => {
392360
const tracked = cancelTrackedResponse(

0 commit comments

Comments
 (0)