import { Test, TestingModule } from "@nestjs/testing"; import { RevalidationService } from "./revalidation.service"; describe("RevalidationService", () => { let service: RevalidationService; const originalEnv = process.env; beforeEach(async () => { process.env = { ...originalEnv }; process.env.REVALIDATION_SECRET = "test-secret-123"; process.env.FRONTEND_INTERNAL_URL = "http://frontend_mock:8080"; const module: TestingModule = await Test.createTestingModule({ providers: [RevalidationService], }).compile(); service = module.get(RevalidationService); }); afterEach(() => { process.env = originalEnv; jest.restoreAllMocks(); }); it("should be defined", () => { expect(service).toBeDefined(); }); it("uses the Next server rather than the nginx API proxy as production fallback", async () => { delete process.env.FRONTEND_INTERNAL_URL; delete process.env.FRONTEND_URL; delete process.env.NEXT_PUBLIC_SITE_URL; process.env.NODE_ENV = "production"; const fetchSpy = jest.spyOn(global, "fetch").mockResolvedValue({ ok: true } as Response); await service.revalidate(["blogs"], ["/blog"]); expect(fetchSpy).toHaveBeenCalledWith("http://frontend_prod:3000/api/revalidate", expect.anything()); }); describe("revalidateTag", () => { it("should send POST request with tag and secret header and return true on 200", async () => { const mockFetch = jest.spyOn(global, "fetch" as any).mockResolvedValue({ ok: true, status: 200, } as any); const result = await service.revalidateTag("products"); expect(result).toBe(true); expect(mockFetch).toHaveBeenCalledWith( "http://frontend_mock:8080/api/revalidate", expect.objectContaining({ method: "POST", headers: expect.objectContaining({ "x-revalidate-secret": "test-secret-123", }), body: JSON.stringify({ tag: "products", secret: "test-secret-123" }), }), ); }); it("should return false if frontend responds with non-ok status", async () => { jest.spyOn(global, "fetch" as any).mockResolvedValue({ ok: false, status: 500, statusText: "Internal Error", } as any); const result = await service.revalidateTag("products"); expect(result).toBe(false); }); it("should catch network errors gracefully and return false", async () => { jest .spyOn(global, "fetch" as any) .mockRejectedValue(new Error("Connection refused")); const result = await service.revalidateTag("products"); expect(result).toBe(false); }); }); describe("revalidatePath", () => { it("should send POST request with path and secret and return true on success", async () => { const mockFetch = jest.spyOn(global, "fetch" as any).mockResolvedValue({ ok: true, status: 200, } as any); const result = await service.revalidatePath("/shop"); expect(result).toBe(true); expect(mockFetch).toHaveBeenCalledWith( "http://frontend_mock:8080/api/revalidate", expect.objectContaining({ method: "POST", body: JSON.stringify({ path: "/shop", secret: "test-secret-123" }), }), ); }); }); describe("revalidateProduct", () => { it("should trigger all tags and paths for product slug and artNo", async () => { const tagSpy = jest .spyOn(service, "revalidateTag") .mockResolvedValue(true); const pathSpy = jest .spyOn(service, "revalidatePath") .mockResolvedValue(true); await service.revalidateProduct("canivita-tonic", "ART-100"); expect(tagSpy).toHaveBeenCalledWith("products"); expect(tagSpy).toHaveBeenCalledWith("home-data"); expect(tagSpy).toHaveBeenCalledWith("product-canivita-tonic"); expect(tagSpy).toHaveBeenCalledWith("product-ART-100"); expect(pathSpy).toHaveBeenCalledWith("/shop"); expect(pathSpy).toHaveBeenCalledWith("/"); expect(pathSpy).toHaveBeenCalledWith("/shop/canivita-tonic"); expect(pathSpy).toHaveBeenCalledWith("/shop/ART-100"); }); }); describe("revalidateBlog", () => { it("should trigger all tags and paths for blog slug and return true when all succeed", async () => { const tagSpy = jest .spyOn(service, "revalidateTag") .mockResolvedValue(true); const pathSpy = jest .spyOn(service, "revalidatePath") .mockResolvedValue(true); const result = await service.revalidateBlog("test-slug"); expect(tagSpy).toHaveBeenCalledWith("blogs"); expect(tagSpy).toHaveBeenCalledWith("home-data"); expect(tagSpy).toHaveBeenCalledWith("blog-test-slug"); expect(pathSpy).toHaveBeenCalledWith("/blog"); expect(pathSpy).toHaveBeenCalledWith("/sitemap.xml"); expect(pathSpy).toHaveBeenCalledWith("/"); expect(pathSpy).toHaveBeenCalledWith("/blog/test-slug"); expect(result).toBe(true); }); it("should return false when a tag or path revalidation fails rather than swallowing false", async () => { jest.spyOn(service, "revalidateTag").mockImplementation(async (tag: string) => { return tag !== "blogs"; }); jest.spyOn(service, "revalidatePath").mockResolvedValue(true); const result = await service.revalidateBlog("test-slug"); expect(result).toBe(false); }); it("should catch unexpected revalidation errors and return false", async () => { jest.spyOn(service, "revalidateTag").mockRejectedValue(new Error("Network disconnected")); jest.spyOn(service, "revalidatePath").mockResolvedValue(true); const result = await service.revalidateBlog("test-slug"); expect(result).toBe(false); }); }); describe("revalidate batch", () => { it("should send deduplicated tags and paths in one request", async () => { const mockFetch = jest.spyOn(global, "fetch" as any).mockResolvedValue({ ok: true, status: 200 } as any); const result = await service.revalidate(["blogs", "blogs"], ["/blog", "/blog"]); expect(result).toBe(true); expect(mockFetch).toHaveBeenCalledTimes(1); expect(mockFetch).toHaveBeenCalledWith( "http://frontend_mock:8080/api/revalidate", expect.objectContaining({ method: "POST", body: JSON.stringify({ tags: ["blogs"], paths: ["/blog"] }), }), ); }); }); });