1
0
mirror of synced 2025-12-19 18:10:59 -05:00

Migrate 6 JavaScript files to TypeScript (#57969)

This commit is contained in:
Kevin Heis
2025-10-15 09:43:53 -07:00
committed by GitHub
parent 5268d1a084
commit a415892096
12 changed files with 152 additions and 106 deletions

View File

@@ -1,12 +1,13 @@
import { describe, expect, test, beforeEach, afterEach } from 'vitest'
import { renderContent } from '@/content-render/index'
import { TitleFromAutotitleError } from '@/content-render/unified/rewrite-local-links'
import type { Context } from '@/types'
describe('link error line numbers', () => {
let fs
let originalReadFileSync
let originalExistsSync
let mockContext
let fs: any // Dynamic import of fs module for mocking in tests
let originalReadFileSync: any // Storing original fs.readFileSync for restoration after test
let originalExistsSync: any // Storing original fs.existsSync for restoration after test
let mockContext: Context
beforeEach(async () => {
// Set up file system mocking
@@ -20,11 +21,11 @@ describe('link error line numbers', () => {
mockContext = {
currentLanguage: 'en',
currentVersion: 'free-pro-team@latest',
pages: new Map(),
redirects: new Map(),
pages: {} as any,
redirects: {} as any,
page: {
fullPath: '/fake/test-file.md',
},
} as any,
}
})
@@ -60,14 +61,16 @@ More content here.`
// The broken link is on line 10 in the original file
// (3 lines of frontmatter + 1 blank line + 1 title + 1 blank + 1 content + 1 blank + 1 link line)
// The error message should reference the correct line number
expect(error.message).toContain('/nonexistent/page')
expect(error.message).toContain('could not be resolved')
expect(error.message).toContain('(Line: 10)')
expect((error as TitleFromAutotitleError).message).toContain('/nonexistent/page')
expect((error as TitleFromAutotitleError).message).toContain('could not be resolved')
expect((error as TitleFromAutotitleError).message).toContain('(Line: 10)')
}
})
test('reports correct line numbers with different frontmatter sizes', async () => {
mockContext.page.fullPath = '/fake/test-file-2.md'
mockContext.page = {
fullPath: '/fake/test-file-2.md',
} as any
// Test with more extensive frontmatter
const template = `---
@@ -96,13 +99,15 @@ Content with a [AUTOTITLE](/another/nonexistent/page) link.`
expect.fail('Expected TitleFromAutotitleError to be thrown')
} catch (error) {
expect(error).toBeInstanceOf(TitleFromAutotitleError)
expect(error.message).toContain('/another/nonexistent/page')
expect(error.message).toContain('could not be resolved')
expect((error as TitleFromAutotitleError).message).toContain('/another/nonexistent/page')
expect((error as TitleFromAutotitleError).message).toContain('could not be resolved')
}
})
test('handles files without frontmatter correctly', async () => {
mockContext.page.fullPath = '/fake/no-frontmatter.md'
mockContext.page = {
fullPath: '/fake/no-frontmatter.md',
} as any
// Test content without frontmatter
const template = `# Simple Title
@@ -118,13 +123,15 @@ Here is a broken link: [AUTOTITLE](/missing/page).`
expect.fail('Expected TitleFromAutotitleError to be thrown')
} catch (error) {
expect(error).toBeInstanceOf(TitleFromAutotitleError)
expect(error.message).toContain('/missing/page')
expect(error.message).toContain('could not be resolved')
expect((error as TitleFromAutotitleError).message).toContain('/missing/page')
expect((error as TitleFromAutotitleError).message).toContain('could not be resolved')
}
})
test('error message format is improved', async () => {
mockContext.page.fullPath = '/fake/message-test.md'
mockContext.page = {
fullPath: '/fake/message-test.md',
} as any
const template = `---
title: Message Test
@@ -142,13 +149,17 @@ title: Message Test
expect(error).toBeInstanceOf(TitleFromAutotitleError)
// Check that the new error message format is used
expect(error.message).toContain('could not be resolved in one or more versions')
expect(error.message).toContain('Make sure that this link can be reached from all versions')
expect(error.message).toContain('/test/broken/link')
expect((error as TitleFromAutotitleError).message).toContain(
'could not be resolved in one or more versions',
)
expect((error as TitleFromAutotitleError).message).toContain(
'Make sure that this link can be reached from all versions',
)
expect((error as TitleFromAutotitleError).message).toContain('/test/broken/link')
// Check that the old error message format is NOT used
expect(error.message).not.toContain('Unable to find Page by')
expect(error.message).not.toContain('To fix it, look at')
expect((error as TitleFromAutotitleError).message).not.toContain('Unable to find Page by')
expect((error as TitleFromAutotitleError).message).not.toContain('To fix it, look at')
}
})
})