refactor(demo): Use handlePdfRoute to reduce boilerplate
All checks were successful
Build & Deploy to Staging / Build & Deploy to Staging (push) Successful in 18m8s

- Refactored demo routes to use shared handlePdfRoute utility
- Added handleDemoPdfRoute wrapper to preserve attachment disposition
- Preserved watermark injection and demo.pdf default filename
- Added comprehensive TDD tests for Content-Disposition behavior
- Reduced demo.ts from 269 to 238 lines (31 lines removed)
- All 628 tests pass including 6 new behavioral tests

Fixes duplicated error handling, validation, and concurrency logic
while maintaining existing demo route behavior.
This commit is contained in:
DocFast Backend Agent 2026-03-10 11:06:34 +01:00
parent 7ae20ea280
commit b1a09f7b3f
2 changed files with 123 additions and 84 deletions

View file

@ -199,4 +199,74 @@ describe("POST /v1/demo/markdown", () => {
expect(res.status).toBe(400);
expect(res.body.error).toMatch(/format/);
});
// NEW TDD TESTS - These should verify current behavior before refactoring
it("returns Content-Disposition attachment header", async () => {
const res = await request(app)
.post("/v1/demo/markdown")
.set("content-type", "application/json")
.send({ markdown: "# Hello" });
expect(res.status).toBe(200);
expect(res.headers["content-disposition"]).toMatch(/attachment/);
expect(res.headers["content-disposition"]).toMatch(/filename="demo\.pdf"/);
});
it("returns custom filename in attachment header", async () => {
const res = await request(app)
.post("/v1/demo/markdown")
.set("content-type", "application/json")
.send({ markdown: "# Hello", filename: "custom.pdf" });
expect(res.status).toBe(200);
expect(res.headers["content-disposition"]).toMatch(/attachment/);
expect(res.headers["content-disposition"]).toMatch(/filename="custom\.pdf"/);
});
it("injects watermark into HTML content", async () => {
const { renderPdf } = await import("../services/browser.js");
const res = await request(app)
.post("/v1/demo/markdown")
.set("content-type", "application/json")
.send({ markdown: "# Hello" });
expect(res.status).toBe(200);
const calledHtml = vi.mocked(renderPdf).mock.calls[0][0];
expect(calledHtml).toContain("Generated by DocFast — docfast.dev");
expect(calledHtml).toContain("Upgrade to Pro for clean PDFs");
expect(calledHtml).toContain("position:fixed;top:0;left:0;width:100%;height:100%"); // watermark overlay
});
// NEW TDD TESTS - These should verify current behavior before refactoring
it("returns Content-Disposition attachment header", async () => {
const res = await request(app)
.post("/v1/demo/html")
.set("content-type", "application/json")
.send({ html: "<h1>Hello</h1>" });
expect(res.status).toBe(200);
expect(res.headers["content-disposition"]).toMatch(/attachment/);
expect(res.headers["content-disposition"]).toMatch(/filename="demo\.pdf"/);
});
it("returns custom filename in attachment header", async () => {
const res = await request(app)
.post("/v1/demo/html")
.set("content-type", "application/json")
.send({ html: "<h1>Hello</h1>", filename: "custom.pdf" });
expect(res.status).toBe(200);
expect(res.headers["content-disposition"]).toMatch(/attachment/);
expect(res.headers["content-disposition"]).toMatch(/filename="custom\.pdf"/);
});
it("injects watermark into HTML content", async () => {
const { renderPdf } = await import("../services/browser.js");
const res = await request(app)
.post("/v1/demo/html")
.set("content-type", "application/json")
.send({ html: "<h1>Hello</h1>" });
expect(res.status).toBe(200);
const calledHtml = vi.mocked(renderPdf).mock.calls[0][0];
expect(calledHtml).toContain("Generated by DocFast — docfast.dev");
expect(calledHtml).toContain("Upgrade to Pro for clean PDFs");
expect(calledHtml).toContain("position:fixed;top:0;left:0;width:100%;height:100%"); // watermark overlay
});
});