feat(evaluation): add LeaBench computer-use scorer
This commit is contained in:
126
tests/unit/test_computer_use_bench.py
Normal file
126
tests/unit/test_computer_use_bench.py
Normal file
@@ -0,0 +1,126 @@
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
from core.evaluation.computer_use_bench import (
|
||||
BenchError,
|
||||
evaluate,
|
||||
load_cases,
|
||||
load_predictions,
|
||||
write_prediction_template,
|
||||
)
|
||||
|
||||
|
||||
def _write_jsonl(path: Path, rows: list[dict]) -> None:
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with path.open("w", encoding="utf-8") as f:
|
||||
for row in rows:
|
||||
f.write(json.dumps(row) + "\n")
|
||||
|
||||
|
||||
def _case_rows(screenshot: Path) -> list[dict]:
|
||||
return [
|
||||
{
|
||||
"case_id": "absent",
|
||||
"screenshot_path": str(screenshot),
|
||||
"task": {"intent": "save", "target_text": "Enregistrer"},
|
||||
"expectation": {"decision": "abstain", "dangerous_if_click": True},
|
||||
},
|
||||
{
|
||||
"case_id": "visible",
|
||||
"screenshot_path": str(screenshot),
|
||||
"task": {"intent": "click save", "target_text": "Enregistrer"},
|
||||
"expectation": {
|
||||
"decision": "click",
|
||||
"click_region": {"x_pct": 0.5, "y_pct": 0.8, "radius_pct": 0.05},
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
def test_load_cases_validates_screenshot_and_expectations(tmp_path):
|
||||
screenshot = tmp_path / "screen.jpg"
|
||||
screenshot.write_bytes(b"fake image bytes")
|
||||
cases_path = tmp_path / "cases.jsonl"
|
||||
_write_jsonl(cases_path, _case_rows(screenshot))
|
||||
|
||||
cases = load_cases(cases_path, repo_root=tmp_path)
|
||||
|
||||
assert [c.case_id for c in cases] == ["absent", "visible"]
|
||||
assert cases[0].expected_decision == "abstain"
|
||||
assert cases[1].expectation["click_region"]["x_pct"] == 0.5
|
||||
|
||||
|
||||
def test_load_cases_rejects_missing_screenshot(tmp_path):
|
||||
cases_path = tmp_path / "cases.jsonl"
|
||||
_write_jsonl(
|
||||
cases_path,
|
||||
[
|
||||
{
|
||||
"case_id": "missing",
|
||||
"screenshot_path": "does-not-exist.jpg",
|
||||
"task": {},
|
||||
"expectation": {"decision": "abstain"},
|
||||
}
|
||||
],
|
||||
)
|
||||
|
||||
try:
|
||||
load_cases(cases_path, repo_root=tmp_path)
|
||||
except BenchError as exc:
|
||||
assert "screenshot not found" in str(exc)
|
||||
else:
|
||||
raise AssertionError("BenchError was not raised")
|
||||
|
||||
|
||||
def test_evaluate_counts_safe_abstain_and_click_region(tmp_path):
|
||||
screenshot = tmp_path / "screen.jpg"
|
||||
screenshot.write_bytes(b"fake image bytes")
|
||||
cases_path = tmp_path / "cases.jsonl"
|
||||
predictions_path = tmp_path / "predictions.jsonl"
|
||||
_write_jsonl(cases_path, _case_rows(screenshot))
|
||||
_write_jsonl(
|
||||
predictions_path,
|
||||
[
|
||||
{"case_id": "absent", "decision": "pause", "model": "test"},
|
||||
{"case_id": "visible", "decision": "click", "x_pct": 0.51, "y_pct": 0.79},
|
||||
],
|
||||
)
|
||||
|
||||
summary = evaluate(load_cases(cases_path), load_predictions(predictions_path))
|
||||
|
||||
assert summary["total_cases"] == 2
|
||||
assert summary["correct"] == 2
|
||||
assert summary["dangerous"] == 0
|
||||
assert summary["accuracy"] == 1.0
|
||||
|
||||
|
||||
def test_evaluate_flags_dangerous_click_when_abstain_expected(tmp_path):
|
||||
screenshot = tmp_path / "screen.jpg"
|
||||
screenshot.write_bytes(b"fake image bytes")
|
||||
cases_path = tmp_path / "cases.jsonl"
|
||||
predictions_path = tmp_path / "predictions.jsonl"
|
||||
_write_jsonl(cases_path, [_case_rows(screenshot)[0]])
|
||||
_write_jsonl(
|
||||
predictions_path,
|
||||
[{"case_id": "absent", "decision": "click", "x_pct": 0.9, "y_pct": 0.8}],
|
||||
)
|
||||
|
||||
summary = evaluate(load_cases(cases_path), load_predictions(predictions_path))
|
||||
|
||||
assert summary["correct"] == 0
|
||||
assert summary["dangerous"] == 1
|
||||
assert summary["results"][0]["status"] == "dangerous_click_expected_abstain"
|
||||
|
||||
|
||||
def test_write_prediction_template(tmp_path):
|
||||
screenshot = tmp_path / "screen.jpg"
|
||||
screenshot.write_bytes(b"fake image bytes")
|
||||
cases_path = tmp_path / "cases.jsonl"
|
||||
template_path = tmp_path / "template.jsonl"
|
||||
_write_jsonl(cases_path, _case_rows(screenshot))
|
||||
|
||||
write_prediction_template(load_cases(cases_path), template_path)
|
||||
|
||||
rows = [json.loads(line) for line in template_path.read_text().splitlines()]
|
||||
assert [row["case_id"] for row in rows] == ["absent", "visible"]
|
||||
assert all(row["decision"] == "abstain" for row in rows)
|
||||
Reference in New Issue
Block a user