import { plainToInstance } from 'class-transformer'; import { validateSync } from 'class-validator'; import { PaginationQueryDto } from './pagination-query.dto'; import { ListWagonsQueryDto } from '../../modules/wagons/dto/list-wagons-query.dto'; import { normalizePagination } from '../utils/pagination.util'; /** * The page-size ceiling is stated in three places that must agree: `@Max` on * PaginationQueryDto, the same `@Max` repeated on ListWagonsQueryDto (which * doesn't extend it), and `MAX_PAGE_SIZE` in pagination.util. A fourth copy * lives outside this package — `MAX_PAGE_SIZE` in @edr/ui-common's data-table * footer, which is what actually asks for the number. Drift between any of * them shows up as a 400 on the largest rows-per-page option, so pin them. */ const errorsFor = (cls: any, pageSize: unknown) => validateSync(plainToInstance(cls, { pageSize }), { whitelist: false }); describe('page size ceiling', () => { it.each([PaginationQueryDto, ListWagonsQueryDto])('accepts 500 on %p', (cls) => { expect(errorsFor(cls, 500)).toHaveLength(0); }); it.each([PaginationQueryDto, ListWagonsQueryDto])('rejects 501 on %p', (cls) => { expect(errorsFor(cls, 501)).not.toHaveLength(0); }); it('does not truncate 500 in the service-side clamp', () => { expect(normalizePagination({ page: 1, pageSize: 500 }).take).toBe(500); expect(normalizePagination({ page: 1, pageSize: 501 }).take).toBe(500); }); });