|
2 | 2 | import type { Model } from "openclaw/plugin-sdk/llm"; |
3 | 3 | import { beforeEach, describe, expect, it, vi, type Mock } from "vitest"; |
4 | 4 | import type { AuthProfileStore } from "../../auth-profiles.js"; |
| 5 | +import { FailoverError } from "../../failover-error.js"; |
5 | 6 | import type { RuntimeAuthState } from "./helpers.js"; |
6 | 7 |
|
7 | 8 | const mocks = vi.hoisted(() => ({ |
@@ -95,21 +96,25 @@ function createMutableEmbeddedRunAuthController(params: { |
95 | 96 | harness: MutableAuthControllerHarness; |
96 | 97 | setRuntimeApiKey: RuntimeApiKeySetter; |
97 | 98 | profileCandidates?: string[]; |
| 99 | + authStore?: AuthProfileStore; |
| 100 | + fallbackConfigured?: boolean; |
98 | 101 | warn?: (message: string) => void; |
99 | 102 | }) { |
100 | 103 | return createEmbeddedRunAuthController({ |
101 | 104 | config: undefined, |
102 | 105 | agentDir: "/tmp/agent", |
103 | 106 | workspaceDir: "/tmp/workspace", |
104 | | - authStore: { |
105 | | - version: 1, |
106 | | - profiles: {}, |
107 | | - } as AuthProfileStore, |
| 107 | + authStore: |
| 108 | + params.authStore ?? |
| 109 | + ({ |
| 110 | + version: 1, |
| 111 | + profiles: {}, |
| 112 | + } as AuthProfileStore), |
108 | 113 | authStorage: { setRuntimeApiKey: params.setRuntimeApiKey }, |
109 | 114 | profileCandidates: params.profileCandidates ?? ["default"], |
110 | 115 | initialThinkLevel: "medium", |
111 | 116 | attemptedThinking: new Set(), |
112 | | - fallbackConfigured: false, |
| 117 | + fallbackConfigured: params.fallbackConfigured ?? false, |
113 | 118 | allowTransientCooldownProbe: false, |
114 | 119 | getProvider: () => "custom-openai", |
115 | 120 | getModelId: () => "test-model", |
@@ -228,6 +233,43 @@ describe("createEmbeddedRunAuthController", () => { |
228 | 233 | }); |
229 | 234 | }); |
230 | 235 |
|
| 236 | + it("preserves OAuth mode when billing-disabled profiles are all unavailable", async () => { |
| 237 | + const harness = createMutableAuthControllerHarness(); |
| 238 | + const profileId = "custom-openai:oauth"; |
| 239 | + const controller = createMutableEmbeddedRunAuthController({ |
| 240 | + harness, |
| 241 | + setRuntimeApiKey: vi.fn(), |
| 242 | + profileCandidates: [profileId], |
| 243 | + fallbackConfigured: true, |
| 244 | + authStore: { |
| 245 | + version: 1, |
| 246 | + profiles: { |
| 247 | + [profileId]: { |
| 248 | + type: "oauth", |
| 249 | + provider: "custom-openai", |
| 250 | + access: "access-token", |
| 251 | + refresh: "refresh-token", |
| 252 | + expires: Date.now() + 60_000, |
| 253 | + }, |
| 254 | + }, |
| 255 | + usageStats: { |
| 256 | + [profileId]: { |
| 257 | + disabledUntil: Date.now() + 60_000, |
| 258 | + disabledReason: "billing", |
| 259 | + }, |
| 260 | + }, |
| 261 | + }, |
| 262 | + }); |
| 263 | + |
| 264 | + const error = await controller.initializeAuthProfile().catch((err: unknown) => err); |
| 265 | + |
| 266 | + expect(error).toBeInstanceOf(FailoverError); |
| 267 | + expect(error).toMatchObject({ |
| 268 | + reason: "billing", |
| 269 | + authMode: "oauth", |
| 270 | + }); |
| 271 | + }); |
| 272 | + |
231 | 273 | it("rejects privileged runtime transport overrides on the first auth exchange", async () => { |
232 | 274 | let runtimeModel = createTestModel(); |
233 | 275 |
|
|
0 commit comments