import { describe, it, expect } from 'vitest'
import { getXmlTag, getXmlChildren } from '../src/prompts/xml.js'
describe('getXmlTag', () => {
it('extracts simple tag content', () => {
expect(getXmlTag('
Hello World', 'title')).toBe('Hello World')
})
it('extracts multiline content', () => {
expect(getXmlTag('\nLine 1\nLine 2\n', 'narrative')).toBe('Line 1\nLine 2')
})
it('returns empty string for missing tag', () => {
expect(getXmlTag('Hello', 'missing')).toBe('')
})
it('returns first match for duplicate tags', () => {
expect(getXmlTag('FirstSecond', 'title')).toBe('First')
})
it('returns empty string for empty tag', () => {
expect(getXmlTag('', 'title')).toBe('')
})
it('trims whitespace', () => {
expect(getXmlTag(' trimmed ', 'title')).toBe('trimmed')
})
it('returns empty for invalid tag names', () => {
expect(getXmlTag('bar', '.*')).toBe('')
})
it('returns empty for tag with special regex chars', () => {
expect(getXmlTag('bar', 'a(b')).toBe('')
})
})
describe('getXmlChildren', () => {
it('extracts child elements', () => {
const xml = 'OneTwo'
expect(getXmlChildren(xml, 'facts', 'fact')).toEqual(['One', 'Two'])
})
it('returns empty array for missing parent', () => {
expect(getXmlChildren('bar', 'facts', 'fact')).toEqual([])
})
it('returns empty array for missing children', () => {
expect(getXmlChildren('', 'facts', 'fact')).toEqual([])
})
it('trims child content', () => {
const xml = ' trimmed '
expect(getXmlChildren(xml, 'facts', 'fact')).toEqual(['trimmed'])
})
it('handles multiline children', () => {
const xml = 'Use JWT\nfor auth'
expect(getXmlChildren(xml, 'decisions', 'decision')).toEqual(['Use JWT\nfor auth'])
})
it('returns empty for invalid parent tag name', () => {
expect(getXmlChildren('A', '.*', 'fact')).toEqual([])
})
})