163 lines
4.8 KiB
JavaScript
163 lines
4.8 KiB
JavaScript
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
|
|
const { streamMock, mockEmbeddings } = vi.hoisted(() => {
|
|
const embeddings = new Map([
|
|
['note_001', [1.0, 0.0, 0.0]],
|
|
['note_002', [0.0, 1.0, 0.0]],
|
|
]);
|
|
return { streamMock: vi.fn(), mockEmbeddings: embeddings };
|
|
});
|
|
|
|
vi.mock('@anthropic-ai/sdk', () => {
|
|
return {
|
|
default: class MockAnthropic {
|
|
constructor() {
|
|
this.messages = { stream: streamMock };
|
|
}
|
|
},
|
|
};
|
|
});
|
|
|
|
vi.mock('../services/embedding.service.js', () => ({
|
|
embedNotes: vi.fn().mockResolvedValue(mockEmbeddings),
|
|
}));
|
|
|
|
import { clusterNotes } from '../services/clustering.service.js';
|
|
|
|
const MOCK_NOTES = [
|
|
{ id: 'note_001', text: 'Login is broken' },
|
|
{ id: 'note_002', text: 'Export fails' },
|
|
];
|
|
|
|
const MOCK_CLUSTERS = [
|
|
{ label: 'Auth Issues', noteIds: ['note_001'] },
|
|
{ label: 'Export Issues', noteIds: ['note_002'] },
|
|
];
|
|
|
|
// Splits text into several text_delta events so the service is exercised
|
|
// against a genuinely incremental stream rather than one whole payload.
|
|
const textEvents = (text, pieces = 4) => {
|
|
const size = Math.max(1, Math.ceil(text.length / pieces));
|
|
const events = [];
|
|
for (let i = 0; i < text.length; i += size) {
|
|
events.push({
|
|
type: 'content_block_delta',
|
|
delta: { type: 'text_delta', text: text.slice(i, i + size) },
|
|
});
|
|
}
|
|
return events;
|
|
};
|
|
|
|
const mockStreamOf = (text) => {
|
|
const events = [
|
|
{ type: 'message_start' },
|
|
...textEvents(text),
|
|
{ type: 'message_stop' },
|
|
];
|
|
streamMock.mockImplementation(() => ({
|
|
async *[Symbol.asyncIterator]() {
|
|
for (const event of events) yield event;
|
|
},
|
|
}));
|
|
};
|
|
|
|
const mockStreamThrowing = (err) => {
|
|
streamMock.mockImplementation(() => ({
|
|
async *[Symbol.asyncIterator]() {
|
|
throw err;
|
|
},
|
|
}));
|
|
};
|
|
|
|
describe('clusterNotes service', () => {
|
|
|
|
beforeEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
it('should call Anthropic messages.stream with the correct model', async () => {
|
|
mockStreamOf(JSON.stringify(MOCK_CLUSTERS));
|
|
|
|
await clusterNotes(MOCK_NOTES);
|
|
|
|
expect(streamMock).toHaveBeenCalledOnce();
|
|
const callArgs = streamMock.mock.calls[0][0];
|
|
expect(callArgs.model).toBe('claude-sonnet-4-20250514');
|
|
expect(callArgs.max_tokens).toBe(4096);
|
|
});
|
|
|
|
it('should forward an abort signal to the LLM request', async () => {
|
|
mockStreamOf(JSON.stringify(MOCK_CLUSTERS));
|
|
const controller = new AbortController();
|
|
|
|
await clusterNotes(MOCK_NOTES, { signal: controller.signal });
|
|
|
|
expect(streamMock.mock.calls[0][1]).toEqual({ signal: controller.signal });
|
|
});
|
|
|
|
it('should include all note texts in prompt sent to the LLM API', async () => {
|
|
mockStreamOf(JSON.stringify(MOCK_CLUSTERS));
|
|
|
|
await clusterNotes(MOCK_NOTES);
|
|
|
|
const prompt = streamMock.mock.calls[0][0].messages[0].content;
|
|
expect(prompt).toContain('note_001');
|
|
expect(prompt).toContain('Login is broken');
|
|
expect(prompt).toContain('note_002');
|
|
expect(prompt).toContain('Export fails');
|
|
});
|
|
|
|
it('should return clusters and a cohesion score', async () => {
|
|
mockStreamOf(JSON.stringify(MOCK_CLUSTERS));
|
|
|
|
const result = await clusterNotes(MOCK_NOTES);
|
|
|
|
expect(result.clusters).toEqual(MOCK_CLUSTERS);
|
|
expect(typeof result.score).toBe('number');
|
|
expect(result.score).toBeGreaterThanOrEqual(-1);
|
|
expect(result.score).toBeLessThanOrEqual(1);
|
|
});
|
|
|
|
it('should reassemble clusters split across many stream deltas', async () => {
|
|
const json = JSON.stringify(MOCK_CLUSTERS);
|
|
streamMock.mockImplementation(() => ({
|
|
async *[Symbol.asyncIterator]() {
|
|
for (const char of json) {
|
|
yield { type: 'content_block_delta', delta: { type: 'text_delta', text: char } };
|
|
}
|
|
},
|
|
}));
|
|
|
|
const result = await clusterNotes(MOCK_NOTES);
|
|
|
|
expect(result.clusters).toEqual(MOCK_CLUSTERS);
|
|
});
|
|
|
|
it('should throw error when the API returns non-JSON', async () => {
|
|
mockStreamOf('An unknown error occured when generting structured response.');
|
|
|
|
await expect(clusterNotes(MOCK_NOTES)).rejects.toThrow('non-JSON response');
|
|
});
|
|
|
|
it('should throw error when the API response has no text content', async () => {
|
|
mockStreamOf('');
|
|
|
|
await expect(clusterNotes(MOCK_NOTES)).rejects.toThrow('no text content returned');
|
|
});
|
|
|
|
it('should throw error when Anthropic API authentication fails', async () => {
|
|
mockStreamThrowing(new Error('401 Unauthorized'));
|
|
|
|
await expect(clusterNotes(MOCK_NOTES)).rejects.toThrow('401 Unauthorized');
|
|
});
|
|
|
|
it('should throw a validation error when a note is missing from clusters', async () => {
|
|
const incompleteClusters = [
|
|
{ label: 'Auth Issues', noteIds: ['note_001'] },
|
|
];
|
|
mockStreamOf(JSON.stringify(incompleteClusters));
|
|
|
|
await expect(clusterNotes(MOCK_NOTES)).rejects.toThrow('Cluster validation failed');
|
|
});
|
|
});
|