Research-Stack/4-Infrastructure/shim/test_validate_rrc_predictions.py
devin-ai-integration[bot] 6db78dccf3 test(coverage): add 138 unit tests for least-covered modules (#78)
Four test files covering the modules with zero or near-zero test coverage:

- scripts/qc-flag/test_lean_qc_flagger.py (55 tests)
  Covers all pure helpers, code-line detection, QCIssue/FileResult classes,
  and all 5 check functions (structural, naming, Q16, proof quality, deps).

- 4-Infrastructure/shim/test_braid_diat_codec.py (22 tests)
  Covers Q0_2 encode/decode, ChiralityDIAT roundtrips and verify_b,
  MountainPacked dict↔bytes, BraidResidualPacked bracket↔bytes.

- 5-Applications/tools-scripts/llm/test_emitter_utils.py (33 tests)
  Covers sha256_bytes, canonical_json_bytes, repo_relative, safe_slug,
  timestamps, context bundles, message building, extract_answer,
  auth guard, and verify_receipt schema rules.

- 4-Infrastructure/shim/test_validate_rrc_predictions.py (28 tests)
  Covers require_path, parse_equation, build_proof_metrics,
  build_receipt with all domain mappings, and MATH_* constant sets.

Build: all 138 tests pass (pytest), py_compile clean

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Allaun Silverfox <bigdataiscoming+9i37y6j2@protonmail.com>
2026-06-14 19:31:16 -05:00

258 lines
8.9 KiB
Python

#!/usr/bin/env python3
"""Unit tests for pure functions in validate_rrc_predictions.py.
The module uses Python 3.12+ f-string syntax, so we extract testable functions
via targeted exec rather than a full module import.
"""
import math
import os
import re
import sys
import unittest
from collections import Counter
from pathlib import Path
# ---------------------------------------------------------------------------
# Extract testable functions from source without full module load
# ---------------------------------------------------------------------------
_SRC = Path(__file__).with_name("validate_rrc_predictions.py").read_text()
_NAMESPACE: dict = {}
# Execute only the pure-function portion (up to the classify function)
# by extracting individual function blocks.
exec(
compile(
"""
import json, os, re, subprocess, sys, tempfile, math
from collections import Counter, defaultdict
"""
+ re.search(
r"(def require_path.*?)(?=\nPIST_DECOMPOSE)",
_SRC,
re.DOTALL,
).group(1)
+ "\n"
+ re.search(
r"(MATH_OPERATORS\s*=\s*\{.*?\})",
_SRC,
re.DOTALL,
).group(1)
+ "\n"
+ re.search(
r"(MATH_VARIABLES\s*=\s*\{.*?\})",
_SRC,
re.DOTALL,
).group(1)
+ "\n"
+ re.search(
r"(MATH_CONSTANTS\s*=\s*\{.*?\})",
_SRC,
re.DOTALL,
).group(1)
+ "\n"
+ re.search(
r"(def parse_equation.*?)(?=\ndef build_proof_metrics)",
_SRC,
re.DOTALL,
).group(1)
+ "\n"
+ re.search(
r"(def build_proof_metrics.*?)(?=\ndef build_receipt)",
_SRC,
re.DOTALL,
).group(1)
+ "\n"
+ re.search(
r"(def build_receipt.*?)(?=\ndef classify)",
_SRC,
re.DOTALL,
).group(1),
"<validate_rrc_extracted>",
"exec",
),
_NAMESPACE,
)
require_path = _NAMESPACE["require_path"]
parse_equation = _NAMESPACE["parse_equation"]
build_proof_metrics = _NAMESPACE["build_proof_metrics"]
build_receipt = _NAMESPACE["build_receipt"]
MATH_OPERATORS = _NAMESPACE["MATH_OPERATORS"]
MATH_VARIABLES = _NAMESPACE["MATH_VARIABLES"]
MATH_CONSTANTS = _NAMESPACE["MATH_CONSTANTS"]
# ---------------------------------------------------------------------------
# require_path
# ---------------------------------------------------------------------------
class TestRequirePath(unittest.TestCase):
def test_single_key(self):
self.assertEqual(require_path({"a": 1}, "a"), 1)
def test_nested_keys(self):
self.assertEqual(require_path({"a": {"b": {"c": 42}}}, "a", "b", "c"), 42)
def test_missing_key_raises(self):
with self.assertRaises((KeyError, TypeError)):
require_path({"a": 1}, "a", "b")
def test_missing_top_key_raises(self):
with self.assertRaises(KeyError):
require_path({}, "missing")
# ---------------------------------------------------------------------------
# parse_equation
# ---------------------------------------------------------------------------
class TestParseEquation(unittest.TestCase):
def test_basic_equation(self):
result = parse_equation("cognitive_load_field")
self.assertEqual(result["equation_text"], "cognitive_load_field")
# "cognitive", "load", "field" are all in MATH_OPERATORS
self.assertIn("cognitive", result["operators"])
def test_operator_extraction(self):
result = parse_equation("adjusted-overflow-gate")
self.assertIn("adjusted", result["operators"])
self.assertIn("overflow", result["operators"])
self.assertIn("gate", result["operators"])
def test_constant_extraction(self):
result = parse_equation("phi-delta-mapping")
self.assertIn("phi", result["constants"])
self.assertIn("delta", result["constants"])
def test_normalized_equation_sorted(self):
result = parse_equation("z-a-m")
self.assertEqual(result["normalized_equation"], "a_m_z")
def test_ast_metrics_present(self):
result = parse_equation("heat-loss-equations")
self.assertIn("node_count", result["ast_metrics"])
self.assertIn("depth", result["ast_metrics"])
self.assertIn("branching_factor", result["ast_metrics"])
self.assertIn("symbol_entropy", result["ast_metrics"])
def test_depth_clamped_at_16(self):
long_eq = "-".join(["x"] * 20)
result = parse_equation(long_eq)
self.assertEqual(result["ast_metrics"]["depth"], 16)
def test_single_part_zero_entropy(self):
result = parse_equation("single")
self.assertEqual(result["ast_metrics"]["symbol_entropy"], 0.0)
def test_operator_counts(self):
result = parse_equation("adjusted-adjusted-gate-loss")
self.assertEqual(result["operator_counts"]["adjusted"], 2)
self.assertEqual(result["operator_counts"]["gate"], 1)
self.assertEqual(result["operator_counts"]["loss"], 1)
self.assertEqual(result["operator_counts"]["overflow"], 0)
# ---------------------------------------------------------------------------
# build_proof_metrics
# ---------------------------------------------------------------------------
class TestBuildProofMetrics(unittest.TestCase):
def test_cognitive_load_field(self):
m = build_proof_metrics("CognitiveLoadField")
self.assertEqual(m["tactic_count"], 3)
self.assertEqual(m["dependency_count"], 2)
def test_signal_shaped(self):
m = build_proof_metrics("SignalShapedRouteCompiler")
self.assertEqual(m["tactic_count"], 4)
def test_projectable_geometry(self):
m = build_proof_metrics("ProjectableGeometryTopology")
self.assertEqual(m["tactic_count"], 5)
def test_cad_force(self):
m = build_proof_metrics("CadForceProbeReceipt")
self.assertEqual(m["tactic_count"], 6)
def test_logogram(self):
m = build_proof_metrics("LogogramProjection")
self.assertEqual(m["tactic_count"], 8)
def test_unknown_shape_defaults(self):
m = build_proof_metrics("SomeUnknownShape")
self.assertEqual(m["tactic_count"], 2)
self.assertEqual(m["dependency_count"], 1)
# ---------------------------------------------------------------------------
# build_receipt
# ---------------------------------------------------------------------------
class TestBuildReceipt(unittest.TestCase):
def test_receipt_structure(self):
eq = {"equation": "heat-loss", "shape": "CognitiveLoadField", "status": "verified"}
r = build_receipt(eq)
self.assertEqual(r["receipt_version"], "rrc-proof-receipt-v2")
self.assertEqual(r["theorem_name"], "heat-loss")
self.assertEqual(r["domain"], "analysis")
self.assertEqual(r["status"], "verified")
self.assertTrue(r["kernel_checked"])
self.assertIn("proof_metrics", r)
self.assertIn("metrics", r)
def test_domain_mapping_topology(self):
eq = {"equation": "x", "shape": "SignalShapedRouteCompiler", "status": "ok"}
r = build_receipt(eq)
self.assertEqual(r["domain"], "topology")
def test_domain_mapping_geometry(self):
eq = {"equation": "x", "shape": "ProjectableGeometryTopology", "status": "ok"}
r = build_receipt(eq)
self.assertEqual(r["domain"], "geometry")
def test_domain_mapping_physics(self):
eq = {"equation": "x", "shape": "CadForceProbeReceipt", "status": "ok"}
r = build_receipt(eq)
self.assertEqual(r["domain"], "physics")
def test_domain_mapping_symbolic(self):
eq = {"equation": "x", "shape": "LogogramProjection", "status": "ok"}
r = build_receipt(eq)
self.assertEqual(r["domain"], "symbolic")
def test_domain_mapping_unknown(self):
eq = {"equation": "x", "shape": "HoldForUnlawfulOrUnderspecifiedShape", "status": "ok"}
r = build_receipt(eq)
self.assertEqual(r["domain"], "unknown")
def test_metrics_present(self):
eq = {"equation": "phi-mapping", "shape": "CognitiveLoadField", "status": "ok"}
r = build_receipt(eq)
self.assertIn("statement_chars", r["metrics"])
self.assertIn("proof_chars", r["metrics"])
self.assertIn("tactic_count", r["metrics"])
# ---------------------------------------------------------------------------
# MATH sets
# ---------------------------------------------------------------------------
class TestMathSets(unittest.TestCase):
def test_operators_non_empty(self):
self.assertTrue(len(MATH_OPERATORS) > 0)
def test_variables_non_empty(self):
self.assertTrue(len(MATH_VARIABLES) > 0)
def test_constants_contain_known(self):
self.assertIn("pi", MATH_CONSTANTS)
self.assertIn("e", MATH_CONSTANTS)
self.assertIn("phi", MATH_CONSTANTS)
if __name__ == "__main__":
unittest.main()