import unittest from typing import Iterator import xml.etree.ElementTree as ET from sia import util class UtilTest(unittest.TestCase): def test_stop_before_value(self): # Helper function to create iterator from list def create_iterator(items: list) -> Iterator[str]: for item in items: yield item # Test case 1: Stop value in middle of sequence input_sequence = ['hello', 'world', 'STOP', 'ignored'] result = list(util.stop_before_value(create_iterator(input_sequence), 'STOP')) self.assertEqual(result, ['hello', 'world']) # Test case 2: Stop value not in sequence input_sequence = ['hello', 'world'] result = list(util.stop_before_value(create_iterator(input_sequence), 'STOP')) self.assertEqual(result, ['hello', 'world']) # Test case 3: Stop value at start of sequence input_sequence = ['STOP', 'ignored'] result = list(util.stop_before_value(create_iterator(input_sequence), 'STOP')) self.assertEqual(result, []) # Test case 4: Stop value as part of an item input_sequence = ['hello', 'woSTOPrld', 'ignored'] result = list(util.stop_before_value(create_iterator(input_sequence), 'STOP')) self.assertEqual(result, ['hello', 'wo']) # Test case 5: Empty sequence input_sequence = [] result = list(util.stop_before_value(create_iterator(input_sequence), 'STOP')) self.assertEqual(result, []) def test_none_value(self): """Test that None values are converted to empty strings""" self.assertEqual(util.escape_text_for_xml(None), '') def test_number_values(self): """Test that numbers are properly converted to strings and wrapped in CDATA""" self.assertEqual(util.escape_text_for_xml(42), '') self.assertEqual(util.escape_text_for_xml(3.14), '') self.assertEqual(util.escape_text_for_xml(-100), '') self.assertEqual(util.escape_text_for_xml(0), '') def test_simple_strings(self): """Test that simple strings are wrapped in CDATA""" self.assertEqual(util.escape_text_for_xml('hello'), '') self.assertEqual(util.escape_text_for_xml(''), '') self.assertEqual(util.escape_text_for_xml(' '), '') def test_strings_with_special_characters(self): """Test strings containing XML special characters but no CDATA end sequence""" self.assertEqual(util.escape_text_for_xml(''), ']]>') self.assertEqual(util.escape_text_for_xml('a & b'), '') self.assertEqual(util.escape_text_for_xml('"quoted"'), '') self.assertEqual(util.escape_text_for_xml("'single'"), "") self.assertEqual(util.escape_text_for_xml('') def test_strings_with_cdata_end_sequence(self): """Test strings containing CDATA end sequence are properly escaped""" # Simple case with just the CDATA end sequence self.assertEqual(util.escape_text_for_xml(']]>'), ']]>') # CDATA end sequence with other special characters self.assertEqual( util.escape_text_for_xml('Text with ]]> and & ampersands'), 'Text with ]]> and <tags> & ampersands' ) # Multiple CDATA end sequences self.assertEqual( util.escape_text_for_xml('Multiple ]]> end ]]> sequences'), 'Multiple ]]> end ]]> sequences' ) def test_multiline_strings(self): """Test multiline strings are properly handled""" multiline = """Line 1 Line 2 Line 3""" self.assertEqual(util.escape_text_for_xml(multiline), '') multiline_with_cdata = """Line 1 Line ]]> 2 Line 3""" self.assertEqual( util.escape_text_for_xml(multiline_with_cdata), 'Line 1\n Line ]]> 2\n Line 3' ) def test_edge_cases(self): """Test edge cases and unusual inputs""" # Empty string self.assertEqual(util.escape_text_for_xml(''), '') # String with only whitespace self.assertEqual(util.escape_text_for_xml('\n\t '), '') # String with Unicode characters self.assertEqual(util.escape_text_for_xml('Hello 世界'), '') # String with control characters self.assertEqual(util.escape_text_for_xml('Hello\0World'), '') # Very long string long_string = 'x' * 1000 self.assertEqual(util.escape_text_for_xml(long_string), f'') def test_boolean_values(self): """Test boolean values are properly converted""" self.assertEqual(util.escape_text_for_xml(True), '') self.assertEqual(util.escape_text_for_xml(False), '') def test_pretty_print_element_basic(self): """Test basic element printing""" elem = ET.Element('root') child = ET.SubElement(elem, 'child', {'attr': 'value'}) child.text = 'text' expected = ''' ''' self.assertEqual(util.pretty_print_element(elem).strip(), expected.strip()) def test_pretty_print_element_long_attributes(self): """Test printing elements with long attributes that need wrapping""" elem = ET.Element('root', { 'very_long_attribute_1': 'value1', 'very_long_attribute_2': 'value2', 'very_long_attribute_3': 'value3' }) result = util.pretty_print_element(elem, max_line=40) # Verify attributes are wrapped to new lines self.assertIn('\n very_long_attribute_1="value1"', result) self.assertIn('\n very_long_attribute_2="value2"', result) self.assertIn('\n very_long_attribute_3="value3"', result) def test_pretty_print_element_cdata(self): """Test printing elements with CDATA sections""" elem = ET.Element('root') elem.text = 'preserved\n spacing' expected = ''' ''' self.assertEqual(util.pretty_print_element(elem).strip(), expected.strip()) def test_pretty_print_element_empty(self): """Test printing empty elements""" elem = ET.Element('empty') self.assertEqual(util.pretty_print_element(elem), '') def test_real_world_context(self): """Test formatting a realistic context example""" # Create a context element with system metrics and a repeat entry context = ET.Element('context', { 'time': '2024-10-18T12:00:00Z', 'cpu': '12', 'gpu': '26', 'memory_used': '9556302234', 'memory_total': '17179869184', 'disk_used': '244434939904', 'disk_total': '273145991168', 'context': '3', 'stdin': '0' }) repeat = ET.SubElement(context, 'repeat', { 'id': 'a3d89ee5-28ec-4c5a-b9e9-a30af53d43a0', 'exit_code': '0' }) repeat.text = 'ls -lah /' stdout = ET.SubElement(repeat, 'stdout') stdout.text = '''total 16K drwxr-xr-x 1 sia 1049089 0 Oct 28 13:40 ./ drwxr-xr-x 1 sia 1049089 0 Oct 28 13:40 ../ drwxr-xr-x 1 sia 1049089 0 Oct 28 13:40 tasks/ drwxr-xr-x 1 sia 1049089 0 Oct 28 13:40 user/''' stderr = ET.SubElement(repeat, 'stderr') formatted = util.pretty_print_element(context) # Verify key formatting expectations self.assertIn(' ', formatted) self.assertIn(' ', formatted) self.assertIn(' ', formatted) self.assertIn(' & " \'' result = util.pretty_print_element(elem) # Should be escaped or wrapped in CDATA self.assertTrue( ('< > & " '' in result) or (' & " \']]>' in result) ) def test_mixed_content(self): """Test formatting elements with mixed text and child nodes""" elem = ET.Element('root') elem.text = 'before' child = ET.SubElement(elem, 'child') child.tail = 'after' result = util.pretty_print_element(elem) # Verify text positioning self.assertIn('before', result) self.assertIn('after', result)