010658aa15
Adds support for automated execution by passing a query argument to adk run. - Defaults both interactive and query modes to human-readable output. - Adds --jsonl flag to force structured JSONL output in both modes. - Supports --timeout in interactive mode to prevent hanging. - Refactors service setup logic in cli.py to remove duplication. - Mandates session_id injection in all JSONL output events. - Adds hasattr check in runners.py to prevent AttributeError for agents without sub_agents. Change-Id: I61a95a0fbf3aeb9feb5fd2803da3dc745b4cb5aa
65 lines
1.7 KiB
Python
65 lines
1.7 KiB
Python
import json
|
|
import os
|
|
from pathlib import Path
|
|
|
|
from click.testing import CliRunner
|
|
from google.adk.cli import cli_tools_click
|
|
import pytest
|
|
|
|
|
|
def test_cli_run_integration(tmp_path: Path) -> None:
|
|
"""Integration test for `adk run` with query using CliRunner (No mocks)."""
|
|
# Arrange
|
|
agent_dir = tmp_path / "dummy_agent"
|
|
agent_dir.mkdir()
|
|
|
|
# Create __init__.py
|
|
with open(agent_dir / "__init__.py", "w") as f:
|
|
f.write("from . import agent\n")
|
|
|
|
# Create agent.py
|
|
agent_code = """
|
|
from google.adk.agents import Agent
|
|
from google.adk.events.event import Event
|
|
from typing import AsyncGenerator
|
|
|
|
class DummyAgent(Agent):
|
|
async def run_async(self, ctx) -> AsyncGenerator[Event, None]:
|
|
# Yield a text response
|
|
from google.adk.events.event import Event
|
|
text = ctx.user_content.parts[0].text if ctx.user_content and ctx.user_content.parts else "No input"
|
|
yield Event(author="dummy", output=f"Echo: {text}")
|
|
|
|
root_agent = DummyAgent(name="dummy", model="gemini-2.5-flash")
|
|
"""
|
|
with open(agent_dir / "agent.py", "w") as f:
|
|
f.write(agent_code)
|
|
|
|
runner = CliRunner()
|
|
|
|
# Act
|
|
result = runner.invoke(
|
|
cli_tools_click.main,
|
|
["run", "--jsonl", str(agent_dir), "hello world"],
|
|
)
|
|
|
|
# Assert
|
|
assert result.exit_code == 0
|
|
|
|
# Check stdout
|
|
stdout = result.stdout
|
|
assert stdout, "Stdout should not be empty"
|
|
|
|
# Parse JSONL lines
|
|
lines = stdout.strip().split("\n")
|
|
assert len(lines) > 0
|
|
|
|
# The last line should be the final output or event
|
|
last_line = lines[-1]
|
|
try:
|
|
data = json.loads(last_line)
|
|
assert "output" in data
|
|
assert "Echo: hello world" in data["output"]
|
|
except json.JSONDecodeError:
|
|
pytest.fail(f"Stdout contained non-JSON line: {last_line}")
|