66 lines
3.0 KiB
TypeScript
66 lines
3.0 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("/video-usage")
|
|
? { used: 1, limit: 3, remaining: 2 }
|
|
: url.pathname.endsWith("/vip-check")
|
|
? { valid: true }
|
|
: 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" });
|
|
|
|
const usage = await app.request("http://backend.test/roleplay/video-usage");
|
|
assert.equal(usage.status, 200);
|
|
assert.deepEqual(await usage.json(), { used: 1, limit: 3, remaining: 2 });
|
|
const vip = await app.request("http://backend.test/roleplay/vip-check?code=grow26");
|
|
assert.equal(vip.status, 200);
|
|
assert.deepEqual(await vip.json(), { valid: true });
|
|
assert.match(requests.find((request) => request.url.includes("/api/v1/roleplays/vip-check"))?.url ?? "", /code=grow26&user_id=user_test$/);
|
|
assert.match(requests.find((request) => request.url.includes("/api/v1/roleplays/video-usage"))?.url ?? "", /\/api\/v1\/roleplays\/video-usage\?user_id=user_test$/);
|
|
assert.ok(requests.length >= 5);
|
|
for (const request of requests.filter((request) => request.url.includes("/api/v1/"))) {
|
|
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;
|
|
}
|