()
| 202 | } |
| 203 | |
| 204 | async function startMockUpstream(): Promise<MockUpstream> { |
| 205 | const requests: MockRequest[] = []; |
| 206 | |
| 207 | const server = createServer(async (req: IncomingMessage, res: ServerResponse) => { |
| 208 | const path = new URL(req.url ?? "/", "http://127.0.0.1").pathname; |
| 209 | |
| 210 | if (req.method === "GET" && path === "/mock-assets/tiny.png") { |
| 211 | const data = Buffer.from(TINY_PNG_DATA_URI.split(",")[1]!, "base64"); |
| 212 | res.writeHead(200, { "Content-Type": "image/png", "Content-Length": data.length }); |
| 213 | res.end(data); |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | if (req.method === "GET" && path === "/mock-assets/tiny.mp3") { |
| 218 | res.writeHead(200, { "Content-Type": "audio/mpeg", "Content-Length": TINY_MP3_BYTES.length }); |
| 219 | res.end(TINY_MP3_BYTES); |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | const bodyText = await readRequestText(req); |
| 224 | let parsed: unknown; |
| 225 | try { |
| 226 | parsed = bodyText ? JSON.parse(bodyText) : undefined; |
| 227 | } catch { |
| 228 | parsed = undefined; |
| 229 | } |
| 230 | requests.push({ method: req.method ?? "GET", path, bodyText, json: parsed }); |
| 231 | |
| 232 | if (req.method === "POST" && path === "/v1/images/generations") { |
| 233 | writeJson(res, 200, { |
| 234 | created: Math.floor(Date.now() / 1000), |
| 235 | data: [{ url: TINY_PNG_DATA_URI, revised_prompt: "e2e mock image" }], |
| 236 | }); |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | if (req.method === "POST" && path === "/v1/audio/generations") { |
| 241 | const port = getListeningPort(server); |
| 242 | writeJson(res, 200, { |
| 243 | created: Math.floor(Date.now() / 1000), |
| 244 | model: "minimax/music-2.5+", |
| 245 | data: [ |
| 246 | { |
| 247 | url: `http://127.0.0.1:${port}/mock-assets/tiny.mp3`, |
| 248 | duration_seconds: 1, |
| 249 | }, |
| 250 | ], |
| 251 | }); |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | if (req.method !== "POST" || path !== "/v1/chat/completions") { |
| 256 | writeJson(res, 404, { error: { message: `mock route not found: ${req.method} ${path}` } }); |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | if (!parsed) { |
| 261 | writeJson(res, 400, { error: { message: "Invalid JSON request body" } }); |
no test coverage detected