import { describe, expect, it } from 'vitest' import { parseDynamicResize } from './parseDynamicResize.js' const DEFAULT_LIMITS = { maxHeight: 4096, maxPixels: 16_777_216, maxWidth: 4096 } const parse = (query: string) => parseDynamicResize({ limits: DEFAULT_LIMITS, searchParams: new URLSearchParams(query) }) describe('parseDynamicResize', () => { it('should route a request containing only withoutEnlargement, but reject it as invalid', () => { const result = parse('withoutEnlargement=true') expect(result.isRouted).toBe(true) expect(result).toMatchObject({ valid: false }) }) it.each([true, false])( 'should parse a valid withoutEnlargement=%s alongside width', (withoutEnlargement) => { expect(parse(`width=500&withoutEnlargement=${withoutEnlargement}`)).toEqual({ height: undefined, isRouted: true, valid: true, width: 500, withoutEnlargement, }) }, ) it('should reject an invalid withoutEnlargement value', () => { const result = parse('width=500&withoutEnlargement=yes') expect(result).toMatchObject({ isRouted: true, valid: false }) }) it.each([ 'width=500&width=600', 'height=500&height=600', 'width=500&withoutEnlargement=true&withoutEnlargement=false', ])('should reject a repeated parameter: %s', (query) => { expect(parse(query)).toMatchObject({ isRouted: true, valid: false }) }) it.each(['abc', '12.5', '-5', '0', '', '5px', ' 5'])( 'should reject a non-positive-integer width: %s', (value) => { const result = parse(`width=${encodeURIComponent(value)}`) expect(result).toMatchObject({ isRouted: true, valid: false }) }, ) it('should reject an unsafe-integer width without throwing', () => { const result = parse('width=99999999999999999999999999999999') expect(result).toMatchObject({ isRouted: true, valid: false }) }) it('should reject a width exceeding the configured maxWidth', () => { const result = parse('width=5000') expect(result).toMatchObject({ isRouted: true, valid: false }) }) it('should reject a height exceeding the configured maxHeight', () => { const result = parse('height=5000') expect(result).toMatchObject({ isRouted: true, valid: false }) }) it('should accept a width exactly at the configured maxWidth', () => { expect(parse('width=4096')).toMatchObject({ isRouted: true, valid: true }) }) it('should reject a width/height combination whose product exceeds maxPixels even when each dimension is individually within its own limit', () => { const result = parseDynamicResize({ limits: { maxHeight: 4096, maxPixels: 1000, maxWidth: 4096 }, searchParams: new URLSearchParams('width=100&height=100'), }) expect(result).toMatchObject({ isRouted: true, valid: false }) }) it('should accept a width/height combination within the configured maxPixels', () => { const result = parseDynamicResize({ limits: { maxHeight: 4096, maxPixels: 1000, maxWidth: 4096 }, searchParams: new URLSearchParams('width=10&height=10'), }) expect(result).toMatchObject({ height: 10, isRouted: true, valid: true, width: 10 }) }) })