xml-tool-output.spec.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { describe, expect, it } from 'vitest'
  2. import { renderUnknownXml } from '../src/components/xml-tool-output.ts'
  3. const render = (source: string, limit = 4, expanded = false): string[] | undefined => renderUnknownXml(
  4. source,
  5. limit,
  6. expanded,
  7. text => text.replace(/[\u0000-\u0009\u000b-\u001f\u007f-\u009f]/gu, control =>
  8. `\\x${control.charCodeAt(0).toString(16).padStart(2, '0')}`),
  9. text => `[label]${text}[/label]`,
  10. text => `[body]${text}[/body]`,
  11. count => ` … +${count} lines`,
  12. )
  13. describe('unknown-tool XML rendering', () => {
  14. it('renders nested elements and attributes as an indented tree', () => {
  15. expect(render(`<result>
  16. <path>/tmp/a.txt</path>
  17. <type>file</type>
  18. <content>
  19. <line number="1">hello</line>
  20. <line number="2">world</line>
  21. </content>
  22. </result>`)).toEqual([
  23. '[label]result[/label]',
  24. ' [label]path:[/label] [body]/tmp/a.txt[/body]',
  25. ' [label]type:[/label] [body]file[/body]',
  26. ' [label]content[/label]',
  27. ' [label]line (number="1"):[/label] [body]hello[/body]',
  28. ' [label]line (number="2"):[/label] [body]world[/body]',
  29. ])
  30. })
  31. it('renders root text, CDATA, empty elements, and multiline nested text', () => {
  32. expect(render(' <result>\nfirst\nsecond\n</result> ')).toEqual([
  33. '[label]result[/label]',
  34. ' [body]first[/body]',
  35. ' [body]second[/body]',
  36. ])
  37. expect(render('<result>\nfirst\nsecond\n</result>', 1, true)).toEqual([
  38. '[label]result[/label]',
  39. ' [body]first[/body]',
  40. ' [body]second[/body]',
  41. ])
  42. expect(render('<result><value><![CDATA[literal <xml>]]></value><empty /></result>')).toEqual([
  43. '[label]result[/label]',
  44. ' [label]value:[/label] [body]literal <xml>[/body]',
  45. ' [label]empty[/label]',
  46. ])
  47. // An interior blank line stays the empty string: styling it would emit an
  48. // escape-only row, which reads as a stray indented blank rather than a gap.
  49. expect(render('<result>\nfirst\n\nsecond\n</result>', 4, true)).toEqual([
  50. '[label]result[/label]',
  51. ' [body]first[/body]',
  52. '',
  53. ' [body]second[/body]',
  54. ])
  55. })
  56. it('previews each top-level child independently and expands all rows', () => {
  57. const xml = '<result><first>\na\nb\nc\nd\ne\nf\n</first><second>\ng\nh\ni\nj\nk\nl\n</second></result>'
  58. expect(render(xml, 3)).toEqual([
  59. '[label]result[/label]',
  60. ' [label]first[/label]',
  61. ' [body]a[/body]',
  62. ' … +4 lines',
  63. ' [body]f[/body]',
  64. ' [label]second[/label]',
  65. ' [body]g[/body]',
  66. ' … +4 lines',
  67. ' [body]l[/body]',
  68. ])
  69. expect(render(xml, 3, true)).toHaveLength(15)
  70. })
  71. it('bounds the collapsed child count and counts the hidden lines', () => {
  72. const xml = `<result>${Array.from({ length: 8 }, (_, index) => `<item>${index}</item>`).join('')}</result>`
  73. expect(render(xml, 3)).toEqual([
  74. '[label]result[/label]',
  75. ' [label]item:[/label] [body]0[/body]',
  76. ' [label]item:[/label] [body]1[/body]',
  77. ' … +5 lines',
  78. ' [label]item:[/label] [body]7[/body]',
  79. ])
  80. expect(render(xml, 3, true)).toHaveLength(9)
  81. })
  82. it('escapes control characters expanded from character references', () => {
  83. expect(render('<result attr="a&#155;b">tab&#9;csi&#155;</result>')).toEqual([
  84. '[label]result (attr="a\\\\x9bb")[/label]',
  85. ' [body]tab\\x09csi\\x9b[/body]',
  86. ])
  87. expect(render('<result><value><![CDATA[del\u007f]]></value></result>')).toEqual([
  88. '[label]result[/label]',
  89. ' [label]value:[/label] [body]del\\x7f[/body]',
  90. ])
  91. })
  92. it.each([
  93. '<result><path>missing close</result>',
  94. '<first /><second />',
  95. '<result /> <![CDATA[trailing]]>',
  96. 'prefix <result><path>/tmp/a</path></result>',
  97. '<result><path>/tmp/a</path></result> suffix',
  98. '<?xml version="1.0"?><result />',
  99. '<result><?target value?></result>',
  100. '<!DOCTYPE result><result />',
  101. '<result><!-- comment --></result>',
  102. '',
  103. ' \n ',
  104. ])('declines malformed or mixed text: %s', (source) => {
  105. expect(render(source)).toBeUndefined()
  106. })
  107. })