-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_output_manager_standalone.py
More file actions
96 lines (78 loc) · 3.11 KB
/
Copy pathtest_output_manager_standalone.py
File metadata and controls
96 lines (78 loc) · 3.11 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
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
#!/usr/bin/env python3
"""Test script to verify the GradioOutputManager works correctly."""
class GradioOutputManager:
"""Manages Gradio outputs by name instead of index to prevent order-related bugs."""
def __init__(self, output_names):
"""Initialize with a list of output component names in the expected order."""
self.output_names = output_names
self.output_map = {name: idx for idx, name in enumerate(output_names)}
self._values = [None] * len(output_names)
def set(self, name, value):
"""Set a value for a named output."""
if name not in self.output_map:
raise ValueError(f"Unknown output name: {name}. Available: {list(self.output_map.keys())}")
self._values[self.output_map[name]] = value
return self
def set_multiple(self, **kwargs):
"""Set multiple outputs at once."""
for name, value in kwargs.items():
self.set(name, value)
return self
def get_tuple(self):
"""Return the values as a tuple in the correct order."""
return tuple(self._values)
def reset(self):
"""Reset all values to None."""
self._values = [None] * len(self.output_names)
return self
# Test output schema
FEEDBACK_OUTPUTS = [
"recommendations", # 0
"keep_btn", # 1
"adjust_btn", # 2
"reject_btn", # 3
"rec_index_state", # 4
"feedback_log_state", # 5
"chatbox", # 6
"agent_memory", # 7
"chat_input", # 8
"send_btn", # 9
"new_recs_btn", # 10
"memory_display" # 11
]
def test_output_manager():
"""Test the output manager functionality."""
print("Testing GradioOutputManager...")
# Create an output manager for feedback outputs
outputs = GradioOutputManager(FEEDBACK_OUTPUTS)
# Test setting individual values
outputs.set("recommendations", "test_recommendation")
outputs.set("chatbox", [{"role": "assistant", "content": "test"}])
# Test setting multiple values
outputs.set_multiple(
keep_btn="keep_value",
adjust_btn="adjust_value",
rec_index_state=5
)
# Get the tuple
result = outputs.get_tuple()
print(f"Output length: {len(result)}")
print(f"Expected length: {len(FEEDBACK_OUTPUTS)}")
print(f"Match: {len(result) == len(FEEDBACK_OUTPUTS)}")
# Check specific values
print(f"recommendations (index 0): {result[0]}")
print(f"keep_btn (index 1): {result[1]}")
print(f"chatbox (index 6): {result[6]}")
print(f"rec_index_state (index 4): {result[4]}")
# Test error handling
try:
outputs.set("invalid_output", "test")
print("ERROR: Should have raised ValueError for invalid output name")
except ValueError as e:
print(f"✅ Correctly caught error: {e}")
# Test that unset values are None
print(f"reject_btn (index 3, unset): {result[3]}")
print(f"send_btn (index 9, unset): {result[9]}")
print("✅ All tests passed!")
if __name__ == "__main__":
test_output_manager()