Files
SnapOtter/tests/unit/web/conversion-preset-image-to-pdf-multifile.test.tsx
T
SnapOtterandGitHub d10d0f544f fix: release QA hardening across processing, media, security, and CI gates (#649)
A release-readiness QA pass over the whole product. The commits split into
defects a user would hit and gates that were reporting green while measuring
nothing.

## Fixes that change behaviour

Rate limiting was bypassable on every install: TRUST_PROXY defaulted to true, so
request.ip came from a client-set header and a forged X-Forwarded-For got past
the login limiter. The default is now a private-network trust list.

A transient Postgres outage stranded in-flight jobs, leaving finished output on
disk with no row pointing at it. A reconciler now resolves those rows and adopts
the bytes rather than dropping the work.

A Redis connection that moved to a new address wedged every read-blocked
consumer, so completions stopped signalling while health still answered 200.
Socket timeouts plus subscriber pings recover it.

Installing more than one AI bundle left the shared venv multi-versioned and
silently broke three tools. The installer now reconciles distributions to one
version each.

Converting an image to JXL at quality 1 through 4 returned a 500, because
libjxl 0.7 rejects the distance those values compute. The quality is floored at
what the encoder honours. A missing ffmpeg was also reported to the user as a
corrupt upload; it now says the engine is unavailable.

RAW uploads reached an unpatched LibRaw on arm64, so it is built from source at
0.22.2, and the release scan was split so it can fail on an unfixed critical
instead of hiding it behind ignore-unfixed.

## Gates that could not fail

Two mutation lanes ran zero mutants because Stryker crawled the gitignored docs
build; coverage discarded its whole report on any failing test; the lint gate
skipped root tests, scripts, and two workspaces; and several generated matrices
counted a host missing ffmpeg as a passing tool. Each now measures what it
claims.

Full evidence and the outstanding release items are tracked locally and are not
part of this branch.
2026-07-27 15:37:30 +08:00

151 lines
4.8 KiB
TypeScript

// @vitest-environment jsdom
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("@/lib/image-preview", () => ({
needsServerPreview: vi.fn(() => false),
fetchDecodedPreview: vi.fn(() => Promise.resolve(null)),
}));
vi.mock("@/lib/analytics", () => ({
track: vi.fn(),
}));
vi.mock("@/lib/api", () => ({
formatHeaders: () => new Map<string, string>(),
parseApiError: () => "error",
}));
let mockToolId = "jpg-to-pdf";
vi.mock("react-router", async (importOriginal) => {
const actual = await importOriginal<typeof import("react-router")>();
return { ...actual, useParams: () => ({ toolId: mockToolId }) };
});
import { ConversionPresetSettings } from "@/components/tools/conversion-preset-settings";
import { useFileStore } from "@/stores/file-store";
interface MockXhr {
open: ReturnType<typeof vi.fn>;
send: ReturnType<typeof vi.fn>;
setRequestHeader: ReturnType<typeof vi.fn>;
abort: ReturnType<typeof vi.fn>;
upload: Record<string, unknown>;
status: number;
responseText: string;
timeout: number;
}
class MockEventSource {
onmessage: ((event: MessageEvent) => void) | null = null;
onerror: (() => void) | null = null;
close = vi.fn();
constructor(readonly url: string) {}
}
function makeFile(name: string, type = "image/jpeg"): File {
return new File([new ArrayBuffer(64)], name, { type });
}
let xhrInstances: MockXhr[];
let fetchMock: ReturnType<typeof vi.fn>;
beforeEach(() => {
vi.stubGlobal("URL", {
...globalThis.URL,
createObjectURL: vi.fn(() => "blob:fake-url"),
revokeObjectURL: vi.fn(),
});
useFileStore.getState().reset();
xhrInstances = [];
vi.stubGlobal("EventSource", MockEventSource);
vi.stubGlobal(
"XMLHttpRequest",
vi.fn(() => {
const xhr: MockXhr = {
status: 0,
responseText: "",
timeout: 0,
upload: {},
open: vi.fn(),
send: vi.fn(),
setRequestHeader: vi.fn(),
abort: vi.fn(),
};
xhrInstances.push(xhr);
return xhr;
}),
);
// Never resolves; these tests only assert the call was (or wasn't) made.
fetchMock = vi.fn(() => new Promise(() => {}));
vi.stubGlobal("fetch", fetchMock);
});
afterEach(() => {
cleanup();
vi.unstubAllGlobals();
});
/**
* Issue #627: the jpg-to-pdf preset (and its image-to-pdf-group siblings)
* combine every uploaded file into one PDF, the same as the base image-to-pdf
* tool. Submitting 2+ files must stay on the single "attach all files" route,
* never the generic per-file /batch endpoint (which 404s for these tools:
* they are registered via registerImageToPdfRoute, not createToolRoute, so
* they were never added to the batch-capable tool registry).
*/
describe("ConversionPresetSettings multi-file dispatch (issue #627)", () => {
it("combines 2 files for jpg-to-pdf in one request instead of calling /batch", () => {
mockToolId = "jpg-to-pdf";
useFileStore.getState().setFiles([makeFile("a.jpg"), makeFile("b.jpg")]);
render(<ConversionPresetSettings />);
fireEvent.click(screen.getByTestId("preset-submit"));
expect(xhrInstances).toHaveLength(1);
expect(xhrInstances[0].open).toHaveBeenCalledWith("POST", "/api/v1/tools/image/jpg-to-pdf");
expect(fetchMock).not.toHaveBeenCalled();
});
it("combines 2 files for every image-to-pdf-group preset, not just jpg-to-pdf", () => {
mockToolId = "png-to-pdf";
useFileStore
.getState()
.setFiles([makeFile("a.png", "image/png"), makeFile("b.png", "image/png")]);
render(<ConversionPresetSettings />);
fireEvent.click(screen.getByTestId("preset-submit"));
expect(xhrInstances).toHaveLength(1);
expect(xhrInstances[0].open).toHaveBeenCalledWith("POST", "/api/v1/tools/image/png-to-pdf");
expect(fetchMock).not.toHaveBeenCalled();
});
it("still uses independent per-file batch processing for ordinary presets like jpg-to-png", () => {
mockToolId = "jpg-to-png";
useFileStore.getState().setFiles([makeFile("a.jpg"), makeFile("b.jpg")]);
render(<ConversionPresetSettings />);
fireEvent.click(screen.getByTestId("preset-submit"));
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(fetchMock.mock.calls[0][0]).toBe("/api/v1/tools/image/jpg-to-png/batch");
expect(xhrInstances).toHaveLength(0);
});
it("uses the single-file request for jpg-to-pdf when only 1 file is selected", () => {
mockToolId = "jpg-to-pdf";
useFileStore.getState().setFiles([makeFile("a.jpg")]);
render(<ConversionPresetSettings />);
fireEvent.click(screen.getByTestId("preset-submit"));
expect(xhrInstances).toHaveLength(1);
expect(xhrInstances[0].open).toHaveBeenCalledWith("POST", "/api/v1/tools/image/jpg-to-pdf");
expect(fetchMock).not.toHaveBeenCalled();
});
});