mpc logging
This commit is contained in:
@@ -5,13 +5,21 @@ describe('AssistantService', () => {
|
||||
const originalFetch = global.fetch;
|
||||
const originalApiKey = process.env.MISTRAL_API_KEY;
|
||||
const originalAgentId = process.env.MISTRAL_AGENT_ID;
|
||||
let chatLogsRepository: {
|
||||
create: jest.Mock;
|
||||
save: jest.Mock;
|
||||
};
|
||||
let service: AssistantService;
|
||||
|
||||
beforeEach(() => {
|
||||
process.env.MISTRAL_API_KEY = 'test-key';
|
||||
process.env.MISTRAL_AGENT_ID = 'agent-listify';
|
||||
global.fetch = jest.fn();
|
||||
service = new AssistantService();
|
||||
chatLogsRepository = {
|
||||
create: jest.fn((input) => input),
|
||||
save: jest.fn(async (input) => input),
|
||||
};
|
||||
service = new AssistantService(chatLogsRepository as never);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -21,7 +29,7 @@ describe('AssistantService', () => {
|
||||
});
|
||||
|
||||
it('forwards messages to the configured Mistral agent', async () => {
|
||||
mockMistralResponse({
|
||||
const providerResponse = {
|
||||
choices: [
|
||||
{
|
||||
message: {
|
||||
@@ -29,7 +37,12 @@ describe('AssistantService', () => {
|
||||
},
|
||||
},
|
||||
],
|
||||
});
|
||||
usage: {
|
||||
prompt_tokens: 12,
|
||||
completion_tokens: 8,
|
||||
},
|
||||
};
|
||||
mockMistralResponse(providerResponse);
|
||||
|
||||
const result = await service.chat('user-1', {
|
||||
messages: [
|
||||
@@ -51,6 +64,13 @@ describe('AssistantService', () => {
|
||||
messages: [
|
||||
{ role: 'assistant', content: 'Hallo' },
|
||||
{ role: 'user', content: 'Erstelle eine Liste' },
|
||||
{ role: 'system', content: 'benutze immer den listify connector' },
|
||||
],
|
||||
tools: [
|
||||
{
|
||||
type: 'connector',
|
||||
connector_id: 'listify',
|
||||
},
|
||||
],
|
||||
stream: false,
|
||||
response_format: {
|
||||
@@ -66,6 +86,46 @@ describe('AssistantService', () => {
|
||||
},
|
||||
actions: [],
|
||||
});
|
||||
expect(chatLogsRepository.save).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
userId: 'user-1',
|
||||
provider: 'mistral',
|
||||
endpoint: 'https://api.mistral.ai/v1/agents/completions',
|
||||
agentId: 'agent-listify',
|
||||
statusCode: 200,
|
||||
requestPayload: expect.objectContaining({
|
||||
agent_id: 'agent-listify',
|
||||
tools: [{ type: 'connector', connector_id: 'listify' }],
|
||||
}),
|
||||
responsePayload: providerResponse,
|
||||
assistantContent: 'Ich habe den Listify-Connector verwendet.',
|
||||
errorMessage: null,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('logs full failed provider responses before throwing', async () => {
|
||||
const providerResponse = {
|
||||
message: 'connector failed',
|
||||
details: { connector_id: 'listify' },
|
||||
};
|
||||
mockMistralResponse(providerResponse, false, 502);
|
||||
|
||||
await expect(
|
||||
service.chat('user-1', {
|
||||
messages: [{ role: 'user', content: 'Hallo' }],
|
||||
}),
|
||||
).rejects.toThrow(ServiceUnavailableException);
|
||||
|
||||
expect(chatLogsRepository.save).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
userId: 'user-1',
|
||||
statusCode: 502,
|
||||
responsePayload: providerResponse,
|
||||
assistantContent: null,
|
||||
errorMessage: 'Mistral agent request failed.',
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('fails clearly when the api key is missing', async () => {
|
||||
@@ -89,9 +149,14 @@ describe('AssistantService', () => {
|
||||
});
|
||||
});
|
||||
|
||||
function mockMistralResponse(response: object): void {
|
||||
function mockMistralResponse(
|
||||
response: object,
|
||||
ok = true,
|
||||
status = 200,
|
||||
): void {
|
||||
jest.mocked(global.fetch).mockResolvedValue({
|
||||
ok: true,
|
||||
json: async () => response,
|
||||
ok,
|
||||
status,
|
||||
text: async () => JSON.stringify(response),
|
||||
} as Response);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user