test: update tests to use mock LLM client

Co-Authored-By: Han Xiao <han.xiao@jina.ai>
This commit is contained in:
Devin AI 2025-02-05 09:59:16 +00:00
parent 0823371ecb
commit 3e08ae1c7a
7 changed files with 85 additions and 0 deletions

View 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);
});
});

View File

@ -1,4 +1,12 @@
import { dedupQueries } from '../dedup'; 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', () => { describe('dedupQueries', () => {
it('should remove duplicate queries', async () => { it('should remove duplicate queries', async () => {

View File

@ -1,4 +1,12 @@
import { analyzeSteps } from '../error-analyzer'; 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', () => { describe('analyzeSteps', () => {
it('should analyze error steps', async () => { it('should analyze error steps', async () => {

View File

@ -1,5 +1,13 @@
import { evaluateAnswer } from '../evaluator'; import { evaluateAnswer } from '../evaluator';
import { TokenTracker } from '../../utils/token-tracker'; 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', () => { describe('evaluateAnswer', () => {
it('should evaluate answer definitiveness', async () => { it('should evaluate answer definitiveness', async () => {

View File

@ -1,4 +1,12 @@
import { rewriteQuery } from '../query-rewriter'; 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', () => { describe('rewriteQuery', () => {
it('should rewrite search query', async () => { it('should rewrite search query', async () => {

View 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 }
}
})
};
}
}

View 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"}'
};