feat: solution for 'Повторный экзамен #2: Сравнительный обзор 3 сущностей (Tavily)'

This commit is contained in:
2026-06-30 17:19:38 +03:00
parent 6f51bf4b64
commit 29e3e8ba1d
6 changed files with 201 additions and 58 deletions
+48
View File
@@ -0,0 +1,48 @@
const { compareEntities } = require('../src/index');
const axios = require('axios');
jest.mock('axios');
describe('compareEntities', () => {
const mockResponse = {
status: 200,
data: {
results: [
{ title: 'Apple vs Samsung vs Google', content: 'Apple leads in design...' },
],
},
};
beforeEach(() => {
axios.post.mockResolvedValue(mockResponse);
});
afterEach(() => {
jest.clearAllMocks();
});
it('should return data from Tavily API', async () => {
const data = await compareEntities('Apple', 'Samsung', 'Google');
expect(data).toEqual(mockResponse.data);
expect(axios.post).toHaveBeenCalledWith(
'https://api.tavily.com/search',
expect.objectContaining({
query: expect.stringContaining('Apple'),
}),
expect.any(Object)
);
});
it('should throw error if any entity is missing', async () => {
await expect(compareEntities('Apple', '', 'Google')).rejects.toThrow(
'All three entity names must be provided.'
);
});
it('should propagate API errors', async () => {
axios.post.mockRejectedValue(new Error('Network error'));
await expect(compareEntities('Apple', 'Samsung', 'Google')).rejects.toThrow(
'Network error'
);
});
});