feat: Refactor result preparation and add contradiction checks
This commit is contained in:
33
backend/tests/LLMService.refactor.test.ts
Normal file
33
backend/tests/LLMService.refactor.test.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { LLMService } from '../src/services/LLMService';
|
||||
require('dotenv').config();
|
||||
|
||||
describe('LLMService Refactor', () => {
|
||||
let llmService: LLMService;
|
||||
|
||||
beforeAll(() => {
|
||||
const apiKey = process.env.GEMINI_API_KEY;
|
||||
if (!apiKey) {
|
||||
throw new Error('GEMINI_API_KEY is not defined in .env file');
|
||||
}
|
||||
llmService = new LLMService(apiKey);
|
||||
});
|
||||
|
||||
it('should correctly categorize desires based on the rules', async () => {
|
||||
const desireSets = [
|
||||
{ participantId: '1', wants: ['Pizza'], accepts: ['Pasta'], noGoes: ['Salad'] },
|
||||
{ participantId: '2', wants: ['Pizza'], accepts: ['Pasta'], noGoes: ['Tacos'] },
|
||||
];
|
||||
|
||||
const result = await llmService.analyzeDesires(desireSets as any);
|
||||
|
||||
expect(result.goTo).toContain('Pizza');
|
||||
expect(result.alsoGood).toContain('Pasta');
|
||||
expect(result.noGoes).toEqual(expect.arrayContaining(['Salad', 'Tacos']));
|
||||
});
|
||||
|
||||
it('should detect inner contradictions in a desire set', async () => {
|
||||
const desireSet = { wants: ['Ice Cream', 'No desserts'], accepts: [], noGoes: [] };
|
||||
const hasContradictions = await llmService.checkForInnerContradictions(desireSet as any);
|
||||
expect(hasContradictions).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -1,54 +0,0 @@
|
||||
import { GoogleGenerativeAI } from '@google/generative-ai';
|
||||
import { LLMService } from '../src/services/LLMService';
|
||||
|
||||
// Mock the GoogleGenerativeAI class and its methods
|
||||
jest.mock('@google/generative-ai', () => ({
|
||||
GoogleGenerativeAI: jest.fn().mockImplementation(() => ({
|
||||
getGenerativeModel: jest.fn().mockReturnValue({
|
||||
generateContent: jest.fn().mockResolvedValue({
|
||||
response: {
|
||||
text: jest.fn().mockReturnValue(
|
||||
JSON.stringify({
|
||||
"item1": "Concept A",
|
||||
"item2": "Concept A",
|
||||
"item3": "Concept B"
|
||||
})
|
||||
),
|
||||
},
|
||||
}),
|
||||
}),
|
||||
})),
|
||||
}));
|
||||
|
||||
describe('LLMService', () => {
|
||||
let llmService: LLMService;
|
||||
const mockApiKey = 'test-api-key';
|
||||
|
||||
beforeEach(() => {
|
||||
llmService = new LLMService(mockApiKey);
|
||||
});
|
||||
|
||||
it('should call the Gemini API with the correct prompt and return parsed content', async () => {
|
||||
const desires = [
|
||||
{ wants: ['item1'], accepts: [], noGoes: [] },
|
||||
{ wants: ['item2'], accepts: [], noGoes: [] },
|
||||
{ wants: [], accepts: ['item3'], noGoes: [] },
|
||||
];
|
||||
|
||||
const result = await llmService.analyzeDesires(desires);
|
||||
|
||||
expect(GoogleGenerativeAI).toHaveBeenCalledWith(mockApiKey);
|
||||
expect(llmService['model'].generateContent).toHaveBeenCalled();
|
||||
expect(result).toEqual({
|
||||
"item1": "Concept A",
|
||||
"item2": "Concept A",
|
||||
"item3": "Concept B"
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle API errors gracefully', async () => {
|
||||
llmService['model'].generateContent.mockRejectedValueOnce(new Error('API Error'));
|
||||
|
||||
await expect(llmService.analyzeDesires([])).rejects.toThrow('API Error');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user