* Handle requests under `/__aimock/`. Returns `true` if the request was * handled, `false` if the path doesn't match the control prefix.
( req: http.IncomingMessage, res: http.ServerResponse, pathname: string, fixtures: Fixture[], journal: Journal, videoStates: VideoStateMap, openRouterVideoJobs: OpenRouterVideoJobMap, veoVideoJobs: VeoVideoJobMap, grokVideoJobs: GrokVideoJobMap, defaults: HandlerDefaults, )
| 280 | * handled, `false` if the path doesn't match the control prefix. |
| 281 | */ |
| 282 | async function handleControlAPI( |
| 283 | req: http.IncomingMessage, |
| 284 | res: http.ServerResponse, |
| 285 | pathname: string, |
| 286 | fixtures: Fixture[], |
| 287 | journal: Journal, |
| 288 | videoStates: VideoStateMap, |
| 289 | openRouterVideoJobs: OpenRouterVideoJobMap, |
| 290 | veoVideoJobs: VeoVideoJobMap, |
| 291 | grokVideoJobs: GrokVideoJobMap, |
| 292 | defaults: HandlerDefaults, |
| 293 | ): Promise<boolean> { |
| 294 | if (!pathname.startsWith(CONTROL_PREFIX)) return false; |
| 295 | |
| 296 | const subPath = pathname.slice(CONTROL_PREFIX.length); |
| 297 | setCorsHeaders(res); |
| 298 | |
| 299 | // GET /__aimock/health |
| 300 | if (subPath === "/health" && req.method === "GET") { |
| 301 | res.writeHead(200, { "Content-Type": "application/json" }); |
| 302 | res.end(JSON.stringify({ status: "ok" })); |
| 303 | return true; |
| 304 | } |
| 305 | |
| 306 | // GET /__aimock/journal |
| 307 | if (subPath === "/journal" && req.method === "GET") { |
| 308 | res.writeHead(200, { "Content-Type": "application/json" }); |
| 309 | res.end(JSON.stringify(journal.getAll())); |
| 310 | return true; |
| 311 | } |
| 312 | |
| 313 | // POST /__aimock/fixtures — add fixtures dynamically |
| 314 | if (subPath === "/fixtures" && req.method === "POST") { |
| 315 | let raw: string; |
| 316 | try { |
| 317 | raw = await readBody(req); |
| 318 | } catch (err) { |
| 319 | const msg = err instanceof Error ? err.message : String(err); |
| 320 | defaults.logger.error(`POST /__aimock/fixtures: failed to read body: ${msg}`); |
| 321 | res.writeHead(400, { "Content-Type": "application/json" }); |
| 322 | res.end(JSON.stringify({ error: `Failed to read request body: ${msg}` })); |
| 323 | return true; |
| 324 | } |
| 325 | |
| 326 | let parsed: { fixtures?: FixtureFileEntry[] }; |
| 327 | try { |
| 328 | parsed = JSON.parse(raw) as { fixtures?: FixtureFileEntry[] }; |
| 329 | } catch (err) { |
| 330 | const msg = err instanceof Error ? err.message : String(err); |
| 331 | defaults.logger.error(`POST /__aimock/fixtures: invalid JSON: ${msg}`); |
| 332 | res.writeHead(400, { "Content-Type": "application/json" }); |
| 333 | res.end(JSON.stringify({ error: `Invalid JSON: ${msg}` })); |
| 334 | return true; |
| 335 | } |
| 336 | |
| 337 | if (!Array.isArray(parsed.fixtures)) { |
| 338 | res.writeHead(400, { "Content-Type": "application/json" }); |
| 339 | res.end(JSON.stringify({ error: 'Missing or invalid "fixtures" array' })); |
no test coverage detected
searching dependent graphs…