|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { createModelAuthAvailabilityResolver } from "./model-auth-availability.js"; |
| 3 | +import { |
| 4 | + authStore, |
| 5 | + dualRoutes, |
| 6 | + evaluate, |
| 7 | + routeResolverFactory, |
| 8 | +} from "./model-auth-availability.test-support.js"; |
| 9 | + |
| 10 | +describe("model auth unavailability reasons", () => { |
| 11 | + it.each(["openai", "anthropic"])( |
| 12 | + "distinguishes missing, failed, unknown, and ready credentials for %s", |
| 13 | + (provider) => { |
| 14 | + const cases = [ |
| 15 | + { label: "no evidence", profiles: {}, reason: "missing-auth" }, |
| 16 | + { |
| 17 | + label: "expired without refresh", |
| 18 | + profiles: { test: { type: "token", provider, token: "expired-token", expires: 1 } }, |
| 19 | + reason: "auth-failed", |
| 20 | + }, |
| 21 | + { |
| 22 | + label: "invalid reference", |
| 23 | + profiles: { |
| 24 | + test: { |
| 25 | + type: "api_key", |
| 26 | + provider, |
| 27 | + keyRef: { source: "file", provider: "absent", id: "key" }, |
| 28 | + }, |
| 29 | + }, |
| 30 | + reason: "auth-failed", |
| 31 | + }, |
| 32 | + { |
| 33 | + label: "unread reference", |
| 34 | + profiles: { |
| 35 | + test: { |
| 36 | + type: "api_key", |
| 37 | + provider, |
| 38 | + keyRef: { source: "env", provider: "default", id: "UNREAD_KEY" }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + reason: undefined, |
| 42 | + }, |
| 43 | + { |
| 44 | + label: "ready", |
| 45 | + profiles: { test: { type: "api_key", provider, key: "test-key" } }, |
| 46 | + reason: undefined, |
| 47 | + }, |
| 48 | + ]; |
| 49 | + for (const { label, profiles, reason } of cases) { |
| 50 | + const result = createModelAuthAvailabilityResolver({ |
| 51 | + cfg: {}, |
| 52 | + authStore: authStore(profiles), |
| 53 | + env: {}, |
| 54 | + routeResolverFactory: routeResolverFactory(dualRoutes), |
| 55 | + }).evaluateModelAuth(provider); |
| 56 | + expect.soft(result.unavailableReason, label).toBe(reason); |
| 57 | + expect.soft(result.unavailableUntil, label).toBeUndefined(); |
| 58 | + } |
| 59 | + }, |
| 60 | + ); |
| 61 | + |
| 62 | + it("reports the earliest retry among viable cooling profiles, excluding invalid or permanently rejected profiles", () => { |
| 63 | + const until = Date.now() + 60_000; |
| 64 | + const store = authStore({ |
| 65 | + first: { type: "api_key", provider: "openai", key: "first-key" }, |
| 66 | + second: { type: "api_key", provider: "openai", key: "second-key" }, |
| 67 | + invalid: { type: "token", provider: "openai", token: "expired-token", expires: 1 }, |
| 68 | + rejected: { type: "api_key", provider: "openai", key: "rejected-key" }, |
| 69 | + }); |
| 70 | + store.usageStats = { |
| 71 | + first: { cooldownUntil: until + 60_000 }, |
| 72 | + second: { cooldownUntil: until }, |
| 73 | + invalid: { cooldownUntil: until - 30_000 }, |
| 74 | + rejected: { disabledUntil: until - 45_000, disabledReason: "auth_permanent" }, |
| 75 | + }; |
| 76 | + expect(evaluate({ store, ref: { preferredProfileId: "first" } })).toMatchObject({ |
| 77 | + availability: false, |
| 78 | + unavailableReason: "cooldown", |
| 79 | + unavailableUntil: until, |
| 80 | + }); |
| 81 | + expect(evaluate({ store, ref: { lockedProfileId: "invalid" } })).toMatchObject({ |
| 82 | + availability: false, |
| 83 | + unavailableReason: "auth-failed", |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + it.each(["openai", "anthropic"])( |
| 88 | + "reports permanent auth rejection for %s without removing explicit profile retry", |
| 89 | + (provider) => { |
| 90 | + const store = authStore({ bound: { type: "api_key", provider, key: "rejected-key" } }); |
| 91 | + store.usageStats = { |
| 92 | + bound: { disabledUntil: Date.now() + 60_000, disabledReason: "auth_permanent" }, |
| 93 | + }; |
| 94 | + const resolver = createModelAuthAvailabilityResolver({ |
| 95 | + cfg: {}, |
| 96 | + authStore: store, |
| 97 | + env: {}, |
| 98 | + routeResolverFactory: routeResolverFactory(dualRoutes), |
| 99 | + }); |
| 100 | + const result = resolver.evaluateModelAuth(provider); |
| 101 | + expect(result).toMatchObject({ availability: false, unavailableReason: "auth-failed" }); |
| 102 | + expect(result.unavailableUntil).toBeUndefined(); |
| 103 | + const locked = resolver.evaluateModelAuth(provider, { lockedProfileId: "bound" }); |
| 104 | + expect(locked.availability).toBe(true); |
| 105 | + expect(locked.unavailableReason).toBeUndefined(); |
| 106 | + }, |
| 107 | + ); |
| 108 | + |
| 109 | + it.each(["profile", "inline"] as const)( |
| 110 | + "distinguishes %s permanent auth rejection from active retry windows", |
| 111 | + (source) => { |
| 112 | + const until = Date.now() + 60_000; |
| 113 | + const cases = [ |
| 114 | + { stats: { cooldownUntil: until }, reason: "cooldown", retryAt: until }, |
| 115 | + { |
| 116 | + stats: { disabledUntil: until, disabledReason: "auth_permanent" }, |
| 117 | + reason: "auth-failed", |
| 118 | + retryAt: undefined, |
| 119 | + }, |
| 120 | + { |
| 121 | + stats: { disabledUntil: 1, disabledReason: "auth_permanent", cooldownUntil: until }, |
| 122 | + reason: "cooldown", |
| 123 | + retryAt: until, |
| 124 | + }, |
| 125 | + { |
| 126 | + stats: { disabledUntil: until, disabledReason: "billing" }, |
| 127 | + reason: "cooldown", |
| 128 | + retryAt: until, |
| 129 | + }, |
| 130 | + ] as const; |
| 131 | + for (const { stats, reason, retryAt } of cases) { |
| 132 | + const store = authStore({ |
| 133 | + bound: { type: "api_key", provider: "anthropic", key: "test-key" }, |
| 134 | + }); |
| 135 | + store.usageStats = { |
| 136 | + [source === "profile" ? "bound" : "inline-api-key:anthropic"]: stats, |
| 137 | + }; |
| 138 | + const result = createModelAuthAvailabilityResolver({ |
| 139 | + cfg: { |
| 140 | + models: { |
| 141 | + providers: { |
| 142 | + anthropic: { |
| 143 | + auth: "api-key", |
| 144 | + apiKey: source === "profile" ? "bound" : "inline-key", |
| 145 | + baseUrl: "https://api.anthropic.com", |
| 146 | + models: [], |
| 147 | + }, |
| 148 | + }, |
| 149 | + }, |
| 150 | + }, |
| 151 | + authStore: store, |
| 152 | + env: {}, |
| 153 | + }).evaluateModelAuth("anthropic"); |
| 154 | + expect.soft(result, JSON.stringify(stats)).toMatchObject({ |
| 155 | + availability: false, |
| 156 | + unavailableReason: reason, |
| 157 | + }); |
| 158 | + expect.soft(result.unavailableUntil, JSON.stringify(stats)).toBe(retryAt); |
| 159 | + } |
| 160 | + }, |
| 161 | + ); |
| 162 | +}); |
0 commit comments