adding data collection
This commit is contained in:
@@ -87,6 +87,58 @@ describe('proxy', () => {
|
||||
expect(fetchImpl.mock.calls[0][1].headers.accept).toBe('text/event-stream');
|
||||
});
|
||||
|
||||
it('collects the request, chat history, and complete streamed response', async () => {
|
||||
const stream = 'data: {"choices":[{"delta":{"reasoning_content":"Think. ","content":"Hi"}}]}\n\ndata: {"choices":[{"delta":{},"finish_reason":"stop"}]}\n\ndata: [DONE]\n\n';
|
||||
const fetchImpl = vi.fn().mockResolvedValue(response(stream, {
|
||||
headers: { 'content-type': 'text/event-stream' },
|
||||
}));
|
||||
const requestLogger = { log: vi.fn() };
|
||||
const app = createProxyApp({ keys: ['key'], fetchImpl, requestLogger });
|
||||
const body = { model: 'm', messages: [{ role: 'user', content: 'Hi' }], stream: true };
|
||||
|
||||
await request(app)
|
||||
.post('/oai/v1/chat/completions?source=test')
|
||||
.set('X-Real-IP', '198.51.100.10')
|
||||
.send(body)
|
||||
.expect(200);
|
||||
|
||||
expect(requestLogger.log).toHaveBeenCalledTimes(1);
|
||||
expect(requestLogger.log.mock.calls[0][0]).toMatchObject({
|
||||
method: 'POST',
|
||||
endpoint: '/oai/v1/chat/completions?source=test',
|
||||
requestBody: body,
|
||||
responseBody: stream,
|
||||
responseStatus: 200,
|
||||
model: 'm',
|
||||
clientIp: '198.51.100.10',
|
||||
stream: true,
|
||||
trainingData: { messages: [
|
||||
{ role: 'user', content: 'Hi' },
|
||||
{ role: 'assistant', content: 'Hi', reasoning_content: 'Think. ' },
|
||||
] },
|
||||
});
|
||||
expect(requestLogger.log.mock.calls[0][0].requestHeaders['content-type']).toMatch('application/json');
|
||||
expect(requestLogger.log.mock.calls[0][0].responseHeaders['content-type']).toMatch('text/event-stream');
|
||||
expect(requestLogger.log.mock.calls[0][0].durationMs).toBeGreaterThanOrEqual(0);
|
||||
expect(requestLogger.log.mock.calls[0][0].trainingData.metadata).toMatchObject({
|
||||
schema_version: 1, api: 'openai_chat_completions', model: 'm', stream: true,
|
||||
tools: [], response_status: 200, response: { finish_reason: 'stop' },
|
||||
});
|
||||
});
|
||||
|
||||
it('collects error and not-found responses too', async () => {
|
||||
const requestLogger = { log: vi.fn() };
|
||||
const app = createProxyApp({ keys: ['key'], requestLogger });
|
||||
|
||||
await request(app).get('/missing').expect(404);
|
||||
|
||||
expect(requestLogger.log).toHaveBeenCalledOnce();
|
||||
expect(requestLogger.log.mock.calls[0][0]).toMatchObject({
|
||||
method: 'GET', endpoint: '/missing', requestBody: null, responseStatus: 404,
|
||||
});
|
||||
expect(requestLogger.log.mock.calls[0][0].responseBody).toContain('Cannot GET /missing');
|
||||
});
|
||||
|
||||
it('translates Responses requests and returns a Responses object', async () => {
|
||||
const fetchImpl = vi.fn().mockResolvedValue(response(JSON.stringify({
|
||||
id: 'chatcmpl-response', model: 'm',
|
||||
|
||||
Reference in New Issue
Block a user