Files
suaveplan/packages/api-stack/api/api-plugin-user/tests/integration/openapi-doc.test.ts
2026-05-27 23:29:53 +01:00

65 lines
2.3 KiB
TypeScript

import { OpenAPIHono } from "@hono/zod-openapi";
import { createOpenAPIRegistry } from "@suaveplan/api-openapi";
import { createPluginContext } from "@suaveplan/api-plugin";
import type { AuthService } from "@suaveplan/api-plugin-auth";
import { EventBus } from "@suaveplan/events";
import { describe, expect, it } from "@suaveplan/testing/runner";
import { createUserPlugin } from "../../src/plugin/plugin.js";
import type { UserPluginConfig } from "../../src/types/types.js";
const noopLogger = {
debug: () => undefined,
info: () => undefined,
warn: () => undefined,
error: () => undefined,
};
function fakeAuthService(): AuthService {
return {
getBetterAuthInstance: () =>
({ $context: Promise.resolve({}) }) as never,
getCurrentPrincipal: () => null,
getSession: async () => null,
};
}
const okDb = {
query: async () => [],
execute: async () => ({ rowsAffected: 0 }),
};
describe("openapi-doc", () => {
it("served doc lists 3 /users/me routes with Zod-typed schemas", {
timeout: 30_000,
}, async () => {
const app = new OpenAPIHono();
const registry = createOpenAPIRegistry({
info: { title: "user-test", version: "0.1.0" },
});
const plugin = createUserPlugin({ applySchemaOnInit: false });
const services = new Map<string, unknown>();
services.set("app", app);
services.set("openAPIRegistry", registry);
services.set("authService", fakeAuthService());
services.set("db", okDb);
await plugin.onInit?.(
createPluginContext<UserPluginConfig>({
logger: noopLogger,
eventBus: new EventBus(),
signal: new AbortController().signal,
config: plugin.defaults as UserPluginConfig,
services,
})
);
registry.applyTo(app, "/openapi.json");
const res = await app.request("/openapi.json");
const doc = (await res.json()) as {
paths: Record<string, Record<string, unknown>>;
};
const me = doc.paths?.["/users/me"];
expect(me).toBeDefined();
const methods = Object.keys(me as object).sort();
expect(methods).toEqual(["delete", "get", "patch"]);
});
});