forked from koaning/bespoken
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_streaming_padding.py
More file actions
60 lines (48 loc) · 1.87 KB
/
test_streaming_padding.py
File metadata and controls
60 lines (48 loc) · 1.87 KB
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
#!/usr/bin/env python3
"""Test script to reproduce the streaming padding bug."""
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
from bespoken import ui
from io import StringIO
import contextlib
def capture_console_output():
"""Capture what gets printed to the console."""
# Create a StringIO object to capture output
captured_output = StringIO()
# Temporarily replace the console's file with our StringIO
original_file = ui._console.file
ui._console.file = captured_output
try:
# Test the streaming behavior
ui.start_streaming(ui.LEFT_PADDING)
ui.stream_chunk("Hello world this is a test", ui.LEFT_PADDING)
ui.end_streaming(ui.LEFT_PADDING)
# Get the captured output
output = captured_output.getvalue()
return output
finally:
# Restore the original file
ui._console.file = original_file
def test_streaming_padding():
"""Test that streaming properly applies padding to the first chunk."""
print("Testing streaming padding...")
# Capture the output
output = capture_console_output()
# Print the raw output for inspection
print("Raw output (with repr to see spaces):")
print(repr(output))
# Check if the output starts with the expected padding
expected_padding = " " # 2 spaces for LEFT_PADDING
# The output should start with padding
if output.startswith(expected_padding):
print("✓ PASS: Output starts with proper padding")
return True
else:
print("✗ FAIL: Output does not start with proper padding")
print(f"Expected to start with: {repr(expected_padding)}")
print(f"Actually starts with: {repr(output[:10])}")
return False
if __name__ == "__main__":
success = test_streaming_padding()
sys.exit(0 if success else 1)