This commit is contained in:
Bastian Wagner
2026-06-24 10:37:14 +02:00
parent fc61ef5ba9
commit cbf1451255
12 changed files with 608 additions and 28 deletions

View File

@@ -12,6 +12,7 @@ describe('AssistantService', () => {
const originalAgentId = process.env.MISTRAL_AGENT_ID;
let chatLogsRepository: {
create: jest.Mock;
find: jest.Mock;
save: jest.Mock;
};
let listRealtimeService: {
@@ -29,6 +30,7 @@ describe('AssistantService', () => {
global.fetch = jest.fn();
chatLogsRepository = {
create: jest.fn((input) => input),
find: jest.fn(),
save: jest.fn(async (input) => input),
};
listRealtimeService = {
@@ -605,6 +607,15 @@ describe('AssistantService', () => {
messages: [{ role: 'user', content: 'Hallo' }],
}),
).rejects.toThrow(ServiceUnavailableException);
expect(chatLogsRepository.save).toHaveBeenCalledWith(
expect.objectContaining({
userId: 'user-1',
statusCode: null,
responsePayload: null,
assistantContent: null,
errorMessage: 'Mistral API key is not configured.',
}),
);
});
it('fails clearly when the agent id is missing', async () => {
@@ -615,14 +626,62 @@ describe('AssistantService', () => {
messages: [{ role: 'user', content: 'Hallo' }],
}),
).rejects.toThrow(ServiceUnavailableException);
expect(chatLogsRepository.save).toHaveBeenCalledWith(
expect.objectContaining({
userId: 'user-1',
agentId: null,
statusCode: null,
responsePayload: null,
assistantContent: null,
errorMessage: 'Mistral agent id is not configured.',
}),
);
});
it('lists the latest chat logs for the current user', async () => {
chatLogsRepository.find.mockResolvedValue([
{
id: 'log-1',
userId: 'user-1',
provider: 'mistral',
endpoint: 'https://api.mistral.ai/v1/agents/completions',
agentId: 'agent-listify',
statusCode: 502,
durationMs: 123,
requestPayload: { messages: [] },
responsePayload: { message: 'connector failed' },
assistantContent: null,
errorMessage: 'Mistral agent request failed.',
createdAt: new Date('2026-06-24T08:00:00.000Z'),
},
]);
const logs = await service.listChatLogs('user-1');
expect(chatLogsRepository.find).toHaveBeenCalledWith({
where: { userId: 'user-1' },
order: { createdAt: 'DESC' },
take: 50,
});
expect(logs).toEqual([
{
id: 'log-1',
provider: 'mistral',
endpoint: 'https://api.mistral.ai/v1/agents/completions',
agentId: 'agent-listify',
statusCode: 502,
durationMs: 123,
requestPayload: { messages: [] },
responsePayload: { message: 'connector failed' },
assistantContent: null,
errorMessage: 'Mistral agent request failed.',
createdAt: '2026-06-24T08:00:00.000Z',
},
]);
});
});
function mockMistralResponse(
response: object,
ok = true,
status = 200,
): void {
function mockMistralResponse(response: object, ok = true, status = 200): void {
jest.mocked(global.fetch).mockResolvedValue({
ok,
status,