Files
SnapOtter/tests/integration/platform/doc-engine-pdfcpu.test.ts
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

203 lines
6.4 KiB
TypeScript

import { mkdtempSync, rmSync } from "node:fs";
import { readFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
pdfcpuAvailable,
pdfcpuBooklet,
pdfcpuCropMargin,
pdfcpuNup,
pdfcpuTextStamp,
} from "@snapotter/doc-engine";
import { describe, expect, it } from "vitest";
import { fixtures } from "../../fixtures/index.js";
const PDF = fixtures.document.pdf3;
// --- Gated integration tests: skip when pdfcpu is not installed locally ---
describe.skipIf(!pdfcpuAvailable())("doc-engine pdfcpu (requires pdfcpu binary)", () => {
it("nup(2) reduces 3 pages to 2 sheets", async () => {
const dir = mkdtempSync(join(tmpdir(), "pdfcpu-"));
try {
const out = join(dir, "nup2.pdf");
await pdfcpuNup(PDF, 2, out);
const bytes = await readFile(out);
expect(bytes.subarray(0, 5).toString()).toBe("%PDF-");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("crop preserves page count", async () => {
const dir = mkdtempSync(join(tmpdir(), "pdfcpu-"));
try {
const out = join(dir, "cropped.pdf");
await pdfcpuCropMargin(PDF, 20, out);
const bytes = await readFile(out);
expect(bytes.subarray(0, 5).toString()).toBe("%PDF-");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("textStamp produces valid pdf output", async () => {
const dir = mkdtempSync(join(tmpdir(), "pdfcpu-"));
try {
const out = join(dir, "stamped.pdf");
await pdfcpuTextStamp(
PDF,
{ text: "CONFIDENTIAL", position: "c", fontSize: 24, opacity: 0.5, rotation: 45 },
out,
);
const bytes = await readFile(out);
expect(bytes.subarray(0, 5).toString()).toBe("%PDF-");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("booklet produces valid pdf output", async () => {
const dir = mkdtempSync(join(tmpdir(), "pdfcpu-"));
try {
const out = join(dir, "booklet.pdf");
await pdfcpuBooklet(PDF, 2, out);
const bytes = await readFile(out);
expect(bytes.subarray(0, 5).toString()).toBe("%PDF-");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it("textStamp with page numbers (%p/%P) produces valid output", async () => {
const dir = mkdtempSync(join(tmpdir(), "pdfcpu-"));
try {
const out = join(dir, "numbered.pdf");
await pdfcpuTextStamp(
PDF,
{ text: "Page %p of %P", position: "bc", fontSize: 10, opacity: 1, rotation: 0 },
out,
);
const bytes = await readFile(out);
expect(bytes.subarray(0, 5).toString()).toBe("%PDF-");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
it.each(["%", "100%", "%x"])("textStamp safely preserves literal text %j", async (text) => {
const dir = mkdtempSync(join(tmpdir(), "pdfcpu-"));
try {
const out = join(dir, "literal-percent.pdf");
await pdfcpuTextStamp(
PDF,
{ text, position: "c", fontSize: 10, opacity: 1, rotation: 0 },
out,
);
const bytes = await readFile(out);
expect(bytes.subarray(0, 5).toString()).toBe("%PDF-");
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
});
// --- Ungated validation tests: run without the binary ---
describe("pdfcpu validation (no binary required)", () => {
it("rejects invalid stamp position", async () => {
await expect(
pdfcpuTextStamp(
"/fake.pdf",
{ text: "test", position: "xx", fontSize: 12, opacity: 0.5, rotation: 0 },
"/out.pdf",
),
).rejects.toThrow(/position/i);
});
it("rejects empty stamp text", async () => {
await expect(
pdfcpuTextStamp(
"/fake.pdf",
{ text: "", position: "c", fontSize: 12, opacity: 0.5, rotation: 0 },
"/out.pdf",
),
).rejects.toThrow(/text/i);
});
it("rejects oversize stamp text (>200 chars)", async () => {
await expect(
pdfcpuTextStamp(
"/fake.pdf",
{ text: "x".repeat(201), position: "c", fontSize: 12, opacity: 0.5, rotation: 0 },
"/out.pdf",
),
).rejects.toThrow(/text/i);
});
it("rejects fontSize out of range", async () => {
await expect(
pdfcpuTextStamp(
"/fake.pdf",
{ text: "test", position: "c", fontSize: 5, opacity: 0.5, rotation: 0 },
"/out.pdf",
),
).rejects.toThrow(/font size/i);
await expect(
pdfcpuTextStamp(
"/fake.pdf",
{ text: "test", position: "c", fontSize: 73, opacity: 0.5, rotation: 0 },
"/out.pdf",
),
).rejects.toThrow(/font size/i);
});
it("rejects opacity out of range", async () => {
await expect(
pdfcpuTextStamp(
"/fake.pdf",
{ text: "test", position: "c", fontSize: 12, opacity: 0.04, rotation: 0 },
"/out.pdf",
),
).rejects.toThrow(/opacity/i);
await expect(
pdfcpuTextStamp(
"/fake.pdf",
{ text: "test", position: "c", fontSize: 12, opacity: 1.1, rotation: 0 },
"/out.pdf",
),
).rejects.toThrow(/opacity/i);
});
it("rejects rotation out of range", async () => {
await expect(
pdfcpuTextStamp(
"/fake.pdf",
{ text: "test", position: "c", fontSize: 12, opacity: 0.5, rotation: -181 },
"/out.pdf",
),
).rejects.toThrow(/rotation/i);
await expect(
pdfcpuTextStamp(
"/fake.pdf",
{ text: "test", position: "c", fontSize: 12, opacity: 0.5, rotation: 181 },
"/out.pdf",
),
).rejects.toThrow(/rotation/i);
});
it("rejects crop margin out of range", async () => {
await expect(pdfcpuCropMargin("/fake.pdf", -1, "/out.pdf")).rejects.toThrow(/margin/i);
await expect(pdfcpuCropMargin("/fake.pdf", 2001, "/out.pdf")).rejects.toThrow(/margin/i);
await expect(pdfcpuCropMargin("/fake.pdf", NaN, "/out.pdf")).rejects.toThrow(/margin/i);
});
it("rejects invalid nup value", async () => {
await expect(pdfcpuNup("/fake.pdf", 5 as never, "/out.pdf")).rejects.toThrow(/n-up/i);
await expect(pdfcpuNup("/fake.pdf", 7 as never, "/out.pdf")).rejects.toThrow(/n-up/i);
});
it("rejects invalid booklet value", async () => {
await expect(pdfcpuBooklet("/fake.pdf", 3 as never, "/out.pdf")).rejects.toThrow(/booklet/i);
await expect(pdfcpuBooklet("/fake.pdf", 5 as never, "/out.pdf")).rejects.toThrow(/booklet/i);
});
});