import { CallHandler, ExecutionContext } from '@nestjs/common';
import { firstValueFrom, of } from 'rxjs';
import { ResponseEnvelopeInterceptor } from './response-envelope.interceptor';

describe('ResponseEnvelopeInterceptor', () => {
  it('wraps data with response status and timestamp', async () => {
    const context = {
      switchToHttp: () => ({ getResponse: () => ({ statusCode: 201 }) }),
    } as unknown as ExecutionContext;
    const next = { handle: () => of({ id: 'one' }) } as CallHandler;
    const value = await firstValueFrom(new ResponseEnvelopeInterceptor().intercept(context, next));
    expect(value).toEqual({
      data: { id: 'one' },
      meta: { statusCode: 201, timestamp: expect.any(String) },
    });
  });

  it('uses HTTP 200 when an adapter does not expose a status', async () => {
    const context = {
      switchToHttp: () => ({ getResponse: () => ({}) }),
    } as unknown as ExecutionContext;
    const value = await firstValueFrom(
      new ResponseEnvelopeInterceptor().intercept(context, { handle: () => of(null) }),
    );
    expect(value).toEqual(
      expect.objectContaining({ meta: expect.objectContaining({ statusCode: 200 }) }),
    );
  });
});
