|
1 | | -import fs from "node:fs/promises"; |
2 | | -import path from "node:path"; |
3 | | -import { describe, expect, it } from "vitest"; |
4 | | -import { resolveOpenClawAgentDir } from "./agent-paths.js"; |
5 | | -import { |
6 | | - installModelsConfigTestHooks, |
7 | | - mockCopilotTokenExchangeSuccess, |
8 | | - withCopilotGithubToken, |
9 | | - withUnsetCopilotTokenEnv, |
10 | | - withModelsTempHome as withTempHome, |
11 | | -} from "./models-config.e2e-harness.js"; |
12 | | -import { ensureOpenClawModelsJson } from "./models-config.js"; |
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { planOpenClawModelsJson } from "./models-config.plan.js"; |
| 3 | +import { createProviderAuthResolver } from "./models-config.providers.secrets.js"; |
13 | 4 |
|
14 | | -installModelsConfigTestHooks({ restoreFetch: true }); |
15 | | - |
16 | | -async function writeAuthProfiles(agentDir: string, profiles: Record<string, unknown>) { |
17 | | - await fs.mkdir(agentDir, { recursive: true }); |
18 | | - await fs.writeFile( |
19 | | - path.join(agentDir, "auth-profiles.json"), |
20 | | - JSON.stringify({ version: 1, profiles }, null, 2), |
21 | | - ); |
22 | | -} |
23 | | - |
24 | | -function expectBearerAuthHeader(fetchMock: { mock: { calls: unknown[][] } }, token: string) { |
25 | | - const [, opts] = fetchMock.mock.calls[0] as [string, { headers?: Record<string, string> }]; |
26 | | - expect(opts?.headers?.Authorization).toBe(`Bearer ${token}`); |
27 | | -} |
| 5 | +vi.mock("./models-config.providers.js", () => ({ |
| 6 | + applyNativeStreamingUsageCompat: (providers: unknown) => providers, |
| 7 | + enforceSourceManagedProviderSecrets: ({ providers }: { providers: unknown }) => providers, |
| 8 | + normalizeProviders: ({ providers }: { providers: unknown }) => providers, |
| 9 | + resolveImplicitProviders: async ({ |
| 10 | + explicitProviders, |
| 11 | + }: { |
| 12 | + explicitProviders?: Record<string, unknown>; |
| 13 | + }) => explicitProviders ?? {}, |
| 14 | +})); |
28 | 15 |
|
29 | 16 | describe("models-config", () => { |
30 | | - it("uses the first github-copilot profile when env tokens are missing", async () => { |
31 | | - await withTempHome(async (home) => { |
32 | | - await withUnsetCopilotTokenEnv(async () => { |
33 | | - const fetchMock = mockCopilotTokenExchangeSuccess(); |
34 | | - const agentDir = path.join(home, "agent-profiles"); |
35 | | - await writeAuthProfiles(agentDir, { |
36 | | - "github-copilot:alpha": { |
37 | | - type: "token", |
38 | | - provider: "github-copilot", |
39 | | - token: "alpha-token", |
40 | | - }, |
41 | | - "github-copilot:beta": { |
42 | | - type: "token", |
43 | | - provider: "github-copilot", |
44 | | - token: "beta-token", |
45 | | - }, |
46 | | - }); |
| 17 | + it("uses the first github-copilot profile when env tokens are missing", () => { |
| 18 | + const auth = createProviderAuthResolver({} as NodeJS.ProcessEnv, { |
| 19 | + version: 1, |
| 20 | + profiles: { |
| 21 | + "github-copilot:alpha": { |
| 22 | + type: "token", |
| 23 | + provider: "github-copilot", |
| 24 | + token: "alpha-token", |
| 25 | + }, |
| 26 | + "github-copilot:beta": { |
| 27 | + type: "token", |
| 28 | + provider: "github-copilot", |
| 29 | + token: "beta-token", |
| 30 | + }, |
| 31 | + }, |
| 32 | + }); |
47 | 33 |
|
48 | | - await ensureOpenClawModelsJson({ models: { providers: {} } }, agentDir); |
49 | | - expectBearerAuthHeader(fetchMock, "alpha-token"); |
50 | | - }); |
| 34 | + expect(auth("github-copilot")).toEqual({ |
| 35 | + apiKey: "alpha-token", |
| 36 | + discoveryApiKey: "alpha-token", |
| 37 | + mode: "token", |
| 38 | + source: "profile", |
| 39 | + profileId: "github-copilot:alpha", |
51 | 40 | }); |
52 | 41 | }); |
53 | 42 |
|
54 | 43 | it("does not override explicit github-copilot provider config", async () => { |
55 | | - await withTempHome(async () => { |
56 | | - await withCopilotGithubToken("gh-token", async () => { |
57 | | - await ensureOpenClawModelsJson({ |
58 | | - models: { |
59 | | - providers: { |
60 | | - "github-copilot": { |
61 | | - baseUrl: "https://copilot.local", |
62 | | - api: "openai-responses", |
63 | | - models: [], |
64 | | - }, |
| 44 | + const plan = await planOpenClawModelsJson({ |
| 45 | + cfg: { |
| 46 | + models: { |
| 47 | + providers: { |
| 48 | + "github-copilot": { |
| 49 | + baseUrl: "https://copilot.local", |
| 50 | + api: "openai-responses", |
| 51 | + models: [], |
65 | 52 | }, |
66 | 53 | }, |
67 | | - }); |
68 | | - |
69 | | - const agentDir = resolveOpenClawAgentDir(); |
70 | | - const raw = await fs.readFile(path.join(agentDir, "models.json"), "utf8"); |
71 | | - const parsed = JSON.parse(raw) as { |
72 | | - providers: Record<string, { baseUrl?: string }>; |
73 | | - }; |
74 | | - |
75 | | - expect(parsed.providers["github-copilot"]?.baseUrl).toBe("https://copilot.local"); |
76 | | - }); |
| 54 | + }, |
| 55 | + }, |
| 56 | + agentDir: "/tmp/openclaw-agent", |
| 57 | + env: {} as NodeJS.ProcessEnv, |
| 58 | + existingRaw: "", |
| 59 | + existingParsed: null, |
77 | 60 | }); |
| 61 | + |
| 62 | + expect(plan.action).toBe("write"); |
| 63 | + expect( |
| 64 | + plan.action === "write" |
| 65 | + ? ( |
| 66 | + JSON.parse(plan.contents) as { |
| 67 | + providers?: Record<string, { baseUrl?: string }>; |
| 68 | + } |
| 69 | + ).providers?.["github-copilot"]?.baseUrl |
| 70 | + : undefined, |
| 71 | + ).toBe("https://copilot.local"); |
78 | 72 | }); |
79 | 73 |
|
80 | | - it("uses tokenRef env var when github-copilot profile omits plaintext token", async () => { |
81 | | - await withTempHome(async (home) => { |
82 | | - await withUnsetCopilotTokenEnv(async () => { |
83 | | - const fetchMock = mockCopilotTokenExchangeSuccess(); |
84 | | - const agentDir = path.join(home, "agent-profiles"); |
85 | | - process.env.COPILOT_REF_TOKEN = "token-from-ref-env"; |
86 | | - try { |
87 | | - await writeAuthProfiles(agentDir, { |
88 | | - "github-copilot:default": { |
89 | | - type: "token", |
90 | | - provider: "github-copilot", |
91 | | - tokenRef: { source: "env", provider: "default", id: "COPILOT_REF_TOKEN" }, |
92 | | - }, |
93 | | - }); |
| 74 | + it("uses tokenRef env var when github-copilot profile omits plaintext token", () => { |
| 75 | + const auth = createProviderAuthResolver( |
| 76 | + { |
| 77 | + COPILOT_REF_TOKEN: "token-from-ref-env", |
| 78 | + } as NodeJS.ProcessEnv, |
| 79 | + { |
| 80 | + version: 1, |
| 81 | + profiles: { |
| 82 | + "github-copilot:default": { |
| 83 | + type: "token", |
| 84 | + provider: "github-copilot", |
| 85 | + tokenRef: { source: "env", provider: "default", id: "COPILOT_REF_TOKEN" }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + ); |
94 | 90 |
|
95 | | - await ensureOpenClawModelsJson({ models: { providers: {} } }, agentDir); |
96 | | - expectBearerAuthHeader(fetchMock, "token-from-ref-env"); |
97 | | - } finally { |
98 | | - delete process.env.COPILOT_REF_TOKEN; |
99 | | - } |
100 | | - }); |
| 91 | + expect(auth("github-copilot")).toEqual({ |
| 92 | + apiKey: "COPILOT_REF_TOKEN", |
| 93 | + discoveryApiKey: "token-from-ref-env", |
| 94 | + mode: "token", |
| 95 | + source: "profile", |
| 96 | + profileId: "github-copilot:default", |
101 | 97 | }); |
102 | 98 | }); |
103 | 99 | }); |
0 commit comments