import { configureHttpBodyParsers, DEFAULT_HTTP_BODY_LIMIT } from './http-body-limit';

jest.mock('express', () => ({
  json: jest.fn(() => 'json-parser'),
  urlencoded: jest.fn(() => 'urlencoded-parser'),
}));

const express = jest.requireMock('express') as {
  json: jest.Mock;
  urlencoded: jest.Mock;
};

describe('configureHttpBodyParsers', () => {
  beforeEach(() => {
    jest.clearAllMocks();
  });

  it('registers JSON and urlencoded parsers with a large AppData-safe limit', () => {
    const app = { use: jest.fn() };

    configureHttpBodyParsers(app as never);

    expect(DEFAULT_HTTP_BODY_LIMIT).toBe('10mb');
    expect(express.json).toHaveBeenCalledWith({ limit: '10mb' });
    expect(express.urlencoded).toHaveBeenCalledWith({ extended: true, limit: '10mb' });
    expect(app.use).toHaveBeenNthCalledWith(1, 'json-parser');
    expect(app.use).toHaveBeenNthCalledWith(2, 'urlencoded-parser');
  });

  it('allows overriding the body limit for deployments with larger documents', () => {
    const app = { use: jest.fn() };

    configureHttpBodyParsers(app as never, '25mb');

    expect(express.json).toHaveBeenCalledWith({ limit: '25mb' });
    expect(express.urlencoded).toHaveBeenCalledWith({ extended: true, limit: '25mb' });
  });
});
