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.
This commit is contained in:
1 parent
1e73715169
commit
ddbf1ca026
439 files changed
+32911
-6107
No files matched your search
@@ -0,0 +1,133 @@
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { SeoController } from "./seo.controller";
|
||||
import { SeoService } from "./seo.service";
|
||||
import { NotFoundException } from "@nestjs/common";
|
||||
|
||||
describe("SeoController", () => {
|
||||
let controller: SeoController;
|
||||
let service: SeoService;
|
||||
|
||||
const mockSeoService = {
|
||||
getSitemapUrls: jest.fn(),
|
||||
getProductSchema: jest.fn(),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [SeoController],
|
||||
providers: [
|
||||
{
|
||||
provide: SeoService,
|
||||
useValue: mockSeoService,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
controller = module.get<SeoController>(SeoController);
|
||||
service = module.get<SeoService>(SeoService);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(controller).toBeDefined();
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
describe("getSitemap", () => {
|
||||
it("should return sitemap URL list successfully", async () => {
|
||||
const mockUrls = [
|
||||
{
|
||||
loc: "https://canina.ir/",
|
||||
lastmod: "2026-10-10",
|
||||
changefreq: "daily",
|
||||
priority: 1.0,
|
||||
},
|
||||
{
|
||||
loc: "https://canina.ir/products",
|
||||
lastmod: "2026-10-11",
|
||||
changefreq: "daily",
|
||||
priority: 0.9,
|
||||
},
|
||||
{
|
||||
loc: "https://canina.ir/products/canhydrox-gag",
|
||||
lastmod: "2026-10-12",
|
||||
changefreq: "weekly",
|
||||
priority: 0.8,
|
||||
},
|
||||
];
|
||||
|
||||
mockSeoService.getSitemapUrls.mockResolvedValue(mockUrls);
|
||||
|
||||
const result = await controller.getSitemap();
|
||||
|
||||
expect(service.getSitemapUrls).toHaveBeenCalledTimes(1);
|
||||
expect(result).toEqual(mockUrls);
|
||||
expect(result).toHaveLength(3);
|
||||
});
|
||||
|
||||
it("should propagate errors from seoService.getSitemapUrls", async () => {
|
||||
mockSeoService.getSitemapUrls.mockRejectedValue(
|
||||
new Error("Redis connection dropped"),
|
||||
);
|
||||
|
||||
await expect(controller.getSitemap()).rejects.toThrow(
|
||||
"Redis connection dropped",
|
||||
);
|
||||
expect(service.getSitemapUrls).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getProductSchema", () => {
|
||||
it("should return product JSON-LD schema when found by id or slug", async () => {
|
||||
const mockSchema = {
|
||||
"@context": "https://schema.org",
|
||||
"@type": "Product",
|
||||
name: "کانین هیدروکس گگ",
|
||||
image: "https://canina.ir/images/canhydrox.jpg",
|
||||
description: "مکمل غذایی تخصصی مفاصل و استخوان سگ",
|
||||
sku: "CAN-HYDROX-100",
|
||||
offers: {
|
||||
"@type": "Offer",
|
||||
priceCurrency: "IRR",
|
||||
price: "25000000",
|
||||
availability: "https://schema.org/InStock",
|
||||
},
|
||||
};
|
||||
|
||||
mockSeoService.getProductSchema.mockResolvedValue(mockSchema);
|
||||
|
||||
const result = await controller.getProductSchema("canina-canhydrox-gag");
|
||||
|
||||
expect(service.getProductSchema).toHaveBeenCalledWith(
|
||||
"canina-canhydrox-gag",
|
||||
);
|
||||
expect(result).toEqual(mockSchema);
|
||||
expect(result["@type"]).toBe("Product");
|
||||
});
|
||||
|
||||
it("should throw NotFoundException when product schema is null or not found", async () => {
|
||||
mockSeoService.getProductSchema.mockResolvedValue(null);
|
||||
|
||||
await expect(
|
||||
controller.getProductSchema("non-existent-product-slug"),
|
||||
).rejects.toThrow(NotFoundException);
|
||||
|
||||
expect(service.getProductSchema).toHaveBeenCalledWith(
|
||||
"non-existent-product-slug",
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw NotFoundException when product schema is undefined", async () => {
|
||||
mockSeoService.getProductSchema.mockResolvedValue(undefined);
|
||||
|
||||
await expect(controller.getProductSchema("unknown-id")).rejects.toThrow(
|
||||
new NotFoundException("Product for schema not found"),
|
||||
);
|
||||
|
||||
expect(service.getProductSchema).toHaveBeenCalledWith("unknown-id");
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,26 +1,26 @@
|
||||
import { Controller, Get, Param, NotFoundException } from '@nestjs/common';
|
||||
import { SeoService } from './seo.service';
|
||||
import { ApiTags, ApiOperation } from '@nestjs/swagger';
|
||||
import { Controller, Get, Param, NotFoundException } from "@nestjs/common";
|
||||
import { SeoService } from "./seo.service";
|
||||
import { ApiTags, ApiOperation } from "@nestjs/swagger";
|
||||
|
||||
@ApiTags('SEO & Metadata')
|
||||
@Controller('seo')
|
||||
@ApiTags("SEO & Metadata")
|
||||
@Controller("seo")
|
||||
export class SeoController {
|
||||
constructor(private readonly seoService: SeoService) {}
|
||||
|
||||
@Get('sitemap')
|
||||
@ApiOperation({ summary: 'دریافت لیست لینکهای نقشه سایت (Sitemap)' })
|
||||
@Get("sitemap")
|
||||
@ApiOperation({ summary: "دریافت لیست لینکهای نقشه سایت (Sitemap)" })
|
||||
getSitemap() {
|
||||
return this.seoService.getSitemapUrls();
|
||||
}
|
||||
|
||||
@Get('product-schema/:idOrSlug')
|
||||
@Get("product-schema/:idOrSlug")
|
||||
@ApiOperation({
|
||||
summary: 'دریافت متادیتای ساختاریافته Schema.org JSON-LD محصول',
|
||||
summary: "دریافت متادیتای ساختاریافته Schema.org JSON-LD محصول",
|
||||
})
|
||||
async getProductSchema(@Param('idOrSlug') idOrSlug: string) {
|
||||
async getProductSchema(@Param("idOrSlug") idOrSlug: string) {
|
||||
const schema = await this.seoService.getProductSchema(idOrSlug);
|
||||
if (!schema) {
|
||||
throw new NotFoundException('Product for schema not found');
|
||||
throw new NotFoundException("Product for schema not found");
|
||||
}
|
||||
return schema;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { SeoController } from './seo.controller';
|
||||
import { SeoService } from './seo.service';
|
||||
import { PrismaModule } from '../prisma/prisma.module';
|
||||
import { Module } from "@nestjs/common";
|
||||
import { SeoController } from "./seo.controller";
|
||||
import { SeoService } from "./seo.service";
|
||||
import { PrismaModule } from "../prisma/prisma.module";
|
||||
|
||||
@Module({
|
||||
imports: [PrismaModule],
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { SeoService } from "./seo.service";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
|
||||
describe("SeoService", () => {
|
||||
let service: SeoService;
|
||||
|
||||
const mockPrisma = {
|
||||
product: {
|
||||
findMany: jest.fn(),
|
||||
findFirst: jest.fn(),
|
||||
},
|
||||
category: {
|
||||
findMany: jest.fn(),
|
||||
},
|
||||
blog: {
|
||||
findMany: jest.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [SeoService, { provide: PrismaService, useValue: mockPrisma }],
|
||||
}).compile();
|
||||
|
||||
service = module.get<SeoService>(SeoService);
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it("should be defined", () => {
|
||||
expect(service).toBeDefined();
|
||||
});
|
||||
|
||||
describe("getSitemapUrls", () => {
|
||||
it("should aggregate sitemap URLs for products, categories, and published blogs", async () => {
|
||||
const now = new Date("2026-10-10T12:00:00Z");
|
||||
mockPrisma.product.findMany.mockResolvedValue([
|
||||
{ slug: "canivita-tonic", createdAt: now },
|
||||
]);
|
||||
mockPrisma.category.findMany.mockResolvedValue([
|
||||
{ slug: "vitamins", createdAt: now },
|
||||
]);
|
||||
mockPrisma.blog.findMany.mockResolvedValue([
|
||||
{ slug: "dog-nutrition-guide", updatedAt: now },
|
||||
]);
|
||||
|
||||
const sitemap = await service.getSitemapUrls();
|
||||
|
||||
expect(sitemap.products).toEqual([
|
||||
{ url: "/shop/canivita-tonic", lastmod: now },
|
||||
]);
|
||||
expect(sitemap.categories).toEqual([
|
||||
{ url: "/shop?category=vitamins", lastmod: now },
|
||||
]);
|
||||
expect(sitemap.blogs).toEqual([
|
||||
{ url: "/blog/dog-nutrition-guide", lastmod: now },
|
||||
]);
|
||||
expect(mockPrisma.blog.findMany).toHaveBeenCalledWith({
|
||||
where: { isPublished: true },
|
||||
select: { slug: true, updatedAt: true },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("getProductSchema", () => {
|
||||
it("should return valid JSON-LD schema by slug", async () => {
|
||||
mockPrisma.product.findFirst.mockResolvedValue({
|
||||
id: "p-1",
|
||||
slug: "petvital-energy",
|
||||
nameFa: "انرژی پتویتال",
|
||||
nameEn: "Petvital Energy",
|
||||
imageUrl: "https://canina.ir/img/energy.jpg",
|
||||
description: "مکمل تقویتی سگ و گربه",
|
||||
artNo: "ART-1002",
|
||||
barcode: "626000000123",
|
||||
priceValue: 850000,
|
||||
});
|
||||
|
||||
const schema = await service.getProductSchema("petvital-energy");
|
||||
|
||||
expect(schema).toBeDefined();
|
||||
expect(schema!["@context"]).toBe("https://schema.org/");
|
||||
expect(schema!["@type"]).toBe("Product");
|
||||
expect(schema!.name).toBe("انرژی پتویتال");
|
||||
expect(schema!.sku).toBe("ART-1002");
|
||||
expect(schema!.gtin).toBe("626000000123");
|
||||
expect(schema!.offers.price).toBe(8500000); // 850,000 Toman * 10 = Rial
|
||||
expect(schema!.offers.priceCurrency).toBe("IRR");
|
||||
expect(mockPrisma.product.findFirst).toHaveBeenCalledWith({
|
||||
where: { slug: "petvital-energy" },
|
||||
});
|
||||
});
|
||||
|
||||
it("should query by UUID if idOrSlug matches UUID regex format", async () => {
|
||||
const validUuid = "12345678-1234-1234-1234-123456789abc";
|
||||
mockPrisma.product.findFirst.mockResolvedValue({
|
||||
id: validUuid,
|
||||
slug: "petvital-energy",
|
||||
nameFa: "انرژی پتویتال",
|
||||
priceValue: 500000,
|
||||
});
|
||||
|
||||
await service.getProductSchema(validUuid);
|
||||
|
||||
expect(mockPrisma.product.findFirst).toHaveBeenCalledWith({
|
||||
where: { id: validUuid },
|
||||
});
|
||||
});
|
||||
|
||||
it("should return null if product is not found", async () => {
|
||||
mockPrisma.product.findFirst.mockResolvedValue(null);
|
||||
|
||||
const res = await service.getProductSchema("non-existent");
|
||||
expect(res).toBeNull();
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { Injectable } from "@nestjs/common";
|
||||
import { PrismaService } from "../prisma/prisma.service";
|
||||
|
||||
@Injectable()
|
||||
export class SeoService {
|
||||
@@ -45,8 +45,8 @@ export class SeoService {
|
||||
if (!product) return null;
|
||||
|
||||
return {
|
||||
'@context': 'https://schema.org/',
|
||||
'@type': 'Product',
|
||||
"@context": "https://schema.org/",
|
||||
"@type": "Product",
|
||||
name: product.nameFa,
|
||||
alternateName: product.nameEn,
|
||||
image: [product.imageUrl],
|
||||
@@ -54,16 +54,16 @@ export class SeoService {
|
||||
sku: product.artNo,
|
||||
gtin: product.barcode || undefined,
|
||||
brand: {
|
||||
'@type': 'Brand',
|
||||
name: 'Canina Pharma',
|
||||
"@type": "Brand",
|
||||
name: "Canina Pharma",
|
||||
},
|
||||
offers: {
|
||||
'@type': 'Offer',
|
||||
"@type": "Offer",
|
||||
url: `https://canina.ir/shop/${product.slug}`,
|
||||
priceCurrency: 'IRR',
|
||||
priceCurrency: "IRR",
|
||||
price: Number(product.priceValue) * 10, // Toman to Rial conversion if needed
|
||||
availability: 'https://schema.org/InStock',
|
||||
itemCondition: 'https://schema.org/NewCondition',
|
||||
availability: "https://schema.org/InStock",
|
||||
itemCondition: "https://schema.org/NewCondition",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user