Discovers test files and rebuilds them by running the agent live.
(path: str)
| 725 | |
| 726 | |
| 727 | def rebuild_tests(path: str): |
| 728 | """Discovers test files and rebuilds them by running the agent live.""" |
| 729 | import json |
| 730 | import sys |
| 731 | |
| 732 | from google.adk.apps.app import App |
| 733 | from google.adk.events.event import Event as AdkEvent |
| 734 | |
| 735 | path_obj = Path(path) |
| 736 | if path_obj.is_dir(): |
| 737 | folder = path |
| 738 | expected_name = None |
| 739 | else: |
| 740 | folder = str(path_obj.parent.parent) |
| 741 | expected_name = path_obj.name |
| 742 | |
| 743 | test_files = get_test_files(folder) |
| 744 | if not test_files: |
| 745 | print(f"No test files found in {folder}") |
| 746 | return |
| 747 | |
| 748 | for item in test_files: |
| 749 | agent_dir, test_file = item.values |
| 750 | if expected_name and test_file.name != expected_name: |
| 751 | continue |
| 752 | print(f"Rebuilding {test_file}...") |
| 753 | |
| 754 | # Add agent_dir.parent to sys.path so relative imports work |
| 755 | sys_path_saved = list(sys.path) |
| 756 | sys.path.insert(0, str(agent_dir.parent)) |
| 757 | |
| 758 | try: |
| 759 | import random |
| 760 | |
| 761 | loader = AgentLoader(str(agent_dir.parent)) |
| 762 | loader.remove_agent_from_cache(agent_dir.name) |
| 763 | agent_or_app = loader.load_agent(agent_dir.name) |
| 764 | |
| 765 | root_agent = ( |
| 766 | agent_or_app.root_agent |
| 767 | if isinstance(agent_or_app, App) |
| 768 | else agent_or_app |
| 769 | ) |
| 770 | _make_nodes_sequential(root_agent) |
| 771 | |
| 772 | with open(test_file, "r") as f: |
| 773 | session_data = json.load(f) |
| 774 | |
| 775 | events_data = session_data.get("events", []) |
| 776 | if not events_data: |
| 777 | print(f"No events in {test_file}, skipping.") |
| 778 | continue |
| 779 | |
| 780 | # Extract user messages |
| 781 | user_messages = [] |
| 782 | for event in events_data: |
| 783 | content = _extract_user_content(event) |
| 784 | if content: |
no test coverage detected