|
| 1 | +import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"; |
| 2 | +import { refreshMiniMaxPortalOAuth } from "./oauth.js"; |
| 3 | + |
| 4 | +const ORIGINAL_FETCH = globalThis.fetch; |
| 5 | + |
| 6 | +function makeJsonResponse(body: unknown, init: ResponseInit = {}): Response { |
| 7 | + return new Response(JSON.stringify(body), { |
| 8 | + status: 200, |
| 9 | + headers: { "Content-Type": "application/json" }, |
| 10 | + ...init, |
| 11 | + }); |
| 12 | +} |
| 13 | + |
| 14 | +beforeEach(() => { |
| 15 | + vi.useFakeTimers({ shouldAdvanceTime: true }); |
| 16 | +}); |
| 17 | + |
| 18 | +afterEach(() => { |
| 19 | + globalThis.fetch = ORIGINAL_FETCH; |
| 20 | + vi.restoreAllMocks(); |
| 21 | + vi.useRealTimers(); |
| 22 | +}); |
| 23 | + |
| 24 | +describe("refreshMiniMaxPortalOAuth", () => { |
| 25 | + it("posts to account.minimax.io/oauth2/token for global region with refresh_token grant", async () => { |
| 26 | + const fetchMock = vi.fn(async (_input: RequestInfo | URL, init?: RequestInit) => { |
| 27 | + const body = (init?.body as string) ?? ""; |
| 28 | + expect(body).toContain("grant_type=refresh_token"); |
| 29 | + expect(body).toContain("client_id=78257093-7e40-4613-99e0-527b14b39113"); |
| 30 | + expect(body).toContain("refresh_token=stale-rt"); |
| 31 | + return makeJsonResponse({ |
| 32 | + status: "success", |
| 33 | + access_token: "fresh-access", |
| 34 | + refresh_token: "rotated-rt", |
| 35 | + expired_in: 1_700_000_000_000, |
| 36 | + resource_url: "https://api.minimax.io/anthropic", |
| 37 | + }); |
| 38 | + }); |
| 39 | + globalThis.fetch = fetchMock as typeof fetch; |
| 40 | + |
| 41 | + const token = await refreshMiniMaxPortalOAuth({ |
| 42 | + refreshToken: "stale-rt", |
| 43 | + region: "global", |
| 44 | + }); |
| 45 | + |
| 46 | + expect(fetchMock).toHaveBeenCalledTimes(1); |
| 47 | + const [url] = fetchMock.mock.calls[0] as unknown as [string, RequestInit]; |
| 48 | + expect(url).toBe("https://account.minimax.io/oauth2/token"); |
| 49 | + expect(token.access).toBe("fresh-access"); |
| 50 | + expect(token.refresh).toBe("rotated-rt"); |
| 51 | + expect(token.expires).toBe(1_700_000_000_000); |
| 52 | + expect(token.resourceUrl).toBe("https://api.minimax.io/anthropic"); |
| 53 | + }); |
| 54 | + |
| 55 | + it("posts to account.minimaxi.com/oauth2/token for cn region", async () => { |
| 56 | + const fetchMock = vi.fn(async () => |
| 57 | + makeJsonResponse({ |
| 58 | + status: "success", |
| 59 | + access_token: "cn-access", |
| 60 | + refresh_token: "cn-rotated-rt", |
| 61 | + expired_in: 1_700_000_000_000, |
| 62 | + resource_url: "https://api.minimaxi.com/anthropic", |
| 63 | + }), |
| 64 | + ); |
| 65 | + globalThis.fetch = fetchMock as typeof fetch; |
| 66 | + |
| 67 | + const token = await refreshMiniMaxPortalOAuth({ |
| 68 | + refreshToken: "stale-rt", |
| 69 | + region: "cn", |
| 70 | + }); |
| 71 | + |
| 72 | + const [url] = fetchMock.mock.calls[0] as unknown as [string, RequestInit]; |
| 73 | + expect(url).toBe("https://account.minimaxi.com/oauth2/token"); |
| 74 | + expect(token.access).toBe("cn-access"); |
| 75 | + expect(token.resourceUrl).toBe("https://api.minimaxi.com/anthropic"); |
| 76 | + }); |
| 77 | + |
| 78 | + it("falls back to the previous refresh_token if the server omits a new one", async () => { |
| 79 | + globalThis.fetch = (async () => |
| 80 | + makeJsonResponse({ |
| 81 | + status: "success", |
| 82 | + access_token: "fresh-access", |
| 83 | + // refresh_token intentionally missing |
| 84 | + expired_in: 1_700_000_000_000, |
| 85 | + })) as typeof fetch; |
| 86 | + |
| 87 | + const token = await refreshMiniMaxPortalOAuth({ |
| 88 | + refreshToken: "old-rt-stays", |
| 89 | + region: "global", |
| 90 | + }); |
| 91 | + |
| 92 | + expect(token.refresh).toBe("old-rt-stays"); |
| 93 | + }); |
| 94 | + |
| 95 | + it("throws on non-2xx (caller should treat as re-login required)", async () => { |
| 96 | + globalThis.fetch = (async () => |
| 97 | + new Response( |
| 98 | + JSON.stringify({ |
| 99 | + base_resp: { status_code: 1, status_msg: "invalid_grant" }, |
| 100 | + }), |
| 101 | + { status: 400, headers: { "Content-Type": "application/json" } }, |
| 102 | + )) as typeof fetch; |
| 103 | + |
| 104 | + await expect( |
| 105 | + refreshMiniMaxPortalOAuth({ refreshToken: "rotten-rt", region: "global" }), |
| 106 | + ).rejects.toThrow(/invalid_grant/); |
| 107 | + }); |
| 108 | + |
| 109 | + it("throws when status is not success even with HTTP 200", async () => { |
| 110 | + globalThis.fetch = (async () => |
| 111 | + makeJsonResponse({ |
| 112 | + status: "error", |
| 113 | + base_resp: { status_code: 5, status_msg: "refresh_token_reused" }, |
| 114 | + })) as typeof fetch; |
| 115 | + |
| 116 | + await expect( |
| 117 | + refreshMiniMaxPortalOAuth({ refreshToken: "rt", region: "global" }), |
| 118 | + ).rejects.toThrow(/incomplete or unsuccessful/); |
| 119 | + }); |
| 120 | + |
| 121 | + it("throws when payload omits access_token (incomplete response)", async () => { |
| 122 | + globalThis.fetch = (async () => |
| 123 | + makeJsonResponse({ |
| 124 | + status: "success", |
| 125 | + // access_token missing |
| 126 | + refresh_token: "rotated-rt", |
| 127 | + expired_in: 1_700_000_000_000, |
| 128 | + })) as typeof fetch; |
| 129 | + |
| 130 | + await expect( |
| 131 | + refreshMiniMaxPortalOAuth({ refreshToken: "rt", region: "global" }), |
| 132 | + ).rejects.toThrow(/incomplete or unsuccessful/); |
| 133 | + }); |
| 134 | +}); |
0 commit comments