|
6 | 6 | } from "./provider-model-auth-source-plan.js"; |
7 | 7 | import { |
8 | 8 | resolveProviderModelRouteMaterializationAuthMode, |
| 9 | + selectProviderModelAuthSources, |
9 | 10 | selectProviderModelRouteAuth, |
10 | 11 | } from "./provider-model-route-auth.js"; |
11 | 12 |
|
@@ -38,12 +39,16 @@ function profile( |
38 | 39 | return { kind: "profile", profileId, mode, readiness, cooldown }; |
39 | 40 | } |
40 | 41 |
|
41 | | -function direct(mode: string): ProviderModelAuthDirectSource { |
| 42 | +function direct( |
| 43 | + mode: string, |
| 44 | + authorization: ProviderModelAuthDirectSource["authorization"] = "declared", |
| 45 | +): ProviderModelAuthDirectSource { |
42 | 46 | return { |
43 | 47 | kind: "direct", |
44 | 48 | mode, |
45 | 49 | readiness: "ready", |
46 | 50 | evidence: "provider-config", |
| 51 | + authorization, |
47 | 52 | }; |
48 | 53 | } |
49 | 54 |
|
@@ -406,3 +411,69 @@ describe("provider model route auth", () => { |
406 | 411 | }); |
407 | 412 | }); |
408 | 413 | }); |
| 414 | + |
| 415 | +describe("ambient credential admission", () => { |
| 416 | + const ambient = (mode: string): ProviderModelAuthDirectSource => direct(mode, "ambient"); |
| 417 | + const ready = (profileId: string, mode: string) => profile(profileId, mode, "ready"); |
| 418 | + const select = (plan: Parameters<typeof selectProviderModelAuthSources>[0]["plan"]) => |
| 419 | + selectProviderModelAuthSources({ provider: "openai", plan }); |
| 420 | + |
| 421 | + it("drops an ambient fallback queued behind usable profiles", () => { |
| 422 | + const decision = select( |
| 423 | + buildProviderModelAuthSourcePlan({ |
| 424 | + profiles: [ready("openai:chatgpt", "oauth")], |
| 425 | + fallback: ambient("api-key"), |
| 426 | + }), |
| 427 | + ); |
| 428 | + |
| 429 | + expect(decision).toMatchObject({ kind: "selected" }); |
| 430 | + expect(decision.kind === "selected" && decision.attempts).toMatchObject([{ kind: "profile" }]); |
| 431 | + }); |
| 432 | + |
| 433 | + it("keeps a declared fallback queued behind usable profiles", () => { |
| 434 | + const decision = select( |
| 435 | + buildProviderModelAuthSourcePlan({ |
| 436 | + profiles: [ready("openai:chatgpt", "oauth")], |
| 437 | + fallback: direct("api-key"), |
| 438 | + }), |
| 439 | + ); |
| 440 | + |
| 441 | + expect(decision.kind === "selected" && decision.attempts).toMatchObject([ |
| 442 | + { kind: "profile" }, |
| 443 | + { kind: "direct" }, |
| 444 | + ]); |
| 445 | + }); |
| 446 | + |
| 447 | + it("keeps an ambient fallback when the provider declares no profiles", () => { |
| 448 | + const decision = select( |
| 449 | + buildProviderModelAuthSourcePlan({ profiles: [], fallback: ambient("api-key") }), |
| 450 | + ); |
| 451 | + |
| 452 | + expect(decision.kind === "selected" && decision.attempts).toMatchObject([{ kind: "direct" }]); |
| 453 | + }); |
| 454 | + |
| 455 | + it("does not substitute an ambient fallback for all-unavailable profiles", () => { |
| 456 | + const decision = select( |
| 457 | + buildProviderModelAuthSourcePlan({ |
| 458 | + profiles: [profile("openai:chatgpt", "oauth", "unavailable")], |
| 459 | + fallback: ambient("api-key"), |
| 460 | + }), |
| 461 | + ); |
| 462 | + |
| 463 | + expect(decision.kind === "selected" && decision.attempts).toMatchObject([]); |
| 464 | + }); |
| 465 | + |
| 466 | + it("does not re-admit an ambient fallback when route filtering empties the profile list", () => { |
| 467 | + // Rebuild shape used by selectProviderModelRouteAuth when narrowing to a |
| 468 | + // route-compatible subset: no profiles survive, but the operator declared one. |
| 469 | + const decision = select( |
| 470 | + buildProviderModelAuthSourcePlan({ |
| 471 | + profiles: [], |
| 472 | + declaredProfileCount: 1, |
| 473 | + fallback: ambient("api-key"), |
| 474 | + }), |
| 475 | + ); |
| 476 | + |
| 477 | + expect(decision.kind === "selected" && decision.attempts).toMatchObject([]); |
| 478 | + }); |
| 479 | +}); |
0 commit comments