Skip to content

Commit 3e8e0cb

Browse files
claude: Add PDF layout verification test infrastructure
Add ensurePdfTextPositions test assertion for verifying element positions in tagged PDFs. Supports: - Role-based element selection (P, Figure, Table, H1, Lbl, Reference, Page) - Spatial relations (left_of, right_of, above, below) - Distance constraints (within_x, within_y) - Edge constraints for precise positioning - Zod schema validation for test specifications Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 50db5a5 commit 3e8e0cb

11 files changed

Lines changed: 1988 additions & 0 deletions

File tree

‎src/import_map.json‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757

5858
"puppeteer": "https://deno.land/x/puppeteer@9.0.2/mod.ts",
5959

60+
"pdfjs-dist": "npm:pdfjs-dist@4.9.155",
61+
6062
"https://deno.land/std@0.196.0/console/unicode_width.ts": "https://deno.land/std@0.224.0/console/unicode_width.ts",
6163
"https://deno.land/std@0.161.0/": "https://deno.land/std@0.217.0/",
6264
"https://deno.land/std@0.101.0/": "https://deno.land/std@0.217.0/",

‎tests/docs/smoke-all/typst/pandoc-template-features.qmd‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ format:
3434
_quarto:
3535
tests:
3636
typst:
37+
ensurePdfMetadata:
38+
title: "Test Document"
39+
author: "Alice Smith"
40+
keywords:
41+
- quarto
42+
- typst
43+
- testing
44+
creator: "Typst"
3745
ensureTypstFileRegexMatches:
3846
- # Patterns that MUST be found
3947
# content-to-string function defined (from Pandoc)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
title: TITLE_UNIQUE_TEXT
3+
format: typst
4+
header-includes: |
5+
#set page(
6+
header: [PAGEHEADER_UNIQUE_TEXT],
7+
footer: [PAGEFOOTER_UNIQUE_TEXT]
8+
)
9+
_quarto:
10+
tests:
11+
typst:
12+
ensurePdfTextPositions:
13+
- # Vertical ordering: header < title < h1 < body < footer (y increases downward)
14+
- subject:
15+
text: "PAGEHEADER_UNIQUE_TEXT"
16+
role: "Decoration"
17+
relation: above
18+
object: "TITLE_UNIQUE_TEXT"
19+
- subject: "TITLE_UNIQUE_TEXT"
20+
relation: above
21+
object: "H1_UNIQUE_TEXT"
22+
- subject: "H1_UNIQUE_TEXT"
23+
relation: above
24+
object: "BODY_UNIQUE_TEXT"
25+
- subject: "BODY_UNIQUE_TEXT"
26+
relation: above
27+
object:
28+
text: "PAGEFOOTER_UNIQUE_TEXT"
29+
role: "Decoration"
30+
# Margin content should be to the right of body and on the same row
31+
- subject: "MARGIN_UNIQUE_TEXT"
32+
relation: rightOf
33+
object: "BODY_UNIQUE_TEXT"
34+
- subject: "MARGIN_UNIQUE_TEXT"
35+
relation: topAligned
36+
object: "BODY_UNIQUE_TEXT"
37+
---
38+
39+
# H1_UNIQUE_TEXT
40+
41+
::: {.column-margin}
42+
MARGIN_UNIQUE_TEXT
43+
:::
44+
45+
BODY_UNIQUE_TEXT is the main paragraph content positioned in the body column.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
title: "PDF Metadata Test Fixture"
3+
author: "Test Author Name"
4+
subject: "Testing PDF Metadata Extraction"
5+
keywords:
6+
- quarto
7+
- typst
8+
- metadata
9+
- testing
10+
format: typst
11+
---
12+
13+
# Test Content
14+
15+
This document tests PDF metadata extraction.
16+
17+
The title, author, subject, and keywords should all appear in the PDF metadata.
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
title: FIXTURE_TITLE_TEXT
3+
format: typst
4+
header-includes: |
5+
#set page(
6+
header: [FIXTURE_HEADER_TEXT],
7+
footer: [FIXTURE_FOOTER_TEXT]
8+
)
9+
---
10+
11+
# FIXTURE_H1_TEXT {#sec-main}
12+
13+
This is the first paragraph. FIXTURE_BODY_P1_TEXT is a unique marker in the first paragraph.
14+
15+
## FIXTURE_H2_TEXT {#sec-second}
16+
17+
::: {.column-margin}
18+
FIXTURE_MARGIN_TEXT is in the margin column.
19+
:::
20+
21+
FIXTURE_BODY_P2_TEXT is a unique marker in the second paragraph, which appears alongside the margin content.
22+
23+
### FIXTURE_H3_TEXT {#sec-third}
24+
25+
More body content here. FIXTURE_BODY_P3_TEXT marks the third paragraph.
26+
27+
## Labeled Elements {#sec-labeled}
28+
29+
### Figure with Label {#sec-figure}
30+
31+
![FIXTURE_FIG_CAPTION_TEXT](data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTAwIiBoZWlnaHQ9IjUwIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjxyZWN0IHdpZHRoPSIxMDAiIGhlaWdodD0iNTAiIGZpbGw9IiNjY2MiLz48dGV4dCB4PSI1MCIgeT0iMjUiIHRleHQtYW5jaG9yPSJtaWRkbGUiIGR5PSIuM2VtIj5JbWFnZTwvdGV4dD48L3N2Zz4=){#fig-sample}
32+
33+
See @fig-sample for the sample figure.
34+
35+
### Table with Label {#sec-table}
36+
37+
| Column A | Column B |
38+
|----------|----------|
39+
| FIXTURE_TBL_CELL_A1 | Cell B1 |
40+
| Cell A2 | Cell B2 |
41+
42+
: FIXTURE_TBL_CAPTION_TEXT {#tbl-data}
43+
44+
See @tbl-data for the data table.
45+
46+
### Equation with Label {#sec-equation}
47+
48+
$$
49+
E = mc^2
50+
$$ {#eq-energy}
51+
52+
FIXTURE_EQ_REF_TEXT: See @eq-energy for the energy equation.
53+
54+
### Code Listing with Label {#sec-listing}
55+
56+
```{#lst-hello .python lst-cap="FIXTURE_LST_CAPTION_TEXT"}
57+
print("FIXTURE_CODE_TEXT")
58+
```
59+
60+
See @lst-hello for the code listing.
61+
62+
### Theorem with Label {#sec-theorem}
63+
64+
::: {#thm-main}
65+
## FIXTURE_THM_TITLE_TEXT
66+
67+
FIXTURE_THM_BODY_TEXT: This is the main theorem content.
68+
:::
69+
70+
See @thm-main for the main theorem.
71+
72+
### Definition with Label {#sec-definition}
73+
74+
::: {#def-concept}
75+
## FIXTURE_DEF_TITLE_TEXT
76+
77+
FIXTURE_DEF_BODY_TEXT: This defines an important concept.
78+
:::
79+
80+
See @def-concept for the definition.
81+
82+
{{< pagebreak >}}
83+
84+
# FIXTURE_PAGE2_H1_TEXT {#sec-page2}
85+
86+
FIXTURE_PAGE2_BODY_TEXT is on the second page for testing cross-page assertions.
87+
88+
## Raw Typst Labels {#sec-raw-typst}
89+
90+
```{=typst}
91+
#figure(
92+
rect(width: 50pt, height: 30pt, fill: gray)[FIXTURE_TYPST_FIG_TEXT],
93+
caption: [FIXTURE_TYPST_FIG_CAPTION]
94+
) <fig-typst-raw>
95+
```
96+
97+
FIXTURE_TYPST_REF_TEXT: See the raw Typst figure above.

‎tests/smoke/smoke-all.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import {
2424
ensureFileRegexMatches,
2525
ensureHtmlElements,
2626
ensurePdfRegexMatches,
27+
ensurePdfTextPositions,
28+
ensurePdfMetadata,
2729
ensureJatsXpath,
2830
ensureOdtXpath,
2931
ensurePptxRegexMatches,
@@ -183,6 +185,8 @@ function resolveTestSpecs(
183185
ensureOdtXpath,
184186
ensureJatsXpath,
185187
ensurePdfRegexMatches,
188+
ensurePdfTextPositions,
189+
ensurePdfMetadata,
186190
ensurePptxRegexMatches,
187191
ensurePptxXpath,
188192
ensurePptxLayout,
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*
2+
* pdf-metadata.test.ts
3+
*
4+
* Tests for the ensurePdfMetadata verify predicate.
5+
* Renders a fixture document and runs various assertions including expected failures.
6+
*
7+
* Copyright (C) 2020-2025 Posit Software, PBC
8+
*/
9+
10+
import { testQuartoCmd } from "../../test.ts";
11+
import { ensurePdfMetadata } from "../../verify-pdf-metadata.ts";
12+
import { assert } from "testing/asserts";
13+
import { join } from "../../../src/deno_ral/path.ts";
14+
import { safeRemoveSync, safeExistsSync } from "../../../src/core/path.ts";
15+
16+
const fixtureDir = "docs/verify/pdf-metadata";
17+
const fixtureQmd = join(fixtureDir, "fixture.qmd");
18+
const fixturePdf = join(fixtureDir, "fixture.pdf");
19+
20+
/**
21+
* Helper to assert that a function throws with error message matching a pattern
22+
*/
23+
async function assertThrowsWithPattern(
24+
fn: () => Promise<void>,
25+
pattern: RegExp,
26+
description: string,
27+
) {
28+
let threw = false;
29+
let errorMessage = "";
30+
try {
31+
await fn();
32+
} catch (e) {
33+
threw = true;
34+
errorMessage = e instanceof Error ? e.message : String(e);
35+
}
36+
37+
assert(threw, `Expected to throw for: ${description}`);
38+
assert(
39+
pattern.test(errorMessage),
40+
`Error message "${errorMessage}" did not match pattern ${pattern} for: ${description}`,
41+
);
42+
}
43+
44+
// Test: Render fixture and run assertions
45+
testQuartoCmd("render", [fixtureQmd, "--to", "typst"], [], {
46+
teardown: async () => {
47+
// Run the test assertions after render completes
48+
await runPositiveTests();
49+
await runExpectedFailureTests();
50+
51+
// Cleanup
52+
if (safeExistsSync(fixturePdf)) {
53+
safeRemoveSync(fixturePdf);
54+
}
55+
},
56+
});
57+
58+
/**
59+
* Test positive assertions that should pass
60+
*/
61+
async function runPositiveTests() {
62+
// Test 1: Title contains expected text
63+
const titleTest = ensurePdfMetadata(fixturePdf, {
64+
title: "PDF Metadata Test Fixture",
65+
});
66+
await titleTest.verify([]);
67+
68+
// Test 2: Author contains expected text
69+
const authorTest = ensurePdfMetadata(fixturePdf, {
70+
author: "Test Author Name",
71+
});
72+
await authorTest.verify([]);
73+
74+
// Test 3: Keywords contain expected values (as array)
75+
const keywordsTest = ensurePdfMetadata(fixturePdf, {
76+
keywords: ["quarto", "typst"],
77+
});
78+
await keywordsTest.verify([]);
79+
80+
// Test 4: Title matches regex
81+
const regexTest = ensurePdfMetadata(fixturePdf, {
82+
title: /PDF.*Fixture/,
83+
});
84+
await regexTest.verify([]);
85+
86+
// Test 5: Multiple fields at once
87+
const multiTest = ensurePdfMetadata(fixturePdf, {
88+
title: "Metadata",
89+
author: "Author",
90+
keywords: ["testing"],
91+
});
92+
await multiTest.verify([]);
93+
94+
// Test 6: Creator field (should contain "Typst" since we render with Typst)
95+
const creatorTest = ensurePdfMetadata(fixturePdf, {
96+
creator: /typst/i,
97+
});
98+
await creatorTest.verify([]);
99+
}
100+
101+
/**
102+
* Test expected failures - each should throw with specific error messages
103+
*/
104+
async function runExpectedFailureTests() {
105+
// Error 1: Title mismatch
106+
await assertThrowsWithPattern(
107+
async () => {
108+
const predicate = ensurePdfMetadata(fixturePdf, {
109+
title: "NONEXISTENT_TITLE_12345",
110+
});
111+
await predicate.verify([]);
112+
},
113+
/title.*expected.*NONEXISTENT_TITLE_12345/i,
114+
"Title mismatch error",
115+
);
116+
117+
// Error 2: Author mismatch
118+
await assertThrowsWithPattern(
119+
async () => {
120+
const predicate = ensurePdfMetadata(fixturePdf, {
121+
author: "NONEXISTENT_AUTHOR_12345",
122+
});
123+
await predicate.verify([]);
124+
},
125+
/author.*expected.*NONEXISTENT_AUTHOR_12345/i,
126+
"Author mismatch error",
127+
);
128+
129+
// Error 3: Keywords mismatch
130+
await assertThrowsWithPattern(
131+
async () => {
132+
const predicate = ensurePdfMetadata(fixturePdf, {
133+
keywords: ["NONEXISTENT_KEYWORD_12345"],
134+
});
135+
await predicate.verify([]);
136+
},
137+
/keywords.*expected.*NONEXISTENT_KEYWORD_12345/i,
138+
"Keywords mismatch error",
139+
);
140+
141+
// Error 4: Regex mismatch
142+
await assertThrowsWithPattern(
143+
async () => {
144+
const predicate = ensurePdfMetadata(fixturePdf, {
145+
title: /^EXACT_NONEXISTENT$/,
146+
});
147+
await predicate.verify([]);
148+
},
149+
/title.*expected.*match/i,
150+
"Regex mismatch error",
151+
);
152+
}

0 commit comments

Comments
 (0)