Repository navigation
Expand file tree
/
Copy pathbatch-protocol-errors.integration.test.ts
More file actions
196 lines (188 loc) · 6.58 KB
/
Copy pathbatch-protocol-errors.integration.test.ts
File metadata and controls
196 lines (188 loc) · 6.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { BadRequestException } from '@nestjs/common';
import { describe, expect, it, vi } from 'vitest';
import { BatchRunnerService } from '@/modules/proxy-gateway/server/modules/batch/batch-runner.service';
import { BatchService } from '@/modules/proxy-gateway/server/modules/batch/batch.service';
import {
BatchJobError,
type BatchJobRecord,
} from '@/modules/proxy-gateway/server/modules/batch/batch-job.types';
import { toOpenAIBatchObject } from '@/modules/proxy-gateway/server/modules/batch/openai-batch-resource';
import { toAnthropicMessageBatch } from '@/modules/proxy-gateway/server/modules/batch/anthropic-batch-resource';
import { batchTestHeaders, createBatchHttpApp } from '../helpers/batch-http-app';
const bareId = '0'.repeat(24);
const surfaces = [
{
path: '/v1/batches',
id: `batch_${bareId}`,
authBody: {
error: {
code: 'invalid_api_key',
message: 'API key validation failed',
param: null,
type: 'invalid_request_error',
},
},
unknownBody: {
error: { code: null, message: 'Batch request failed', param: null, type: 'server_error' },
},
handlerBody: {
error: { code: null, message: 'handler rejected', param: null, type: 'server_error' },
},
},
{
path: '/v1/messages/batches',
id: `msgbatch_${bareId}`,
authBody: {
type: 'error',
error: { message: 'API key validation failed', type: 'authentication_error' },
},
unknownBody: {
type: 'error',
error: { message: 'File request failed', type: 'api_error' },
},
handlerBody: {
type: 'error',
error: { message: 'handler rejected', type: 'api_error' },
},
},
{
path: '/v1beta/batches',
id: bareId,
authBody: {
error: { code: 401, message: 'API key validation failed', status: 'UNAUTHENTICATED' },
},
unknownBody: {
error: { code: 500, message: 'Batch request failed', status: 'UNKNOWN' },
},
handlerBody: {
error: { code: 500, message: 'handler rejected', status: 'UNKNOWN' },
},
},
] as const;
function createService() {
return new BatchService(new BatchRunnerService({ maxConcurrency: 1 }));
}
describe('Batch protocol errors through the Nest HTTP pipeline', () => {
it.each(surfaces)('preserves guard denials on $path', async ({ path, authBody }) => {
const batches = createService();
const spies = [
vi.spyOn(batches, 'listOpenAI'),
vi.spyOn(batches, 'listAnthropic'),
vi.spyOn(batches, 'listGemini'),
];
const app = await createBatchHttpApp(batches);
for (const headers of [{}, { authorization: 'Bearer incorrect-key' }]) {
const response = await app.inject({ method: 'GET', url: path, headers });
expect({ status: response.statusCode, body: response.json() }).toEqual({
status: 401,
body: authBody,
});
}
for (const spy of spies) {
expect(spy).not.toHaveBeenCalled();
}
});
it.each(surfaces)('preserves non-Error fallbacks on $path', async ({ path, id, unknownBody }) => {
const batches = createService();
vi.spyOn(batches, 'get').mockImplementation(() => {
throw 'unexpected rejection';
});
const app = await createBatchHttpApp(batches);
const response = await app.inject({
method: 'GET',
url: `${path}/${id}`,
headers: batchTestHeaders,
});
expect({ status: response.statusCode, body: response.json() }).toEqual({
status: 500,
body: unknownBody,
});
});
it.each(surfaces)(
'maps handler HTTP exceptions through the existing dialect on $path',
async ({ path, id, handlerBody }) => {
const batches = createService();
vi.spyOn(batches, 'get').mockImplementation(() => {
throw new BadRequestException('handler rejected');
});
const app = await createBatchHttpApp(batches);
const response = await app.inject({
method: 'GET',
url: `${path}/${id}`,
headers: batchTestHeaders,
});
expect({ status: response.statusCode, body: response.json() }).toEqual({
status: 500,
body: handlerBody,
});
},
);
it('maps asynchronous input-file failures before creating an OpenAI batch', async () => {
const batches = createService();
vi.spyOn(batches, 'readOpenAIInput').mockRejectedValue(
BatchJobError.invalid('input file rejected', 'input_file_id'),
);
const create = vi.spyOn(batches, 'create');
const app = await createBatchHttpApp(batches);
const response = await app.inject({
method: 'POST',
url: '/v1/batches',
headers: batchTestHeaders,
payload: {
endpoint: '/v1/chat/completions',
completion_window: '24h',
input_file_id: 'file-synthetic',
},
});
expect({ status: response.statusCode, body: response.json() }).toEqual({
status: 400,
body: {
error: {
code: 'invalid_request',
message: 'input file rejected',
param: 'input_file_id',
type: 'invalid_request_error',
},
},
});
expect(create).not.toHaveBeenCalled();
});
it.each(['openai', 'anthropic'] as const)(
'preserves HTTP 200 for %s create and cancel',
async (dialect) => {
const job: BatchJobRecord = {
id: bareId,
dialect,
endpoint: dialect === 'openai' ? '/v1/chat/completions' : '/v1/messages',
status: 'validating',
requests: [],
createdAtMs: 1_000,
expiresAtMs: 2_000,
};
const batches = createService();
vi.spyOn(batches, 'create').mockReturnValue(job);
vi.spyOn(batches, 'cancel').mockReturnValue(job);
vi.spyOn(batches, 'readOpenAIInput').mockResolvedValue(
JSON.stringify({ custom_id: 'line-1', body: { model: 'synthetic', messages: [] } }),
);
const app = await createBatchHttpApp(batches);
const path = dialect === 'openai' ? '/v1/batches' : '/v1/messages/batches';
const body = dialect === 'openai' ? toOpenAIBatchObject(job) : toAnthropicMessageBatch(job);
const payload =
dialect === 'openai'
? { endpoint: job.endpoint, completion_window: '24h', input_file_id: 'file-synthetic' }
: { requests: [{ custom_id: 'line-1', params: { model: 'synthetic', messages: [] } }] };
for (const request of [{ url: path, payload }, { url: `${path}/${body.id}/cancel` }]) {
const response = await app.inject({
method: 'POST',
headers: batchTestHeaders,
...request,
});
expect({ status: response.statusCode, body: response.json() }).toEqual({
status: 200,
body,
});
}
},
);
});