|
2 | 2 | import { createServer } from "node:http"; |
3 | 3 | import type { Socket } from "node:net"; |
4 | 4 | import { MAX_TIMER_TIMEOUT_MS } from "openclaw/plugin-sdk/number-runtime"; |
| 5 | +import { withTimeout } from "openclaw/plugin-sdk/text-utility-runtime"; |
5 | 6 | import { afterEach, describe, expect, it, vi } from "vitest"; |
6 | 7 | import { loginMiniMaxPortalOAuth } from "./oauth.js"; |
7 | 8 |
|
@@ -189,24 +190,15 @@ async function expectFetchWithoutDeadlineToStayPending(params: { |
189 | 190 | expect(result).toBe("pending"); |
190 | 191 | } |
191 | 192 |
|
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( |
204 | 195 | promise.then( |
205 | 196 | () => ({ status: "resolved" as const }), |
206 | 197 | (error: unknown) => ({ status: "rejected" as const, error }), |
207 | 198 | ), |
208 | | - timeoutResult({ status: "pending" as const }, timeoutMs), |
209 | | - ]); |
| 199 | + timeoutMs, |
| 200 | + { message: "MiniMax OAuth did not settle within the test deadline" }, |
| 201 | + ); |
210 | 202 | } |
211 | 203 |
|
212 | 204 | function expectAbortOrTimeoutError(error: unknown) { |
@@ -305,88 +297,64 @@ describe("loginMiniMaxPortalOAuth", () => { |
305 | 297 | await expect(loginMiniMax()).rejects.toThrow("invalid token expiry"); |
306 | 298 | }); |
307 | 299 |
|
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 | + }); |
370 | 327 |
|
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); |
376 | 355 | } |
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 | + ); |
390 | 358 |
|
391 | 359 | it("bounds authorization error bodies without using response.text()", async () => { |
392 | 360 | const tracked = cancelTrackedResponse( |
|
0 commit comments