import { AD_DIRECTION_ENCODER, AD_MESSAGE_PUBLISHER, AD_ROUTE_PROVIDER, } from '@modules/ad/ad.di-tokens'; import { AdMapper } from '@modules/ad/ad.mapper'; import { AdEntity } from '@modules/ad/core/domain/ad.entity'; import { Frequency } from '@modules/ad/core/domain/ad.types'; import { AdRepository } from '@modules/ad/infrastructure/ad.repository'; import { PrismaService } from '@modules/ad/infrastructure/prisma.service'; import { DirectionEncoderPort } from '@modules/geography/core/application/ports/direction-encoder.port'; import { EventEmitterModule } from '@nestjs/event-emitter'; import { Test, TestingModule } from '@nestjs/testing'; import { bareMockGeorouter } from '../georouter.mock'; const mockMessagePublisher = { publish: jest.fn().mockImplementation(), }; const mockDirectionEncoder: DirectionEncoderPort = { encode: jest.fn(), decode: jest .fn() .mockImplementationOnce(() => [ { lon: 6.1765102, lat: 48.689445, }, { lon: 2.3522, lat: 48.8566, }, ]) .mockImplementationOnce(() => [ { lon: 6.1765109, lat: 48.689455, }, { lon: 2.3598, lat: 48.8589, }, ]) .mockImplementationOnce(() => [ { lon: 6.1765102, lat: 48.689445, }, { lon: 2.3522, lat: 48.8566, }, ]) .mockImplementationOnce(() => [ { lon: 6.1765102, lat: 48.689445, }, { lon: 2.3522, lat: 48.8566, }, ]) .mockImplementationOnce(() => [ { lon: 6.1765102, lat: 48.689445, }, { lon: 2.3522, lat: 48.8566, }, ]), }; const mockPrismaService = { $queryRawUnsafe: jest .fn() .mockImplementationOnce(() => { return [ { uuid: 'cc260669-1c6d-441f-80a5-19cd59afb777', driver: true, passenger: true, frequency: Frequency.PUNCTUAL, fromDate: new Date('2023-06-21'), toDate: new Date('2023-06-21'), seatsProposed: 3, seatsRequested: 1, strict: false, driverDistance: 350000, driverDuration: 14400, passengerDistance: 350000, passengerDuration: 14400, fwdAzimuth: 273, backAzimuth: 93, createdAt: new Date('2023-06-20T17:05:00Z'), updatedAt: new Date('2023-06-20T17:05:00Z'), waypoints: 'LINESTRING(6.1765102 48.689445,2.3522 48.8566)', scheduleItemUuid: 'b6bfac1f-e62e-4622-9641-a3475e15fc00', day: 3, time: new Date('2023-06-21T07:05:00Z'), margin: 900, scheduleItemCreatedAt: new Date('2023-06-20T17:05:00Z'), scheduleItemUpdatedAt: new Date('2023-06-20T17:05:00Z'), }, { uuid: '84af18ff-8779-4cac-9651-1ed5ab0713c4', driver: true, passenger: false, frequency: Frequency.PUNCTUAL, fromDate: new Date('2023-06-21'), toDate: new Date('2023-06-21'), seatsProposed: 3, seatsRequested: 1, strict: false, driverDistance: 349000, driverDuration: 14300, passengerDistance: 350000, passengerDuration: 14400, fwdAzimuth: 273, backAzimuth: 93, createdAt: new Date('2023-06-18T14:16:10Z'), updatedAt: new Date('2023-06-18T14:16:10Z'), waypoints: 'LINESTRING(6.1765109 48.689455,2.3598 48.8589)', scheduleItemUuid: '01524541-2044-49dc-8be6-1a3ccdc653b0', day: 3, time: new Date('2023-06-21T07:14:00Z'), margin: 900, scheduleItemCreatedAt: new Date('2023-06-18T14:16:10Z'), scheduleItemUpdatedAt: new Date('2023-06-18T14:16:10Z'), }, ]; }) .mockImplementationOnce(() => { return [ { uuid: 'cc260669-1c6d-441f-80a5-19cd59afb777', driver: true, passenger: true, frequency: Frequency.RECURRENT, fromDate: new Date('2023-06-21'), toDate: new Date('2024-06-21'), seatsProposed: 3, seatsRequested: 1, strict: false, driverDistance: 350000, driverDuration: 14400, passengerDistance: 350000, passengerDuration: 14400, fwdAzimuth: 273, backAzimuth: 93, createdAt: new Date('2023-06-20T17:05:00Z'), updatedAt: new Date('2023-06-20T17:05:00Z'), waypoints: 'LINESTRING(6.1765102 48.689445,2.3522 48.8566)', scheduleItemUuid: '1387b34f-8ab1-46e0-8d0f-803af0f40f28', day: 3, time: new Date('2023-06-21T07:05:00Z'), margin: 900, scheduleItemCreatedAt: new Date('2023-06-20T17:05:00Z'), scheduleItemUpdatedAt: new Date('2023-06-20T17:05:00Z'), }, { uuid: 'cc260669-1c6d-441f-80a5-19cd59afb777', driver: true, passenger: true, frequency: Frequency.RECURRENT, fromDate: new Date('2023-06-21'), toDate: new Date('2024-06-21'), seatsProposed: 3, seatsRequested: 1, strict: false, driverDistance: 350000, driverDuration: 14400, passengerDistance: 350000, passengerDuration: 14400, fwdAzimuth: 273, backAzimuth: 93, createdAt: new Date('2023-06-20T17:05:00Z'), updatedAt: new Date('2023-06-20T17:05:00Z'), waypoints: 'LINESTRING(6.1765102 48.689445,2.3522 48.8566)', scheduleItemUuid: '1fa88104-c50b-4f10-b8ce-389df765f3a6', day: 4, time: new Date('2023-06-21T07:15:00Z'), margin: 900, scheduleItemCreatedAt: new Date('2023-06-20T17:05:00Z'), scheduleItemUpdatedAt: new Date('2023-06-20T17:05:00Z'), }, { uuid: 'cc260669-1c6d-441f-80a5-19cd59afb777', driver: true, passenger: true, frequency: Frequency.RECURRENT, fromDate: new Date('2023-06-21'), toDate: new Date('2024-06-21'), seatsProposed: 3, seatsRequested: 1, strict: false, driverDistance: 350000, driverDuration: 14400, passengerDistance: 350000, passengerDuration: 14400, fwdAzimuth: 273, backAzimuth: 93, createdAt: new Date('2023-06-20T17:05:00Z'), updatedAt: new Date('2023-06-20T17:05:00Z'), waypoints: 'LINESTRING(6.1765102 48.689445,2.3522 48.8566)', scheduleItemUuid: '760bb1bb-256b-4e79-9d82-6d13011118f1', day: 5, time: new Date('2023-06-21T07:16:00Z'), margin: 900, scheduleItemCreatedAt: new Date('2023-06-20T17:05:00Z'), scheduleItemUpdatedAt: new Date('2023-06-20T17:05:00Z'), }, ]; }) .mockImplementationOnce(() => { return []; }), }; describe('Ad repository', () => { let adRepository: AdRepository; beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ imports: [EventEmitterModule.forRoot()], providers: [ AdMapper, AdRepository, { provide: AD_DIRECTION_ENCODER, useValue: mockDirectionEncoder, }, { provide: AD_ROUTE_PROVIDER, useValue: bareMockGeorouter, }, { provide: AD_MESSAGE_PUBLISHER, useValue: mockMessagePublisher, }, { provide: PrismaService, useValue: mockPrismaService, }, ], }).compile(); adRepository = module.get(AdRepository); }); it('should be defined', () => { expect(adRepository).toBeDefined(); }); it('should get candidates if query returns punctual Ads', async () => { const candidates: AdEntity[] = await adRepository.getCandidateAds( 'somePunctualQueryString', ); expect(candidates.length).toBe(2); }); it('should get candidates if query returns recurrent Ads', async () => { const candidates: AdEntity[] = await adRepository.getCandidateAds( 'someRecurrentQueryString', ); expect(candidates.length).toBe(1); expect(candidates[0].getProps().schedule.length).toBe(3); }); it('should return an empty array of candidates if query does not return Ads', async () => { const candidates: AdEntity[] = await adRepository.getCandidateAds('someQueryString'); expect(candidates.length).toBe(0); }); });