feat: enhance provider functionality with base URL normalization and API protocol support; add tests for provider creation
This commit is contained in:
90
tests/provider-create.test.ts
Normal file
90
tests/provider-create.test.ts
Normal file
@@ -0,0 +1,90 @@
|
||||
// @vitest-environment node
|
||||
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const mocks = vi.hoisted(() => ({
|
||||
getAccounts: vi.fn(),
|
||||
getApiKey: vi.fn(),
|
||||
openAI: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('@electron/service/provider-api-service', () => ({
|
||||
providerApiService: {
|
||||
getAccounts: mocks.getAccounts,
|
||||
getApiKey: mocks.getApiKey,
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('@electron/service/logger', () => ({
|
||||
default: {
|
||||
logApiRequest: vi.fn(),
|
||||
logApiResponse: vi.fn(),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('openai', () => ({
|
||||
default: mocks.openAI,
|
||||
}));
|
||||
|
||||
describe('createProvider', () => {
|
||||
beforeEach(() => {
|
||||
vi.resetModules();
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('falls back to the MiniMax CN OpenAI-compatible base URL for legacy accounts', async () => {
|
||||
mocks.getAccounts.mockReturnValue([
|
||||
{
|
||||
id: 'minimax-portal-cn-account',
|
||||
vendorId: 'minimax-portal-cn',
|
||||
label: 'MiniMax (CN)',
|
||||
authMode: 'local',
|
||||
model: 'MiniMax-M2.7',
|
||||
enabled: true,
|
||||
isDefault: true,
|
||||
createdAt: '2026-04-20T00:00:00.000Z',
|
||||
updatedAt: '2026-04-20T00:00:00.000Z',
|
||||
},
|
||||
]);
|
||||
mocks.getApiKey.mockReturnValue({ apiKey: 'secret-key' });
|
||||
|
||||
const { createProvider, OpenAIProvider } = await import('../electron/providers');
|
||||
const provider = createProvider('minimax-portal-cn-account');
|
||||
|
||||
expect(provider).toBeInstanceOf(OpenAIProvider);
|
||||
expect(mocks.openAI).toHaveBeenCalledWith({
|
||||
apiKey: 'secret-key',
|
||||
baseURL: 'https://api.minimaxi.com/v1',
|
||||
defaultHeaders: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it('prefers metadata resource URLs when a legacy OAuth account has no stored base URL', async () => {
|
||||
mocks.getAccounts.mockReturnValue([
|
||||
{
|
||||
id: 'minimax-portal-account',
|
||||
vendorId: 'minimax-portal',
|
||||
label: 'MiniMax (Global)',
|
||||
authMode: 'oauth_browser',
|
||||
model: 'MiniMax-M2.7',
|
||||
enabled: true,
|
||||
isDefault: true,
|
||||
metadata: {
|
||||
resourceUrl: 'tenant.runtime.minimax.io/v1',
|
||||
},
|
||||
createdAt: '2026-04-20T00:00:00.000Z',
|
||||
updatedAt: '2026-04-20T00:00:00.000Z',
|
||||
},
|
||||
]);
|
||||
mocks.getApiKey.mockReturnValue({ apiKey: 'oauth-token' });
|
||||
|
||||
const { createProvider } = await import('../electron/providers');
|
||||
createProvider('minimax-portal-account');
|
||||
|
||||
expect(mocks.openAI).toHaveBeenCalledWith({
|
||||
apiKey: 'oauth-token',
|
||||
baseURL: 'https://tenant.runtime.minimax.io/v1',
|
||||
defaultHeaders: undefined,
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user