|
| 1 | +import { |
| 2 | + afterAll, |
| 3 | + beforeEach, |
| 4 | + describe, |
| 5 | + expect, |
| 6 | + mock, |
| 7 | + spyOn, |
| 8 | + test, |
| 9 | +} from 'bun:test' |
| 10 | +import { mkdirSync, mkdtempSync, rmSync } from 'fs' |
| 11 | +import { tmpdir } from 'os' |
| 12 | +import { join } from 'path' |
| 13 | +import { |
| 14 | + resetSettingsCache, |
| 15 | + setSessionSettingsCache, |
| 16 | +} from '../../settings/settingsCache.js' |
| 17 | +import type { SettingsJson } from '../../settings/types.js' |
| 18 | + |
| 19 | +const testRoot = mkdtempSync(join(tmpdir(), 'provider-gates-')) |
| 20 | +const testConfigDir = join(testRoot, 'config') |
| 21 | +process.env.CLAUDE_CONFIG_DIR = testConfigDir |
| 22 | +process.env.NODE_ENV = 'test' |
| 23 | +mkdirSync(testConfigDir, { recursive: true }) |
| 24 | + |
| 25 | +const isolatedEnvKeys = [ |
| 26 | + 'ANTHROPIC_API_KEY', |
| 27 | + 'ANTHROPIC_AUTH_TOKEN', |
| 28 | + 'ANTHROPIC_BASE_URL', |
| 29 | + 'ANTHROPIC_UNIX_SOCKET', |
| 30 | + 'CLAUDE_CODE_API_KEY_FILE_DESCRIPTOR', |
| 31 | + 'CLAUDE_CODE_OAUTH_TOKEN', |
| 32 | + 'CLAUDE_CODE_OAUTH_TOKEN_FILE_DESCRIPTOR', |
| 33 | + 'CLAUDE_CODE_SIMPLE', |
| 34 | + 'CLAUDE_CODE_USE_BEDROCK', |
| 35 | + 'CLAUDE_CODE_USE_FOUNDRY', |
| 36 | + 'CLAUDE_CODE_USE_GEMINI', |
| 37 | + 'CLAUDE_CODE_USE_GROK', |
| 38 | + 'CLAUDE_CODE_USE_OPENAI', |
| 39 | + 'CLAUDE_CODE_USE_VERTEX', |
| 40 | + 'DISABLE_INSTALL_GITHUB_APP_COMMAND', |
| 41 | + 'DISABLE_LOGIN_COMMAND', |
| 42 | + 'DISABLE_LOGOUT_COMMAND', |
| 43 | + 'GEMINI_BASE_URL', |
| 44 | + 'OPENAI_BASE_URL', |
| 45 | +] as const |
| 46 | + |
| 47 | +function clearIsolatedEnv(): void { |
| 48 | + for (const key of isolatedEnvKeys) delete process.env[key] |
| 49 | +} |
| 50 | + |
| 51 | +function setModelType(modelType?: SettingsJson['modelType']): void { |
| 52 | + setSessionSettingsCache({ |
| 53 | + settings: modelType ? { modelType } : {}, |
| 54 | + errors: [], |
| 55 | + }) |
| 56 | +} |
| 57 | + |
| 58 | +clearIsolatedEnv() |
| 59 | +setModelType() |
| 60 | + |
| 61 | +// Keep the real auth implementation while preventing platform keychain reads. |
| 62 | +// The subprocess wrapper contains this module mock so it cannot leak globally. |
| 63 | +mock.module('src/utils/secureStorage/index.ts', () => ({ |
| 64 | + getSecureStorage: () => ({ |
| 65 | + name: 'provider-gates-test', |
| 66 | + read: () => null, |
| 67 | + readAsync: async () => null, |
| 68 | + update: () => ({ success: true }), |
| 69 | + delete: () => true, |
| 70 | + }), |
| 71 | +})) |
| 72 | + |
| 73 | +const { authStatus } = await import('../../../cli/handlers/auth.js') |
| 74 | +const { clearCommandsCache, getCommands, meetsAvailabilityRequirement } = |
| 75 | + await import('../../../commands.js') |
| 76 | +const { isAnthropicAuthEnabled, isUsing3PServices } = await import( |
| 77 | + '../../auth.js' |
| 78 | +) |
| 79 | +const { default: fast } = await import('../../../commands/fast/index.js') |
| 80 | +const { default: installGitHubApp } = await import( |
| 81 | + '../../../commands/install-github-app/index.js' |
| 82 | +) |
| 83 | + |
| 84 | +const envProviderCases = [ |
| 85 | + ['CLAUDE_CODE_USE_BEDROCK', 'bedrock'], |
| 86 | + ['CLAUDE_CODE_USE_VERTEX', 'vertex'], |
| 87 | + ['CLAUDE_CODE_USE_FOUNDRY', 'foundry'], |
| 88 | + ['CLAUDE_CODE_USE_OPENAI', 'openai'], |
| 89 | + ['CLAUDE_CODE_USE_GEMINI', 'gemini'], |
| 90 | + ['CLAUDE_CODE_USE_GROK', 'grok'], |
| 91 | +] as const |
| 92 | + |
| 93 | +beforeEach(() => { |
| 94 | + clearIsolatedEnv() |
| 95 | + resetSettingsCache() |
| 96 | + setModelType() |
| 97 | +}) |
| 98 | + |
| 99 | +afterAll(() => { |
| 100 | + clearCommandsCache() |
| 101 | + resetSettingsCache() |
| 102 | + mock.restore() |
| 103 | + rmSync(testRoot, { recursive: true, force: true }) |
| 104 | +}) |
| 105 | + |
| 106 | +describe('Console command availability', () => { |
| 107 | + for (const modelType of ['openai', 'gemini', 'grok'] as const) { |
| 108 | + test(`rejects settings modelType=${modelType}`, () => { |
| 109 | + setModelType(modelType) |
| 110 | + |
| 111 | + expect(meetsAvailabilityRequirement(fast)).toBe(false) |
| 112 | + expect(meetsAvailabilityRequirement(installGitHubApp)).toBe(false) |
| 113 | + }) |
| 114 | + } |
| 115 | + |
| 116 | + for (const [envKey] of envProviderCases) { |
| 117 | + test(`rejects ${envKey}`, () => { |
| 118 | + process.env[envKey] = '1' |
| 119 | + |
| 120 | + expect(meetsAvailabilityRequirement(fast)).toBe(false) |
| 121 | + expect(meetsAvailabilityRequirement(installGitHubApp)).toBe(false) |
| 122 | + }) |
| 123 | + } |
| 124 | + |
| 125 | + test('continues accepting direct first-party Anthropic', () => { |
| 126 | + process.env.CLAUDE_CODE_OAUTH_TOKEN = 'provider-gates-test-token' |
| 127 | + expect(meetsAvailabilityRequirement(fast)).toBe(true) |
| 128 | + expect(meetsAvailabilityRequirement(installGitHubApp)).toBe(true) |
| 129 | + }) |
| 130 | +}) |
| 131 | + |
| 132 | +describe('Anthropic auth provider gate', () => { |
| 133 | + for (const modelType of ['openai', 'gemini', 'grok'] as const) { |
| 134 | + test(`disables Anthropic auth for settings modelType=${modelType}`, () => { |
| 135 | + setModelType(modelType) |
| 136 | + expect(isAnthropicAuthEnabled()).toBe(false) |
| 137 | + }) |
| 138 | + } |
| 139 | + |
| 140 | + for (const [envKey] of envProviderCases) { |
| 141 | + test(`disables Anthropic auth for ${envKey}`, () => { |
| 142 | + process.env[envKey] = '1' |
| 143 | + expect(isAnthropicAuthEnabled()).toBe(false) |
| 144 | + }) |
| 145 | + } |
| 146 | + |
| 147 | + for (const envKey of ['OPENAI_BASE_URL', 'GEMINI_BASE_URL'] as const) { |
| 148 | + test(`disables Anthropic auth for ${envKey}`, () => { |
| 149 | + process.env[envKey] = 'https://provider-gates.invalid' |
| 150 | + expect(isAnthropicAuthEnabled()).toBe(false) |
| 151 | + }) |
| 152 | + } |
| 153 | + |
| 154 | + test('continues enabling Anthropic auth for first-party OAuth', () => { |
| 155 | + process.env.CLAUDE_CODE_OAUTH_TOKEN = 'provider-gates-test-token' |
| 156 | + expect(isAnthropicAuthEnabled()).toBe(true) |
| 157 | + }) |
| 158 | +}) |
| 159 | + |
| 160 | +describe('environment-only third-party compatibility gate', () => { |
| 161 | + for (const modelType of ['openai', 'gemini', 'grok'] as const) { |
| 162 | + test(`ignores settings modelType=${modelType}`, () => { |
| 163 | + setModelType(modelType) |
| 164 | + |
| 165 | + expect(isUsing3PServices()).toBe(false) |
| 166 | + }) |
| 167 | + } |
| 168 | +}) |
| 169 | + |
| 170 | +class ExitSignal extends Error { |
| 171 | + constructor(readonly code: string | number | null | undefined) { |
| 172 | + super(`process.exit(${String(code)})`) |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +async function captureAuthStatus() { |
| 177 | + let stdout = '' |
| 178 | + const writeSpy = spyOn(process.stdout, 'write').mockImplementation((( |
| 179 | + chunk: string | Uint8Array, |
| 180 | + ) => { |
| 181 | + stdout += chunk.toString() |
| 182 | + return true |
| 183 | + }) as typeof process.stdout.write) |
| 184 | + const exitSpy = spyOn(process, 'exit').mockImplementation((( |
| 185 | + code?: string | number | null, |
| 186 | + ): never => { |
| 187 | + throw new ExitSignal(code) |
| 188 | + }) as typeof process.exit) |
| 189 | + |
| 190 | + let exitCode: ExitSignal['code'] |
| 191 | + try { |
| 192 | + await authStatus({ json: true }) |
| 193 | + throw new Error('authStatus did not call process.exit') |
| 194 | + } catch (error) { |
| 195 | + if (!(error instanceof ExitSignal)) throw error |
| 196 | + exitCode = error.code |
| 197 | + } finally { |
| 198 | + exitSpy.mockRestore() |
| 199 | + writeSpy.mockRestore() |
| 200 | + } |
| 201 | + |
| 202 | + return { |
| 203 | + exitCode, |
| 204 | + output: JSON.parse(stdout) as Record<string, unknown>, |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +describe('auth status provider reporting', () => { |
| 209 | + for (const modelType of ['openai', 'gemini', 'grok'] as const) { |
| 210 | + test(`reports settings modelType=${modelType} as third_party`, async () => { |
| 211 | + setModelType(modelType) |
| 212 | + process.env.CLAUDE_CODE_OAUTH_TOKEN = 'provider-gates-test-token' |
| 213 | + |
| 214 | + const { exitCode, output } = await captureAuthStatus() |
| 215 | + |
| 216 | + expect(exitCode).toBe(0) |
| 217 | + expect(output.apiProvider).toBe(modelType) |
| 218 | + expect(output.authMethod).toBe('third_party') |
| 219 | + expect(output.loggedIn).toBe(true) |
| 220 | + }) |
| 221 | + } |
| 222 | + |
| 223 | + for (const [envKey, apiProvider] of envProviderCases) { |
| 224 | + test(`reports ${envKey} as third_party`, async () => { |
| 225 | + process.env[envKey] = '1' |
| 226 | + process.env.CLAUDE_CODE_OAUTH_TOKEN = 'provider-gates-test-token' |
| 227 | + |
| 228 | + const { exitCode, output } = await captureAuthStatus() |
| 229 | + |
| 230 | + expect(exitCode).toBe(0) |
| 231 | + expect(output.apiProvider).toBe(apiProvider) |
| 232 | + expect(output.authMethod).toBe('third_party') |
| 233 | + expect(output.loggedIn).toBe(true) |
| 234 | + }) |
| 235 | + } |
| 236 | + |
| 237 | + test('continues reporting direct first-party OAuth', async () => { |
| 238 | + process.env.CLAUDE_CODE_OAUTH_TOKEN = 'provider-gates-test-token' |
| 239 | + |
| 240 | + const { exitCode, output } = await captureAuthStatus() |
| 241 | + |
| 242 | + expect(exitCode).toBe(0) |
| 243 | + expect(output.apiProvider).toBe('firstParty') |
| 244 | + expect(output.authMethod).toBe('oauth_token') |
| 245 | + expect(output.loggedIn).toBe(true) |
| 246 | + }) |
| 247 | +}) |
| 248 | + |
| 249 | +describe('login and logout visibility', () => { |
| 250 | + for (const modelType of ['openai', 'gemini', 'grok'] as const) { |
| 251 | + test(`keeps both commands visible for settings modelType=${modelType}`, async () => { |
| 252 | + setModelType(modelType) |
| 253 | + process.env.CLAUDE_CODE_OAUTH_TOKEN = 'provider-gates-test-token' |
| 254 | + clearCommandsCache() |
| 255 | + |
| 256 | + const commandNames = new Set( |
| 257 | + (await getCommands(testRoot)).map(command => command.name), |
| 258 | + ) |
| 259 | + |
| 260 | + expect(commandNames.has('login')).toBe(true) |
| 261 | + expect(commandNames.has('logout')).toBe(true) |
| 262 | + }) |
| 263 | + } |
| 264 | +}) |
0 commit comments