import { expect, test } from "vitest" import { createApiSchema } from "#Source/http/index.ts" import type { Standard } from "#Source/validation/index.ts" const isRecord = (value: unknown): value is Record => { return typeof value === "object" && value !== null } const createSchema = ( validate: (value: unknown) => Standard.Schema.Result, ): Standard.Schema => { return { "~standard": { version: 1 as const, vendor: "mobius-http-test", validate, }, } } const inputQuerySchema = createSchema<{ page: string }>((value) => { if (isRecord(value) && typeof value["page"] === "string") { return { value: { page: value["page"], }, } } return { issues: [{ message: "page is required" }], } }) const inputBodySchema = createSchema<{ name: string }>((value) => { if (isRecord(value) && typeof value["name"] === "string") { return { value: { name: value["name"], }, } } return { issues: [{ message: "name is required" }], } }) const outputErrorSchema = createSchema<{ status: "error" }>((value) => { if ( typeof value === "object" && value !== null && (value as { status?: unknown }).status === "error" ) { return { value: { status: "error", }, } } return { issues: [{ message: "status must be error" }], } }) const outputSuccessSchema = createSchema<{ status: "success" }>((value) => { if ( typeof value === "object" && value !== null && (value as { status?: unknown }).status === "success" ) { return { value: { status: "success", }, } } return { issues: [{ message: "status must be success" }], } }) const apiSchema = createApiSchema({ path: "/users", method: "post", inputQuerySchema, inputBodySchema, outputErrorSchema, outputSuccessSchema, }) test("ApiSchema.getApiSchemaOptions returns the configured schema options", () => { expect(apiSchema.getApiSchemaOptions()).toEqual({ path: "/users", method: "post", inputQuerySchema, inputBodySchema, outputErrorSchema, outputSuccessSchema, }) }) test("ApiSchema.getPath returns the configured path", () => { expect(apiSchema.getPath()).toBe("/users") }) test("ApiSchema.getMethod returns the configured method", () => { expect(apiSchema.getMethod()).toBe("post") }) test("ApiSchema.getInputQuerySchema returns the configured query schema", () => { expect(apiSchema.getInputQuerySchema()).toBe(inputQuerySchema) }) test("ApiSchema.getInputBodySchema returns the configured body schema", () => { expect(apiSchema.getInputBodySchema()).toBe(inputBodySchema) }) test("ApiSchema.getOutputErrorSchema returns the configured error schema", () => { expect(apiSchema.getOutputErrorSchema()).toBe(outputErrorSchema) }) test("ApiSchema.getOutputSuccessSchema returns the configured success schema", () => { expect(apiSchema.getOutputSuccessSchema()).toBe(outputSuccessSchema) })