Download source/tests/test_grading_and_env.py from rca-umb/five-desks-control: direct link, hf CLI and curl.
- Browser
- Download file 4 kB
-
https://huggingface.co/datasets/rca-umb/five-desks-control/resolve/main/source/tests/test_grading_and_env.py
- Command line
-
hf download hf://datasets/rca-umb/five-desks-control/source/tests/test_grading_and_env.py
-
curl -L -o test_grading_and_env.py https://huggingface.co/datasets/rca-umb/five-desks-control/resolve/main/source/tests/test_grading_and_env.py
4 kB
| import pytest | |
| from five_desks.calc import CalcError, evaluate | |
| from five_desks.environment import FiveDesksEnvironment | |
| from five_desks.grading import Part, grade | |
| from five_desks.models import DeskAction | |
| from five_desks.tasks import TASK_IDS, build | |
| def test_number_part(given, score): | |
| assert Part("x", "number", 1234.5, "", abs_tol=1.0).score(given) == score | |
| def test_int_part(given, score): | |
| assert Part("x", "int", 1183, "").score(given) == score | |
| def test_rule_part(given, expected, score): | |
| assert Part("x", "rule", expected, "").score(given) == score | |
| def test_items_part_exact_and_near_miss(): | |
| part = Part("x", "items", ["P1", "P3"], "") | |
| assert part.score(["p1", "P3"]) == 1 | |
| assert part.score("P1, P3") == 1 | |
| assert part.score(["P1"]) == pytest.approx(0.25) | |
| assert part.score(["P1", "P2", "P3", "P4"]) == pytest.approx(0.25) | |
| assert part.score([]) == 0 | |
| assert Part("x", "items", [], "").score([]) == 1 | |
| assert Part("x", "items", [], "").score("none") == 1 | |
| def test_grade_ignores_key_case_and_extra_keys(): | |
| parts = [Part("Net_USD", "number", 10.0, "", abs_tol=0.5), Part("id", "text", "T101", "")] | |
| assert grade(parts, {"net_usd": 10.2, "ID": "t101", "extra": 1})[0] == 1 | |
| assert grade(parts, {"net_usd": 10.2})[0] == 0.5 | |
| assert grade(parts, "not a dict")[0] == 0 | |
| def test_calc(expr, value): | |
| assert evaluate(expr) == pytest.approx(value) | |
| def test_calc_rejects(expr): | |
| with pytest.raises(CalcError): | |
| evaluate(expr) | |
| def test_episode_flow_and_budget(): | |
| env = FiveDesksEnvironment() | |
| obs = env.reset(task_id="finance-loan-d1", seed=3) | |
| assert not obs.done and obs.steps_left == 10 and "loan_terms.md" in obs.output | |
| inst = build("finance-loan-d1", 3) | |
| assert "Principal" in env.step(DeskAction(tool="read", path="./loan_terms.md")).output | |
| assert "No file" in env.step(DeskAction(tool="read", path="secret.py")).output | |
| assert "calc error" in env.step(DeskAction(tool="calc", expr="import os")).output | |
| final = env.step(DeskAction(tool="submit", answer={p.key: p.expected for p in inst.parts})) | |
| assert final.done and final.reward == 1.0 | |
| assert env.step(DeskAction(tool="list")).done | |
| def test_unseeded_resets_differ_and_unknown_task_fails(): | |
| env = FiveDesksEnvironment() | |
| outputs = set() | |
| for _ in range(5): | |
| env.reset(task_id="math-modular-d1") | |
| outputs.add(env.step(DeskAction(tool="read", path="problem.md")).output) | |
| assert len(outputs) > 1 | |
| with pytest.raises(ValueError): | |
| env.reset(task_id="nope-d1") | |
| def test_extra_action_keys_are_ignored(): | |
| action = DeskAction.model_validate({"tool": "list", "thought": "checking files"}) | |
| assert action.tool == "list" | |
| def test_observations_stay_small(): | |
| for task_id in TASK_IDS: | |
| for seed in range(30): | |
| inst = build(task_id, seed) | |
| assert max(len(body) for body in inst.files.values()) < 4500, task_id | |