mirror of
https://github.com/zadam/trilium.git
synced 2025-11-21 08:04:24 +01:00
test(server): LLM provider tests failing due to mocks
This commit is contained in:
parent
50501aef56
commit
c15ae293aa
@ -31,49 +31,7 @@ vi.mock('./providers.js', () => ({
|
||||
}));
|
||||
|
||||
vi.mock('@anthropic-ai/sdk', () => {
|
||||
const mockStream = {
|
||||
[Symbol.asyncIterator]: async function* () {
|
||||
yield {
|
||||
type: 'content_block_delta',
|
||||
delta: { text: 'Hello' }
|
||||
};
|
||||
yield {
|
||||
type: 'content_block_delta',
|
||||
delta: { text: ' world' }
|
||||
};
|
||||
yield {
|
||||
type: 'message_delta',
|
||||
delta: { stop_reason: 'end_turn' }
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
class MockAnthropic {
|
||||
messages = {
|
||||
create: vi.fn().mockImplementation((params) => {
|
||||
if (params.stream) {
|
||||
return Promise.resolve(mockStream);
|
||||
}
|
||||
return Promise.resolve({
|
||||
id: 'msg_123',
|
||||
type: 'message',
|
||||
role: 'assistant',
|
||||
content: [{
|
||||
type: 'text',
|
||||
text: 'Hello! How can I help you today?'
|
||||
}],
|
||||
model: 'claude-3-opus-20240229',
|
||||
stop_reason: 'end_turn',
|
||||
stop_sequence: null,
|
||||
usage: {
|
||||
input_tokens: 10,
|
||||
output_tokens: 25
|
||||
}
|
||||
});
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
const MockAnthropic = vi.fn();
|
||||
return { default: MockAnthropic };
|
||||
});
|
||||
|
||||
@ -85,7 +43,6 @@ describe('AnthropicService', () => {
|
||||
vi.clearAllMocks();
|
||||
|
||||
// Get the mocked Anthropic instance before creating the service
|
||||
const AnthropicMock = vi.mocked(Anthropic);
|
||||
mockAnthropicInstance = {
|
||||
messages: {
|
||||
create: vi.fn().mockImplementation((params) => {
|
||||
@ -127,8 +84,8 @@ describe('AnthropicService', () => {
|
||||
}
|
||||
};
|
||||
|
||||
AnthropicMock.mockImplementation(function () {
|
||||
Object.assign(this, mockAnthropicInstance);
|
||||
(Anthropic as any).mockImplementation(function(this: any) {
|
||||
return mockAnthropicInstance;
|
||||
});
|
||||
|
||||
service = new AnthropicService();
|
||||
@ -355,14 +312,13 @@ describe('AnthropicService', () => {
|
||||
vi.mocked(providers.getAnthropicOptions).mockReturnValueOnce(mockOptions);
|
||||
|
||||
// Spy on Anthropic constructor
|
||||
const AnthropicMock = vi.mocked(Anthropic);
|
||||
AnthropicMock.mockClear();
|
||||
(Anthropic as any).mockClear();
|
||||
|
||||
// Create new service to trigger client creation
|
||||
const newService = new AnthropicService();
|
||||
await newService.generateChatCompletion(messages);
|
||||
|
||||
expect(AnthropicMock).toHaveBeenCalledWith({
|
||||
expect(Anthropic).toHaveBeenCalledWith({
|
||||
apiKey: 'test-key',
|
||||
baseURL: 'https://api.anthropic.com',
|
||||
defaultHeaders: {
|
||||
@ -382,14 +338,13 @@ describe('AnthropicService', () => {
|
||||
vi.mocked(providers.getAnthropicOptions).mockReturnValueOnce(mockOptions);
|
||||
|
||||
// Spy on Anthropic constructor
|
||||
const AnthropicMock = vi.mocked(Anthropic);
|
||||
AnthropicMock.mockClear();
|
||||
(Anthropic as any).mockClear();
|
||||
|
||||
// Create new service to trigger client creation
|
||||
const newService = new AnthropicService();
|
||||
await newService.generateChatCompletion(messages);
|
||||
|
||||
expect(AnthropicMock).toHaveBeenCalledWith({
|
||||
expect(Anthropic).toHaveBeenCalledWith({
|
||||
apiKey: 'test-key',
|
||||
baseURL: 'https://api.anthropic.com',
|
||||
defaultHeaders: {
|
||||
|
||||
@ -65,63 +65,7 @@ vi.mock('./stream_handler.js', () => ({
|
||||
}));
|
||||
|
||||
vi.mock('ollama', () => {
|
||||
const mockStream = {
|
||||
[Symbol.asyncIterator]: async function* () {
|
||||
yield {
|
||||
message: {
|
||||
role: 'assistant',
|
||||
content: 'Hello'
|
||||
},
|
||||
done: false
|
||||
};
|
||||
yield {
|
||||
message: {
|
||||
role: 'assistant',
|
||||
content: ' world'
|
||||
},
|
||||
done: true
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
class MockOllama {
|
||||
chat = vi.fn().mockImplementation((params) => {
|
||||
if (params.stream) {
|
||||
return Promise.resolve(mockStream);
|
||||
}
|
||||
return Promise.resolve({
|
||||
message: {
|
||||
role: 'assistant',
|
||||
content: 'Hello! How can I help you today?'
|
||||
},
|
||||
created_at: '2024-01-01T00:00:00Z',
|
||||
model: 'llama2',
|
||||
done: true
|
||||
});
|
||||
});
|
||||
show = vi.fn().mockResolvedValue({
|
||||
modelfile: 'FROM llama2',
|
||||
parameters: {},
|
||||
template: '',
|
||||
details: {
|
||||
format: 'gguf',
|
||||
family: 'llama',
|
||||
families: ['llama'],
|
||||
parameter_size: '7B',
|
||||
quantization_level: 'Q4_0'
|
||||
}
|
||||
});
|
||||
list = vi.fn().mockResolvedValue({
|
||||
models: [
|
||||
{
|
||||
name: 'llama2:latest',
|
||||
modified_at: '2024-01-01T00:00:00Z',
|
||||
size: 3800000000
|
||||
}
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
const MockOllama = vi.fn();
|
||||
return { Ollama: MockOllama };
|
||||
});
|
||||
|
||||
@ -141,7 +85,6 @@ describe('OllamaService', () => {
|
||||
vi.clearAllMocks();
|
||||
|
||||
// Create the mock instance before creating the service
|
||||
const OllamaMock = vi.mocked(Ollama);
|
||||
mockOllamaInstance = {
|
||||
chat: vi.fn().mockImplementation((params) => {
|
||||
if (params.stream) {
|
||||
@ -197,8 +140,9 @@ describe('OllamaService', () => {
|
||||
})
|
||||
};
|
||||
|
||||
OllamaMock.mockImplementation(function () {
|
||||
Object.assign(this, mockOllamaInstance);
|
||||
// Mock the Ollama constructor to return our mock instance
|
||||
(Ollama as any).mockImplementation(function(this: any) {
|
||||
return mockOllamaInstance;
|
||||
});
|
||||
|
||||
service = new OllamaService();
|
||||
@ -401,8 +345,7 @@ describe('OllamaService', () => {
|
||||
vi.mocked(providers.getOllamaOptions).mockResolvedValueOnce(mockOptions);
|
||||
|
||||
// Spy on Ollama constructor
|
||||
const OllamaMock = vi.mocked(Ollama);
|
||||
OllamaMock.mockClear();
|
||||
(Ollama as any).mockClear();
|
||||
|
||||
// Create new service to trigger client creation
|
||||
const newService = new OllamaService();
|
||||
@ -416,7 +359,7 @@ describe('OllamaService', () => {
|
||||
|
||||
await newService.generateChatCompletion(messages);
|
||||
|
||||
expect(OllamaMock).toHaveBeenCalledWith({
|
||||
expect(Ollama).toHaveBeenCalledWith({
|
||||
host: 'http://localhost:11434',
|
||||
fetch: expect.any(Function)
|
||||
});
|
||||
@ -576,15 +519,14 @@ describe('OllamaService', () => {
|
||||
};
|
||||
vi.mocked(providers.getOllamaOptions).mockResolvedValue(mockOptions);
|
||||
|
||||
const OllamaMock = vi.mocked(Ollama);
|
||||
OllamaMock.mockClear();
|
||||
(Ollama as any).mockClear();
|
||||
|
||||
// Make two calls
|
||||
await service.generateChatCompletion([{ role: 'user', content: 'Hello' }]);
|
||||
await service.generateChatCompletion([{ role: 'user', content: 'Hi' }]);
|
||||
|
||||
// Should only create client once
|
||||
expect(OllamaMock).toHaveBeenCalledTimes(1);
|
||||
expect(Ollama).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user