import { describe, expect, it } from 'vitest'
import { validateXmlDocument, XML_PARSER_ERROR_NAMESPACES } from '../src/lib/xml'
function parserWithCandidates(candidates: Array<{ localName: string; namespaceURI: string | null; textContent: string | null }>) {
return {
parseFromString: () => ({
getElementsByTagName: (localName: string) => {
expect(localName).toBe('parsererror')
return candidates
},
}),
}
}
describe('XML validation', () => {
it('accepts valid content even when the user root element is named parsererror', () => {
expect(() => validateXmlDocument('valid', parserWithCandidates([
{ localName: 'parsererror', namespaceURI: null, textContent: 'valid' },
]))).not.toThrow()
})
it('reports parser errors from the browser parser-error namespace', () => {
for (const namespaceURI of XML_PARSER_ERROR_NAMESPACES) {
expect(() => validateXmlDocument('', parserWithCandidates([
{ localName: 'parsererror', namespaceURI, textContent: 'XML parsing error\nDetails' },
]))).toThrow('XML parsing error')
}
})
it('rejects empty input before invoking the parser', () => {
expect(() => validateXmlDocument(' ', parserWithCandidates([]))).toThrow('请输入 XML')
})
})