import { Test, TestingModule } from "@nestjs/testing"; import { MediaService } from "./media.service"; import { PrismaService } from "../prisma/prisma.service"; import { BadRequestException } from "@nestjs/common"; import * as fs from "fs"; jest.mock("fs"); describe("MediaService", () => { let service: MediaService; const mockPrisma = { media: { findMany: jest.fn(), findUnique: jest.fn(), create: jest.fn(), delete: jest.fn(), deleteMany: jest.fn(), }, }; beforeEach(async () => { const module: TestingModule = await Test.createTestingModule({ providers: [ MediaService, { provide: PrismaService, useValue: mockPrisma }, ], }).compile(); service = module.get(MediaService); jest.clearAllMocks(); }); it("should be defined", () => { expect(service).toBeDefined(); }); describe("getAllMedia", () => { it("should deduplicate media items by lowercase normalized URL", async () => { mockPrisma.media.findMany.mockResolvedValue([ { id: "1", url: "/uploads/canivita.jpg", filename: "canivita.jpg" }, { id: "2", url: "/uploads/CANIVITA.jpg", filename: "canivita-dup.jpg" }, { id: "3", url: "/uploads/petvital.png", filename: "petvital.png" }, ]); const result = await service.getAllMedia(); expect(result).toHaveLength(2); expect(result[0].id).toBe("1"); expect(result[1].id).toBe("3"); }); }); describe("uploadFile", () => { it("should throw BadRequestException if file is missing", async () => { await expect(service.uploadFile(undefined as any)).rejects.toThrow( BadRequestException, ); }); it("should throw BadRequestException for disallowed file extensions (e.g. svg, exe)", async () => { const svgFile = { originalname: "icon.svg", buffer: Buffer.from(""), mimetype: "image/svg+xml", size: 100, } as any; await expect(service.uploadFile(svgFile)).rejects.toThrow( "پسوند فایل ارسالی مجاز نیست", ); }); it("should successfully save allowed image file (.png, .webp, .jpg) and create db record", async () => { (fs.existsSync as jest.Mock).mockReturnValue(true); (fs.writeFileSync as jest.Mock).mockReturnValue(undefined); const validFile = { originalname: "product-cover.webp", buffer: Buffer.from([0x52, 0x49, 0x46, 0x46, 0x00, 0x00, 0x00, 0x00, 0x57, 0x45, 0x42, 0x50, 0x56, 0x50, 0x38]), mimetype: "image/webp", size: 5000, } as any; mockPrisma.media.create.mockImplementation(({ data }) => ({ id: "med-123", ...data, })); const res = await service.uploadFile(validFile); expect(res.id).toBe("med-123"); expect(res.filename).toBe("product-cover.webp"); expect(res.mimetype).toBe("image/webp"); expect(res.url).toMatch(/^\/uploads\/\d+-\d+\.webp$/); expect(fs.writeFileSync).toHaveBeenCalled(); }); }); describe("deleteMedia", () => { it("should throw BadRequestException if media not found", async () => { mockPrisma.media.findUnique.mockResolvedValue(null); await expect(service.deleteMedia("not-found")).rejects.toThrow( BadRequestException, ); }); it("should unlink file and delete database record if media exists", async () => { mockPrisma.media.findUnique.mockResolvedValue({ id: "med-1", url: "/uploads/test.jpg", }); (fs.existsSync as jest.Mock).mockReturnValue(true); (fs.unlinkSync as jest.Mock).mockReturnValue(undefined); mockPrisma.media.delete.mockResolvedValue({ id: "med-1" }); const res = await service.deleteMedia("med-1"); expect(res.success).toBe(true); expect(fs.unlinkSync).toHaveBeenCalled(); expect(mockPrisma.media.delete).toHaveBeenCalledWith({ where: { id: "med-1" }, }); }); }); });