Skip to content

Commit 50501ae

Browse files
committed
test: fix typecheck issues by using classes
1 parent c9424d6 commit 50501ae

File tree

7 files changed

+75
-65
lines changed

7 files changed

+75
-65
lines changed

apps/server/src/routes/api/llm.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ vi.mock("../../services/llm/ai_service_manager.js", () => ({
5252

5353
// Mock chat pipeline
5454
const mockChatPipelineExecute = vi.fn();
55-
const MockChatPipeline = vi.fn().mockImplementation(function () {
56-
this.execute = mockChatPipelineExecute;
57-
});
55+
class MockChatPipeline {
56+
execute = mockChatPipelineExecute;
57+
}
5858
vi.mock("../../services/llm/pipeline/chat_pipeline.js", () => ({
5959
ChatPipeline: MockChatPipeline
6060
}));

apps/server/src/services/llm/ai_service_manager.spec.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,26 +34,29 @@ vi.mock('../log.js', () => ({
3434
}
3535
}));
3636

37-
vi.mock('./providers/anthropic_service.js', () => ({
38-
AnthropicService: vi.fn().mockImplementation(function () {
39-
this.isAvailable = vi.fn().mockReturnValue(true);
40-
this.generateChatCompletion = vi.fn();
41-
})
42-
}));
37+
vi.mock('./providers/anthropic_service.js', () => {
38+
class AnthropicService {
39+
isAvailable = vi.fn().mockReturnValue(true);
40+
generateChatCompletion = vi.fn();
41+
}
42+
return { AnthropicService };
43+
});
4344

44-
vi.mock('./providers/openai_service.js', () => ({
45-
OpenAIService: vi.fn().mockImplementation(function () {
46-
this.isAvailable = vi.fn().mockReturnValue(true);
47-
this.generateChatCompletion = vi.fn();
48-
})
49-
}));
45+
vi.mock('./providers/openai_service.js', () => {
46+
class OpenAIService {
47+
isAvailable = vi.fn().mockReturnValue(true);
48+
generateChatCompletion = vi.fn();
49+
}
50+
return { OpenAIService };
51+
});
5052

51-
vi.mock('./providers/ollama_service.js', () => ({
52-
OllamaService: vi.fn().mockImplementation(function () {
53-
this.isAvailable = vi.fn().mockReturnValue(true);
54-
this.generateChatCompletion = vi.fn();
55-
})
56-
}));
53+
vi.mock('./providers/ollama_service.js', () => {
54+
class OllamaService {
55+
isAvailable = vi.fn().mockReturnValue(true);
56+
generateChatCompletion = vi.fn();
57+
}
58+
return { OllamaService };
59+
});
5760

5861
vi.mock('./config/configuration_helpers.js', () => ({
5962
getSelectedProvider: vi.fn(),

apps/server/src/services/llm/chat/rest_chat_service.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ vi.mock('../pipeline/chat_pipeline.js', () => ({
3838
}))
3939
}));
4040

41-
vi.mock('./handlers/tool_handler.js', () => ({
42-
ToolHandler: vi.fn().mockImplementation(function () {
43-
this.handleToolCalls = vi.fn()
44-
})
45-
}));
41+
vi.mock('./handlers/tool_handler.js', () => {
42+
class ToolHandler {
43+
handleToolCalls = vi.fn()
44+
}
45+
return { ToolHandler };
46+
});
4647

4748
vi.mock('../chat_storage_service.js', () => ({
4849
default: {

apps/server/src/services/llm/chat_service.spec.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,28 @@ vi.mock('./constants/llm_prompt_constants.js', () => ({
3535
}
3636
}));
3737

38-
vi.mock('./pipeline/chat_pipeline.js', () => ({
39-
ChatPipeline: vi.fn().mockImplementation(function (config) {
40-
Object.assign(this, {
41-
config,
42-
execute: vi.fn(),
43-
getMetrics: vi.fn(),
44-
resetMetrics: vi.fn(),
45-
stages: {
46-
contextExtraction: {
47-
execute: vi.fn()
48-
},
49-
semanticContextExtraction: {
50-
execute: vi.fn()
51-
}
38+
vi.mock('./pipeline/chat_pipeline.js', () => {
39+
class ChatPipeline {
40+
config: any;
41+
42+
constructor(config: any) {
43+
this.config = config;
44+
}
45+
46+
execute = vi.fn();
47+
getMetrics = vi.fn();
48+
resetMetrics = vi.fn();
49+
stages = {
50+
contextExtraction: {
51+
execute: vi.fn()
52+
},
53+
semanticContextExtraction: {
54+
execute: vi.fn()
5255
}
53-
});
54-
})
55-
}));
56+
}
57+
}
58+
return { ChatPipeline };
59+
});
5660

5761
vi.mock('./ai_service_manager.js', () => ({
5862
default: {

apps/server/src/services/llm/context/services/context_service.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ vi.mock('../../ai_service_manager.js', () => ({
4646
}
4747
}));
4848

49-
vi.mock('../index.js', () => ({
50-
ContextExtractor: vi.fn().mockImplementation(function () {
51-
this.findRelevantNotes = vi.fn().mockResolvedValue([])
52-
})
53-
}));
49+
vi.mock('../index.js', () => {
50+
class ContextExtractor {
51+
findRelevantNotes = vi.fn().mockResolvedValue([])
52+
}
53+
return { ContextExtractor };
54+
});
5455

5556
describe('ContextService', () => {
5657
let service: ContextService;

apps/server/src/services/llm/providers/anthropic_service.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ vi.mock('@anthropic-ai/sdk', () => {
4848
}
4949
};
5050

51-
const mockAnthropic = vi.fn().mockImplementation(function () {
52-
this.messages = {
51+
class MockAnthropic {
52+
messages = {
5353
create: vi.fn().mockImplementation((params) => {
5454
if (params.stream) {
5555
return Promise.resolve(mockStream);
@@ -72,9 +72,9 @@ vi.mock('@anthropic-ai/sdk', () => {
7272
});
7373
})
7474
};
75-
});
75+
}
7676

77-
return { default: mockAnthropic };
77+
return { default: MockAnthropic };
7878
});
7979

8080
describe('AnthropicService', () => {

apps/server/src/services/llm/providers/ollama_service.spec.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ vi.mock('./providers.js', () => ({
2929
getOllamaOptions: vi.fn()
3030
}));
3131

32-
vi.mock('../formatters/ollama_formatter.js', () => ({
33-
OllamaMessageFormatter: vi.fn().mockImplementation(function () {
34-
this.formatMessages = vi.fn().mockReturnValue([
32+
vi.mock('../formatters/ollama_formatter.js', () => {
33+
class MockFormatter {
34+
formatMessages = vi.fn().mockReturnValue([
3535
{ role: 'user', content: 'Hello' }
3636
]);
37-
this.formatResponse = vi.fn().mockReturnValue({
37+
formatResponse = vi.fn().mockReturnValue({
3838
text: 'Hello! How can I help you today?',
3939
provider: 'Ollama',
4040
model: 'llama2',
@@ -45,8 +45,9 @@ vi.mock('../formatters/ollama_formatter.js', () => ({
4545
},
4646
tool_calls: null
4747
});
48-
})
49-
}));
48+
}
49+
return { OllamaMessageFormatter: MockFormatter };
50+
});
5051

5152
vi.mock('../tools/tool_registry.js', () => ({
5253
default: {
@@ -83,8 +84,8 @@ vi.mock('ollama', () => {
8384
}
8485
};
8586

86-
const mockOllama = vi.fn().mockImplementation(function () {
87-
this.chat = vi.fn().mockImplementation((params) => {
87+
class MockOllama {
88+
chat = vi.fn().mockImplementation((params) => {
8889
if (params.stream) {
8990
return Promise.resolve(mockStream);
9091
}
@@ -98,7 +99,7 @@ vi.mock('ollama', () => {
9899
done: true
99100
});
100101
});
101-
this.show = vi.fn().mockResolvedValue({
102+
show = vi.fn().mockResolvedValue({
102103
modelfile: 'FROM llama2',
103104
parameters: {},
104105
template: '',
@@ -110,18 +111,18 @@ vi.mock('ollama', () => {
110111
quantization_level: 'Q4_0'
111112
}
112113
});
113-
this.list = vi.fn().mockResolvedValue({
114+
list = vi.fn().mockResolvedValue({
114115
models: [
115116
{
116117
name: 'llama2:latest',
117118
modified_at: '2024-01-01T00:00:00Z',
118119
size: 3800000000
119120
}
120121
]
121-
})
122-
});
122+
});
123+
}
123124

124-
return { Ollama: mockOllama };
125+
return { Ollama: MockOllama };
125126
});
126127

127128
// Mock global fetch

0 commit comments

Comments
 (0)