import { describe, it, expect, vi, beforeEach } from 'vitest';
vi.mock('cohere-ai', () => ({
CohereClientV2: vi.fn().mockImplementation(() => ({
chat: vi.fn().mockResolvedValue({
message: { content: [{ type: 'text', text: 'mocked response' }] },
finishReason: 'COMPLETE',
usage: { billedUnits: { inputTokens: 5, outputTokens: 3 } },
}),
embed: vi.fn().mockResolvedValue({
embeddings: { float: [[0.1, 0.2, 0.3]] },
}),
rerank: vi.fn().mockResolvedValue({
results: [{ index: 0, relevanceScore: 0.95 }],
}),
})),
}));
describe('Chat service', () => {
it('returns text from chat completion', async () => {
const { CohereClientV2 } = await import('cohere-ai');
const cohere = new CohereClientV2();
const response = await cohere.chat({
model: 'command-a-03-2025',
messages: [{ role: 'user', content: 'test' }],
});
expect(response.message?.content?.[0]?.text).toBe('mocked response');
expect(cohere.chat).toHaveBeenCalledWith(
expect.objectContaining({ model: 'command-a-03-2025' })
);
});
it('handles embed with correct input type', async () => {
const { CohereClientV2 } = await import('cohere-ai');
const cohere = new CohereClientV2();
const response = await cohere.embed({
model: 'embed-v4.0',
texts: ['test'],
inputType: 'search_document',
embeddingTypes: ['float'],
});
expect(response.embeddings.float).toHaveLength(1);
});
});
import { describe, it, expect } from 'vitest';
import { CohereClientV2 } from 'cohere-ai';
const hasApiKey = !!process.env.CO_API_KEY;
describe.skipIf(!hasApiKey)('Cohere Integration', () => {
const cohere = new CohereClientV2();
it('chat completion works', async () => {
const response = await cohere.chat({
model: 'command-r7b-12-2024',
messages: [{ role: 'user', content: 'Reply with exactly: OK' }],
maxTokens: 5,
});
expect(response.message?.content?.[0]?.text).toBeTruthy();
expect(response.finishReason).toBe('COMPLETE');
}, 15_000);
it('embed generates vectors', async () => {
const response = await cohere.embed({
model: 'embed-v4.0',
texts: ['CI test embedding'],
inputType: 'search_document',
embeddingTypes: ['float'],
});
expect(response.embeddings.float[0].length).toBeGreaterThan(0);
}, 15_000);
it('rerank scores documents', async () => {
const response = await cohere.rerank({
model: 'rerank-v3.5',
query: 'machine learning',
documents: ['ML is AI', 'cooking recipes', 'deep learning'],
topN: 2,
});
expect(response.results).toHaveLength(2);
expect(response.results[0].relevanceScore).toBeGreaterThan(0.5);
}, 15_000);
});
{
"scripts": {
"test": "vitest --run",
"test:watch": "vitest --watch",
"test:integration": "COHERE_INTEGRATION=1 vitest --run tests/integration/",
"test:coverage": "vitest --run --coverage"
}
}