import { Test, TestingModule } from "@nestjs/testing"; import { SmartAdvisorController } from "./smart-advisor.controller"; import { SmartAdvisorService } from "./smart-advisor.service"; import { Prisma } from "@prisma/client"; describe("SmartAdvisorController", () => { let controller: SmartAdvisorController; let service: SmartAdvisorService; const mockSmartAdvisorService = { findAll: jest.fn(), create: jest.fn(), update: jest.fn(), remove: jest.fn(), }; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ controllers: [SmartAdvisorController], providers: [ { provide: SmartAdvisorService, useValue: mockSmartAdvisorService, }, ], }).compile(); controller = module.get(SmartAdvisorController); service = module.get(SmartAdvisorService); }); afterEach(() => { jest.clearAllMocks(); }); it("should be defined", () => { expect(controller).toBeDefined(); expect(service).toBeDefined(); }); describe("findAll", () => { it("should return all smart advisor rules", async () => { const mockRules = [ { id: "rule-1", petType: "DOG", ageGroup: "SENIOR", healthIssue: "JOINTS", reason: "Canhydrox GAG for senior dogs joint health", productId: "prod-gag-1", product: { id: "prod-gag-1", name: "Canhydrox GAG" }, createdAt: new Date(), updatedAt: new Date(), }, { id: "rule-2", petType: "CAT", ageGroup: "ADULT", healthIssue: "SKIN_COAT", reason: "Cat Mineral tabs for glossy coat and strong bones", productId: "prod-cat-min", product: { id: "prod-cat-min", name: "Cat Mineral" }, createdAt: new Date(), updatedAt: new Date(), }, ]; mockSmartAdvisorService.findAll.mockResolvedValue(mockRules); const result = await controller.findAll(); expect(service.findAll).toHaveBeenCalledTimes(1); expect(result).toEqual(mockRules); }); }); describe("create", () => { it("should create a new advisor rule via service.create", async () => { const createDto: Prisma.SmartAdvisorRuleCreateInput = { petType: "DOG", ageGroup: "PUPPY", healthIssue: "GROWTH_BONES", reason: "Welpenkalk for balanced skeletal growth", product: { connect: { id: "prod-welpenkalk" }, }, } as any; const createdRule = { id: "rule-3", petType: "DOG", ageGroup: "PUPPY", healthIssue: "GROWTH_BONES", reason: "Welpenkalk for balanced skeletal growth", productId: "prod-welpenkalk", product: { id: "prod-welpenkalk", name: "Welpenkalk" }, createdAt: new Date(), updatedAt: new Date(), }; mockSmartAdvisorService.create.mockResolvedValue(createdRule); const result = await controller.create(createDto); expect(service.create).toHaveBeenCalledWith(createDto); expect(result).toEqual(createdRule); }); }); describe("update", () => { it("should update an existing rule via service.update", async () => { const ruleId = "rule-1"; const updateDto: Prisma.SmartAdvisorRuleUpdateInput = { reason: "Updated dosage: 2 tablets per 10kg body weight", }; const updatedRule = { id: ruleId, petType: "DOG", ageGroup: "SENIOR", healthIssue: "JOINTS", reason: "Updated dosage: 2 tablets per 10kg body weight", productId: "prod-gag-1", product: { id: "prod-gag-1", name: "Canhydrox GAG" }, createdAt: new Date(), updatedAt: new Date(), }; mockSmartAdvisorService.update.mockResolvedValue(updatedRule); const result = await controller.update(ruleId, updateDto); expect(service.update).toHaveBeenCalledWith(ruleId, updateDto); expect(result).toEqual(updatedRule); }); }); describe("remove", () => { it("should delete an advisor rule by id via service.remove", async () => { const ruleId = "rule-delete-1"; const deletedRule = { id: ruleId, petType: "CAT", healthIssue: "DIGESTION", }; mockSmartAdvisorService.remove.mockResolvedValue(deletedRule); const result = await controller.remove(ruleId); expect(service.remove).toHaveBeenCalledWith(ruleId); expect(result).toEqual(deletedRule); }); }); });