Add nonblocking/asyn I/O operations
This commit is contained in:
@@ -1,18 +1,18 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
|
||||
const { createMock, mockEmbeddings } = vi.hoisted(() => {
|
||||
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 { createMock: vi.fn(), mockEmbeddings: embeddings };
|
||||
return { streamMock: vi.fn(), mockEmbeddings: embeddings };
|
||||
});
|
||||
|
||||
vi.mock('@anthropic-ai/sdk', () => {
|
||||
return {
|
||||
default: class MockAnthropic {
|
||||
constructor() {
|
||||
this.messages = { create: createMock };
|
||||
this.messages = { stream: streamMock };
|
||||
}
|
||||
},
|
||||
};
|
||||
@@ -34,6 +34,40 @@ const MOCK_CLUSTERS = [
|
||||
{ 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', () => {
|
||||
|
||||
@@ -41,27 +75,32 @@ describe('clusterNotes service', () => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should call Anthropic messages.create with the correct model', async () => {
|
||||
createMock.mockResolvedValue({
|
||||
content: [{ type: 'text', text: JSON.stringify(MOCK_CLUSTERS) }],
|
||||
});
|
||||
it('should call Anthropic messages.stream with the correct model', async () => {
|
||||
mockStreamOf(JSON.stringify(MOCK_CLUSTERS));
|
||||
|
||||
await clusterNotes(MOCK_NOTES);
|
||||
|
||||
expect(createMock).toHaveBeenCalledOnce();
|
||||
const callArgs = createMock.mock.calls[0][0];
|
||||
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 () => {
|
||||
createMock.mockResolvedValue({
|
||||
content: [{ type: 'text', text: JSON.stringify(MOCK_CLUSTERS) }],
|
||||
});
|
||||
mockStreamOf(JSON.stringify(MOCK_CLUSTERS));
|
||||
|
||||
await clusterNotes(MOCK_NOTES);
|
||||
|
||||
const prompt = createMock.mock.calls[0][0].messages[0].content;
|
||||
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');
|
||||
@@ -69,9 +108,7 @@ describe('clusterNotes service', () => {
|
||||
});
|
||||
|
||||
it('should return clusters and a cohesion score', async () => {
|
||||
createMock.mockResolvedValue({
|
||||
content: [{ type: 'text', text: JSON.stringify(MOCK_CLUSTERS) }],
|
||||
});
|
||||
mockStreamOf(JSON.stringify(MOCK_CLUSTERS));
|
||||
|
||||
const result = await clusterNotes(MOCK_NOTES);
|
||||
|
||||
@@ -81,22 +118,35 @@ describe('clusterNotes service', () => {
|
||||
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 () => {
|
||||
createMock.mockResolvedValue({
|
||||
content: [{ type: 'text', text: 'An unknown error occured when generting structured response.' }],
|
||||
});
|
||||
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 () => {
|
||||
createMock.mockResolvedValue({ content: [] });
|
||||
mockStreamOf('');
|
||||
|
||||
await expect(clusterNotes(MOCK_NOTES)).rejects.toThrow('no text content returned');
|
||||
});
|
||||
|
||||
it('should throw error when Anthropic API authentication fails', async () => {
|
||||
createMock.mockRejectedValue(new Error('401 Unauthorized'));
|
||||
mockStreamThrowing(new Error('401 Unauthorized'));
|
||||
|
||||
await expect(clusterNotes(MOCK_NOTES)).rejects.toThrow('401 Unauthorized');
|
||||
});
|
||||
@@ -105,9 +155,7 @@ describe('clusterNotes service', () => {
|
||||
const incompleteClusters = [
|
||||
{ label: 'Auth Issues', noteIds: ['note_001'] },
|
||||
];
|
||||
createMock.mockResolvedValue({
|
||||
content: [{ type: 'text', text: JSON.stringify(incompleteClusters) }],
|
||||
});
|
||||
mockStreamOf(JSON.stringify(incompleteClusters));
|
||||
|
||||
await expect(clusterNotes(MOCK_NOTES)).rejects.toThrow('Cluster validation failed');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user