47 lines
3.2 KiB
TypeScript
47 lines
3.2 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { serviceRoutes } from "../src/routes/services.js";
|
|
import { config } from "../src/config.js";
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
const calls: Array<{ url: string; body: Record<string, unknown>; headers: Headers }> = [];
|
|
globalThis.fetch = (async (input, init) => {
|
|
const body = JSON.parse(String(init?.body ?? "{}")) as Record<string, unknown>;
|
|
calls.push({ url: String(input), body, headers: new Headers(init?.headers) });
|
|
if (body.action === "force_forbidden") {
|
|
return new Response(JSON.stringify({ detail: "Invalid A2A auth token" }), { status: 403, headers: { "content-type": "application/json" } });
|
|
}
|
|
if (body.action === "agent_error") {
|
|
return new Response(JSON.stringify({ task_id: "a2a-task", status: "completed", messages: [{ type: "agent_error", action: "search_failed", data: { code: "job_boards_unavailable" } }] }), { status: 200, headers: { "content-type": "application/json" } });
|
|
}
|
|
return new Response(JSON.stringify({ task_id: "a2a-task", status: "completed", messages: [] }), { status: 200, headers: { "content-type": "application/json" } });
|
|
}) as typeof fetch;
|
|
const app = serviceRoutes({ skipAuth: true });
|
|
try {
|
|
const courseResponse = await app.request("http://backend.test/courses/a2a?missionId=mission-1&missionInstanceId=instance-1&stageId=stage-1&curatorTaskId=task-1", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ action: "record_swipe", params: { video_id: "video-1", anchor_id: "anchor-1" } }) });
|
|
assert.equal(courseResponse.status, 200);
|
|
assert.ok(calls[0]?.url.endsWith("/a2a/tasks"));
|
|
assert.equal(calls[0]?.body.user_id, "user_test");
|
|
const params = calls[0]?.body.params as Record<string, unknown>;
|
|
assert.equal(params.missionId, "mission-1");
|
|
assert.equal(params.missionInstanceId, "instance-1");
|
|
assert.equal(params.stageId, "stage-1");
|
|
assert.equal(params.curatorTaskId, "task-1");
|
|
|
|
calls.length = 0;
|
|
const matchmakingResponse = await app.request("http://backend.test/matchmaking/a2a", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ action: "mark_applied", params: { opportunity_id: "job-1" } }) });
|
|
assert.equal(matchmakingResponse.status, 200);
|
|
assert.ok(calls[0]?.url.endsWith("/a2a/tasks"));
|
|
assert.equal(calls[0]?.headers.get("authorization"), config.a2aAllowedKey ? `Bearer ${config.a2aAllowedKey}` : null);
|
|
|
|
const forbiddenResponse = await app.request("http://backend.test/matchmaking/a2a", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ action: "force_forbidden" }) });
|
|
assert.equal(forbiddenResponse.status, 502);
|
|
assert.match(await forbiddenResponse.text(), /matchmaking service authentication failed/);
|
|
|
|
const agentErrorResponse = await app.request("http://backend.test/matchmaking/a2a", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ action: "agent_error" }) });
|
|
assert.equal(agentErrorResponse.status, 502);
|
|
assert.match(await agentErrorResponse.text(), /could not complete the search/);
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|
|
console.log("course-matchmaking HTTP route tests passed");
|