|
1 | 1 | // Verifies CLI runtime alias resolution and runtime model-ref equivalence. |
2 | 2 | import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
3 | 3 | import type { OpenClawConfig } from "../config/types.openclaw.js"; |
| 4 | +import { |
| 5 | + clearRuntimeAuthProfileStoreSnapshots, |
| 6 | + setRuntimeAuthProfileStoreSnapshot, |
| 7 | +} from "./auth-profiles/runtime-snapshots.js"; |
| 8 | +import type { AuthProfileStore } from "./auth-profiles/types.js"; |
4 | 9 | import { testing as cliBackendsTesting } from "./cli-backends.test-support.js"; |
5 | 10 | import { |
6 | 11 | createModelPickerVisibleProviderPredicate, |
@@ -39,17 +44,21 @@ function resolveCliRuntimeExecutionProvider( |
39 | 44 |
|
40 | 45 | function createAnthropicAuthConfig(params: { |
41 | 46 | order?: string[]; |
| 47 | + orderKey?: string; |
| 48 | + onlyCliProfile?: boolean; |
42 | 49 | models?: NonNullable<NonNullable<OpenClawConfig["agents"]>["defaults"]>["models"]; |
43 | 50 | }): OpenClawConfig { |
44 | 51 | // Auth order controls whether Anthropic execution is direct API or Claude |
45 | 52 | // CLI-backed when no explicit runtime policy overrides it. |
46 | 53 | return { |
47 | 54 | auth: { |
48 | | - order: params.order ? { anthropic: params.order } : undefined, |
49 | | - profiles: { |
50 | | - "anthropic:api": { provider: "anthropic", mode: "api_key" }, |
51 | | - "anthropic:claude-cli": { provider: "claude-cli", mode: "oauth" }, |
52 | | - }, |
| 55 | + order: params.order ? { [params.orderKey ?? "anthropic"]: params.order } : undefined, |
| 56 | + profiles: params.onlyCliProfile |
| 57 | + ? { "anthropic:claude-cli": { provider: "claude-cli", mode: "oauth" } } |
| 58 | + : { |
| 59 | + "anthropic:api": { provider: "anthropic", mode: "api_key" }, |
| 60 | + "anthropic:claude-cli": { provider: "claude-cli", mode: "oauth" }, |
| 61 | + }, |
53 | 62 | }, |
54 | 63 | agents: { |
55 | 64 | defaults: { |
@@ -83,6 +92,168 @@ describe("resolveCliRuntimeExecutionProvider", () => { |
83 | 92 |
|
84 | 93 | afterEach(() => { |
85 | 94 | cliBackendsTesting.resetDepsForTest(); |
| 95 | + clearRuntimeAuthProfileStoreSnapshots(); |
| 96 | + }); |
| 97 | + |
| 98 | + function seedStoredAuthOrder( |
| 99 | + order: string[], |
| 100 | + providerKey = "anthropic", |
| 101 | + profiles: AuthProfileStore["profiles"] = {}, |
| 102 | + ): void { |
| 103 | + setRuntimeAuthProfileStoreSnapshot({ |
| 104 | + version: 1, |
| 105 | + profiles, |
| 106 | + order: { [providerKey]: order }, |
| 107 | + }); |
| 108 | + } |
| 109 | + |
| 110 | + it("honors a stored auth order when config declares none", () => { |
| 111 | + // `models auth order set` writes the persisted store, not the config file. |
| 112 | + // With no config order the resolver used to build an empty ordered list and |
| 113 | + // fall through to the "exactly one compatible profile" branch, which returns |
| 114 | + // undefined whenever two profiles share the provider auth key. |
| 115 | + seedStoredAuthOrder(["anthropic:claude-cli"]); |
| 116 | + expect( |
| 117 | + resolveCliRuntimeExecutionProvider({ |
| 118 | + cfg: createAnthropicAuthConfig({}), |
| 119 | + provider: "anthropic", |
| 120 | + modelId: "opus-4.7", |
| 121 | + }), |
| 122 | + ).toBe("claude-cli"); |
| 123 | + }); |
| 124 | + |
| 125 | + it.each(["order", "pin"])( |
| 126 | + "routes a stored CLI profile selected by %s without config metadata", |
| 127 | + (selection) => { |
| 128 | + seedStoredAuthOrder(selection === "order" ? ["anthropic:claude-cli"] : [], "anthropic", { |
| 129 | + "anthropic:claude-cli": { |
| 130 | + type: "oauth", |
| 131 | + provider: "claude-cli", |
| 132 | + access: "test-access", |
| 133 | + refresh: "test-refresh", |
| 134 | + expires: Date.now() + 60_000, |
| 135 | + }, |
| 136 | + }); |
| 137 | + expect( |
| 138 | + resolveCliRuntimeExecutionProvider({ |
| 139 | + provider: "anthropic", |
| 140 | + modelId: "opus-4.7", |
| 141 | + authProfileId: selection === "pin" ? "anthropic:claude-cli" : undefined, |
| 142 | + }), |
| 143 | + ).toBe("claude-cli"); |
| 144 | + }, |
| 145 | + ); |
| 146 | + |
| 147 | + it("matches a stored order key that is not already canonically normalized", () => { |
| 148 | + // Persisted state only lowercases provider keys, and the canonical resolver |
| 149 | + // looks the order up through normalized provider matching rather than an exact |
| 150 | + // key hit, so an un-normalized stored key must still beat the config order. |
| 151 | + seedStoredAuthOrder(["anthropic:claude-cli"], "Anthropic"); |
| 152 | + expect( |
| 153 | + resolveCliRuntimeExecutionProvider({ |
| 154 | + cfg: createAnthropicAuthConfig({ order: ["anthropic:api"] }), |
| 155 | + provider: "anthropic", |
| 156 | + modelId: "opus-4.7", |
| 157 | + }), |
| 158 | + ).toBe("claude-cli"); |
| 159 | + }); |
| 160 | + |
| 161 | + it("does not route a provider disabled by an explicitly empty configured order", () => { |
| 162 | + expect( |
| 163 | + resolveCliRuntimeExecutionProvider({ |
| 164 | + cfg: createAnthropicAuthConfig({ order: [], onlyCliProfile: true }), |
| 165 | + provider: "anthropic", |
| 166 | + modelId: "opus-4.7", |
| 167 | + }), |
| 168 | + ).toBeUndefined(); |
| 169 | + }); |
| 170 | + |
| 171 | + it.each(["configured", "stored"])( |
| 172 | + "repairs a %s order containing only deleted profiles", |
| 173 | + (source) => { |
| 174 | + if (source === "stored") { |
| 175 | + seedStoredAuthOrder(["anthropic:deleted"]); |
| 176 | + } |
| 177 | + expect( |
| 178 | + resolveCliRuntimeExecutionProvider({ |
| 179 | + cfg: createAnthropicAuthConfig({ |
| 180 | + order: source === "configured" ? ["anthropic:deleted"] : undefined, |
| 181 | + onlyCliProfile: true, |
| 182 | + }), |
| 183 | + provider: "anthropic", |
| 184 | + modelId: "opus-4.7", |
| 185 | + }), |
| 186 | + ).toBe("claude-cli"); |
| 187 | + }, |
| 188 | + ); |
| 189 | + |
| 190 | + it.each([ |
| 191 | + { name: "alone", order: ["anthropic:stored-api"] }, |
| 192 | + { |
| 193 | + name: "before a configured CLI profile", |
| 194 | + order: ["anthropic:stored-api", "anthropic:claude-cli"], |
| 195 | + }, |
| 196 | + ])("keeps a stored profile missing from config authoritative $name", ({ order }) => { |
| 197 | + seedStoredAuthOrder(order, "anthropic", { |
| 198 | + "anthropic:stored-api": { type: "api_key", provider: "anthropic", key: "test-key" }, |
| 199 | + }); |
| 200 | + expect( |
| 201 | + resolveCliRuntimeExecutionProvider({ |
| 202 | + cfg: createAnthropicAuthConfig({ onlyCliProfile: true }), |
| 203 | + provider: "anthropic", |
| 204 | + modelId: "opus-4.7", |
| 205 | + }), |
| 206 | + ).toBeUndefined(); |
| 207 | + }); |
| 208 | + |
| 209 | + it("still picks the sole compatible profile when no stored order was authored", () => { |
| 210 | + // Control for the case above: without a stored order the fallback must survive. |
| 211 | + expect( |
| 212 | + resolveCliRuntimeExecutionProvider({ |
| 213 | + cfg: createAnthropicAuthConfig({ onlyCliProfile: true }), |
| 214 | + provider: "anthropic", |
| 215 | + modelId: "opus-4.7", |
| 216 | + }), |
| 217 | + ).toBe("claude-cli"); |
| 218 | + }); |
| 219 | + |
| 220 | + it("matches a config order key through the same normalized lookup as profile selection", () => { |
| 221 | + expect( |
| 222 | + resolveCliRuntimeExecutionProvider({ |
| 223 | + cfg: createAnthropicAuthConfig({ order: ["anthropic:claude-cli"], orderKey: "Anthropic" }), |
| 224 | + provider: "anthropic", |
| 225 | + modelId: "opus-4.7", |
| 226 | + }), |
| 227 | + ).toBe("claude-cli"); |
| 228 | + }); |
| 229 | + |
| 230 | + it("inherits the main-agent stored order for an agent with no snapshot of its own", () => { |
| 231 | + // Named agents inherit auth state from the main agent, and only the main |
| 232 | + // snapshot may be published. An exact-agent lookup would miss it and fall |
| 233 | + // back to config, so the order read resolves inheritance the way |
| 234 | + // getPreparedRuntimeAuthProfileStoreSnapshotCore does. |
| 235 | + seedStoredAuthOrder(["anthropic:claude-cli"]); |
| 236 | + expect( |
| 237 | + resolveCliRuntimeExecutionProvider({ |
| 238 | + agentId: "secondary", |
| 239 | + cfg: createAnthropicAuthConfig({}), |
| 240 | + provider: "anthropic", |
| 241 | + modelId: "opus-4.7", |
| 242 | + }), |
| 243 | + ).toBe("claude-cli"); |
| 244 | + }); |
| 245 | + |
| 246 | + it("prefers the stored auth order over a conflicting config order", () => { |
| 247 | + // Same precedence as resolveAuthProfileOrderWithMetadata: stored order wins, |
| 248 | + // config order is only the fallback. |
| 249 | + seedStoredAuthOrder(["anthropic:claude-cli"]); |
| 250 | + expect( |
| 251 | + resolveCliRuntimeExecutionProvider({ |
| 252 | + cfg: createAnthropicAuthConfig({ order: ["anthropic:api"] }), |
| 253 | + provider: "anthropic", |
| 254 | + modelId: "opus-4.7", |
| 255 | + }), |
| 256 | + ).toBe("claude-cli"); |
86 | 257 | }); |
87 | 258 |
|
88 | 259 | it("routes Anthropic execution to Claude CLI when the selected auth profile is Claude CLI", () => { |
|
0 commit comments