|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Script to fix all ConfigLoader tests to use top-level keys.""" |
| 3 | + |
| 4 | +import re |
| 5 | +import os |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +def fix_agent_tests(): |
| 9 | + """Fix agent configuration tests.""" |
| 10 | + test_files = [ |
| 11 | + "tests/strands/experimental/config_loader/agent/test_agent_config_loader_structured_output.py", |
| 12 | + "tests/strands/experimental/config_loader/agent/test_integration.py" |
| 13 | + ] |
| 14 | + |
| 15 | + for test_file in test_files: |
| 16 | + if not os.path.exists(test_file): |
| 17 | + continue |
| 18 | + |
| 19 | + with open(test_file, 'r') as f: |
| 20 | + content = f.read() |
| 21 | + |
| 22 | + # Fix config dictionaries that need agent wrapper |
| 23 | + # Pattern 1: Simple configs without schemas |
| 24 | + pattern1 = r'config = \{\s*\n(\s*)"name":\s*"[^"]*",\s*\n(\s*)"model":\s*"[^"]*",\s*\n(\s*)"system_prompt":\s*"[^"]*",?\s*\n(\s*)"structured_output":\s*[^}]*\n(\s*)\}' |
| 25 | + |
| 26 | + def replace_simple_config(match): |
| 27 | + indent = match.group(1) |
| 28 | + return f'''config = {{ |
| 29 | +{indent}"agent": {{ |
| 30 | +{match.group(1)}"name": {match.group(0).split('"name": ')[1].split(',')[0]}, |
| 31 | +{match.group(2)}"model": {match.group(0).split('"model": ')[1].split(',')[0]}, |
| 32 | +{match.group(3)}"system_prompt": {match.group(0).split('"system_prompt": ')[1].split(',')[0]}, |
| 33 | +{match.group(4)}"structured_output": {match.group(0).split('"structured_output": ')[1].split('\n')[0]} |
| 34 | +{indent}}} |
| 35 | +{match.group(5)}}}''' |
| 36 | + |
| 37 | + # Pattern 2: Configs with schemas at top level |
| 38 | + pattern2 = r'config = \{\s*\n(\s*)"schemas":\s*\[[^\]]*\],\s*\n(\s*)"name":\s*"[^"]*",\s*\n(\s*)"model":\s*"[^"]*",\s*\n(\s*)"system_prompt":\s*"[^"]*",?\s*\n(\s*)"structured_output":\s*[^}]*\n(\s*)\}' |
| 39 | + |
| 40 | + # For now, let's do a simpler approach - just wrap everything that doesn't have agent key |
| 41 | + lines = content.split('\n') |
| 42 | + new_lines = [] |
| 43 | + in_config = False |
| 44 | + config_indent = "" |
| 45 | + config_lines = [] |
| 46 | + |
| 47 | + i = 0 |
| 48 | + while i < len(lines): |
| 49 | + line = lines[i] |
| 50 | + |
| 51 | + if 'config = {' in line and '"agent"' not in line: |
| 52 | + # Start of config that needs fixing |
| 53 | + in_config = True |
| 54 | + config_indent = line[:line.index('config')] |
| 55 | + config_lines = [line] |
| 56 | + |
| 57 | + # Find the matching closing brace |
| 58 | + brace_count = line.count('{') - line.count('}') |
| 59 | + i += 1 |
| 60 | + |
| 61 | + while i < len(lines) and brace_count > 0: |
| 62 | + line = lines[i] |
| 63 | + config_lines.append(line) |
| 64 | + brace_count += line.count('{') - line.count('}') |
| 65 | + i += 1 |
| 66 | + |
| 67 | + # Now fix the config |
| 68 | + config_content = '\n'.join(config_lines) |
| 69 | + |
| 70 | + # Check if it has schemas at top level |
| 71 | + if '"schemas"' in config_content: |
| 72 | + # Extract schemas and agent parts |
| 73 | + fixed_config = fix_config_with_schemas(config_content, config_indent) |
| 74 | + else: |
| 75 | + # Simple case - just wrap in agent |
| 76 | + fixed_config = fix_simple_config(config_content, config_indent) |
| 77 | + |
| 78 | + new_lines.extend(fixed_config.split('\n')) |
| 79 | + in_config = False |
| 80 | + else: |
| 81 | + new_lines.append(line) |
| 82 | + i += 1 |
| 83 | + |
| 84 | + new_content = '\n'.join(new_lines) |
| 85 | + |
| 86 | + if new_content != content: |
| 87 | + with open(test_file, 'w') as f: |
| 88 | + f.write(new_content) |
| 89 | + print(f"Fixed {test_file}") |
| 90 | + |
| 91 | +def fix_config_with_schemas(config_content, base_indent): |
| 92 | + """Fix config that has schemas at top level.""" |
| 93 | + lines = config_content.split('\n') |
| 94 | + |
| 95 | + # Find where schemas end |
| 96 | + schemas_end = -1 |
| 97 | + brace_count = 0 |
| 98 | + in_schemas = False |
| 99 | + |
| 100 | + for i, line in enumerate(lines): |
| 101 | + if '"schemas"' in line: |
| 102 | + in_schemas = True |
| 103 | + if in_schemas: |
| 104 | + brace_count += line.count('[') - line.count(']') |
| 105 | + if brace_count == 0 and ']' in line: |
| 106 | + schemas_end = i |
| 107 | + break |
| 108 | + |
| 109 | + if schemas_end == -1: |
| 110 | + return config_content # Couldn't parse, return as-is |
| 111 | + |
| 112 | + # Extract schemas part |
| 113 | + schemas_lines = lines[1:schemas_end+1] # Skip 'config = {' |
| 114 | + |
| 115 | + # Extract agent part (everything after schemas) |
| 116 | + agent_lines = [] |
| 117 | + for i in range(schemas_end + 1, len(lines) - 1): # Skip closing '}' |
| 118 | + line = lines[i] |
| 119 | + if line.strip() and not line.strip() == ',': |
| 120 | + agent_lines.append(line) |
| 121 | + |
| 122 | + # Reconstruct |
| 123 | + result = [lines[0]] # 'config = {' |
| 124 | + result.extend(schemas_lines) |
| 125 | + if schemas_lines and agent_lines: |
| 126 | + result.append(base_indent + ' ,') |
| 127 | + result.append(base_indent + ' "agent": {') |
| 128 | + result.extend([base_indent + ' ' + line.lstrip() for line in agent_lines]) |
| 129 | + result.append(base_indent + ' }') |
| 130 | + result.append(lines[-1]) # '}' |
| 131 | + |
| 132 | + return '\n'.join(result) |
| 133 | + |
| 134 | +def fix_simple_config(config_content, base_indent): |
| 135 | + """Fix simple config without schemas.""" |
| 136 | + lines = config_content.split('\n') |
| 137 | + |
| 138 | + # Extract inner content (skip first and last lines) |
| 139 | + inner_lines = lines[1:-1] |
| 140 | + |
| 141 | + result = [lines[0]] # 'config = {' |
| 142 | + result.append(base_indent + ' "agent": {') |
| 143 | + result.extend([base_indent + ' ' + line.lstrip() for line in inner_lines]) |
| 144 | + result.append(base_indent + ' }') |
| 145 | + result.append(lines[-1]) # '}' |
| 146 | + |
| 147 | + return '\n'.join(result) |
| 148 | + |
| 149 | +def fix_graph_tests(): |
| 150 | + """Fix graph configuration tests.""" |
| 151 | + test_file = "tests/strands/experimental/config_loader/graph/test_graph_config_loader.py" |
| 152 | + |
| 153 | + if not os.path.exists(test_file): |
| 154 | + return |
| 155 | + |
| 156 | + with open(test_file, 'r') as f: |
| 157 | + content = f.read() |
| 158 | + |
| 159 | + # Replace config dictionaries with graph wrapper |
| 160 | + # Look for configs that have nodes, edges, entry_points |
| 161 | + pattern = r'config = \{([^}]*(?:"nodes"|"edges"|"entry_points")[^}]*)\}' |
| 162 | + |
| 163 | + def replace_graph_config(match): |
| 164 | + inner_config = match.group(1) |
| 165 | + # Check if already has graph key |
| 166 | + if '"graph"' in inner_config and inner_config.strip().startswith('"graph"'): |
| 167 | + return match.group(0) |
| 168 | + return f'config = {{"graph": {{{inner_config}}}}}' |
| 169 | + |
| 170 | + new_content = re.sub(pattern, replace_graph_config, content, flags=re.DOTALL) |
| 171 | + |
| 172 | + # Also fix test assertions that expect top-level keys |
| 173 | + new_content = new_content.replace('assert "nodes" in config', 'assert "nodes" in config["graph"]') |
| 174 | + new_content = new_content.replace('assert "edges" in config', 'assert "edges" in config["graph"]') |
| 175 | + new_content = new_content.replace('assert "entry_points" in config', 'assert "entry_points" in config["graph"]') |
| 176 | + |
| 177 | + if new_content != content: |
| 178 | + with open(test_file, 'w') as f: |
| 179 | + f.write(new_content) |
| 180 | + print(f"Fixed {test_file}") |
| 181 | + |
| 182 | +def fix_swarm_tests(): |
| 183 | + """Fix swarm configuration tests.""" |
| 184 | + test_file = "tests/strands/experimental/config_loader/swarm/test_swarm_config_loader.py" |
| 185 | + |
| 186 | + if not os.path.exists(test_file): |
| 187 | + return |
| 188 | + |
| 189 | + with open(test_file, 'r') as f: |
| 190 | + content = f.read() |
| 191 | + |
| 192 | + # Replace config dictionaries with swarm wrapper |
| 193 | + pattern = r'config = \{([^}]*"agents"[^}]*)\}' |
| 194 | + |
| 195 | + def replace_swarm_config(match): |
| 196 | + inner_config = match.group(1) |
| 197 | + # Check if already has swarm key |
| 198 | + if '"swarm"' in inner_config and inner_config.strip().startswith('"swarm"'): |
| 199 | + return match.group(0) |
| 200 | + return f'config = {{"swarm": {{{inner_config}}}}}' |
| 201 | + |
| 202 | + new_content = re.sub(pattern, replace_swarm_config, content, flags=re.DOTALL) |
| 203 | + |
| 204 | + # Fix test assertions that expect top-level keys |
| 205 | + new_content = new_content.replace('config["max_handoffs"]', 'config["swarm"]["max_handoffs"]') |
| 206 | + new_content = new_content.replace('config["agents"]', 'config["swarm"]["agents"]') |
| 207 | + |
| 208 | + if new_content != content: |
| 209 | + with open(test_file, 'w') as f: |
| 210 | + f.write(new_content) |
| 211 | + print(f"Fixed {test_file}") |
| 212 | + |
| 213 | +if __name__ == "__main__": |
| 214 | + print("Fixing ConfigLoader tests...") |
| 215 | + fix_agent_tests() |
| 216 | + fix_graph_tests() |
| 217 | + fix_swarm_tests() |
| 218 | + print("Done!") |
0 commit comments