import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import request from 'supertest';

import { MiddlewareModule } from '../../../../../src/modules/free/essentials/middleware/module';

async function createApplication(): Promise<INestApplication> {
  const moduleRef = await Test.createTestingModule({
    imports: [MiddlewareModule],
  }).compile();

  const app = moduleRef.createNestApplication();
  await app.init();
  return app;
}

describe('MiddlewareModule (Integration)', () => {
  let app: INestApplication;

  beforeAll(async () => {
    app = await createApplication();
  });

  afterAll(async () => {
    await app.close();
  });

  it('exposes metadata endpoint', async () => {
    const server = app.getHttpServer();
    const response = await request(server).get('/middleware');

    expect(response.status).toBe(200);
    expect(response.body).toHaveProperty('module', '{{ rapidkit_vendor_module }}');
    expect(response.body).toHaveProperty('process_time_header');
    expect(response.body).toHaveProperty('service_header');
  });

  it('declares health status endpoint', async () => {
    const server = app.getHttpServer();
    const response = await request(server).get('/middleware/health');

    expect(response.status).toBe(200);
    expect(response.body).toHaveProperty('status');
  });
});
