|
| 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