Files
cfw-gateway/tests/openapi-aggregation.test.ts

110 lines
3.3 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { composeOpenApiDocument } from "../src/openapi";
describe("openapi aggregation", () => {
it("prefixes service paths and rewrites component refs", () => {
const document = composeOpenApiDocument([
{
name: "auth",
mountPath: "/api/auth",
document: {
openapi: "3.1.0",
info: { title: "auth", version: "1.0.0" },
paths: {
"/api/auth/open-api/generate-schema": {
get: {
responses: {
"200": {
description: "ok",
content: {
"application/json": {
schema: { $ref: "#/components/schemas/AuthSchema" },
},
},
},
},
},
},
},
components: {
schemas: {
AuthSchema: {
type: "object",
properties: {
message: { type: "string" },
},
},
},
},
},
},
{
name: "ops",
mountPath: "/api/ops",
document: {
openapi: "3.1.0",
info: { title: "ops", version: "1.0.0" },
paths: {
"/observations": {
get: {
responses: {
"200": {
description: "ok",
content: {
"application/json": {
schema: { $ref: "#/components/schemas/ObservationList" },
},
},
},
},
},
},
},
components: {
schemas: {
ObservationList: {
type: "object",
properties: {
items: {
type: "array",
items: { $ref: "#/components/schemas/Observation" },
},
},
},
Observation: {
type: "object",
properties: {
id: { type: "string" },
},
},
},
},
},
},
]);
expect(document.openapi).toBe("3.1.0");
expect(document.info.title).toBe("cfw-gateway API");
expect(document.paths["/api/auth/open-api/generate-schema"]).toBeTruthy();
expect(document.paths["/api/ops/observations"]).toBeTruthy();
expect(document.components?.schemas?.["auth_AuthSchema"]).toBeTruthy();
expect(document.components?.schemas?.["ops_ObservationList"]).toBeTruthy();
expect(document.components?.schemas?.["ops_Observation"]).toBeTruthy();
expect(
(document.paths["/api/auth/open-api/generate-schema"] as any).get.responses["200"].content[
"application/json"
].schema.$ref,
).toBe("#/components/schemas/auth_AuthSchema");
expect(
(document.paths["/api/ops/observations"] as any).get.responses["200"].content[
"application/json"
].schema.$ref,
).toBe("#/components/schemas/ops_ObservationList");
expect(
(document.components?.schemas?.["ops_ObservationList"] as any).properties.items.items.$ref,
).toBe("#/components/schemas/ops_Observation");
});
});