|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import type { SessionEntry } from "../../config/sessions/types.js"; |
| 4 | +import { |
| 5 | + authStoreMocks, |
| 6 | + configureProviderRoutes, |
| 7 | + createAutomaticSessionEntry, |
| 8 | + prepareCooldownAuthState, |
| 9 | + resolveSession, |
| 10 | + TEST_PRIMARY_PROFILE_ID, |
| 11 | + TEST_SECONDARY_PROFILE_ID, |
| 12 | + withAuthState, |
| 13 | +} from "./session-override.test-support.js"; |
| 14 | +import type { AuthProfileStore } from "./types.js"; |
| 15 | + |
| 16 | +const OPENAI_MODEL_ID = "gpt-5.6-sol"; |
| 17 | +const API_PRIMARY_PROFILE_ID = "openai:api-primary"; |
| 18 | +const API_BACKUP_PROFILE_ID = "openai:api-backup"; |
| 19 | +const OAUTH_PROFILE_ID = "openai:subscription"; |
| 20 | +const ANTHROPIC_API_PROFILE_ID = "anthropic:api"; |
| 21 | +const CLAUDE_CLI_PROFILE_ID = "anthropic:claude-cli"; |
| 22 | + |
| 23 | +type RotationTrigger = "compaction" | "cooldown"; |
| 24 | + |
| 25 | +function configureMixedOpenAiAuthStore(): void { |
| 26 | + authStoreMocks.state.hasSource = true; |
| 27 | + authStoreMocks.state.store = { |
| 28 | + version: 1, |
| 29 | + profiles: { |
| 30 | + [API_PRIMARY_PROFILE_ID]: { |
| 31 | + type: "api_key", |
| 32 | + provider: "openai", |
| 33 | + key: "sk-primary", |
| 34 | + }, |
| 35 | + [API_BACKUP_PROFILE_ID]: { |
| 36 | + type: "api_key", |
| 37 | + provider: "openai", |
| 38 | + key: "sk-backup", |
| 39 | + }, |
| 40 | + [OAUTH_PROFILE_ID]: { |
| 41 | + type: "oauth", |
| 42 | + provider: "openai", |
| 43 | + access: "test-access", |
| 44 | + refresh: "test-refresh", |
| 45 | + expires: Date.now() + 60_000, |
| 46 | + }, |
| 47 | + }, |
| 48 | + order: { |
| 49 | + openai: [API_PRIMARY_PROFILE_ID, OAUTH_PROFILE_ID, API_BACKUP_PROFILE_ID], |
| 50 | + }, |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +function configureAnthropicFallbackStore(): void { |
| 55 | + authStoreMocks.state.hasSource = true; |
| 56 | + authStoreMocks.state.store = { |
| 57 | + version: 1, |
| 58 | + profiles: { |
| 59 | + [ANTHROPIC_API_PROFILE_ID]: { |
| 60 | + type: "api_key", |
| 61 | + provider: "anthropic", |
| 62 | + key: "sk-anthropic", |
| 63 | + }, |
| 64 | + [CLAUDE_CLI_PROFILE_ID]: { |
| 65 | + type: "oauth", |
| 66 | + provider: "claude-cli", |
| 67 | + access: "test-access", |
| 68 | + refresh: "test-refresh", |
| 69 | + expires: Date.now() + 60_000, |
| 70 | + }, |
| 71 | + }, |
| 72 | + order: { |
| 73 | + anthropic: [ANTHROPIC_API_PROFILE_ID, CLAUDE_CLI_PROFILE_ID], |
| 74 | + }, |
| 75 | + }; |
| 76 | +} |
| 77 | + |
| 78 | +function createTriggeredSessionEntry(params: { |
| 79 | + profileId: string; |
| 80 | + model: string; |
| 81 | + trigger: RotationTrigger; |
| 82 | +}): SessionEntry { |
| 83 | + if (params.trigger === "cooldown") { |
| 84 | + authStoreMocks.isProfileInCooldown.mockImplementation( |
| 85 | + (_store: AuthProfileStore, profileId: string) => profileId === params.profileId, |
| 86 | + ); |
| 87 | + } |
| 88 | + return createAutomaticSessionEntry({ |
| 89 | + model: params.model, |
| 90 | + authProfileOverride: params.profileId, |
| 91 | + compactionCount: params.trigger === "compaction" ? 1 : 0, |
| 92 | + authProfileOverrideCompactionCount: 0, |
| 93 | + }); |
| 94 | +} |
| 95 | + |
| 96 | +describe("session auth-profile rotation", () => { |
| 97 | + it.each(["compaction", "cooldown"] as const)( |
| 98 | + "keeps a multi-route OpenAI session on its physical route after %s", |
| 99 | + async (trigger) => { |
| 100 | + await withAuthState(async (state) => { |
| 101 | + const agentDir = state.agentDir(); |
| 102 | + await fs.mkdir(agentDir, { recursive: true }); |
| 103 | + configureMixedOpenAiAuthStore(); |
| 104 | + configureProviderRoutes({ |
| 105 | + provider: "openai", |
| 106 | + modelId: OPENAI_MODEL_ID, |
| 107 | + requirements: ["api-key", "subscription"], |
| 108 | + }); |
| 109 | + const sessionEntry = createTriggeredSessionEntry({ |
| 110 | + profileId: API_PRIMARY_PROFILE_ID, |
| 111 | + model: OPENAI_MODEL_ID, |
| 112 | + trigger, |
| 113 | + }); |
| 114 | + const sessionStore = { "agent:main:main": sessionEntry }; |
| 115 | + |
| 116 | + const resolved = await resolveSession({ agentDir, sessionEntry, sessionStore }); |
| 117 | + |
| 118 | + expect(resolved).toBe(API_BACKUP_PROFILE_ID); |
| 119 | + expect(sessionEntry.authProfileOverride).toBe(API_BACKUP_PROFILE_ID); |
| 120 | + expect(sessionEntry.authProfileOverrideSource).toBe("auto"); |
| 121 | + }); |
| 122 | + }, |
| 123 | + ); |
| 124 | + |
| 125 | + it.each(["compaction", "cooldown"] as const)( |
| 126 | + "retains Anthropic API-key to Claude CLI OAuth fallback after %s", |
| 127 | + async (trigger) => { |
| 128 | + await withAuthState(async (state) => { |
| 129 | + const agentDir = state.agentDir(); |
| 130 | + await fs.mkdir(agentDir, { recursive: true }); |
| 131 | + configureAnthropicFallbackStore(); |
| 132 | + const sessionEntry = createTriggeredSessionEntry({ |
| 133 | + profileId: ANTHROPIC_API_PROFILE_ID, |
| 134 | + model: "claude-sonnet-4-6", |
| 135 | + trigger, |
| 136 | + }); |
| 137 | + const sessionStore = { "agent:main:main": sessionEntry }; |
| 138 | + |
| 139 | + const resolved = await resolveSession({ |
| 140 | + provider: "anthropic", |
| 141 | + agentDir, |
| 142 | + sessionEntry, |
| 143 | + sessionStore, |
| 144 | + }); |
| 145 | + |
| 146 | + expect(resolved).toBe(CLAUDE_CLI_PROFILE_ID); |
| 147 | + expect(sessionEntry.authProfileOverride).toBe(CLAUDE_CLI_PROFILE_ID); |
| 148 | + }); |
| 149 | + }, |
| 150 | + ); |
| 151 | + |
| 152 | + it("retains mixed-mode rotation when the provider exposes only one route", async () => { |
| 153 | + await withAuthState(async (state) => { |
| 154 | + const agentDir = state.agentDir(); |
| 155 | + await fs.mkdir(agentDir, { recursive: true }); |
| 156 | + configureMixedOpenAiAuthStore(); |
| 157 | + configureProviderRoutes({ |
| 158 | + provider: "openai", |
| 159 | + modelId: OPENAI_MODEL_ID, |
| 160 | + requirements: ["api-key"], |
| 161 | + }); |
| 162 | + const sessionEntry = createTriggeredSessionEntry({ |
| 163 | + profileId: API_PRIMARY_PROFILE_ID, |
| 164 | + model: OPENAI_MODEL_ID, |
| 165 | + trigger: "compaction", |
| 166 | + }); |
| 167 | + const sessionStore = { "agent:main:main": sessionEntry }; |
| 168 | + |
| 169 | + const resolved = await resolveSession({ agentDir, sessionEntry, sessionStore }); |
| 170 | + |
| 171 | + expect(resolved).toBe(OAUTH_PROFILE_ID); |
| 172 | + expect(sessionEntry.authProfileOverride).toBe(OAUTH_PROFILE_ID); |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + it("rotates an automatic override to an auth profile that is not in cooldown", async () => { |
| 177 | + await withAuthState(async (state) => { |
| 178 | + const agentDir = await prepareCooldownAuthState(state, { |
| 179 | + profileIds: [TEST_PRIMARY_PROFILE_ID, TEST_SECONDARY_PROFILE_ID], |
| 180 | + }); |
| 181 | + authStoreMocks.isProfileInCooldown.mockImplementation( |
| 182 | + (_store: AuthProfileStore, profileId: string) => profileId === TEST_PRIMARY_PROFILE_ID, |
| 183 | + ); |
| 184 | + const sessionEntry = createAutomaticSessionEntry(); |
| 185 | + const sessionStore = { "agent:main:main": sessionEntry }; |
| 186 | + |
| 187 | + const resolved = await resolveSession({ agentDir, sessionEntry, sessionStore }); |
| 188 | + |
| 189 | + expect(resolved).toBe(TEST_SECONDARY_PROFILE_ID); |
| 190 | + expect(sessionEntry.authProfileOverride).toBe(TEST_SECONDARY_PROFILE_ID); |
| 191 | + expect(sessionEntry.authProfileOverrideSource).toBe("auto"); |
| 192 | + }); |
| 193 | + }); |
| 194 | + |
| 195 | + it("keeps an automatic override after its auth-profile cooldown expires", async () => { |
| 196 | + await withAuthState(async (state) => { |
| 197 | + const agentDir = await prepareCooldownAuthState(state, { |
| 198 | + usageStats: { [TEST_PRIMARY_PROFILE_ID]: { cooldownUntil: Date.now() - 1 } }, |
| 199 | + }); |
| 200 | + authStoreMocks.isProfileInCooldown.mockImplementation( |
| 201 | + (store: AuthProfileStore, profileId: string) => |
| 202 | + (store.usageStats?.[profileId]?.cooldownUntil ?? 0) > Date.now(), |
| 203 | + ); |
| 204 | + const sessionEntry = createAutomaticSessionEntry({ authProfileOverrideCompactionCount: 0 }); |
| 205 | + const sessionStore = { "agent:main:main": sessionEntry }; |
| 206 | + |
| 207 | + const resolved = await resolveSession({ agentDir, sessionEntry, sessionStore }); |
| 208 | + |
| 209 | + expect(resolved).toBe(TEST_PRIMARY_PROFILE_ID); |
| 210 | + expect(sessionEntry.authProfileOverrideSource).toBe("auto"); |
| 211 | + expect(sessionEntry.updatedAt).toBe(1); |
| 212 | + }); |
| 213 | + }); |
| 214 | +}); |
0 commit comments