"""Unit tests for paper/check_numbers.py on small temporary files (no Volve data needed).""" import importlib.util import json import sys from pathlib import Path import pytest SCRIPT = Path(__file__).resolve().parents[1] / "paper" / "check_numbers.py" @pytest.fixture(scope="module") def cn(): spec = importlib.util.spec_from_file_location("check_numbers", SCRIPT) mod = importlib.util.module_from_spec(spec) sys.modules["check_numbers"] = mod # dataclasses resolve annotations through sys.modules spec.loader.exec_module(mod) return mod @pytest.fixture() def repo(tmp_path): (tmp_path / "docs").mkdir() (tmp_path / "docs/plan.md").write_text("Độ chính xác C4 XML 0,151; HTML 0,177; 1.376 câu hỏi; κ 0,6.\n", encoding="utf-8") (tmp_path / "AGENTS.md").write_text("AP 0.463 masked, 23,447 activities, CER 2.40%.\n", encoding="utf-8") rep = tmp_path / "outputs/m34" rep.mkdir(parents=True) (rep / "report.json").write_text(json.dumps({"ap": 0.61834, "flag": True, "rows": [{"n": 932}], "note": "delta -0.531"}), encoding="utf-8") (rep / "qa.csv").write_text("mode,acc\ntool_only,0.7123\n", encoding="utf-8") (tmp_path / "paper/sections").mkdir(parents=True) return tmp_path def _found(cn, text): return [(f.token, f.value, f.decimals, f.percent) for f in cn.extract_numbers(text)] def test_registry_reads_vietnamese_and_outputs(cn, repo): reg = cn.build_registry(repo) for value, dec in [(0.151, 3), (1376, 0), (1.376, 3), (0.6, 1), (23447, 0), (0.0240, 4), (0.618, 3), (0.62, 2), (932, 0), (0.531, 3), (0.712, 3)]: assert reg.contains(value, dec), value assert not reg.contains(0.619, 3) assert not reg.contains(2.40, 2) # a percentage in the docs enters as its fraction only assert "outputs/**/summary.json" in reg.missing # missing sources are reported, not fatal def test_extract_ignores_references_dates_and_identifiers(cn): text = (r"See Table~\ref{tab:t4} and \citep{smith2019}. On 20/11/2026 (20 November 2026) protocol\_v6" r" and T4 ran on 15/9-F-15~S with sha 3c8021f9, matplotlib 3.11.2, width 0.8\textwidth, 2pt." "\n% a comment 0.999\n" r"\label{sec:x7}\todo{check 0.5} Accuracy 0.151 on 1{,}376 and 23,447 items, 15.1\% or 15.1 per cent.") found = _found(cn, text) assert [t for t, *_ in found] == ["0.151", "1,376", "23,447", "15.1", "15.1"] assert found[0][1:] == (0.151, 3, False) assert found[1][1] == 1376 and found[2][1] == 23447 assert found[3][3] and found[4][3] def test_table_column_with_percent_header(cn): text = ("\\begin{tabular}{lrr}\n\\toprule\nItem & Exact & CER (\\%) \\\\\n\\midrule\n" "Rig name & 0.339 & 5.16 \\\\\nHeaders & 3,504 & 2.51 \\\\\n\\end{tabular}\nThen 2.51 again.\n") found = _found(cn, text) assert [(t, p) for t, _, _, p in found] == [("0.339", False), ("5.16", True), ("3,504", False), ("2.51", True), ("2.51", False)] def test_check_lines_percent_and_allowlist(cn, repo): tex = repo / "paper/sections/06_results.tex" tex.write_text("XML 0.151 and HTML 0.18 with 1,376 questions.\n" "Tool use 71.2\\% but 0.712\\% is wrong, and 15.1 is not a percent.\n" "A difference of $-0.531$; derived 0.123; CER 2.4\\% but 2.40.\n", encoding="utf-8") allow = repo / "paper/number_allowlist.txt" allow.write_text("# hand-checked\n0.123 # 0.712 - 0.589, computed in the text\n0.4\n", encoding="utf-8") entries, problems = cn.read_allowlist(allow) assert entries == [(0.123, 3)] and len(problems) == 1 res = cn.check([tex], cn.build_registry(repo), entries)[str(tex)] status = {(f.line, f.token): f.status for f in res} assert status[(1, "0.151")] == "matched" and status[(1, "0.18")] == "matched" assert status[(1, "1,376")] == "matched" assert status[(2, "71.2")] == "matched-percent" assert status[(2, "0.712")] == "unmatched" # 0.712 % would be 0.00712 assert status[(2, "15.1")] == "unmatched" # 0.151 is only accepted when marked as a percentage assert status[(3, "0.531")] == "matched" and status[(3, "0.123")] == "allowlisted" assert status[(3, "2.4")] == "matched-percent" and status[(3, "2.40")] == "unmatched" def test_main_exit_codes_and_missing_files(cn, repo, capsys): assert cn.main(["--root", str(repo)]) == 0 # no .tex files and no allowlist assert "No .tex files found" in capsys.readouterr().out assert cn.main(["--root", str(repo), "--sections", str(repo / "absent")]) == 0 (repo / "paper/sections/01.tex").write_text("ok 0.151\nbad 0.999\n", encoding="utf-8") assert cn.main(["--root", str(repo)]) == 1 out = capsys.readouterr().out assert "L2: 0.999" in out and "1 unmatched" in out assert cn.main(["--root", str(repo), "--summary"]) == 1 assert "L2:" not in capsys.readouterr().out