Files
javis_bot/tests/test_calculator_tool.py
javis-bot 4cdfecee82 fix(tools): calculator reads each written number from its text
The float ast had already rounded each literal, so ceil(1.0000000000000001)
gave 1, 1e-400/1e-400 was refused as a division by zero and 1e400-1e400
as out of range. Float literals are now read from the expression text with
Decimal (written numbers past 1e4000 are refused unparsed).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-23 11:52:41 +09:00

103 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""The calculator tool (src/jarvis/tools/builtin/calculator.spec.md): exact arithmetic for the reply
model, evaluated from a whitelist of number operations, never as code."""
from __future__ import annotations
import time
import pytest
from jarvis.tools.builtin.calculator import CalculatorTool
from jarvis.tools.registry import enabled_builtin_tools, run_tool_with_retries
def run(expression):
return CalculatorTool().run({"expression": expression}, None)
def value_of(result):
assert result.success, result.error_message
return result.reply_text.rsplit("=", 1)[1].strip()
@pytest.mark.unit
@pytest.mark.parametrize("expression, expected", [
("3847*2963", str(3847 * 2963)),
("478500*17/100", "81345"),
("2370000 * 14", str(2370000 * 14)),
("2^10", "1024"),
("12 × 3 ÷ 4", "9"),
("sqrt(144) + abs(-3)", "15"),
("(1 + 2) * -3", "-9"),
])
def test_it_gives_the_exact_value(expression, expected):
assert value_of(run(expression)) == expected
@pytest.mark.unit
@pytest.mark.parametrize("expression, expected", [
("ceil(1.1*100)", "110"),
("floor(0.3/0.1)", "3"),
("0.1+0.2", "0.3"),
("1.1*3 - 3.3", "0"),
("10.5 % 0.1", "0"),
("2.675*100 // 1", "267"),
("0.5^-2", "4"),
("ceil(1.0000000000000001)", "2"),
("1e-400/1e-400", "1"),
("1e400-1e400", "0"),
])
def test_decimals_are_exact_as_written(expression, expected):
assert value_of(run(expression)) == expected
@pytest.mark.unit
def test_fractions_are_rounded_to_a_readable_length():
got = float(value_of(run("1.07^12")))
assert got == pytest.approx(1.07 ** 12, rel=1e-9)
assert len(value_of(run("98765/37"))) <= 16
@pytest.mark.unit
def test_the_answer_names_the_expression_it_computed():
assert run("3847*2963").reply_text.startswith("3847*2963")
@pytest.mark.unit
@pytest.mark.parametrize("expression", [
"__import__('os').system('echo hacked')",
"open('/etc/passwd').read()",
"(1).__class__",
"[1, 2, 3]",
"x + 1",
"'a' * 3",
])
def test_anything_but_arithmetic_is_refused(expression):
r = run(expression)
assert not r.success and r.error_message
@pytest.mark.unit
@pytest.mark.parametrize("expression", ["9**9**9", "10**100000", "(10**300)**300", "1.5**100000", "2**-100000", "1e99999999", "1e-99999999*2"])
def test_a_huge_power_fails_fast_instead_of_hanging(expression):
start = time.monotonic()
r = run(expression)
assert not r.success and r.error_message
assert time.monotonic() - start < 1
@pytest.mark.unit
@pytest.mark.parametrize("expression", ["1/0", "", " ", "2 +", "1" * 300])
def test_a_bad_expression_gets_an_error_the_model_can_act_on(expression):
r = run(expression)
assert not r.success and r.error_message
@pytest.mark.unit
def test_it_is_offered_and_runs_through_the_registry(mock_config):
cfg = mock_config
assert "calculator" in enabled_builtin_tools(cfg)
r = run_tool_with_retries(db=None, cfg=cfg, tool_name="calculator", tool_args={"expression": "6*7"},
system_prompt="", original_prompt="", redacted_text="", max_retries=0)
assert r.success and r.reply_text.endswith("42")