import { lastValueFrom, of } from 'rxjs'; import type { CallHandler, ExecutionContext } from '@nestjs/common'; import { Paginated } from '../dto/paginated.dto.js'; import { ResponseInterceptor } from './response.interceptor.js'; const run = (body: unknown) => lastValueFrom(new ResponseInterceptor().intercept({} as ExecutionContext, { handle: () => of(body) } as CallHandler)); describe('ResponseInterceptor', () => { it('wraps a body in { success, data }', async () => { expect(await run({ id: 1 })).toEqual({ success: true, data: { id: 1 } }); }); it('turns a page into data plus pagination meta', async () => { const page = new Paginated(['a', 'b'], 5, { page: 2, pageSize: 2 }); expect(await run(page)).toEqual({ success: true, data: ['a', 'b'], meta: { page: 2, pageSize: 2, total: 5, totalPages: 3 }, }); }); it('leaves empty responses empty', async () => { expect(await run(undefined)).toBeUndefined(); }); });