137 lines
5.1 KiB
Python
137 lines
5.1 KiB
Python
from copy import deepcopy
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
from netexpert_scenarios.evaluate import (
|
|
EvaluationError,
|
|
grade,
|
|
load_cases,
|
|
replay,
|
|
scenario_prompt,
|
|
)
|
|
|
|
CASES = load_cases()
|
|
SKILLS = Path(__file__).resolve().parents[1] / "plugins/netexpert/skills"
|
|
|
|
|
|
def contract_trace(case):
|
|
"""Exercise the declared fixture contract without running a model."""
|
|
calls = []
|
|
for number, step in enumerate(case["steps"], 1):
|
|
call = {"tool": step["tool"], "arguments": dict(step.get("arguments", {}))}
|
|
if call["tool"] == "search_docs":
|
|
call["arguments"]["query"] = f"fixture query {number}"
|
|
calls.append(call)
|
|
answer = "\n".join(
|
|
case.get("answer_contains", [])
|
|
+ [group[0] for group in case.get("answer_contains_any", [])]
|
|
+ case.get("citations", [])
|
|
)
|
|
return {"skill": case["skill"], "calls": calls, "answer": answer}
|
|
|
|
|
|
@pytest.mark.parametrize("name,case", CASES.items(), ids=CASES)
|
|
def test_scenario_contract_is_runnable(name, case):
|
|
for skill in (case["skill"], *case.get("alternate_skills", ())):
|
|
assert (SKILLS / skill / "SKILL.md").is_file(), name
|
|
prompt = scenario_prompt(case)
|
|
assert set(prompt) == {"prompt", "available_tools"}
|
|
assert all(step["tool"] in prompt["available_tools"] for step in case["steps"])
|
|
trace = contract_trace(case)
|
|
assert replay(case, []) == []
|
|
assert replay(case, trace["calls"][:1]) == [case["steps"][0]["response"]]
|
|
outcomes = replay(case, trace["calls"])
|
|
assert len(outcomes) == len(case["steps"])
|
|
report = grade(case, trace)
|
|
assert report["skill"] == case["skill"]
|
|
assert report["web_calls"] == int(name == "refine-then-publisher")
|
|
assert report["mcp_calls"] + report["web_calls"] == len(outcomes)
|
|
|
|
|
|
def test_reject_wrong_skill_missing_calls_and_reordered_results():
|
|
case = CASES["vpn-no-traffic"]
|
|
trace = contract_trace(case)
|
|
with pytest.raises(EvaluationError, match="Expected skill diagnostics"):
|
|
grade(case, {**trace, "skill": "dns"})
|
|
with pytest.raises(EvaluationError, match="ended before"):
|
|
grade(case, {**trace, "calls": trace["calls"][:1]})
|
|
with pytest.raises(EvaluationError, match="expected search_docs"):
|
|
replay(case, trace["calls"][::-1])
|
|
|
|
|
|
def test_reject_web_after_a_sufficient_hit():
|
|
case = CASES["split-client-dns"]
|
|
trace = contract_trace(case)
|
|
trace["calls"].append({"tool": "web", "arguments": {"query": "DNS"}})
|
|
with pytest.raises(EvaluationError, match="Call budget exceeded"):
|
|
grade(case, trace)
|
|
|
|
|
|
def test_diagnostics_can_select_the_split_dns_playbook():
|
|
case = CASES["split-client-dns"]
|
|
trace = {**contract_trace(case), "skill": "diagnostics"}
|
|
assert grade(case, trace)["skill"] == "diagnostics"
|
|
with pytest.raises(EvaluationError, match="Expected skill dns or diagnostics"):
|
|
grade(case, {**trace, "skill": "performance"})
|
|
|
|
|
|
def test_reject_repeated_search_before_web_fallback():
|
|
case = CASES["refine-then-publisher"]
|
|
trace = contract_trace(case)
|
|
trace["calls"][1]["arguments"] = deepcopy(trace["calls"][0]["arguments"])
|
|
with pytest.raises(EvaluationError, match="expected a refined query"):
|
|
grade(case, trace)
|
|
|
|
|
|
def test_reject_wrong_citation_and_wrong_version():
|
|
case = CASES["split-client-dns"]
|
|
trace = contract_trace(case)
|
|
trace["calls"][0]["arguments"]["version"] = "1.12.0"
|
|
with pytest.raises(EvaluationError, match="matching version"):
|
|
grade(case, trace)
|
|
|
|
trace = contract_trace(case)
|
|
trace["answer"] += "-unverified"
|
|
with pytest.raises(EvaluationError, match="Missing citation"):
|
|
grade(case, trace)
|
|
|
|
|
|
def test_reject_retries_after_throttle_and_invented_expert_tool():
|
|
case = CASES["rate-limited"]
|
|
trace = contract_trace(case)
|
|
with pytest.raises(EvaluationError, match="Call budget exceeded"):
|
|
replay(case, trace["calls"] * 2)
|
|
|
|
case = CASES["upgrade-with-core-tools"]
|
|
trace = contract_trace(case)
|
|
trace["calls"][0]["tool"] = "compare_versions"
|
|
with pytest.raises(EvaluationError, match="expected search_docs"):
|
|
grade(case, trace)
|
|
|
|
|
|
def test_upgrade_accepts_restoring_the_previous_version_as_rollback():
|
|
case = CASES["upgrade-with-core-tools"]
|
|
trace = contract_trace(case)
|
|
trace["answer"] = trace["answer"].replace(
|
|
"rollback", "restore the previous version and repeat the baseline checks"
|
|
)
|
|
assert grade(case, trace)["mcp_calls"] == 1
|
|
trace["answer"] = trace["answer"].replace("restore", "review")
|
|
with pytest.raises(EvaluationError, match="Missing answer context: rollback"):
|
|
grade(case, trace)
|
|
|
|
|
|
def test_reject_secret_leaks_without_echoing_secret():
|
|
case = CASES["bad-token"]
|
|
trace = contract_trace(case)
|
|
canary = case["canaries"][0]
|
|
trace["calls"][0]["arguments"]["query"] += " " + canary.replace("-", "%2d")
|
|
with pytest.raises(EvaluationError, match="contains a fixture secret") as error:
|
|
grade(case, trace)
|
|
assert canary not in str(error.value)
|
|
|
|
trace = contract_trace(case)
|
|
trace["answer"] += canary
|
|
with pytest.raises(EvaluationError, match="final answer contains"):
|
|
grade(case, trace)
|