import type { EndpointParams } from '@navios/core' import { builder } from '@navios/builder' import { Controller, Endpoint, Module, NaviosApplication, NaviosFactory, } from '@navios/core' import { afterAll, beforeAll, describe, expect, it } from 'bun:test' import supertest from 'supertest' import { z } from 'zod/v4' import type { BunEnvironment } from '../../src/index.mjs' import { defineBunEnvironment } from '../../src/index.mjs' describe('POST variants', () => { let server: NaviosApplication let realServer: string const simple = builder().declareEndpoint({ url: '/simple', method: 'POST', responseSchema: z.object({ message: z.string(), }), }) const withUrlParams = builder().declareEndpoint({ url: '/with-url-params/$id', method: 'POST', responseSchema: z.object({ id: z.string(), }), }) const withRequestAndQueryParams = builder().declareEndpoint({ url: '/with-request-and-query-params', method: 'POST', requestSchema: z.object({ foo: z.string(), }), responseSchema: z.object({ foo: z.string(), bar: z.string(), }), querySchema: z.object({ bar: z.string(), }), }) @Controller() class SomethingController { @Endpoint(simple) async getSomething(_req: EndpointParams) { return { message: 'Hello, world!' } } @Endpoint(withUrlParams) async getWithUrlParams(req: EndpointParams) { return { id: req.urlParams.id as string } } @Endpoint(withRequestAndQueryParams) async getWithQueryParams( req: EndpointParams, ) { return { foo: req.data.foo as string, bar: req.params.bar as string, } } } @Module({ controllers: [SomethingController], }) class SomethingModule {} beforeAll(async () => { server = await NaviosFactory.create(SomethingModule, { adapter: defineBunEnvironment(), }) await server.init() await server.listen({ port: 4000, hostname: 'localhost' }) realServer = server.getServer().url.href }) afterAll(async () => { await server.close() }) it('should return 200', async () => { const response = await supertest(realServer).post('/simple') expect(response.status).toBe(200) expect(response.body.message).toBe('Hello, world!') }) it('should return 200 with url params', async () => { const response = await supertest(realServer).post('/with-url-params/123') expect(response.status).toBe(200) expect(response.body.id).toBe('123') }) it('should return 200 with query params', async () => { const response = await supertest(realServer) .post('/with-request-and-query-params') .send({ foo: 'John', }) .query({ bar: 'Doe', }) expect(response.status).toBe(200) expect(response.body.foo).toBe('John') expect(response.body.bar).toBe('Doe') }) })