59 lines
2.5 KiB
TypeScript
59 lines
2.5 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import { serviceRoutes } from "../src/routes/services.js";
|
|
|
|
const originalFetch = globalThis.fetch;
|
|
const requests: Request[] = [];
|
|
|
|
globalThis.fetch = (async (input, init) => {
|
|
const request = new Request(input, init);
|
|
requests.push(request);
|
|
const url = new URL(request.url);
|
|
const body = url.pathname.endsWith("/drafts")
|
|
? { drafts: [{ session_id: "draft-1" }] }
|
|
: url.pathname.endsWith("/plan")
|
|
? { session_id: "draft-1", status: "draft" }
|
|
: url.pathname.endsWith("/configure/approve")
|
|
? { session_id: "draft-1", status: "configured", approved: true }
|
|
: { deleted: true, session_id: "draft-1" };
|
|
return new Response(JSON.stringify(body), {
|
|
status: 200,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
}) as typeof fetch;
|
|
|
|
const app = serviceRoutes({ skipAuth: true });
|
|
try {
|
|
const drafts = await app.request("http://backend.test/roleplay/drafts?limit=25");
|
|
assert.equal(drafts.status, 200);
|
|
assert.deepEqual(await drafts.json(), { drafts: [{ session_id: "draft-1" }] });
|
|
|
|
const plan = await app.request("http://backend.test/roleplay/plan/draft-1");
|
|
assert.equal(plan.status, 200);
|
|
assert.deepEqual(await plan.json(), { session_id: "draft-1", status: "draft" });
|
|
const approved = await app.request("http://backend.test/roleplay/approve", {
|
|
method: "POST",
|
|
headers: { "content-type": "application/json" },
|
|
body: JSON.stringify({ session_id: "draft-1" }),
|
|
});
|
|
assert.equal(approved.status, 200);
|
|
assert.deepEqual(await approved.json(), { session_id: "draft-1", status: "configured", approved: true });
|
|
|
|
const deleted = await app.request("http://backend.test/roleplay/drafts/draft-1", { method: "DELETE" });
|
|
assert.equal(deleted.status, 200);
|
|
assert.deepEqual(await deleted.json(), { deleted: true, session_id: "draft-1" });
|
|
|
|
assert.equal(requests.length, 4);
|
|
assert.match(requests[0]!.url, /\/api\/v1\/roleplays\/drafts\?user_id=user_test&limit=25$/);
|
|
assert.match(requests[1]!.url, /\/api\/v1\/roleplays\/sessions\/draft-1\/plan$/);
|
|
assert.match(requests[2]!.url, /\/api\/v1\/roleplays\/configure\/approve$/);
|
|
assert.equal(requests[3]!.method, "DELETE");
|
|
assert.match(requests[3]!.url, /\/api\/v1\/roleplays\/drafts\/draft-1$/);
|
|
for (const request of requests) {
|
|
assert.equal(request.headers.get("x-growqr-user"), "user_test");
|
|
assert.match(request.headers.get("authorization") ?? "", /^Bearer /);
|
|
}
|
|
console.log("roleplay draft gateway: all assertions passed");
|
|
} finally {
|
|
globalThis.fetch = originalFetch;
|
|
}
|