|
1 | 1 | import { registerSingleProviderPlugin } from "openclaw/plugin-sdk/plugin-test-runtime"; |
2 | 2 | import { resolveAgentModelPrimaryValue } from "openclaw/plugin-sdk/provider-onboard"; |
3 | | -import { describe, expect, it, vi } from "vitest"; |
| 3 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
4 | 4 | import plugin from "./index.js"; |
5 | 5 | import { applyCerebrasConfig, CEREBRAS_DEFAULT_MODEL_REF } from "./onboard.js"; |
6 | 6 | import manifest from "./openclaw.plugin.json" with { type: "json" }; |
7 | 7 |
|
| 8 | +const ssrfRuntimeMocks = vi.hoisted(() => ({ |
| 9 | + fetchWithSsrFGuard: vi.fn(), |
| 10 | + ssrfPolicyFromHttpBaseUrlAllowedHostname: vi.fn((baseUrl: string) => ({ |
| 11 | + allowedHostnames: [new URL(baseUrl).hostname], |
| 12 | + })), |
| 13 | +})); |
| 14 | + |
| 15 | +vi.mock("openclaw/plugin-sdk/ssrf-runtime", () => ssrfRuntimeMocks); |
| 16 | + |
| 17 | +afterEach(() => { |
| 18 | + ssrfRuntimeMocks.fetchWithSsrFGuard.mockReset(); |
| 19 | + ssrfRuntimeMocks.ssrfPolicyFromHttpBaseUrlAllowedHostname.mockClear(); |
| 20 | +}); |
| 21 | + |
8 | 22 | describe("Cerebras onboarding", () => { |
9 | 23 | it("applies the manifest catalog, default, and alias", () => { |
10 | 24 | const config = applyCerebrasConfig({}); |
@@ -51,4 +65,55 @@ describe("Cerebras onboarding", () => { |
51 | 65 | [CEREBRAS_DEFAULT_MODEL_REF]: { alias: "Cerebras Gemma 4 31B" }, |
52 | 66 | }); |
53 | 67 | }); |
| 68 | + |
| 69 | + it("keeps the authenticated runtime catalog static without querying the model endpoint", async () => { |
| 70 | + ssrfRuntimeMocks.fetchWithSsrFGuard.mockResolvedValueOnce({ |
| 71 | + response: Response.json({ data: [{ id: "gpt-oss-120b", object: "model" }] }), |
| 72 | + finalUrl: "https://api.cerebras.ai/v1/models", |
| 73 | + release: vi.fn(), |
| 74 | + }); |
| 75 | + const provider = await registerSingleProviderPlugin(plugin); |
| 76 | + const result = await provider.catalog?.run({ |
| 77 | + config: {}, |
| 78 | + env: {}, |
| 79 | + resolveProviderApiKey: () => ({ apiKey: "fixture-cerebras-key" }), |
| 80 | + } as never); |
| 81 | + |
| 82 | + expect(ssrfRuntimeMocks.fetchWithSsrFGuard).not.toHaveBeenCalled(); |
| 83 | + if (!result || !("provider" in result)) { |
| 84 | + throw new Error("expected authenticated Cerebras provider catalog"); |
| 85 | + } |
| 86 | + expect(result.provider.apiKey).toBe("fixture-cerebras-key"); |
| 87 | + expect(result.provider.models.map((model) => model.id)).toEqual( |
| 88 | + manifest.modelCatalog.providers.cerebras.models.map((model) => model.id), |
| 89 | + ); |
| 90 | + expect(result.provider.models.find((model) => model.id === "gemma-4-31b")?.input).toEqual([ |
| 91 | + "text", |
| 92 | + "image", |
| 93 | + ]); |
| 94 | + const staticResult = await provider.staticCatalog?.run({ config: {}, env: {} } as never); |
| 95 | + if (!staticResult || !("provider" in staticResult)) { |
| 96 | + throw new Error("expected static Cerebras provider catalog"); |
| 97 | + } |
| 98 | + expect(staticResult.provider.models.map((model) => model.id)).toEqual( |
| 99 | + manifest.modelCatalog.providers.cerebras.models.map((model) => model.id), |
| 100 | + ); |
| 101 | + }); |
| 102 | + |
| 103 | + it("declares the bundled provider catalog as static", () => { |
| 104 | + expect(manifest.modelCatalog.discovery.cerebras).toBe("static"); |
| 105 | + }); |
| 106 | + |
| 107 | + it("keeps the runtime catalog inactive without a Cerebras credential", async () => { |
| 108 | + const provider = await registerSingleProviderPlugin(plugin); |
| 109 | + |
| 110 | + await expect( |
| 111 | + provider.catalog?.run({ |
| 112 | + config: {}, |
| 113 | + env: {}, |
| 114 | + resolveProviderApiKey: () => ({}), |
| 115 | + } as never), |
| 116 | + ).resolves.toBeNull(); |
| 117 | + expect(ssrfRuntimeMocks.fetchWithSsrFGuard).not.toHaveBeenCalled(); |
| 118 | + }); |
54 | 119 | }); |
0 commit comments