import { Test, TestingModule } from "@nestjs/testing"; import { TorobController } from "./torob.controller"; import { PrismaService } from "../prisma/prisma.service"; describe("TorobController", () => { let controller: TorobController; const mockPrismaService = { product: { count: jest.fn(), findMany: jest.fn(), }, }; const originalEnv = process.env; beforeEach(async () => { process.env = { ...originalEnv, FRONTEND_URL: "https://canina.ir" }; const module: TestingModule = await Test.createTestingModule({ controllers: [TorobController], providers: [ { provide: PrismaService, useValue: mockPrismaService, }, ], }).compile(); controller = module.get(TorobController); }); afterEach(() => { jest.clearAllMocks(); process.env = originalEnv; }); it("should be defined", () => { expect(controller).toBeDefined(); }); describe("getTorobProducts", () => { it("should return formatted torob products with default pagination", async () => { const mockProducts = [ { id: "prod-1", artNo: "1001", slug: "canina-gag-oil", nameFa: "روغن کانینا جی ای جی", nameEn: "Canina GAG Oil", packageSize: 250, unit: "میلی‌لیتر", suitableFor: "سگ و گربه", stockStatus: "IN_STOCK", stock: 15, priceValue: 750000, imageUrl: "https://api.canina.ir/uploads/products/gag.jpg", category: { name: "مفاصل و استخوان" }, categorySlug: "joints", noIndex: false, }, ]; mockPrismaService.product.count.mockResolvedValue(1); mockPrismaService.product.findMany.mockResolvedValue(mockProducts); const result = await controller.getTorobProducts(); expect(mockPrismaService.product.count).toHaveBeenCalledWith({ where: { noIndex: false, stockStatus: { not: "DISCONTINUED" }, }, }); expect(mockPrismaService.product.findMany).toHaveBeenCalledWith({ where: { noIndex: false, stockStatus: { not: "DISCONTINUED" }, }, skip: 0, take: 100, include: { category: { select: { name: true } }, }, orderBy: { createdAt: "desc" }, }); expect(result.count).toBe(1); expect(result.max_pages).toBe(1); expect(result.results).toHaveLength(1); const item = result.results[0]; expect(item.product_id).toBe("1001"); expect(item.page_unique_code).toBe("1001"); expect(item.page_url).toBe("https://canina.ir/shop/canina-gag-oil"); expect(item.price).toBe(750000); expect(item.old_price).toBe(750000); expect(item.availability).toBe("instock"); expect(item.category_name).toBe("مفاصل و استخوان"); expect(item.image_link).toBe( "https://canina.ir/api/uploads/products/gag.jpg", ); expect(item.spec["گونه هدف"]).toBe("سگ و گربه"); expect(item.spec["کد محصول (Art.No)"]).toBe("1001"); expect(item.spec["دسته دارویی و درمانی"]).toBe("مفاصل و استخوان"); expect(item.spec["حجم / وزن / تعداد"]).toBe("250 میلی‌لیتر"); }); it("should handle custom page and page_size within clamped bounds", async () => { mockPrismaService.product.count.mockResolvedValue(150); mockPrismaService.product.findMany.mockResolvedValue([]); const result = await controller.getTorobProducts("2", "50"); expect(mockPrismaService.product.findMany).toHaveBeenCalledWith( expect.objectContaining({ skip: 50, take: 50, }), ); expect(result.count).toBe(150); expect(result.max_pages).toBe(3); }); it("should clamp invalid pagination inputs safely", async () => { mockPrismaService.product.count.mockResolvedValue(0); mockPrismaService.product.findMany.mockResolvedValue([]); const result = await controller.getTorobProducts("-5", "999"); // page Math.max(1, -5) -> 1 // pageSize Math.min(100, Math.max(10, 999)) -> 100 expect(mockPrismaService.product.findMany).toHaveBeenCalledWith( expect.objectContaining({ skip: 0, take: 100, }), ); expect(result.max_pages).toBe(1); }); it.each([['garbage', 'garbage', 100], ['2', 'garbage', 100], ['garbage', '20', 20]])( "defaults nonnumeric pagination safely (%s, %s)", async (page, size, take) => { mockPrismaService.product.count.mockResolvedValue(0); mockPrismaService.product.findMany.mockResolvedValue([]); const result = await controller.getTorobProducts(page, size); expect(mockPrismaService.product.findMany).toHaveBeenCalledWith( expect.objectContaining({ skip: page === '2' ? 100 : 0, take }), ); expect(result.max_pages).toBe(1); }, ); it("should correctly mark out-of-stock products", async () => { const mockOutOfStock = [ { id: "prod-2", artNo: "1002", slug: "out-stock-item", nameFa: "محصول ناموجود", nameEn: "Out of Stock Item", packageSize: 0, unit: null, suitableFor: null, stockStatus: "OUT_OF_STOCK", stock: 0, priceValue: 0, imageUrl: null, ogImage: null, category: null, categorySlug: null, noIndex: false, }, ]; mockPrismaService.product.count.mockResolvedValue(1); mockPrismaService.product.findMany.mockResolvedValue(mockOutOfStock); const result = await controller.getTorobProducts(); expect(result.results[0].availability).toBe("outofstock"); expect(result.results[0].image_link).toBe(""); expect(result.results[0].category_name).toBe("مکمل حیوانات خانگی"); }); it("should handle full URL in images and normalize relative paths", async () => { const mockProductWithExternalImage = [ { id: "prod-3", artNo: null, slug: "external-img-item", nameFa: "محصول با عکس خارجی", nameEn: null, stockStatus: "in_stock", stock: 5, priceValue: "300000", ogImage: "https://external-cdn.com/images/prod.png", imageUrl: null, category: null, categorySlug: "vitamins", }, ]; mockPrismaService.product.count.mockResolvedValue(1); mockPrismaService.product.findMany.mockResolvedValue( mockProductWithExternalImage, ); const result = await controller.getTorobProducts(); expect(result.results[0].image_link).toBe( "https://external-cdn.com/images/prod.png", ); expect(result.results[0].product_id).toBe("prod-3"); expect(result.results[0].category_name).toBe("vitamins"); }); it("should format pet specifications correctly for 'هر دو' and specific pets", async () => { const mockProducts = [ { id: "p1", artNo: "111", slug: "p1", nameFa: "مکمل هر دو", nameEn: "Both Pet", suitableFor: "هر دو", stockStatus: "INSTOCK", stock: 10, }, { id: "p2", artNo: "222", slug: "p2", nameFa: "مکمل سگ", nameEn: "Dog Only", suitableFor: "سگ", stockStatus: "INSTOCK", stock: 10, }, ]; mockPrismaService.product.count.mockResolvedValue(2); mockPrismaService.product.findMany.mockResolvedValue(mockProducts); const result = await controller.getTorobProducts(); expect(result.results[0].spec["گونه هدف"]).toBe("سگ و گربه"); expect(result.results[1].spec["گونه هدف"]).toBe("سگ"); }); }); });