Files
canina/backend/src/smart-advisor/smart-advisor.controller.spec.ts
T
devops ddbf1ca026
Deploy Canina / deploy (push) Successful in 4m1s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 7s
test: comprehensive test suites and quality gates pass across backend, storefront and admin
- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation.
- Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings.
- Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors.
- Fixed unmount lifecycle guards, promise error handling, and component edge cases.
2026-10-13 16:01:00 +00:00

156 lines
4.5 KiB
TypeScript

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>(SmartAdvisorController);
service = module.get<SmartAdvisorService>(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);
});
});
});