Files
screenjob/tests/test_storage.py

75 lines
2.3 KiB
Python

from pathlib import Path
import json
from src.storage import HistoryDB
def test_history_db_job_and_events_roundtrip(tmp_path: Path) -> None:
db = HistoryDB(tmp_path / "screenjob_test.db")
job_id = "job_test_001"
db.create_job(
job_id=job_id,
objective="Open example.com",
model="gpt-5.4-mini",
created_at="2026-05-27T00:00:00Z",
safety_override=False,
disabled_tools=["click"],
)
db.add_event(
job_id=job_id,
ts="2026-05-27T00:00:01Z",
step=1,
event_type="tool_called",
payload={"tool": "see_screen"},
)
db.update_job(
job_id,
status="completed",
ended_at="2026-05-27T00:00:02Z",
result="Done",
response_json=json.dumps({"return": "Done", "data": {"files": ["a.txt", "b.txt"]}}, ensure_ascii=False),
steps=2,
estimated_cost_usd=0.1234,
)
job = db.get_job(job_id)
assert job is not None
assert job["status"] == "completed"
assert job["model"] == "gpt-5.4-mini"
assert job["disabled_tools"] == ["click"]
assert job["response"]["return"] == "Done"
assert job["response"]["data"]["files"] == ["a.txt", "b.txt"]
assert job["usage"]["estimated_cost_usd"] == 0.1234
events = db.get_job_events(job_id, limit=10)
assert len(events) == 1
assert events[0]["event_type"] == "tool_called"
assert events[0]["payload"]["tool"] == "see_screen"
jobs = db.list_jobs(limit=10)
assert len(jobs) == 1
assert jobs[0]["job_id"] == job_id
stats = db.stats()
assert stats["total_jobs"] == 1
assert stats["completed_jobs"] == 1
assert abs(stats["total_estimated_cost"] - 0.1234) < 1e-9
def test_storage_response_fallback_uses_result_when_json_missing(tmp_path: Path) -> None:
db = HistoryDB(tmp_path / "screenjob_test_fallback.db")
job_id = "job_test_002"
db.create_job(
job_id=job_id,
objective="Fallback check",
model="gpt-5.4-mini",
created_at="2026-05-27T00:00:00Z",
safety_override=False,
disabled_tools=[],
)
db.update_job(job_id, status="completed", result="Legacy result string")
job = db.get_job(job_id)
assert job is not None
assert job["response"]["return"] == "Legacy result string"
assert job["response"]["data"] is None