Files
SnapOtter/tests/e2e/pdf-to-image-preset-multifile.spec.ts
T
SnapOtterandGitHub a7137958a1 fix(pdf): pdf-to-image presets no longer 404 on 2+ files (#643)
Upload two PDFs to pdf-to-jpg and it answered `Tool "pdf-to-jpg" not
found`. pdf-to-jpg, pdf-to-png and pdf-to-tiff share
registerPdfToImageRoute, which registered a single-file endpoint and
nothing else, so the shared preset settings component's 2+-file
submission fell through to the generic `:section/:toolId/batch` route,
whose registry lookup misses every tool outside
createToolRoute/registerToolProcessFn.

Mirror of #627, different fix. image-to-pdf is many-to-one, so #633 sent
every file in one request. This direction is one-to-many: separate PDFs
want separate conversions, which is what /batch is for. The route now
serves its own /batch, the shape svg-to-raster already uses, and the
literal path beats the generic parametric one.

One PDF fans out to many page images, so a per-file result is a ZIP, same
as the single-file route. A batch returns a ZIP of per-document ZIPs in
upload order, keyed by X-File-Results so each result pairs with the file
it came from. A document that is unreadable, locked, empty, short of the
requested page range, or carrying no pages at all fails alone; 422 with a
reason per file when none survive.

That literal path also shadows the generic route's requireToolAccess
call, which would have turned a 403 into a converted ZIP for roles
without tools:use. All four endpoints in this file now gate.

Four ways the batch path could have reported something untrue are closed
with it: a storage fault blamed on the document (statusCode-carrying
errors now reach the error handler, the rest are logged before being
reduced to a generic message), per-file reasons stranded in a field
parseApiError never reads, a zero-byte upload dropped so that later
results landed on the wrong file, and a mid-stream failure ended cleanly
enough to pass for success (the socket is destroyed instead).

Page rendering and ZIP assembly are shared helpers now, createUniqueNamer
moves to lib/filename.ts next to its two existing copies, and
tool-route-drift fails if any batch-dispatched preset loses its /batch
route. Follow-up for the same defects in the sibling custom routes: #645.

Fixes #632
2026-07-26 01:35:42 +08:00

83 lines
3.2 KiB
TypeScript

import path from "node:path";
import type { Page } from "@playwright/test";
import { expect, test } from "./helpers";
const FIXTURES_DIR = path.join(process.cwd(), "tests", "fixtures", "document", "valid");
async function uploadPdfs(page: Page, filenames: string[]): Promise<void> {
const fileChooserPromise = page.waitForEvent("filechooser");
await page
.getByRole("button", { name: /upload from computer/i })
.first()
.click();
const fileChooser = await fileChooserPromise;
await fileChooser.setFiles(filenames.map((f) => path.join(FIXTURES_DIR, f)));
await page.waitForTimeout(500);
}
/**
* Issue #632: pdf-to-image-group presets (pdf-to-jpg, pdf-to-png, pdf-to-tiff)
* turn one PDF into many page images, so 2+ uploads belong on the per-file
* /batch endpoint. Those presets register through registerPdfToImageRoute,
* which never entered the registry the generic
* `:section/:toolId/batch` route reads, so the second file used to 404 with
* `Tool "<id>" not found`. The route now serves its own /batch.
*/
test.describe("pdf-to-image conversion presets with multiple files (issue #632)", () => {
test("pdf-to-jpg converts 2 PDFs instead of failing", async ({ loggedInPage: page }) => {
const batchResponse = page.waitForResponse(
(res) => res.url().includes("/pdf-to-jpg/batch") && res.request().method() === "POST",
);
await page.goto("/pdf/pdf-to-jpg");
await uploadPdfs(page, ["test-3page.pdf", "alt-2page.pdf"]);
await expect(page.getByText("Files (2)")).toBeVisible();
await page.getByTestId("preset-submit").click();
const response = await batchResponse;
expect(response.status()).toBe(200);
await expect(page.getByText("Conversion complete").first()).toBeVisible({ timeout: 30_000 });
});
test("pdf-to-png reports the error in the panel when every PDF is unreadable", async ({
loggedInPage: page,
}) => {
await page.goto("/pdf/pdf-to-png");
// Route the batch call so the failure path is deterministic without
// needing a corrupt fixture that the dropzone would reject up front.
await page.route("**/pdf-to-png/batch", (route) =>
route.fulfill({
status: 422,
contentType: "application/json",
body: JSON.stringify({ error: "All files failed processing", errors: [] }),
}),
);
await uploadPdfs(page, ["test-3page.pdf", "alt-2page.pdf"]);
await expect(page.getByText("Files (2)")).toBeVisible();
await page.getByTestId("preset-submit").click();
await expect(page.getByText(/All files failed processing/i)).toBeVisible({ timeout: 15_000 });
});
test("a single PDF still uses the single-file route", async ({ loggedInPage: page }) => {
const singleResponse = page.waitForResponse(
(res) => /\/pdf-to-jpg$/.test(res.url()) && res.request().method() === "POST",
);
await page.goto("/pdf/pdf-to-jpg");
await uploadPdfs(page, ["alt-2page.pdf"]);
await page.getByTestId("preset-submit").click();
const response = await singleResponse;
expect(response.status()).toBe(200);
const download = page.getByTestId("preset-download");
await expect(download).toBeVisible({ timeout: 30_000 });
await expect(download).toHaveAttribute("href", /\/api\/v1\/download\/[^/]+\/pdf-pages\.zip$/);
});
});