import runpy from pathlib import Path import numpy as np import pytest SCRIPT = runpy.run_path( str(Path(__file__).resolve().parents[1] / "scripts" / "build_complete_comparison.py") ) _decode_action = SCRIPT["_decode_action"] _extract_component = SCRIPT["_extract_component"] _prediction_metrics = SCRIPT["_prediction_metrics"] def test_decode_action_uses_statistics_and_nonzero_history_anchor() -> None: history_xy = np.array([[8.0, -4.0], [10.0, -3.0]]) action = np.array([[1.0, -1.0], [0.0, 2.0]]) decoded = _decode_action( action, history_xy, mean=np.array([0.5, 0.25]), scale=np.array([2.0, 0.5]), ) np.testing.assert_allclose(decoded, np.array([[12.5, -3.25], [13.0, -2.0]])) def test_extract_component_requires_one_ordered_nonempty_pair() -> None: text = "<|cot_start|>keep left<|cot_end|>" assert _extract_component(text, "cot") == "keep left" with pytest.raises(ValueError): _extract_component(text + text, "cot") with pytest.raises(ValueError): _extract_component("<|cot_start|><|cot_end|>", "cot") def test_prediction_metrics_use_normalized_action_and_euclidean_xy() -> None: target_action = np.zeros((2, 2)) predicted_action = np.ones((2, 2)) target_xy = np.zeros((2, 2)) predicted_xy = np.array([[3.0, 4.0], [0.0, 0.0]]) assert _prediction_metrics(predicted_action, predicted_xy, target_action, target_xy) == { "action_mse": 1.0, "ade_m": 2.5, "fde_m": 0.0, "max_error_m": 5.0, }