import { ExecutionContext } from '@nestjs/common';

jest.mock('@aurorajs.dev/core-back', () => ({
  Api: { getRequestFromExecutionContext: jest.fn() },
}));

import { Api } from '@aurorajs.dev/core-back';
import { GqlThrottlerGuard } from './gql-throttler.guard';

type RequestResponse = { req: Record<string, unknown>; res: Record<string, unknown> };

describe('GqlThrottlerGuard', () => {
  afterEach(() => jest.clearAllMocks());

  // getRequestResponse is protected; instantiate without the ThrottlerGuard constructor deps
  // (the override does not touch them) and access the method through a narrowed cast.
  const callGetRequestResponse = (context: ExecutionContext): RequestResponse => {
    const guard = Object.create(GqlThrottlerGuard.prototype) as {
      getRequestResponse: (c: ExecutionContext) => RequestResponse;
    };
    return guard.getRequestResponse(context);
  };

  it('resolves { req, res } from the dual-context request via Api.getRequestFromExecutionContext', () => {
    const res = { setHeader: jest.fn() };
    const req = { ip: '203.0.113.7', res };
    (Api.getRequestFromExecutionContext as jest.Mock).mockReturnValue(req);

    const result = callGetRequestResponse({} as ExecutionContext);

    expect(Api.getRequestFromExecutionContext).toHaveBeenCalledTimes(1);
    expect(result.req).toBe(req);
    expect(result.res).toBe(res);
  });
});
