session start works

This commit is contained in:
aodulov
2025-10-10 12:48:06 +03:00
parent 556df015e8
commit 3c192b136c
51 changed files with 29002 additions and 46 deletions

View File

@@ -0,0 +1,54 @@
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');
});
});

View File

@@ -0,0 +1,79 @@
import request from 'supertest';
import express from 'express';
import { createServer } from 'http';
import { LLMService } from '../src/services/LLMService';
// Mock the LLMService
jest.mock('../src/services/LLMService');
// Mock the routes
const app = express();
app.use(express.json());
// Mock session storage for testing analyze endpoint
const mockSessions = new Map<string, any>();
mockSessions.set('test-session-id', { /* session data */ });
app.post('/sessions', (req, res) => {
res.status(201).json({ sessionId: 'mock-session-id' });
});
app.post('/sessions/:sessionId/analyze', async (req, res) => {
const { sessionId } = req.params;
if (!mockSessions.has(sessionId)) {
return res.status(404).send('Session not found');
}
// Mock LLMService call
const mockLLMService = new LLMService('mock-api-key');
const analysisResult = await mockLLMService.analyzeDesires(req.body.allDesires);
res.status(202).json({ message: 'Analysis triggered', result: analysisResult });
});
describe('POST /sessions', () => {
it('should create a new session and return a session ID', async () => {
const response = await request(app)
.post('/sessions')
.send();
expect(response.status).toBe(201);
expect(response.body).toHaveProperty('sessionId');
expect(typeof response.body.sessionId).toBe('string');
});
});
describe('POST /sessions/:sessionId/analyze', () => {
it('should trigger analysis for a valid session', async () => {
const mockDesires = [
{ wants: ['item1'], accepts: [], noGoes: [] },
{ wants: ['item2'], accepts: [], noGoes: [] },
];
// Mock the analyzeDesires method to return a predictable result
(LLMService as jest.Mock).mockImplementation(() => ({
analyzeDesires: jest.fn().mockResolvedValue({
"item1": "Concept A",
"item2": "Concept A"
}),
}));
const response = await request(app)
.post('/sessions/test-session-id/analyze')
.send({ allDesires: mockDesires });
expect(response.status).toBe(202);
expect(response.body).toHaveProperty('message', 'Analysis triggered');
expect(response.body).toHaveProperty('result');
expect(response.body.result).toEqual({
"item1": "Concept A",
"item2": "Concept A"
});
});
it('should return 404 if session is not found', async () => {
const response = await request(app)
.post('/sessions/non-existent-session/analyze')
.send({ allDesires: [] });
expect(response.status).toBe(404);
});
});