import { Test, TestingModule } from '@nestjs/testing'; import { INestApplication } from '@nestjs/common'; import request from 'supertest'; import { App } from 'supertest/types'; import { AppModule } from './../src/app.module'; import { HiboutikService } from './../src/services/hiboutik/hiboutik.service'; import { PrismaService } from './../prisma/prisma.service'; // Boots the real AppModule wiring, without database nor Hiboutik calls describe('AppController (e2e)', () => { let app: INestApplication; const hiboutikService = { startSync: jest.fn() }; // Rate limiting counters (ThrottlerDatabaseStorage): the database answers "not blocked" unless a test says otherwise const notBlocked = { totalHits: 1, timeToExpire: 60, isBlocked: false, timeToBlockExpire: 0 }; const prisma = { $queryRaw: jest.fn(), throttlerRecord: { deleteMany: jest.fn().mockResolvedValue({ count: 0 }) }, brand: { findMany: jest.fn() }, category: { findMany: jest.fn() }, product: { findMany: jest.fn() }, }; beforeEach(async () => { hiboutikService.startSync.mockReset(); prisma.$queryRaw.mockReset().mockResolvedValue([notBlocked]); const moduleFixture: TestingModule = await Test.createTestingModule({ imports: [AppModule], }) .overrideProvider(PrismaService) .useValue(prisma) .overrideProvider(HiboutikService) .useValue(hiboutikService) .compile(); app = moduleFixture.createNestApplication(); await app.init(); }); it('POST /sync starts a synchronization (202)', () => { hiboutikService.startSync.mockResolvedValue(true); return request(app.getHttpServer()).post('/sync').expect(202).expect({ message: 'Hiboutik synchronization started' }); }); it('POST /sync answers 409 when a synchronization is already running', () => { hiboutikService.startSync.mockResolvedValue(false); return request(app.getHttpServer()).post('/sync').expect(409); }); it('POST /sync answers 429 when the shared rate limiting counter is blocked', async () => { prisma.$queryRaw.mockResolvedValue([{ totalHits: 6, timeToExpire: 30, isBlocked: true, timeToBlockExpire: 30 }]); await request(app.getHttpServer()).post('/sync').expect(429).expect('Retry-After', '30'); expect(hiboutikService.startSync).not.toHaveBeenCalled(); }); it('GET /sitemap lists the public pages', async () => { prisma.brand.findMany.mockResolvedValue([{ slug: 'bts' }]); prisma.category.findMany.mockResolvedValue([{ slug: 'albums' }]); prisma.product.findMany.mockResolvedValue([ { slug: 'bts-proof', hiboutikUpdatedAt: new Date('2026-11-05T08:00:00Z') }, { slug: 'bts-wings', hiboutikUpdatedAt: null }, ]); await request(app.getHttpServer()) .get('/sitemap') .expect(200) .expect('Cache-Control', 'public, max-age=600') .expect({ brands: [{ slug: 'bts' }], categories: [{ slug: 'albums' }], products: [ { slug: 'bts-proof', updatedAt: '2026-11-05T08:00:00.000Z' }, { slug: 'bts-wings', updatedAt: null }, ], }); // only public rows with a slug are read; products are dated by Hiboutik, not by the last sync expect(prisma.brand.findMany).toHaveBeenCalledWith(expect.objectContaining({ where: { slug: { not: null }, enabled: true } })); expect(prisma.category.findMany).toHaveBeenCalledWith(expect.objectContaining({ where: { slug: { not: null }, webEnabled: true } })); expect(prisma.product.findMany).toHaveBeenCalledWith( expect.objectContaining({ where: { slug: { not: null }, enabled: true, archived: false }, select: { slug: true, hiboutikUpdatedAt: true }, }), ); }); afterEach(async () => { await app.close(); }); });