test: add convert string body branch coverage tests
All checks were successful
Build & Deploy to Staging / Build & Deploy to Staging (push) Successful in 19m48s

This commit is contained in:
DocFast Bot 2026-03-21 20:07:01 +01:00
parent e0c4214e53
commit 6ce773a071

View file

@ -0,0 +1,55 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import express from "express";
import request from "supertest";
vi.mock("node:dns/promises", () => ({
default: { lookup: vi.fn() },
lookup: 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 { convertRouter } = await import("../routes/convert.js");
app = express();
// Parse application/json as text to produce a string req.body,
// exercising the typeof === "string" branches in convert routes.
app.use(express.text({ type: "application/json", limit: "500kb" }));
app.use("/v1/convert", convertRouter);
});
describe("convert routes handle string body (branch coverage)", () => {
it("POST /v1/convert/html with string body parses it as HTML", async () => {
const { renderPdf } = await import("../services/browser.js");
const res = await request(app)
.post("/v1/convert/html")
.set("content-type", "application/json")
.send("<h1>Hello</h1>");
expect(res.status).toBe(200);
expect(vi.mocked(renderPdf)).toHaveBeenCalledOnce();
const html = vi.mocked(renderPdf).mock.calls[0]![0] as string;
expect(html).toContain("<h1>Hello</h1>");
});
it("POST /v1/convert/markdown with string body parses it as markdown", async () => {
const { renderPdf } = await import("../services/browser.js");
const res = await request(app)
.post("/v1/convert/markdown")
.set("content-type", "application/json")
.send("# Hello");
expect(res.status).toBe(200);
expect(vi.mocked(renderPdf)).toHaveBeenCalledOnce();
const html = vi.mocked(renderPdf).mock.calls[0]![0] as string;
expect(html).toContain("Hello");
});
});