import { describe, it, expect, vi, beforeEach } from "vitest"; import express from "express"; import request from "supertest"; vi.mock("../services/browser.js", () => ({ renderPdf: vi.fn(), renderUrlPdf: vi.fn(), })); let app: express.Express; beforeEach(async () => { vi.clearAllMocks(); vi.resetModules(); const { renderPdf } = await import("../services/browser.js"); vi.mocked(renderPdf).mockResolvedValue({ pdf: Buffer.from("%PDF-1.4 mock"), durationMs: 10 }); const { demoRouter } = await import("../routes/demo.js"); app = express(); app.use(express.json({ limit: "500kb" })); app.use("/v1/demo", demoRouter); }); describe("Demo Branch Coverage", () => { describe("injectWatermark fallback branch (line 19)", () => { it("should append watermark when full HTML document doesn't contain
Content here
`; const res = await request(app) .post("/v1/demo/html") .set("content-type", "application/json") .send({ html: htmlWithoutClosingBody }); expect(res.status).toBe(200); expect(res.headers["content-type"]).toMatch(/application\/pdf/); // Verify watermark was appended (not replaced) const calledHtml = vi.mocked(renderPdf).mock.calls[0][0]; // The fallback should append the watermark at the end expect(calledHtml).toContain("Hello"); expect(calledHtml).toContain("Content here"); expect(calledHtml).toContain("DEMO"); expect(calledHtml).toContain("Generated by DocFast"); // Ensure the original HTML is preserved before the watermark expect(calledHtml.indexOf("Hello")).toBeLessThan(calledHtml.indexOf("DEMO")); // Ensure watermark is appended at the end (since there's noWith closing body tag
tag", async () => { const { renderPdf } = await import("../services/browser.js"); // Send full HTML (with ) but without to hit the fallback branch const htmlWithoutClosingBody = `
to replace) const lastBodyCloseIndex = calledHtml.lastIndexOf(""); const watermarkIndex = calledHtml.indexOf("Generated by DocFast"); // If there's a at the very end (from wrapping), the watermark should be before it if (lastBodyCloseIndex > -1) { expect(watermarkIndex).toBeLessThan(lastBodyCloseIndex); } }); it("should append watermark to plain HTML fragment without ", async () => { const { renderPdf } = await import("../services/browser.js"); const res = await request(app) .post("/v1/demo/html") .set("content-type", "application/json") .send({ html: "
" }); expect(res.status).toBe(200); const calledHtml = vi.mocked(renderPdf).mock.calls[0][0]; expect(calledHtml).toContain("
"); expect(calledHtml).toContain("DEMO"); expect(calledHtml).toContain("position:fixed;bottom:0;left:0;right:0;"); }); it("should handle markdown that results in HTML without and injects watermark", async () => { const { renderPdf } = await import("../services/browser.js"); const res = await request(app) .post("/v1/demo/markdown") .set("content-type", "application/json") .send({ markdown: "# Just a heading\n\nSome text" }); expect(res.status).toBe(200); const calledHtml = vi.mocked(renderPdf).mock.calls[0][0]; // Should contain watermark expect(calledHtml).toContain("DEMO"); expect(calledHtml).toContain("Generated by DocFast"); expect(calledHtml).toContain("Upgrade to Pro for clean PDFs"); }); it("should still work correctly when HTML contains (replace branch)", async () => { const { renderPdf } = await import("../services/browser.js"); const fullHtml = `