0d3cb498a3
CI / Shell Format Check (push) Has been cancelled
CI / Check Ruby (3.4) (push) Has been cancelled
CI / CI Config (push) Has been cancelled
CI / Test on Node ${{ matrix.node }} and ${{ matrix.os }}${{ matrix.shard && format(' (shard {0}/3)', matrix.shard) || '' }} (push) Has been cancelled
CI / Build on Node ${{ matrix.node }} (push) Has been cancelled
CI / Style Check (push) Has been cancelled
CI / Generate Assets (push) Has been cancelled
CI / Check Python (3.14) (push) Has been cancelled
CI / Check Python (3.9) (push) Has been cancelled
CI / Build Docs (push) Has been cancelled
CI / Code Scan Action (push) Has been cancelled
CI / Site tests (push) Has been cancelled
CI / webui tests (push) Has been cancelled
CI / Run Integration Tests (push) Has been cancelled
CI / Run Smoke Tests (push) Has been cancelled
CI / Go Tests (push) Has been cancelled
CI / Share Test (push) Has been cancelled
CI / Redteam (Production API) (push) Has been cancelled
CI / Redteam (Staging API) (push) Has been cancelled
CI / GitHub Actions Lint (push) Has been cancelled
CI / Check Ruby (3.0) (push) Has been cancelled
release-please / release-please (push) Has been cancelled
release-please / build (push) Has been cancelled
release-please / publish-npm (push) Has been cancelled
release-please / publish-npm-backfill (push) Has been cancelled
release-please / docker (push) Has been cancelled
release-please / publish-code-scan-action (push) Has been cancelled
release-please / attest-code-scan-action (push) Has been cancelled
Deploy local.promptfoo.app / Deploy to Cloudflare Pages (push) Has been cancelled
Test and Publish Multi-arch Docker Image / test (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-amd64 platform:linux/amd64 runner:ubuntu-latest]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / build-docker-and-push-digests (map[digest-suffix:linux-arm64 platform:linux/arm64 runner:ubuntu-24.04-arm]) (push) Has been cancelled
Test and Publish Multi-arch Docker Image / merge-docker-digests (push) Has been cancelled
Test and Publish Multi-arch Docker Image / Attest Multi-arch Image (push) Has been cancelled
Validate Renovate Config / Validate Renovate Configuration (push) Has been cancelled
160 lines
4.0 KiB
TypeScript
160 lines
4.0 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
|
import { fetchWithCache } from '../../../src/cache';
|
|
import { AzureEmbeddingProvider } from '../../../src/providers/azure/embedding';
|
|
|
|
vi.mock('../../../src/cache');
|
|
|
|
describe('AzureEmbeddingProvider', () => {
|
|
let provider: AzureEmbeddingProvider;
|
|
|
|
beforeEach(() => {
|
|
provider = new AzureEmbeddingProvider('test-deployment', {
|
|
endpoint: 'https://test.openai.azure.com',
|
|
apiKey: 'test-key',
|
|
headers: {
|
|
'Custom-Header': 'custom-value',
|
|
},
|
|
} as any);
|
|
|
|
(provider as any).getApiBaseUrl = () => 'https://test.openai.azure.com';
|
|
(provider as any).authHeaders = {
|
|
'api-key': 'test-key',
|
|
};
|
|
vi.spyOn(provider as any, 'ensureInitialized').mockImplementation(function () {
|
|
return Promise.resolve();
|
|
});
|
|
|
|
vi.mocked(fetchWithCache).mockReset();
|
|
});
|
|
|
|
it('should handle cached response', async () => {
|
|
const mockResponse = {
|
|
data: {
|
|
data: [
|
|
{
|
|
embedding: [0.1, 0.2, 0.3],
|
|
},
|
|
],
|
|
usage: {
|
|
total_tokens: 10,
|
|
},
|
|
},
|
|
cached: true,
|
|
};
|
|
|
|
vi.mocked(fetchWithCache).mockResolvedValueOnce(mockResponse as any);
|
|
|
|
const result = await provider.callEmbeddingApi('test text');
|
|
|
|
expect(result).toEqual({
|
|
embedding: [0.1, 0.2, 0.3],
|
|
cached: true,
|
|
tokenUsage: {
|
|
cached: 10,
|
|
total: 10,
|
|
numRequests: 1,
|
|
},
|
|
});
|
|
});
|
|
|
|
it('should handle API call errors', async () => {
|
|
vi.mocked(fetchWithCache).mockRejectedValueOnce(new Error('API error'));
|
|
|
|
const result = await provider.callEmbeddingApi('test text');
|
|
|
|
expect(result).toEqual({
|
|
error: 'API call error: Error: API error',
|
|
tokenUsage: {
|
|
total: 0,
|
|
prompt: 0,
|
|
completion: 0,
|
|
numRequests: 1,
|
|
},
|
|
});
|
|
});
|
|
|
|
it('should handle missing embedding in response', async () => {
|
|
const mockResponse = {
|
|
data: {
|
|
data: [{}],
|
|
usage: {
|
|
total_tokens: 10,
|
|
prompt_tokens: 5,
|
|
completion_tokens: 5,
|
|
},
|
|
},
|
|
cached: false,
|
|
};
|
|
|
|
vi.mocked(fetchWithCache).mockResolvedValueOnce(mockResponse as any);
|
|
|
|
const result = await provider.callEmbeddingApi('test text');
|
|
|
|
expect(result).toEqual({
|
|
error: expect.stringContaining('No embedding returned'),
|
|
tokenUsage: {
|
|
total: 10,
|
|
prompt: 5,
|
|
completion: 5,
|
|
numRequests: 1,
|
|
},
|
|
});
|
|
});
|
|
|
|
it('should handle missing API host', async () => {
|
|
(provider as any).getApiBaseUrl = () => undefined;
|
|
|
|
await expect(provider.callEmbeddingApi('test text')).rejects.toThrow(
|
|
'Azure API host must be set.',
|
|
);
|
|
});
|
|
|
|
it('should handle API response error with missing usage fields', async () => {
|
|
const mockResponse = {
|
|
data: {
|
|
data: [{}],
|
|
// usage is missing
|
|
},
|
|
cached: false,
|
|
};
|
|
|
|
vi.mocked(fetchWithCache).mockResolvedValueOnce(mockResponse as any);
|
|
|
|
const result = await provider.callEmbeddingApi('test text');
|
|
|
|
expect(result).toEqual({
|
|
error: expect.stringContaining('No embedding returned'),
|
|
tokenUsage: {
|
|
total: undefined,
|
|
prompt: undefined,
|
|
completion: undefined,
|
|
numRequests: 1,
|
|
},
|
|
});
|
|
});
|
|
|
|
it('handles a cached response with missing usage fields gracefully (no throw)', async () => {
|
|
const mockResponse = {
|
|
data: {
|
|
data: [{}],
|
|
// usage is missing
|
|
},
|
|
cached: true,
|
|
};
|
|
|
|
vi.mocked(fetchWithCache).mockResolvedValueOnce(mockResponse as any);
|
|
|
|
// Previously the cached error path dereferenced data.usage.total_tokens and threw a
|
|
// TypeError; it must now degrade to a clean error object.
|
|
const result = await provider.callEmbeddingApi('test text');
|
|
expect(result).toEqual({
|
|
error: expect.stringContaining('No embedding returned'),
|
|
tokenUsage: {
|
|
cached: undefined,
|
|
total: undefined,
|
|
numRequests: 1,
|
|
},
|
|
});
|
|
});
|
|
});
|