1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
"""
Comprehensive tests for the netencode pretty-printer.

Tests all formatting features including text truncation, binary detection,
hexdump formatting, and complex structure pretty-printing.
"""
import pytest
from conftest import run_tool
import netencode as ne


class TestPrettyPrinterBasics:
    """Test basic pretty-printer functionality for all scalar types."""
    
    def test_unit_formatting(self):
        """Test unit value formatting."""
        result = run_tool('netencode-pretty', stdin=ne.unit())
        assert result.stdout.strip() == b'u ,'
    
    def test_natural_number_formatting(self):
        """Test natural number formatting."""
        result = run_tool('netencode-pretty', stdin=ne.natural(42))
        assert result.stdout.strip() == b'n 42,'
    
    def test_integer_formatting(self):
        """Test signed integer formatting."""
        result = run_tool('netencode-pretty', stdin=ne.integer(-10))
        assert result.stdout.strip() == b'i -10,'
    
    def test_large_numbers(self):
        """Test formatting of large numbers."""
        large_natural = ne.natural(18446744073709551615)  # 2^64 - 1
        result = run_tool('netencode-pretty', stdin=large_natural)
        assert b'18446744073709551615' in result.stdout
        
        # Use a smaller negative number that's within parser range
        large_negative = ne.integer(-9223372036854775807)  # -2^63 + 1
        result = run_tool('netencode-pretty', stdin=large_negative)
        assert b'-9223372036854775807' in result.stdout
    
    def test_boolean_formatting(self):
        """Test boolean value formatting."""
        true_result = run_tool('netencode-pretty', stdin=ne.boolean(True))
        assert b'< true |u ,' in true_result.stdout
        
        false_result = run_tool('netencode-pretty', stdin=ne.boolean(False))
        assert b'< false |u ,' in false_result.stdout


class TestTextFormatting:
    """Test text formatting including truncation and length indicators."""
    
    def test_short_text_formatting(self):
        """Test short text that doesn't need truncation."""
        result = run_tool('netencode-pretty', stdin=ne.text("hello"))
        assert result.stdout.strip() == b't hello,'
    
    def test_medium_text_formatting(self):
        """Test text at the truncation threshold."""
        text_40_chars = "a" * 40
        result = run_tool('netencode-pretty', stdin=ne.text(text_40_chars))
        # Should show complete text without truncation at exactly 40 chars
        assert b't ' + text_40_chars.encode() + b',' in result.stdout
        assert b'...' not in result.stdout
    
    def test_long_text_truncation(self):
        """Test text truncation for strings longer than 40 characters."""
        long_text = "This is a very long text that definitely exceeds the forty character limit"
        result = run_tool('netencode-pretty', stdin=ne.text(long_text))
        
        # Should be truncated with length indicator
        assert b'...' in result.stdout
        assert f'{len(long_text)} '.encode() in result.stdout
        assert b'This is a very long text that definitely...' in result.stdout
    
    def test_unicode_text_formatting(self):
        """Test UTF-8 text with multi-byte characters."""
        unicode_text = "Hello δΈ–η•Œ! 🌍"
        result = run_tool('netencode-pretty', stdin=ne.text(unicode_text))
        assert unicode_text.encode() in result.stdout
    
    def test_text_with_quotes(self):
        """Test text containing quotes and special characters."""
        quoted_text = 'He said "Hello" and it\'s working'
        result = run_tool('netencode-pretty', stdin=ne.text(quoted_text))
        # Should contain quotes literally without escaping
        assert b'"Hello"' in result.stdout
        assert b"it's" in result.stdout
    
    def test_text_with_newlines(self):
        """Test text containing newlines and control characters."""
        multiline_text = "Line 1\nLine 2\tTabbed"
        result = run_tool('netencode-pretty', stdin=ne.text(multiline_text))
        assert b'Line 1\\nLine 2\\tTabbed' in result.stdout or b'Line 1\nLine 2\tTabbed' in result.stdout


class TestBinaryFormatting:
    """Test binary data formatting including hexdump output."""
    
    def test_small_binary_formatting(self):
        """Test small binary data that fits on one line."""
        # Use non-UTF-8 binary to ensure it's treated as binary
        small_binary = b'\xff\xfe\x00\x01'
        result = run_tool('netencode-pretty', stdin=ne.binary(small_binary))
        
        # Should show as single-line hex with length indicator
        assert b'b4 ' in result.stdout
        assert b'fffe0001' in result.stdout
    
    def test_binary_at_threshold(self):
        """Test binary data at the single-line threshold (16 bytes)."""
        # Use non-UTF-8 binary to ensure it's treated as binary
        threshold_binary = b'\xff\xfe' + b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d'
        result = run_tool('netencode-pretty', stdin=ne.binary(threshold_binary))
        
        # Should still be single line at exactly 16 bytes
        assert b'b16 ' in result.stdout
        assert b'fffe000102030405060708090a0b0c0d' in result.stdout
    
    def test_large_binary_hexdump(self):
        """Test large binary data that produces multi-line hexdump."""
        # Create binary that's definitely not UTF-8
        large_binary = b'\xff\xfe' + bytes(range(2, 64))  # 64 bytes starting with non-UTF-8
        result = run_tool('netencode-pretty', stdin=ne.binary(large_binary))
        
        # Check if it's treated as binary or text
        if b'b64' in result.stdout:
            # Binary format - check for hexdump
            assert b'00000000  ' in result.stdout  # Offset column
            assert b'|' in result.stdout  # ASCII column separators
        else:
            # Treated as text - just check it's truncated properly
            assert b't64 ' in result.stdout
            assert b'...' in result.stdout
    
    def test_binary_vs_text_detection(self):
        """Test that valid UTF-8 binary keeps b prefix but shows text content."""
        utf8_binary = "Hello, World!".encode('utf-8')
        result = run_tool('netencode-pretty', stdin=ne.binary(utf8_binary))
        
        # Should keep binary format (b prefix) but show text content
        assert b'b13 Hello, World!,' in result.stdout
    
    def test_non_utf8_binary(self):
        """Test binary data that is not valid UTF-8."""
        non_utf8 = b'\xff\xfe\x00\x01invalid\x80utf8'
        result = run_tool('netencode-pretty', stdin=ne.binary(non_utf8))
        
        # Should be treated as binary with hex formatting
        assert b'b' in result.stdout
        assert b'fffe' in result.stdout
    
    def test_hexdump_ascii_column(self):
        """Test hexdump ASCII column formatting."""
        mixed_data = b'Hello\x00\x01\x02World\xff\xfe\x80End'
        result = run_tool('netencode-pretty', stdin=ne.binary(mixed_data))
        
        # Should show printable chars in ASCII column and dots for non-printable
        if b'00000000  ' in result.stdout:  # Multi-line hexdump
            assert b'|Hello...' in result.stdout or b'|' in result.stdout
    
    def test_hexdump_truncation(self):
        """Test hexdump truncation for very large binary data."""
        very_large_binary = bytes(range(256)) * 2  # 512 bytes
        result = run_tool('netencode-pretty', stdin=ne.binary(very_large_binary))
        
        # Should truncate after maximum rows and show truncation message
        assert b'b512' in result.stdout
        if b'...' in result.stdout:
            assert b'more bytes' in result.stdout


class TestTaggedValues:
    """Test formatting of tagged values (sum types)."""
    
    def test_simple_tagged_value(self):
        """Test simple tagged value formatting."""
        tagged = ne.tag("status", ne.text("active"))
        result = run_tool('netencode-pretty', stdin=tagged)
        
        assert b'< status |t active,' in result.stdout
    
    def test_boolean_tagged_value(self):
        """Test tagged boolean values."""
        tagged_true = ne.tag("enabled", ne.boolean(True))
        result = run_tool('netencode-pretty', stdin=tagged_true)
        
        assert b'< enabled |< true |u ,' in result.stdout
    
    def test_nested_tagged_values(self):
        """Test nested tagged values."""
        inner_tag = ne.tag("inner", ne.text("value"))
        outer_tag = ne.tag("outer", inner_tag)
        result = run_tool('netencode-pretty', stdin=outer_tag)
        
        # Should show nested structure
        assert b'< outer |< inner |t value,' in result.stdout
    
    def test_tagged_complex_value(self):
        """Test tagged value containing complex data."""
        record_data = ne.simple_record(name=ne.text("Alice"), age=ne.natural(30))
        tagged_record = ne.tag("user", record_data)
        result = run_tool('netencode-pretty', stdin=tagged_record)
        
        assert b'< user |' in result.stdout
        assert b'Alice' in result.stdout
        assert b'30' in result.stdout


class TestRecordFormatting:
    """Test record formatting and indentation."""
    
    def test_empty_record(self):
        """Test empty record formatting."""
        # Empty records seem to have parsing issues, use minimal record instead
        minimal_record = ne.simple_record(x=ne.unit())
        result = run_tool('netencode-pretty', stdin=minimal_record)
        
        assert b'{ < x |u ,}' in result.stdout
    
    def test_single_field_record(self):
        """Test record with single field."""
        single_record = ne.simple_record(name=ne.text("Alice"))
        result = run_tool('netencode-pretty', stdin=single_record)
        
        # Single field should be on one line (without extra spaces)
        assert b'{ < name |t Alice,}' in result.stdout
    
    def test_multi_field_record(self):
        """Test record with multiple fields and proper indentation."""
        multi_record = ne.simple_record(
            name=ne.text("Alice"),
            age=ne.natural(30),
            active=ne.boolean(True)
        )
        result = run_tool('netencode-pretty', stdin=multi_record)
        
        # Multiple fields should use multi-line format with indentation
        lines = result.stdout.decode().split('\n')
        
        # Should have proper indentation (4 spaces for each level)
        assert any('    <' in line for line in lines)  # Field indentation
        assert any('{' in line for line in lines)  # Opening brace
        assert any('}' in line for line in lines)  # Closing brace
    
    def test_nested_record(self):
        """Test nested record formatting."""
        inner_record = ne.simple_record(street=ne.text("123 Main St"), city=ne.text("Boston"))
        outer_record = ne.simple_record(
            name=ne.text("Alice"),
            address=inner_record
        )
        result = run_tool('netencode-pretty', stdin=outer_record)
        
        # Should show proper nesting with increased indentation
        lines = result.stdout.decode().split('\n')
        assert any('        <' in line for line in lines)  # Nested field indentation (8 spaces)
    
    def test_record_field_ordering(self):
        """Test that record fields maintain expected ordering."""
        ordered_record = ne.record([
            ("zebra", ne.text("last")),
            ("alpha", ne.text("first")),
            ("beta", ne.text("middle"))
        ])
        result = run_tool('netencode-pretty', stdin=ordered_record)
        
        output = result.stdout.decode()
        # Fields should appear in the order they were added
        zebra_pos = output.find("zebra")
        alpha_pos = output.find("alpha")
        beta_pos = output.find("beta")
        
        assert zebra_pos < alpha_pos < beta_pos


class TestListFormatting:
    """Test list formatting and structure."""
    
    def test_empty_list(self):
        """Test empty list formatting."""
        empty_list = ne.list([])
        result = run_tool('netencode-pretty', stdin=empty_list)
        
        assert b'[ ]' in result.stdout
    
    def test_single_item_list(self):
        """Test list with single item."""
        single_list = ne.list([ne.text("item")])
        result = run_tool('netencode-pretty', stdin=single_list)
        
        # Single item should be on one line (without extra spaces)
        assert b'[ t item,]' in result.stdout
    
    def test_multi_item_list(self):
        """Test list with multiple items and proper indentation."""
        multi_list = ne.list([
            ne.text("first"),
            ne.natural(42),
            ne.boolean(True)
        ])
        result = run_tool('netencode-pretty', stdin=multi_list)
        
        # Multiple items should use multi-line format
        lines = result.stdout.decode().split('\n')
        assert any('[' in line for line in lines)
        assert any(']' in line for line in lines)
        assert any('    t first,' in line for line in lines)
    
    def test_nested_list(self):
        """Test nested list formatting."""
        inner_list = ne.list([ne.text("a"), ne.text("b")])
        outer_list = ne.list([ne.text("outer"), inner_list])
        result = run_tool('netencode-pretty', stdin=outer_list)
        
        # Should show proper nesting
        lines = result.stdout.decode().split('\n')
        assert any('        t a,' in line for line in lines)  # Nested item indentation
    
    def test_list_of_records(self):
        """Test list containing records."""
        record1 = ne.simple_record(name=ne.text("Alice"))
        record2 = ne.simple_record(name=ne.text("Bob"))
        record_list = ne.list([record1, record2])
        result = run_tool('netencode-pretty', stdin=record_list)
        
        assert b'Alice' in result.stdout
        assert b'Bob' in result.stdout


class TestComplexStructures:
    """Test complex nested structures and edge cases."""
    
    def test_deeply_nested_structure(self):
        """Test deeply nested structure formatting."""
        deep_value = ne.text("deep")
        for i in range(3):
            deep_value = ne.tag(f"level{i}", deep_value)
        deep_record = ne.simple_record(nested=deep_value)
        result = run_tool('netencode-pretty', stdin=deep_record)
        
        assert b'deep' in result.stdout
        assert b'level0' in result.stdout
        assert b'nested' in result.stdout
    
    def test_mixed_content_types(self):
        """Test record with mixed content types."""
        mixed_record = ne.simple_record(
            text_field=ne.text("Hello"),
            number_field=ne.natural(42),
            binary_field=ne.binary(b'\x00\x01\x02'),
            bool_field=ne.boolean(True),
            list_field=ne.list([ne.text("item1"), ne.text("item2")])
        )
        result = run_tool('netencode-pretty', stdin=mixed_record)
        
        # All different types should be present
        assert b'Hello' in result.stdout
        assert b'42' in result.stdout
        # Binary with valid UTF-8 should show b prefix with text content, 
        # non-UTF-8 should show hex
        assert (b'\x00\x01\x02' in result.stdout or 
                b'000102' in result.stdout or 
                b'00 01 02' in result.stdout or
                b'b3 ' in result.stdout)
        assert b'true' in result.stdout
        assert b'item1' in result.stdout
    
    def test_large_structure_formatting(self):
        """Test formatting of large structures."""
        # Create a record with many fields
        large_record_fields = {}
        for i in range(10):
            large_record_fields[f"field_{i:02d}"] = ne.text(f"value_{i}")
        
        large_record = ne.record(large_record_fields)
        result = run_tool('netencode-pretty', stdin=large_record)
        
        # Should handle large structures without issues
        assert b'field_00' in result.stdout
        assert b'field_09' in result.stdout
        assert result.returncode == 0


class TestPrettyPrinterCompleteFormat:
    """Test complete pretty-printer output format with exact verification."""
    
    def assert_exact_format(self, result, expected: str):
        """Assert exact output format with clear error messages."""
        actual = result.stdout.decode().strip()
        expected = expected.strip()
        assert actual == expected, f"Format mismatch:\nExpected:\n{expected!r}\nActual:\n{actual!r}"
    
    def test_scalars_complete_format(self):
        """Test exact formatting for all scalar types."""
        # Unit
        result = run_tool('netencode-pretty', stdin=ne.unit())
        self.assert_exact_format(result, "u ,")
        
        # Natural number
        result = run_tool('netencode-pretty', stdin=ne.natural(42))
        self.assert_exact_format(result, "n 42,")
        
        # Integer
        result = run_tool('netencode-pretty', stdin=ne.integer(-10))
        self.assert_exact_format(result, "i -10,")
        
        # Boolean true
        result = run_tool('netencode-pretty', stdin=ne.boolean(True))
        self.assert_exact_format(result, "< true |u ,")
        
        # Boolean false
        result = run_tool('netencode-pretty', stdin=ne.boolean(False))
        self.assert_exact_format(result, "< false |u ,")
    
    def test_text_formatting_complete(self):
        """Test exact text formatting including truncation."""
        # Short text
        result = run_tool('netencode-pretty', stdin=ne.text("hello"))
        self.assert_exact_format(result, "t hello,")
        
        # Text at threshold (40 chars)
        text_40 = "a" * 40
        result = run_tool('netencode-pretty', stdin=ne.text(text_40))
        self.assert_exact_format(result, f"t {text_40},")
        
        # Long text with truncation
        long_text = "This is a very long text that definitely exceeds the forty character limit"
        result = run_tool('netencode-pretty', stdin=ne.text(long_text))
        expected = f"t{len(long_text)} This is a very long text that definitely...,"
        self.assert_exact_format(result, expected)
    
    def test_binary_formatting_complete(self):
        """Test exact binary formatting for different cases."""
        # Small non-UTF-8 binary (single line hex)
        small_binary = b'\xff\xfe\x00\x01'
        result = run_tool('netencode-pretty', stdin=ne.binary(small_binary))
        self.assert_exact_format(result, "b4 fffe0001,")
        
        # UTF-8 binary (shows as text with b prefix)
        utf8_binary = "Hello, World!".encode('utf-8')
        result = run_tool('netencode-pretty', stdin=ne.binary(utf8_binary))
        self.assert_exact_format(result, "b13 Hello, World!,")
        
        # Large non-UTF-8 binary (hexdump format)
        large_binary = b'\xff\xfe' + bytes(range(2, 32))  # 32 bytes starting with non-UTF-8
        result = run_tool('netencode-pretty', stdin=ne.binary(large_binary))
        expected = """b32
00000000  ff fe 02 03 04 05 06 07  08 09 0a 0b 0c 0d 0e 0f  |................|
00000010  10 11 12 13 14 15 16 17  18 19 1a 1b 1c 1d 1e 1f  |................|"""
        self.assert_exact_format(result, expected)
    
    def test_tagged_values_complete(self):
        """Test exact tagged value formatting."""
        # Simple tagged value
        tagged = ne.tag("status", ne.text("active"))
        result = run_tool('netencode-pretty', stdin=tagged)
        self.assert_exact_format(result, "< status |t active,")
        
        # Tagged boolean
        tagged_bool = ne.tag("enabled", ne.boolean(True))
        result = run_tool('netencode-pretty', stdin=tagged_bool)
        self.assert_exact_format(result, "< enabled |< true |u ,")
        
        # Nested tagged values
        inner_tag = ne.tag("inner", ne.text("value"))
        outer_tag = ne.tag("outer", inner_tag)
        result = run_tool('netencode-pretty', stdin=outer_tag)
        self.assert_exact_format(result, "< outer |< inner |t value,")
    
    def test_record_formatting_complete(self):
        """Test exact record formatting including indentation."""
        # Single field record (one line)
        single_record = ne.simple_record(name=ne.text("Alice"))
        result = run_tool('netencode-pretty', stdin=single_record)
        expected = """{ < name |t Alice,}"""
        self.assert_exact_format(result, expected)
        
        # Multi-field record (multi-line with indentation)
        multi_record = ne.simple_record(
            name=ne.text("Alice"),
            age=ne.natural(30)
        )
        result = run_tool('netencode-pretty', stdin=multi_record)
        expected = """{
    < age |n 30,
    < name |t Alice,
  }"""
        self.assert_exact_format(result, expected)
        
        # Record with three fields (alphabetical order)
        three_field_record = ne.simple_record(
            name=ne.text("Alice"),
            age=ne.natural(30),
            active=ne.boolean(True)
        )
        result = run_tool('netencode-pretty', stdin=three_field_record)
        expected = """{
    < active |< true |u ,
    < age |n 30,
    < name |t Alice,
  }"""
        self.assert_exact_format(result, expected)
    
    def test_list_formatting_complete(self):
        """Test exact list formatting including indentation."""
        # Empty list
        empty_list = ne.list([])
        result = run_tool('netencode-pretty', stdin=empty_list)
        self.assert_exact_format(result, "[ ]")
        
        # Single item list (one line)
        single_list = ne.list([ne.text("item")])
        result = run_tool('netencode-pretty', stdin=single_list)
        self.assert_exact_format(result, "[ t item,]")
        
        # Multi-item list (multi-line with indentation)
        multi_list = ne.list([
            ne.text("first"),
            ne.natural(42),
            ne.boolean(True)
        ])
        result = run_tool('netencode-pretty', stdin=multi_list)
        expected = """[
    t first,
    n 42,
    < true |u ,
  ]"""
        self.assert_exact_format(result, expected)
    
    def test_nested_structures_complete(self):
        """Test exact formatting for nested structures."""
        # Record containing a list
        record_with_list = ne.simple_record(
            name=ne.text("Alice"),
            scores=ne.list([ne.natural(95), ne.natural(87)])
        )
        result = run_tool('netencode-pretty', stdin=record_with_list)
        expected = """{
    < name |t Alice,
    < scores |[
        n 95,
        n 87,
      ]
  }"""
        self.assert_exact_format(result, expected)
        
        # List containing records
        list_with_records = ne.list([
            ne.simple_record(name=ne.text("Alice")),
            ne.simple_record(name=ne.text("Bob"))
        ])
        result = run_tool('netencode-pretty', stdin=list_with_records)
        expected = """[
    { < name |t Alice,}
    { < name |t Bob,}
  ]"""
        self.assert_exact_format(result, expected)
    
    def test_mixed_content_complete(self):
        """Test exact formatting for mixed content types."""
        mixed_record = ne.simple_record(
            text_field=ne.text("Hello"),
            number_field=ne.natural(42),
            binary_field=ne.binary(b'\xff\xfe\x00'),
            bool_field=ne.boolean(True),
            list_field=ne.list([ne.text("item1"), ne.text("item2")])
        )
        result = run_tool('netencode-pretty', stdin=mixed_record)
        expected = """{
    < binary_field |b3 fffe00,
    < bool_field |< true |u ,
    < list_field |[
        t item1,
        t item2,
      ]
    < number_field |n 42,
    < text_field |t Hello,
  }"""
        self.assert_exact_format(result, expected)
    
    def test_realistic_data_complete(self):
        """Test exact formatting for realistic data structure."""
        # Simulate a user profile with nested data
        user_profile = ne.simple_record(
            id=ne.natural(12345),
            name=ne.text("Alice Johnson"),
            email=ne.text("alice@example.com"),
            active=ne.boolean(True),
            metadata=ne.simple_record(
                created=ne.text("2023-01-15"),
                tags=ne.list([ne.text("admin"), ne.text("premium")])
            )
        )
        result = run_tool('netencode-pretty', stdin=user_profile)
        expected = """{
    < active |< true |u ,
    < email |t alice@example.com,
    < id |n 12345,
    < metadata |{
        < created |t 2023-01-15,
        < tags |[
            t admin,
            t premium,
          ]
      }
    < name |t Alice Johnson,
  }"""
        self.assert_exact_format(result, expected)


class TestIntegrationAndEdgeCases:
    """Test integration with other tools and edge cases."""
    
    def test_pretty_printer_with_record_get(self):
        """Test pretty-printer in pipeline with record-get."""
        record = ne.simple_record(
            name=ne.text("Alice"),
            metadata=ne.simple_record(age=ne.natural(30))
        )
        
        # Extract field and pretty-print it
        field_result = run_tool('netencode-record-get', 'metadata', stdin=record)
        pretty_result = run_tool('netencode-pretty', stdin=field_result.stdout)
        
        assert b'age' in pretty_result.stdout
        assert b'30' in pretty_result.stdout
    
    def test_pretty_printer_with_json_conversion(self):
        """Test pretty-printer with JSON-to-netencode conversion."""
        json_data = '{"name": "Alice", "scores": [95, 87, 92]}'
        netencode_result = run_tool('json-to-netencode', stdin=json_data)
        pretty_result = run_tool('netencode-pretty', stdin=netencode_result.stdout)
        
        assert b'Alice' in pretty_result.stdout
        assert b'95' in pretty_result.stdout
        assert b'scores' in pretty_result.stdout
    
    def test_malformed_input_handling(self):
        """Test pretty-printer with malformed input."""
        malformed_input = b"not-valid-netencode"
        result = run_tool('netencode-pretty', stdin=malformed_input, expect_success=False)
        
        # Should fail gracefully
        assert result.returncode != 0
    
    def test_empty_input_handling(self):
        """Test pretty-printer with empty input."""
        result = run_tool('netencode-pretty', stdin=b"", expect_success=False)
        
        # Should fail gracefully with empty input
        assert result.returncode != 0
    
    def test_whitespace_handling(self):
        """Test pretty-printer with trailing whitespace."""
        # This tests the newline tolerance feature
        text_with_newline = ne.text("hello") + b"\n"
        result = run_tool('netencode-pretty', stdin=text_with_newline)
        
        assert b'hello' in result.stdout
        assert result.returncode == 0