| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- import { describe, expect, it, vi } from 'vitest';
- import type { McodeProviderModel, McodeProviderView } from '../../src/provider/contract.js';
- import { McodeProviderApplication } from '../../src/provider/application.js';
- function createPort() {
- return {
- discoverUserModelsCandidate: vi.fn(async (): Promise<readonly McodeProviderModel[]> => []),
- listProviderPresets: vi.fn(async () => []),
- getCodexOAuthStatus: vi.fn(async () => ({
- state: 'disconnected' as const,
- providerId: 'openai-codex' as const,
- })),
- cancelCodexOAuthLogin: vi.fn(async () => ({ state: 'disconnected' as const, providerId: 'openai-codex' as const })),
- startCodexOAuthLogin: vi.fn(async () => ({
- state: 'pending' as const,
- providerId: 'openai-codex' as const,
- authUrl: 'https://auth.openai.example/authorize',
- })),
- listUserModelProviders: vi.fn(async () => [
- {
- providerId: 'custom_provider:openai',
- name: 'OpenAI',
- kind: 'custom' as const,
- enabled: true,
- apiFormat: 'openai-completions' as const,
- baseUrl: 'https://api.openai.com/v1',
- hasApiKey: true,
- configRevision: 'rev-1',
- maskedApiKey: 'sk-****1234',
- rawApiKey: 'must-never-cross-the-cli-boundary',
- models: [{ modelId: 'gpt-4.1', displayName: 'GPT-4.1' }],
- },
- ]),
- getMiniMaxApiKeyStatus: vi.fn(async () => ({
- hasApiKey: true,
- maskedApiKey: 'sk-****5678',
- rawApiKey: 'must-never-cross-the-cli-boundary',
- })),
- getMiniMaxModelSource: vi.fn(async () => 'minimax_api_key' as const),
- setMiniMaxModelSource: vi.fn(async (source: 'token_plan' | 'minimax_api_key') => source),
- upsertMiniMaxApiKey: vi.fn(async () => undefined),
- createUserModelProvider: vi.fn(async () => undefined),
- saveUserModelProviderCandidate: vi.fn(async () => ({
- success: true,
- provider: { providerId: 'custom_provider:openai' },
- })),
- updateUserModelProvider: vi.fn(async () => undefined),
- deleteUserModelProvider: vi.fn(async () => undefined),
- testUserModelProvider: vi.fn(async () => ({
- success: true,
- status: { state: 'available' },
- })),
- testUserModel: vi.fn(async () => ({
- success: true,
- status: { state: 'available' },
- })),
- };
- }
- describe('McodeProviderApplication', () => {
- it('exposes a disconnected Codex OAuth row when Runtime makes it visible', async () => {
- const application = new McodeProviderApplication(createPort());
- const snapshot = await application.snapshot({ includeCodexOAuth: true });
- expect(snapshot.providers[0]).toMatchObject({
- providerId: 'openai-codex',
- name: 'OpenAI Codex',
- kind: 'codex-oauth',
- active: false,
- enabled: true,
- readOnly: true,
- hasApiKey: false,
- status: { state: 'disconnected' },
- });
- });
- it('omits the Codex OAuth row when Runtime marks it hidden', async () => {
- const port = createPort();
- port.getCodexOAuthStatus.mockResolvedValueOnce({
- state: 'hidden',
- providerId: 'openai-codex',
- });
- const application = new McodeProviderApplication(port);
- const snapshot = await application.snapshot({ includeCodexOAuth: true });
- expect(snapshot.providers).not.toContainEqual(
- expect.objectContaining({ providerId: 'openai-codex' }),
- );
- });
- it('builds a CLI-owned snapshot without exposing raw API keys', async () => {
- const port = createPort();
- const application = new McodeProviderApplication(port);
- const snapshot = await application.snapshot();
- expect(snapshot.providers.map((provider) => provider.providerId)).toEqual([
- 'minimax_oauth',
- 'minimax_api',
- 'custom_provider:openai',
- ]);
- expect(snapshot.providers[0]).toMatchObject({
- providerId: 'minimax_oauth',
- name: 'MiniMax OAuth',
- kind: 'minimax-oauth',
- active: false,
- enabled: true,
- readOnly: true,
- hasApiKey: false,
- });
- expect(snapshot.providers[1]).toMatchObject({
- kind: 'minimax-api-key',
- active: true,
- hasApiKey: true,
- });
- expect(JSON.stringify(snapshot)).toContain('MiniMax OAuth');
- expect(JSON.stringify(snapshot)).not.toContain('must-never-cross-the-cli-boundary');
- expect(snapshot.providers[2]).not.toHaveProperty('rawApiKey');
- expect(port.getCodexOAuthStatus).not.toHaveBeenCalled();
- });
- it('marks OAuth active when the MiniMax source is Token Plan', async () => {
- const port = createPort();
- port.getMiniMaxModelSource.mockResolvedValueOnce('token_plan');
- const application = new McodeProviderApplication(port);
- const snapshot = await application.snapshot();
- expect(snapshot.minimaxModelSource).toBe('token_plan');
- expect(snapshot.providers[0]).toMatchObject({ kind: 'minimax-oauth', active: true });
- expect(snapshot.providers[1]).toMatchObject({ kind: 'minimax-api-key', active: false });
- });
- it('forwards MiniMax source changes through the CLI port', async () => {
- const port = createPort();
- const application = new McodeProviderApplication(port);
- await expect(application.setMiniMaxSource('token_plan')).resolves.toBe('token_plan');
- expect(port.setMiniMaxModelSource).toHaveBeenCalledWith('token_plan');
- });
- it('forwards custom provider creation through the CLI port', async () => {
- const port = createPort();
- const application = new McodeProviderApplication(port);
- await application.create({
- name: 'OpenAI',
- baseUrl: 'https://api.openai.com/v1',
- apiKey: 'sk-live',
- apiFormat: 'openai-completions',
- models: [{ modelId: 'gpt-4.1' }],
- saveAndUse: true,
- });
- expect(port.createUserModelProvider).toHaveBeenCalledWith({
- name: 'OpenAI',
- baseUrl: 'https://api.openai.com/v1',
- apiKey: 'sk-live',
- apiFormat: 'openai-completions',
- models: [{ modelId: 'gpt-4.1' }],
- saveAndUse: true,
- });
- });
- it('saves a tested provider candidate and selects its chosen model atomically', async () => {
- const port = createPort();
- const application = new McodeProviderApplication(port);
- const result = await application.saveCandidate({
- name: 'OpenAI',
- baseUrl: 'https://api.openai.com/v1',
- apiKey: 'sk-live',
- apiFormat: 'openai-responses',
- models: [{ modelId: 'gpt-5.6' }],
- modelId: 'gpt-5.6',
- saveAndUse: true,
- });
- expect(result).toMatchObject({ success: true });
- expect(port.saveUserModelProviderCandidate).toHaveBeenCalledWith({
- name: 'OpenAI',
- baseUrl: 'https://api.openai.com/v1',
- apiKey: 'sk-live',
- apiFormat: 'openai-responses',
- models: [{ modelId: 'gpt-5.6' }],
- modelId: 'gpt-5.6',
- saveAndUse: true,
- });
- });
- it('preserves the Runtime-supported OpenAI Responses protocol', async () => {
- const port = createPort();
- port.listUserModelProviders.mockResolvedValueOnce([
- {
- providerId: 'custom_provider:responses',
- name: 'OpenAI Responses',
- kind: 'custom',
- enabled: true,
- apiFormat: 'openai-responses',
- baseUrl: 'https://api.openai.com/v1',
- hasApiKey: true,
- models: [{ modelId: 'gpt-5.6' }],
- },
- ]);
- const application = new McodeProviderApplication(port);
- const snapshot = await application.snapshot();
- expect(
- snapshot.providers.find((provider) => provider.providerId === 'custom_provider:responses'),
- ).toMatchObject({ apiFormat: 'openai-responses' });
- });
- it('uses provider and model-specific connectivity tests', async () => {
- const port = createPort();
- const application = new McodeProviderApplication(port);
- await application.test('custom_provider:openai');
- await application.test('custom_provider:openai', 'gpt-4.1');
- expect(port.testUserModelProvider).toHaveBeenCalledWith('custom_provider:openai');
- expect(port.testUserModel).toHaveBeenCalledWith('custom_provider:openai', 'gpt-4.1');
- });
- it('keeps a disabled provider inactive even when a model is still selected', async () => {
- const port = createPort();
- port.listUserModelProviders.mockResolvedValueOnce([
- {
- providerId: 'custom_provider:byok',
- name: 'BYOK Vendor',
- kind: 'custom' as const,
- enabled: false,
- apiFormat: 'openai-completions' as const,
- baseUrl: 'https://api.byok.example/v1',
- hasApiKey: true,
- maskedApiKey: 'sk-****9999',
- rawApiKey: 'must-never-cross-the-cli-boundary',
- models: [{ modelId: 'byok-large-5', selected: true }],
- },
- ]);
- const application = new McodeProviderApplication(port);
- const snapshot = await application.snapshot();
- // Runtime drops disabled providers from the roster, so rendering the row
- // as active would contradict the "Disabled" label on the same line.
- expect(snapshot.providers[2]).toMatchObject({
- providerId: 'custom_provider:byok',
- enabled: false,
- active: false,
- });
- });
- it('keeps an enabled provider active while a model is selected', async () => {
- const port = createPort();
- port.listUserModelProviders.mockResolvedValueOnce([
- {
- providerId: 'custom_provider:byok',
- name: 'BYOK Vendor',
- kind: 'custom' as const,
- enabled: true,
- apiFormat: 'openai-completions' as const,
- baseUrl: 'https://api.byok.example/v1',
- hasApiKey: true,
- models: [{ modelId: 'byok-large-5', selected: true }],
- },
- ]);
- const application = new McodeProviderApplication(port);
- const snapshot = await application.snapshot();
- expect(snapshot.providers[2]).toMatchObject({ enabled: true, active: true });
- });
- });
- describe('saved provider model refresh', () => {
- const provider: McodeProviderView = {
- providerId: 'custom_provider:work', name: 'Work', kind: 'custom',
- enabled: true, readOnly: false, active: true, hasApiKey: true,
- configRevision: 'rev-1', baseUrl: 'https://models.example/v1',
- models: [{ modelId: 'old-model', selected: true }],
- };
- it('discovers with saved credentials and adds only new IDs without switching models', async () => {
- const port = createPort();
- port.discoverUserModelsCandidate.mockResolvedValue([
- { modelId: 'old-model', displayName: 'Do not overwrite saved settings' },
- { modelId: 'new-model', displayName: 'New model' }, { modelId: ' new-model ' }, { modelId: ' old-model ' },
- ]);
- await expect(new McodeProviderApplication(port).refreshModels(provider)).resolves.toBe(1);
- const identity = { providerId: provider.providerId, expectedRevision: 'rev-1', baseUrl: provider.baseUrl };
- expect(port.discoverUserModelsCandidate).toHaveBeenCalledWith(identity);
- expect(port.saveUserModelProviderCandidate).toHaveBeenCalledWith({
- ...identity, models: [{ modelId: 'old-model' }, { modelId: 'new-model', displayName: 'New model', configurationSource: 'discovered' }],
- modelId: 'new-model', skipConnectionTest: true, saveAndUse: false,
- });
- expect(port.createUserModelProvider).not.toHaveBeenCalled();
- expect(port.setMiniMaxModelSource).not.toHaveBeenCalled();
- expect(provider.models).toEqual([{ modelId: 'old-model', selected: true }]);
- });
- it.each([{ models: [] }, { models: [{ modelId: 'old-model' }] }])('does not save an empty or unchanged discovery result', async ({ models }) => {
- const port = createPort();
- port.discoverUserModelsCandidate.mockResolvedValue(models);
- await expect(new McodeProviderApplication(port).refreshModels(provider)).resolves.toBe(0);
- expect(port.saveUserModelProviderCandidate).not.toHaveBeenCalled();
- });
- it('keeps the saved configuration when discovery fails', async () => {
- const port = createPort();
- port.discoverUserModelsCandidate.mockRejectedValue(new Error('Authentication failed'));
- await expect(new McodeProviderApplication(port).refreshModels(provider)).rejects.toThrow('Authentication failed');
- expect(port.saveUserModelProviderCandidate).not.toHaveBeenCalled();
- });
- it('surfaces a revision conflict without retrying or creating a new account', async () => {
- const port = createPort();
- port.discoverUserModelsCandidate.mockResolvedValue([{ modelId: 'new-model' }]);
- port.saveUserModelProviderCandidate.mockRejectedValue(new Error('Configuration changed'));
- await expect(new McodeProviderApplication(port).refreshModels(provider)).rejects.toThrow('Configuration changed');
- expect(port.saveUserModelProviderCandidate).toHaveBeenCalledOnce();
- expect(port.createUserModelProvider).not.toHaveBeenCalled();
- });
- it('rejects a missing revision before making any request', async () => {
- const port = createPort();
- await expect(new McodeProviderApplication(port).refreshModels({ ...provider, configRevision: undefined })).rejects.toThrow('Reopen /provider');
- expect(port.discoverUserModelsCandidate).not.toHaveBeenCalled();
- });
- });
|