|
| 1 | +// DeepInfra proxy tests cover the live model discovery transport policy. |
| 2 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +const fetchWithSsrFGuardMock = vi.hoisted(() => vi.fn()); |
| 5 | + |
| 6 | +vi.mock("openclaw/plugin-sdk/ssrf-runtime", async (importOriginal) => ({ |
| 7 | + ...(await importOriginal<typeof import("openclaw/plugin-sdk/ssrf-runtime")>()), |
| 8 | + fetchWithSsrFGuard: fetchWithSsrFGuardMock, |
| 9 | +})); |
| 10 | + |
| 11 | +import { discoverDeepInfraModels } from "./provider-models.js"; |
| 12 | + |
| 13 | +const ORIGINAL_VITEST = process.env.VITEST; |
| 14 | +const ORIGINAL_NODE_ENV = process.env.NODE_ENV; |
| 15 | + |
| 16 | +function restoreEnv(key: "VITEST" | "NODE_ENV", value: string | undefined) { |
| 17 | + if (value === undefined) { |
| 18 | + delete process.env[key]; |
| 19 | + } else { |
| 20 | + process.env[key] = value; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +afterEach(() => { |
| 25 | + restoreEnv("VITEST", ORIGINAL_VITEST); |
| 26 | + restoreEnv("NODE_ENV", ORIGINAL_NODE_ENV); |
| 27 | + fetchWithSsrFGuardMock.mockReset(); |
| 28 | + vi.restoreAllMocks(); |
| 29 | +}); |
| 30 | + |
| 31 | +describe("DeepInfra model discovery proxy policy", () => { |
| 32 | + it("allows the guarded official catalog request to use an eligible HTTP proxy", async () => { |
| 33 | + delete process.env.VITEST; |
| 34 | + process.env.NODE_ENV = "development"; |
| 35 | + const release = vi.fn(async () => undefined); |
| 36 | + fetchWithSsrFGuardMock.mockResolvedValue({ |
| 37 | + response: new Response("unavailable", { status: 503 }), |
| 38 | + release, |
| 39 | + }); |
| 40 | + |
| 41 | + await discoverDeepInfraModels({ hasApiKey: true }); |
| 42 | + |
| 43 | + expect(fetchWithSsrFGuardMock).toHaveBeenCalledWith( |
| 44 | + expect.objectContaining({ |
| 45 | + mode: "trusted_env_proxy", |
| 46 | + url: "https://api.deepinfra.com/v1/openai/models?sort_by=openclaw&filter=with_meta", |
| 47 | + }), |
| 48 | + ); |
| 49 | + expect(release).toHaveBeenCalledOnce(); |
| 50 | + }); |
| 51 | +}); |
0 commit comments