69 lines
2.4 KiB
Python
69 lines
2.4 KiB
Python
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
from typing import Any
|
|
|
|
import src.cli as cli_module
|
|
from src.config import AppConfig
|
|
from src.models import AgentResult, RunArtifacts, UsageSummary
|
|
|
|
|
|
def test_cli_emits_structured_return_and_data(monkeypatch: Any, capsys, tmp_path: Path) -> None:
|
|
config = AppConfig(
|
|
openai_api_key="test_key",
|
|
screenjob_token="test_token",
|
|
disable_ui=False,
|
|
default_model="gpt-5.4-mini",
|
|
safety_model="gpt-5.4-mini",
|
|
host="127.0.0.1",
|
|
port=8787,
|
|
runs_dir=tmp_path / "runs",
|
|
db_path=tmp_path / "screenjob.db",
|
|
)
|
|
config.runs_dir.mkdir(parents=True, exist_ok=True)
|
|
|
|
def fake_load_app_config(_: Path) -> AppConfig:
|
|
return config
|
|
|
|
def fake_assess_task_safety(*_args, **_kwargs):
|
|
return True, "safe", {"safe": True}
|
|
|
|
def fake_run_job(*_args, **_kwargs):
|
|
result = AgentResult(
|
|
completed=True,
|
|
result="Done",
|
|
return_message="Task completed successfully",
|
|
data="file1.txt\nfile2.txt",
|
|
steps=3,
|
|
started_at=10.0,
|
|
ended_at=12.5,
|
|
usage=UsageSummary(total_tokens=123),
|
|
error=None,
|
|
cancelled=False,
|
|
)
|
|
artifacts = RunArtifacts(
|
|
run_id="20260527_000001",
|
|
root_dir=config.runs_dir / "run_20260527_000001",
|
|
logs_dir=config.runs_dir / "run_20260527_000001" / "logs",
|
|
shots_dir=config.runs_dir / "run_20260527_000001" / "shots",
|
|
enhance_dir=config.runs_dir / "run_20260527_000001" / "enhance",
|
|
log_file=config.runs_dir / "run_20260527_000001" / "screenjob.log",
|
|
)
|
|
return result, artifacts
|
|
|
|
monkeypatch.setattr(cli_module, "load_app_config", fake_load_app_config)
|
|
monkeypatch.setattr(cli_module, "assess_task_safety", fake_assess_task_safety)
|
|
monkeypatch.setattr(cli_module, "run_job", fake_run_job)
|
|
monkeypatch.setattr(cli_module, "create_openai_client", lambda *_args, **_kwargs: object())
|
|
|
|
code = cli_module.main(["Open amazon.de"])
|
|
assert code == 0
|
|
|
|
out = capsys.readouterr().out
|
|
payload = json.loads(out)
|
|
assert payload["response"]["return"] == "Task completed successfully"
|
|
assert payload["response"]["data"] == "file1.txt\nfile2.txt"
|
|
assert payload["return"] == "Task completed successfully"
|
|
assert payload["data"] == "file1.txt\nfile2.txt"
|