mirror of
https://github.com/jina-ai/node-DeepResearch.git
synced 2025-12-26 06:28:56 +08:00
test: update tests to use mock LLM client
Co-Authored-By: Han Xiao <han.xiao@jina.ai>
This commit is contained in:
parent
0823371ecb
commit
3e08ae1c7a
31
src/__tests__/llm-client.test.ts
Normal file
31
src/__tests__/llm-client.test.ts
Normal file
@ -0,0 +1,31 @@
|
||||
import { GoogleAIWrapper, LocalLLMClient } from '../utils/llm-client';
|
||||
import * as config from '../config';
|
||||
|
||||
describe('LLM Client', () => {
|
||||
const originalEnv = process.env;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetModules();
|
||||
process.env = { ...originalEnv };
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
process.env = originalEnv;
|
||||
});
|
||||
|
||||
it('should use GoogleAIWrapper by default', () => {
|
||||
process.env.LLM_PROVIDER = 'gemini';
|
||||
process.env.GEMINI_API_KEY = 'test-key';
|
||||
const { llmClient } = require('../config');
|
||||
expect(llmClient).toBeInstanceOf(GoogleAIWrapper);
|
||||
});
|
||||
|
||||
it('should use LocalLLMClient when configured', () => {
|
||||
process.env.LLM_PROVIDER = 'local';
|
||||
process.env.LOCAL_LLM_HOSTNAME = 'localhost';
|
||||
process.env.LOCAL_LLM_PORT = '8000';
|
||||
process.env.LOCAL_LLM_MODEL = 'test-model';
|
||||
const { llmClient } = require('../config');
|
||||
expect(llmClient).toBeInstanceOf(LocalLLMClient);
|
||||
});
|
||||
});
|
||||
@ -1,4 +1,12 @@
|
||||
import { dedupQueries } from '../dedup';
|
||||
import { MockLLMClient } from './utils/llm-mock';
|
||||
import { TEST_RESPONSES } from './utils/test-config';
|
||||
import * as config from '../../config';
|
||||
|
||||
jest.mock('../../config', () => ({
|
||||
...jest.requireActual('../../config'),
|
||||
llmClient: new MockLLMClient(TEST_RESPONSES.dedup)
|
||||
}));
|
||||
|
||||
describe('dedupQueries', () => {
|
||||
it('should remove duplicate queries', async () => {
|
||||
|
||||
@ -1,4 +1,12 @@
|
||||
import { analyzeSteps } from '../error-analyzer';
|
||||
import { MockLLMClient } from './utils/llm-mock';
|
||||
import { TEST_RESPONSES } from './utils/test-config';
|
||||
import * as config from '../../config';
|
||||
|
||||
jest.mock('../../config', () => ({
|
||||
...jest.requireActual('../../config'),
|
||||
llmClient: new MockLLMClient(TEST_RESPONSES.errorAnalyzer)
|
||||
}));
|
||||
|
||||
describe('analyzeSteps', () => {
|
||||
it('should analyze error steps', async () => {
|
||||
|
||||
@ -1,5 +1,13 @@
|
||||
import { evaluateAnswer } from '../evaluator';
|
||||
import { TokenTracker } from '../../utils/token-tracker';
|
||||
import { MockLLMClient } from './utils/llm-mock';
|
||||
import { TEST_RESPONSES } from './utils/test-config';
|
||||
import * as config from '../../config';
|
||||
|
||||
jest.mock('../../config', () => ({
|
||||
...jest.requireActual('../../config'),
|
||||
llmClient: new MockLLMClient(TEST_RESPONSES.evaluator)
|
||||
}));
|
||||
|
||||
describe('evaluateAnswer', () => {
|
||||
it('should evaluate answer definitiveness', async () => {
|
||||
|
||||
@ -1,4 +1,12 @@
|
||||
import { rewriteQuery } from '../query-rewriter';
|
||||
import { MockLLMClient } from './utils/llm-mock';
|
||||
import { TEST_RESPONSES } from './utils/test-config';
|
||||
import * as config from '../../config';
|
||||
|
||||
jest.mock('../../config', () => ({
|
||||
...jest.requireActual('../../config'),
|
||||
llmClient: new MockLLMClient(TEST_RESPONSES.queryRewriter)
|
||||
}));
|
||||
|
||||
describe('rewriteQuery', () => {
|
||||
it('should rewrite search query', async () => {
|
||||
|
||||
16
src/tools/__tests__/utils/llm-mock.ts
Normal file
16
src/tools/__tests__/utils/llm-mock.ts
Normal file
@ -0,0 +1,16 @@
|
||||
import { LLMClient, LLMClientConfig } from '../../../utils/llm-client';
|
||||
|
||||
export class MockLLMClient implements LLMClient {
|
||||
constructor(private mockResponse: string = '{"queries": ["test query"]}') {}
|
||||
|
||||
getGenerativeModel(config: LLMClientConfig) {
|
||||
return {
|
||||
generateContent: async () => ({
|
||||
response: {
|
||||
text: () => this.mockResponse,
|
||||
usageMetadata: { totalTokenCount: 100 }
|
||||
}
|
||||
})
|
||||
};
|
||||
}
|
||||
}
|
||||
6
src/tools/__tests__/utils/test-config.ts
Normal file
6
src/tools/__tests__/utils/test-config.ts
Normal file
@ -0,0 +1,6 @@
|
||||
export const TEST_RESPONSES = {
|
||||
queryRewriter: '{"think": "test thought", "queries": ["test query"]}',
|
||||
evaluator: '{"is_definitive": true, "reasoning": "test reason"}',
|
||||
dedup: '{"think": "test thought", "unique_queries": ["test query"]}',
|
||||
errorAnalyzer: '{"recap": "test recap", "blame": "test blame", "improvement": "test improvement"}'
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user