From 9a8cac54072eeeab9c21fe12998c75898f3d8242 Mon Sep 17 00:00:00 2001 From: Adithya Date: Sat, 17 Oct 2026 15:37:28 +0530 Subject: [PATCH] test(api): cover SearchService limit and offset clamp bounds (#1252) --- apps/api/src/search/search.service.spec.ts | 46 ++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/apps/api/src/search/search.service.spec.ts b/apps/api/src/search/search.service.spec.ts index 30a044ac..d28ec801 100644 --- a/apps/api/src/search/search.service.spec.ts +++ b/apps/api/src/search/search.service.spec.ts @@ -88,6 +88,52 @@ describe('SearchService', () => { expect(prisma.$queryRawUnsafe.mock.calls[1].slice(-2)).toEqual([50, 20]); }); + it.each([ + ['the maximum limit', 50, 50], + ['a limit just over the maximum', 51, 50], + ['an infinite limit', Infinity, 50], + ['the minimum limit', 1, 1], + ['a zero limit, which falls back to the default', 0, 10], + ['a negative limit', -5, 1], + ['a NaN limit, which falls back to the default', NaN, 10], + ['a fractional limit, which is truncated', 2.9, 2], + ])('clamps %s', async (_label, rawLimit, expectedLimit) => { + prisma.$queryRawUnsafe.mockResolvedValue([]); + const service = new SearchService(prisma as never); + + await service.search('usd', rawLimit); + + expect(prisma.$queryRawUnsafe.mock.calls[0].slice(-2)).toEqual([ + expectedLimit, + 0, + ]); + expect(prisma.$queryRawUnsafe.mock.calls[1].slice(-2)).toEqual([ + expectedLimit, + 0, + ]); + }); + + it.each([ + ['a negative offset', -3, 0], + ['a NaN offset', NaN, 0], + ['a fractional offset, which is truncated', 4.7, 4], + ['a large offset, which is not capped', 5000, 5000], + ])('clamps %s', async (_label, rawOffset, expectedOffset) => { + prisma.$queryRawUnsafe.mockResolvedValue([]); + const service = new SearchService(prisma as never); + + await service.search('usd', 10, rawOffset); + + expect(prisma.$queryRawUnsafe.mock.calls[0].slice(-2)).toEqual([ + 10, + expectedOffset, + ]); + expect(prisma.$queryRawUnsafe.mock.calls[1].slice(-2)).toEqual([ + 10, + expectedOffset, + ]); + }); + it('returns empty arrays when no matches are found', async () => { prisma.$queryRawUnsafe.mockResolvedValue([]); const service = new SearchService(prisma as never);