import { expectTypeOf } from "expect-type"; import type { FastifyPluginAsync, FastifyReply, FastifyRequest, RouteOptions, } from "fastify"; import type { FastifyOpenapiGlueOptions, SecurityError } from "./index.js"; import fastifyOpenapiGlue from "./index.js"; // Test plugin type expectTypeOf(fastifyOpenapiGlue).toEqualTypeOf< FastifyPluginAsync >(); // Test minimal options expectTypeOf({ specification: {}, }); expectTypeOf({ specification: "path/to/spec.json", }); // Test all options expectTypeOf({ specification: "path/to/spec.json", serviceHandlers: {}, service: {}, // deprecated but should still work securityHandlers: {}, prefix: "v1", addEmptySchema: true, addCookieSchema: false, }); // Test operation resolver returning handler function const handlerResolver = ( _operationId: string, _method: string, _path: string, ) => { return (_req: FastifyRequest, _res: FastifyReply) => {}; }; expectTypeOf({ specification: {}, operationResolver: handlerResolver, }); // Test operation resolver returning RouteOptions const routeOptionsResolver = ( _operationId: string, _method: string, _path: string, ): RouteOptions => { return { method: "GET", url: "/test", handler: (_req, _res) => {}, }; }; expectTypeOf({ specification: {}, operationResolver: routeOptionsResolver, }); // Test SecurityError interface const securityError: SecurityError = { statusCode: 401, name: "SecurityError", message: "Unauthorized", errors: [], }; expectTypeOf(securityError).toEqualTypeOf(); expectTypeOf(securityError).toExtend(); expectTypeOf(securityError.statusCode).toEqualTypeOf(); expectTypeOf(securityError.name).toEqualTypeOf(); expectTypeOf(securityError.errors).toEqualTypeOf>(); // Test option types expectTypeOf().toEqualTypeOf< object | string >(); expectTypeOf().toEqualTypeOf< object | undefined >(); expectTypeOf().toEqualTypeOf< object | undefined >(); expectTypeOf().toEqualTypeOf< object | undefined >(); expectTypeOf().toEqualTypeOf< string | undefined >(); expectTypeOf().toEqualTypeOf< boolean | undefined >(); expectTypeOf().toEqualTypeOf< boolean | undefined >(); // Test that serviceHandlers and operationResolver are mutually exclusive expectTypeOf({ specification: {}, serviceHandlers: {}, // operationResolver should not be used with serviceHandlers }); expectTypeOf({ specification: {}, operationResolver: handlerResolver, // serviceHandlers should not be used with operationResolver });