"""``calculator`` 工具的单元测试:计算正确性 + 输入溯源。""" from __future__ import annotations from decimal import Decimal import pytest from toolsvc.calc import CalcError, calculate from toolsvc.ledger import ledger from toolsvc.objects import new_id def test_sum_and_mean(): assert calculate("sum", [{"value": "1.5"}, {"value": "2.5"}])["result"] == "4.0" assert calculate("mean", [{"value": 1}, {"value": 2}, {"value": 3}])["result"] == "2" assert calculate("median", [{"value": 1}, {"value": 5}, {"value": 3}])["result"] == "3" assert calculate("min", [{"value": 4}, {"value": 2}])["result"] == "2" assert calculate("max", [{"value": 4}, {"value": 2}])["result"] == "4" assert calculate("count", [{"value": 4}, {"value": 2}])["result"] == "2" def _result(payload: dict) -> Decimal: return Decimal(payload["result"]) def test_growth_ratio_index_share(): assert _result(calculate("growth", [{"value": 120}, {"value": 100}])) == Decimal("0.2") assert _result(calculate("ratio", [{"value": 10}, {"value": 4}])) == Decimal("2.5") assert _result(calculate("index", [{"value": 150}, {"value": 100}])) == Decimal("1500") assert _result(calculate("share", [{"value": 25}, {"value": 100}])) == Decimal("0.25") def test_division_by_zero_is_rejected_not_silently_zero(): with pytest.raises(CalcError): calculate("growth", [{"value": 1}, {"value": 0}]) with pytest.raises(CalcError): calculate("ratio", [{"value": 1}, {"value": 0}]) with pytest.raises(CalcError): calculate("expression", expression="1/0") def test_expression_evaluator_is_whitelisted(): payload = calculate("expression", [{"value": 2}, {"value": 3}], expression="(a + b) * 2") assert payload["result"] == "10" # 不能调用函数、不能访问属性、不能注入名字 for expression in ["__import__('os')", "abs(-1)", "a.real", "sum([1,2])"]: with pytest.raises(CalcError): calculate("expression", [{"value": 2}], expression=expression) def test_unknown_operation_and_bad_inputs(): with pytest.raises(CalcError): calculate("nope", [{"value": 1}]) with pytest.raises(CalcError): calculate("sum", []) with pytest.raises(CalcError): calculate("sum", [{"value": "abc"}]) def test_result_is_registered_in_ledger_and_has_trace(): payload = calculate("sum", [{"value": 7}, {"value": 8}], session_id="s-test") assert payload["result"] == "15" assert payload["_trace"]["calculation_id"].startswith("calc_") assert payload["_trace"]["result_hash"].startswith("sha256:") entry = ledger.get(payload["call_id"]) assert entry is not None and entry["tool"] == "calculator" assert ledger.find_calculation(payload["call_id"]) is not None def test_inputs_without_provenance_are_marked_unverified(): payload = calculate("sum", [{"value": 1}, {"value": 2}]) assert payload["all_inputs_verified"] is False assert payload["unverified_inputs"] == 2 def test_inputs_can_reference_a_previous_calculator_call(): first = calculate("sum", [{"value": 1}, {"value": 2}], session_id="s-chain") second = calculate( "sum", [ {"value": 3, "from_call_id": first["call_id"], "row_hash": first["_trace"]["result_hash"]}, {"value": 4}, ], session_id="s-chain", ) assert second["result"] == "7" assert second["inputs"][0]["verified"] is True assert second["inputs"][1]["verified"] is False def test_inputs_with_wrong_row_hash_are_not_verified(): first = calculate("sum", [{"value": 1}, {"value": 2}], session_id="s-bad") second = calculate( "sum", [{"value": 3, "from_call_id": first["call_id"], "row_hash": "sha256:deadbeef"}], session_id="s-bad", ) assert second["inputs"][0]["verified"] is False