|
| 1 | +import { describe, expect, test, beforeEach, afterEach } from 'vitest' |
| 2 | +import { renderContent } from '#src/content-render/index.js' |
| 3 | +import { TitleFromAutotitleError } from '#src/content-render/unified/rewrite-local-links.js' |
| 4 | + |
| 5 | +describe('link error line numbers', () => { |
| 6 | + let fs |
| 7 | + let originalReadFileSync |
| 8 | + let originalExistsSync |
| 9 | + let mockContext |
| 10 | + |
| 11 | + beforeEach(async () => { |
| 12 | + // Set up file system mocking |
| 13 | + fs = await import('fs') |
| 14 | + originalReadFileSync = fs.default.readFileSync |
| 15 | + originalExistsSync = fs.default.existsSync |
| 16 | + |
| 17 | + fs.default.existsSync = () => true |
| 18 | + |
| 19 | + // Set up basic mock context |
| 20 | + mockContext = { |
| 21 | + currentLanguage: 'en', |
| 22 | + currentVersion: 'free-pro-team@latest', |
| 23 | + pages: new Map(), |
| 24 | + redirects: new Map(), |
| 25 | + page: { |
| 26 | + fullPath: '/fake/test-file.md', |
| 27 | + }, |
| 28 | + } |
| 29 | + }) |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + // Restore original functions |
| 33 | + fs.default.readFileSync = originalReadFileSync |
| 34 | + fs.default.existsSync = originalExistsSync |
| 35 | + }) |
| 36 | + |
| 37 | + test('reports correct line numbers for broken AUTOTITLE links', async () => { |
| 38 | + // Test content with frontmatter followed by content with a broken link |
| 39 | + const template = `--- |
| 40 | +title: Test Page |
| 41 | +version: 1.0 |
| 42 | +--- |
| 43 | +
|
| 44 | +# Introduction |
| 45 | +
|
| 46 | +This is some content. |
| 47 | +
|
| 48 | +Here is a broken link: [AUTOTITLE](/nonexistent/page). |
| 49 | +
|
| 50 | +More content here.` |
| 51 | + |
| 52 | + fs.default.readFileSync = () => template |
| 53 | + |
| 54 | + try { |
| 55 | + await renderContent(template, mockContext) |
| 56 | + expect.fail('Expected TitleFromAutotitleError to be thrown') |
| 57 | + } catch (error) { |
| 58 | + expect(error).toBeInstanceOf(TitleFromAutotitleError) |
| 59 | + |
| 60 | + // The broken link is on line 10 in the original file |
| 61 | + // (3 lines of frontmatter + 1 blank line + 1 title + 1 blank + 1 content + 1 blank + 1 link line) |
| 62 | + // The error message should reference the correct line number |
| 63 | + expect(error.message).toContain('/nonexistent/page') |
| 64 | + expect(error.message).toContain('could not be resolved') |
| 65 | + expect(error.message).toContain('(Line: 10)') |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + test('reports correct line numbers with different frontmatter sizes', async () => { |
| 70 | + mockContext.page.fullPath = '/fake/test-file-2.md' |
| 71 | + |
| 72 | + // Test with more extensive frontmatter |
| 73 | + const template = `--- |
| 74 | +title: Another Test Page |
| 75 | +description: This is a test |
| 76 | +author: Test Author |
| 77 | +date: 2024-01-01 |
| 78 | +tags: |
| 79 | + - test |
| 80 | + - documentation |
| 81 | +version: 2.0 |
| 82 | +--- |
| 83 | +
|
| 84 | +# Main Title |
| 85 | +
|
| 86 | +Some introductory text here. |
| 87 | +
|
| 88 | +## Section |
| 89 | +
|
| 90 | +Content with a [AUTOTITLE](/another/nonexistent/page) link.` |
| 91 | + |
| 92 | + fs.default.readFileSync = () => template |
| 93 | + |
| 94 | + try { |
| 95 | + await renderContent(template, mockContext) |
| 96 | + expect.fail('Expected TitleFromAutotitleError to be thrown') |
| 97 | + } catch (error) { |
| 98 | + expect(error).toBeInstanceOf(TitleFromAutotitleError) |
| 99 | + expect(error.message).toContain('/another/nonexistent/page') |
| 100 | + expect(error.message).toContain('could not be resolved') |
| 101 | + } |
| 102 | + }) |
| 103 | + |
| 104 | + test('handles files without frontmatter correctly', async () => { |
| 105 | + mockContext.page.fullPath = '/fake/no-frontmatter.md' |
| 106 | + |
| 107 | + // Test content without frontmatter |
| 108 | + const template = `# Simple Title |
| 109 | +
|
| 110 | +This is content without frontmatter. |
| 111 | +
|
| 112 | +Here is a broken link: [AUTOTITLE](/missing/page).` |
| 113 | + |
| 114 | + fs.default.readFileSync = () => template |
| 115 | + |
| 116 | + try { |
| 117 | + await renderContent(template, mockContext) |
| 118 | + expect.fail('Expected TitleFromAutotitleError to be thrown') |
| 119 | + } catch (error) { |
| 120 | + expect(error).toBeInstanceOf(TitleFromAutotitleError) |
| 121 | + expect(error.message).toContain('/missing/page') |
| 122 | + expect(error.message).toContain('could not be resolved') |
| 123 | + } |
| 124 | + }) |
| 125 | + |
| 126 | + test('error message format is improved', async () => { |
| 127 | + mockContext.page.fullPath = '/fake/message-test.md' |
| 128 | + |
| 129 | + const template = `--- |
| 130 | +title: Message Test |
| 131 | +--- |
| 132 | +
|
| 133 | +[AUTOTITLE](/test/broken/link) |
| 134 | +` |
| 135 | + |
| 136 | + fs.default.readFileSync = () => template |
| 137 | + |
| 138 | + try { |
| 139 | + await renderContent(template, mockContext) |
| 140 | + expect.fail('Expected TitleFromAutotitleError to be thrown') |
| 141 | + } catch (error) { |
| 142 | + expect(error).toBeInstanceOf(TitleFromAutotitleError) |
| 143 | + |
| 144 | + // Check that the new error message format is used |
| 145 | + expect(error.message).toContain('could not be resolved in one or more versions') |
| 146 | + expect(error.message).toContain('Make sure that this link can be reached from all versions') |
| 147 | + expect(error.message).toContain('/test/broken/link') |
| 148 | + |
| 149 | + // Check that the old error message format is NOT used |
| 150 | + expect(error.message).not.toContain('Unable to find Page by') |
| 151 | + expect(error.message).not.toContain('To fix it, look at') |
| 152 | + } |
| 153 | + }) |
| 154 | +}) |
0 commit comments