- 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.
110 lines
3.0 KiB
TypeScript
110 lines
3.0 KiB
TypeScript
import { Test, TestingModule } from "@nestjs/testing";
|
|
import { SmsService } from "./sms.service";
|
|
import { PrismaService } from "../../prisma/prisma.service";
|
|
|
|
describe("SmsService", () => {
|
|
let service: SmsService;
|
|
|
|
const mockPrisma = {
|
|
setting: {
|
|
findFirst: jest.fn(),
|
|
},
|
|
smsLog: {
|
|
create: jest.fn(),
|
|
findMany: jest.fn(),
|
|
count: jest.fn(),
|
|
},
|
|
};
|
|
|
|
beforeEach(async () => {
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
providers: [SmsService, { provide: PrismaService, useValue: mockPrisma }],
|
|
}).compile();
|
|
|
|
service = module.get<SmsService>(SmsService);
|
|
jest.clearAllMocks();
|
|
});
|
|
|
|
it("should be defined", () => {
|
|
expect(service).toBeDefined();
|
|
});
|
|
|
|
describe("getSmsConfig", () => {
|
|
it("should return configuration from database when available", async () => {
|
|
mockPrisma.setting.findFirst.mockResolvedValue({
|
|
category: "sms",
|
|
value: {
|
|
enabled: true,
|
|
username: "custom_user",
|
|
otpBodyId: 99999,
|
|
},
|
|
});
|
|
|
|
const config = await service.getSmsConfig();
|
|
expect(config.enabled).toBe(true);
|
|
expect(config.username).toBe("custom_user");
|
|
expect(config.otpBodyId).toBe(99999);
|
|
});
|
|
|
|
it("should fallback gracefully to defaults if database query fails", async () => {
|
|
mockPrisma.setting.findFirst.mockRejectedValue(new Error("DB error"));
|
|
|
|
const config = await service.getSmsConfig();
|
|
expect(config.enabled).toBe(true);
|
|
expect(config.otpBodyId).toBeGreaterThan(0);
|
|
});
|
|
});
|
|
|
|
describe("triggerEvent", () => {
|
|
it("should skip sending if SMS gateway is disabled in config", async () => {
|
|
mockPrisma.setting.findFirst.mockResolvedValue({
|
|
category: "sms",
|
|
value: { enabled: false },
|
|
});
|
|
|
|
const res = await service.triggerEvent("ORDER_PAID", {
|
|
orderNumber: "123",
|
|
});
|
|
expect(res).toBe(false);
|
|
});
|
|
|
|
it("should execute configured SMS rule and invoke sendPatternSms", async () => {
|
|
mockPrisma.setting.findFirst.mockResolvedValue({
|
|
category: "sms",
|
|
value: {
|
|
enabled: true,
|
|
rules: [
|
|
{
|
|
id: "rule-1",
|
|
title: "پیامک پرداخت به مشتری",
|
|
event: "ORDER_PAID",
|
|
enabled: true,
|
|
recipientType: "customer",
|
|
patternId: 508081,
|
|
variables: ["orderNumber", "customerName"],
|
|
},
|
|
],
|
|
},
|
|
});
|
|
|
|
const sendPatternSpy = jest
|
|
.spyOn(service, "sendPatternSms")
|
|
.mockResolvedValue({ Value: 100 } as any);
|
|
|
|
const res = await service.triggerEvent("ORDER_PAID", {
|
|
orderNumber: "ORD-999",
|
|
customerName: "پارسا",
|
|
customerPhone: "09121111111",
|
|
});
|
|
|
|
expect(res).toBe(true);
|
|
expect(sendPatternSpy).toHaveBeenCalledWith({
|
|
to: "09121111111",
|
|
bodyId: 508081,
|
|
args: ["ORD-999", "پارسا"],
|
|
type: "ORDER_PAID_CUSTOMER",
|
|
});
|
|
});
|
|
});
|
|
});
|