|
| 1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | + |
| 3 | +const mocks = vi.hoisted(() => ({ |
| 4 | + ensureGlobalUndiciEnvProxyDispatcher: vi.fn(), |
| 5 | + getOAuthApiKey: vi.fn(), |
| 6 | +})); |
| 7 | + |
| 8 | +vi.mock("openclaw/plugin-sdk/infra-runtime", () => ({ |
| 9 | + ensureGlobalUndiciEnvProxyDispatcher: mocks.ensureGlobalUndiciEnvProxyDispatcher, |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock("@mariozechner/pi-ai/oauth", () => ({ |
| 13 | + getOAuthApiKey: mocks.getOAuthApiKey, |
| 14 | +})); |
| 15 | + |
| 16 | +import { getOAuthApiKey } from "./openai-codex-provider.runtime.js"; |
| 17 | + |
| 18 | +describe("openai-codex-provider.runtime", () => { |
| 19 | + beforeEach(() => { |
| 20 | + vi.clearAllMocks(); |
| 21 | + }); |
| 22 | + |
| 23 | + it("bootstraps the env proxy dispatcher before refreshing oauth credentials", async () => { |
| 24 | + const refreshed = { |
| 25 | + newCredentials: { |
| 26 | + access: "next-access", |
| 27 | + refresh: "next-refresh", |
| 28 | + expires: Date.now() + 60_000, |
| 29 | + }, |
| 30 | + }; |
| 31 | + mocks.getOAuthApiKey.mockResolvedValue(refreshed); |
| 32 | + |
| 33 | + await expect( |
| 34 | + getOAuthApiKey("openai-codex", { |
| 35 | + "openai-codex": { |
| 36 | + provider: "openai-codex", |
| 37 | + type: "oauth", |
| 38 | + access: "access-token", |
| 39 | + refresh: "refresh-token", |
| 40 | + expires: Date.now(), |
| 41 | + }, |
| 42 | + }), |
| 43 | + ).resolves.toBe(refreshed); |
| 44 | + |
| 45 | + expect(mocks.ensureGlobalUndiciEnvProxyDispatcher).toHaveBeenCalledOnce(); |
| 46 | + expect(mocks.getOAuthApiKey).toHaveBeenCalledOnce(); |
| 47 | + expect(mocks.ensureGlobalUndiciEnvProxyDispatcher.mock.invocationCallOrder[0]).toBeLessThan( |
| 48 | + mocks.getOAuthApiKey.mock.invocationCallOrder[0], |
| 49 | + ); |
| 50 | + }); |
| 51 | +}); |
0 commit comments