|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import type { CatalogProviderEntry, PythinkerConfig } from '@pythoughts/pythinker-code-sdk'; |
| 3 | + |
| 4 | +import { connectCatalogProvider } from '#/tui/commands/auth'; |
| 5 | +import type { SlashCommandHost } from '#/tui/commands/dispatch'; |
| 6 | + |
| 7 | +vi.mock('#/tui/commands/prompts', () => ({ |
| 8 | + promptApiKey: vi.fn(), |
| 9 | + promptLogoutProviderSelection: vi.fn(), |
| 10 | + promptModelSelectionForCatalog: vi.fn(), |
| 11 | + promptModelSelectionForOpenPlatform: vi.fn(), |
| 12 | + promptPlatformSelection: vi.fn(), |
| 13 | +})); |
| 14 | + |
| 15 | +const { promptApiKey, promptModelSelectionForCatalog } = await import('#/tui/commands/prompts'); |
| 16 | + |
| 17 | +const CATALOG_ENTRY: CatalogProviderEntry = { |
| 18 | + id: 'anthropic', |
| 19 | + name: 'Anthropic', |
| 20 | + npm: '@ai-sdk/anthropic', |
| 21 | + api: 'https://api.anthropic.com', |
| 22 | + env: ['TEST_CATALOG_API_KEY'], |
| 23 | + models: { |
| 24 | + 'claude-opus-4-7': { |
| 25 | + id: 'claude-opus-4-7', |
| 26 | + name: 'Claude Opus 4.7', |
| 27 | + limit: { context: 200_000, output: 64_000 }, |
| 28 | + tool_call: true, |
| 29 | + reasoning: true, |
| 30 | + modalities: { input: ['text', 'image'], output: ['text'] }, |
| 31 | + }, |
| 32 | + }, |
| 33 | +} as CatalogProviderEntry; |
| 34 | + |
| 35 | +function makeHost(initial: PythinkerConfig) { |
| 36 | + let config = initial; |
| 37 | + const errors: string[] = []; |
| 38 | + const host = { |
| 39 | + harness: { |
| 40 | + getConfig: vi.fn(async () => config), |
| 41 | + setConfig: vi.fn(async (patch: Partial<PythinkerConfig>) => { |
| 42 | + config = { ...config, ...patch }; |
| 43 | + }), |
| 44 | + removeProvider: vi.fn(async (id: string) => { |
| 45 | + delete config.providers[id]; |
| 46 | + return config; |
| 47 | + }), |
| 48 | + }, |
| 49 | + authFlow: { refreshConfigAfterLogin: vi.fn(async () => undefined) }, |
| 50 | + showError: vi.fn((msg: string) => errors.push(msg)), |
| 51 | + showStatus: vi.fn(), |
| 52 | + track: vi.fn(), |
| 53 | + restoreEditor: vi.fn(), |
| 54 | + mountEditorReplacement: vi.fn(), |
| 55 | + cancelInFlight: undefined, |
| 56 | + } as unknown as SlashCommandHost; |
| 57 | + return { host, errors, current: () => config }; |
| 58 | +} |
| 59 | + |
| 60 | +describe('connectCatalogProvider credential acquisition', () => { |
| 61 | + beforeEach(() => { |
| 62 | + vi.mocked(promptModelSelectionForCatalog).mockResolvedValue({ |
| 63 | + model: { id: 'claude-opus-4-7' } as never, |
| 64 | + effort: 'off', |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + afterEach(() => { |
| 69 | + vi.unstubAllEnvs(); |
| 70 | + vi.mocked(promptApiKey).mockReset(); |
| 71 | + vi.mocked(promptModelSelectionForCatalog).mockReset(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('uses the env var without prompting when it is set', async () => { |
| 75 | + vi.stubEnv('TEST_CATALOG_API_KEY', 'from-env'); |
| 76 | + const { host, current } = makeHost({ providers: {} } as PythinkerConfig); |
| 77 | + |
| 78 | + await connectCatalogProvider(host, 'anthropic', CATALOG_ENTRY); |
| 79 | + |
| 80 | + expect(promptApiKey).not.toHaveBeenCalled(); |
| 81 | + expect(current().providers['anthropic']).toMatchObject({ |
| 82 | + apiKeyEnvVar: 'TEST_CATALOG_API_KEY', |
| 83 | + }); |
| 84 | + expect(current().providers['anthropic']?.apiKey).toBeUndefined(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('prompts for a key and stores it literally when the env var is unset', async () => { |
| 88 | + vi.stubEnv('TEST_CATALOG_API_KEY', ''); |
| 89 | + vi.mocked(promptApiKey).mockResolvedValue('sk-typed-in'); |
| 90 | + const { host, errors, current } = makeHost({ providers: {} } as PythinkerConfig); |
| 91 | + |
| 92 | + await connectCatalogProvider(host, 'anthropic', CATALOG_ENTRY); |
| 93 | + |
| 94 | + expect(errors).toEqual([]); |
| 95 | + expect(promptApiKey).toHaveBeenCalledWith( |
| 96 | + host, |
| 97 | + 'Anthropic', |
| 98 | + expect.arrayContaining([expect.stringContaining('config.toml')]), |
| 99 | + ); |
| 100 | + expect(current().providers['anthropic']?.apiKey).toBe('sk-typed-in'); |
| 101 | + expect(current().providers['anthropic']?.apiKeyEnvVar).toBeUndefined(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('prompts for a key when the catalog entry declares no env var', async () => { |
| 105 | + vi.mocked(promptApiKey).mockResolvedValue('sk-typed-in'); |
| 106 | + const entry = { ...CATALOG_ENTRY, env: undefined } as CatalogProviderEntry; |
| 107 | + const { host, errors, current } = makeHost({ providers: {} } as PythinkerConfig); |
| 108 | + |
| 109 | + await connectCatalogProvider(host, 'anthropic', entry); |
| 110 | + |
| 111 | + expect(errors).toEqual([]); |
| 112 | + expect(current().providers['anthropic']?.apiKey).toBe('sk-typed-in'); |
| 113 | + expect(current().providers['anthropic']?.apiKeyEnvVar).toBeUndefined(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('aborts without writing config when the key prompt is cancelled', async () => { |
| 117 | + vi.stubEnv('TEST_CATALOG_API_KEY', ''); |
| 118 | + vi.mocked(promptApiKey).mockResolvedValue(undefined); |
| 119 | + const { host, current } = makeHost({ providers: {} } as PythinkerConfig); |
| 120 | + |
| 121 | + await connectCatalogProvider(host, 'anthropic', CATALOG_ENTRY); |
| 122 | + |
| 123 | + expect(current().providers['anthropic']).toBeUndefined(); |
| 124 | + expect(promptModelSelectionForCatalog).not.toHaveBeenCalled(); |
| 125 | + }); |
| 126 | +}); |
0 commit comments