From 6ce773a071830944076060021d1ccb89f94b0569 Mon Sep 17 00:00:00 2001 From: DocFast Bot Date: Sat, 21 Mar 2026 20:07:01 +0100 Subject: [PATCH] test: add convert string body branch coverage tests --- src/__tests__/convert-string-body.test.ts | 55 +++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/__tests__/convert-string-body.test.ts diff --git a/src/__tests__/convert-string-body.test.ts b/src/__tests__/convert-string-body.test.ts new file mode 100644 index 0000000..e40fa6e --- /dev/null +++ b/src/__tests__/convert-string-body.test.ts @@ -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("

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

"); + }); + + 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"); + }); +});