import { BadRequestException, NotFoundException } from '@nestjs/common'; import { QrService } from './qr.service'; import { PrismaService } from '../prisma/prisma.service'; describe('QrService', () => { let service: QrService; let prisma: { colony: { findUnique: jest.Mock }; apiary: { findUnique: jest.Mock }; queen: { findUnique: jest.Mock }; honeyBatch: { findUnique: jest.Mock }; storageContainer: { findUnique: jest.Mock }; bottlingBatch: { findUnique: jest.Mock }; }; beforeEach(() => { prisma = { colony: { findUnique: jest.fn() }, apiary: { findUnique: jest.fn() }, queen: { findUnique: jest.fn() }, honeyBatch: { findUnique: jest.fn() }, storageContainer: { findUnique: jest.fn() }, bottlingBatch: { findUnique: jest.fn() }, }; service = new QrService(prisma as unknown as PrismaService); }); it('rejects an unsupported entity type', async () => { await expect(service.generateSvg('unknown', 'id-1')).rejects.toBeInstanceOf( BadRequestException, ); }); it('throws NotFoundException when the object does not exist', async () => { prisma.colony.findUnique.mockResolvedValueOnce(null); await expect( service.generateSvg('colonies', 'missing'), ).rejects.toBeInstanceOf(NotFoundException); }); it('generates an SVG QR code for an existing colony', async () => { prisma.colony.findUnique.mockResolvedValueOnce({ id: 'colony-1' }); const svg = await service.generateSvg('colonies', 'colony-1'); expect(svg).toContain(' { prisma.storageContainer.findUnique.mockResolvedValueOnce({ id: 'sc-1' }); const svg = await service.generateSvg('storage-containers', 'sc-1'); expect(svg).toContain(' { prisma.bottlingBatch.findUnique.mockResolvedValueOnce({ lotNumber: 'WH-2026-001', }); const svg = await service.generateSvg('bottling-batches', 'bb-1'); expect(svg).toContain(' { prisma.colony.findUnique.mockResolvedValueOnce({ id: 'colony-1' }); const url = await service.resolveUrl('colonies', 'colony-1'); expect(url).toBe('http://localhost:3000/colonies/colony-1'); }); it('resolveUrl rejects an unsupported entity type', async () => { await expect(service.resolveUrl('unknown', 'id-1')).rejects.toBeInstanceOf( BadRequestException, ); }); it('resolveUrl throws NotFoundException when the object does not exist', async () => { prisma.colony.findUnique.mockResolvedValueOnce(null); await expect( service.resolveUrl('colonies', 'missing'), ).rejects.toBeInstanceOf(NotFoundException); }); });