refactoring, numbering plan
This commit is contained in:
1 parent
b5279434b2
commit
3b04039aa9
47 files changed
+3485
-953
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import { expect, test, type Page } from '@playwright/test';
|
||||
import { PDFDocument } from 'pdf-lib';
|
||||
import { PAGE_NUMBER_MARGIN } from '../../src/lib/pdf-pagination';
|
||||
import {
|
||||
createEncryptedPdf,
|
||||
createPdf,
|
||||
@@ -8,10 +9,21 @@ import {
|
||||
expectPdfDownload,
|
||||
getPageContentText,
|
||||
getPageText,
|
||||
getPageTextAngle,
|
||||
getPageTextMatrices,
|
||||
getPageTextValues,
|
||||
pdfDropzone,
|
||||
trackPageErrors
|
||||
} from './helpers';
|
||||
|
||||
/**
|
||||
* Number of text operands equal to `label` on a page. The source documents carry
|
||||
* their own text, so this counts the stamped sheet label instead of comparing
|
||||
* the whole page.
|
||||
*/
|
||||
const countLabel = (document: PDFDocument, pageIndex: number, label: string) =>
|
||||
getPageTextValues(document, pageIndex).filter((value) => value === label).length;
|
||||
|
||||
const downloadPaginated = async (page: Page, fileName: RegExp) => {
|
||||
const downloadPromise = page.waitForEvent('download');
|
||||
await page.getByRole('button', { name: 'Paginieren & herunterladen' }).click();
|
||||
@@ -41,9 +53,10 @@ test('pagination stamps every page with an Aktenzeichen and the configured start
|
||||
const bytes = await downloadPaginated(page, /^akte_paginiert\.pdf$/);
|
||||
const output = await PDFDocument.load(bytes);
|
||||
expect(output.getPageCount()).toBe(3);
|
||||
expect(getPageText(output, 0)).toContain('123 Ja 60/26, Bl. 26');
|
||||
expect(getPageText(output, 1)).toContain('123 Ja 60/26, Bl. 27');
|
||||
expect(getPageText(output, 2)).toContain('123 Ja 60/26, Bl. 28');
|
||||
// Exactly one label per page: a duplicated draw would still pass `toContain`.
|
||||
expect(countLabel(output, 0, '123 Ja 60/26, Bl. 26')).toBe(1);
|
||||
expect(countLabel(output, 1, '123 Ja 60/26, Bl. 27')).toBe(1);
|
||||
expect(countLabel(output, 2, '123 Ja 60/26, Bl. 28')).toBe(1);
|
||||
expect(pageErrors).toEqual([]);
|
||||
});
|
||||
|
||||
@@ -111,30 +124,38 @@ test('pagination keeps the page number upright on rotated scans', async ({ page
|
||||
const output = await PDFDocument.load(bytes);
|
||||
expect(output.getPageCount()).toBe(2);
|
||||
|
||||
// Page 1 is stored with /Rotate 90: the text is counter-rotated so it reads
|
||||
// `createPreRotatedPdf` stores 420×594 pages and puts /Rotate 90 on page 1, so
|
||||
// the page is displayed as 594×420. The label is counter-rotated so it reads
|
||||
// horizontally on screen instead of sideways.
|
||||
const rotatedContent = getPageContentText(output, 0);
|
||||
expect(getPageText(output, 0)).toContain('Bl. 1');
|
||||
const rotatedMatrix =
|
||||
/([-\d.]+) ([-\d.]+) ([-\d.]+) ([-\d.]+) ([-\d.]+) ([-\d.]+) Tm\n<426C2E2031> Tj/.exec(
|
||||
rotatedContent
|
||||
);
|
||||
expect(rotatedMatrix).not.toBeNull();
|
||||
const [a, b, c, d, x, y] = rotatedMatrix!.slice(1).map(Number);
|
||||
// 90° counter-clockwise in user space cancels the viewer's page rotation.
|
||||
expect(Math.abs(a)).toBeLessThan(0.001);
|
||||
expect(b).toBeCloseTo(1, 3);
|
||||
expect(c).toBeCloseTo(-1, 3);
|
||||
expect(Math.abs(d)).toBeLessThan(0.001);
|
||||
// Visual bottom center of the 594×420 display: 28 pt margin, centered.
|
||||
expect(x).toBeGreaterThan(384);
|
||||
expect(x).toBeLessThan(396);
|
||||
expect(y).toBeGreaterThan(280);
|
||||
expect(y).toBeLessThan(292);
|
||||
// The stamp is the last text drawn on the page.
|
||||
const rotated = getPageTextMatrices(output, 0).at(-1)!;
|
||||
expect(Math.round(getPageTextAngle(rotated))).toBe(90);
|
||||
expect(countLabel(output, 0, 'Bl. 1')).toBe(1);
|
||||
// Visual bottom centre maps back into user space as x = stored width − margin.
|
||||
expect(rotated.x).toBeCloseTo(420 - PAGE_NUMBER_MARGIN, -1);
|
||||
// …and stays horizontally centred on the displayed width.
|
||||
expect(Math.abs(rotated.y - 594 / 2)).toBeLessThan(15);
|
||||
|
||||
const plainContent = getPageContentText(output, 1);
|
||||
expect(getPageText(output, 1)).toContain('Bl. 2');
|
||||
expect(plainContent).toMatch(/1 0 0 1 [-\d.]+ [-\d.]+ Tm\n<426C2E2032> Tj/);
|
||||
const plain = getPageTextMatrices(output, 1).at(-1)!;
|
||||
expect(Math.round(getPageTextAngle(plain))).toBe(0);
|
||||
expect(countLabel(output, 1, 'Bl. 2')).toBe(1);
|
||||
expect(pageErrors).toEqual([]);
|
||||
});
|
||||
|
||||
test('pagination offers the "Seite" format and applies it to every page', async ({ page }) => {
|
||||
const pageErrors = trackPageErrors(page);
|
||||
await page.goto('/tools/pagination');
|
||||
await dropFiles(page, pdfDropzone(page), [
|
||||
{ name: 'formate.pdf', mimeType: 'application/pdf', buffer: await createPdf(2, 'Formate') }
|
||||
]);
|
||||
|
||||
await page.locator('#pagination-format').selectOption('seite');
|
||||
await expect(page.getByTestId('pagination-example')).toHaveText('Beispiel: Seite 1');
|
||||
|
||||
const bytes = await downloadPaginated(page, /^formate_paginiert\.pdf$/);
|
||||
const output = await PDFDocument.load(bytes);
|
||||
expect(countLabel(output, 0, 'Seite 1')).toBe(1);
|
||||
expect(countLabel(output, 1, 'Seite 2')).toBe(1);
|
||||
expect(pageErrors).toEqual([]);
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user