Files
DeskClaw/tests/unit/provider-store-validation.test.ts
2026-04-10 15:15:29 +08:00

43 lines
1.3 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { beforeEach, describe, expect, it, vi } from 'vitest';
const mockFetchProviderSnapshot = vi.fn();
const mockHostApiFetch = vi.fn();
vi.mock('@/lib/provider-accounts', () => ({
fetchProviderSnapshot: (...args: unknown[]) => mockFetchProviderSnapshot(...args),
}));
vi.mock('@/lib/host-api', () => ({
hostApiFetch: (...args: unknown[]) => mockHostApiFetch(...args),
}));
import { useProviderStore } from '@/stores/providers';
describe('useProviderStore validateAccountApiKey()', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('trims API keys before sending provider validation requests', async () => {
mockHostApiFetch.mockResolvedValueOnce({ valid: true });
const result = await useProviderStore.getState().validateAccountApiKey('custom', ' sk-lm-test \n', {
baseUrl: 'http://127.0.0.1:1234/v1',
apiProtocol: 'openai-completions',
});
expect(result).toEqual({ valid: true });
expect(mockHostApiFetch).toHaveBeenCalledWith('/api/providers/validate', {
method: 'POST',
body: JSON.stringify({
providerId: 'custom',
apiKey: 'sk-lm-test',
options: {
baseUrl: 'http://127.0.0.1:1234/v1',
apiProtocol: 'openai-completions',
},
}),
});
});
});