"""Synthetic offline checker tests, not simulation/controller acceptance.""" import importlib.util import json from pathlib import Path import pytest import torch ROOT = Path(__file__).resolve().parents[1] spec = importlib.util.spec_from_file_location('functional_audit', ROOT / 'scripts/audit_contact_cycle_functional.py') audit = importlib.util.module_from_spec(spec) spec.loader.exec_module(audit) CFG = json.loads((ROOT / 'configs/contact_cycle/simulation_functional_v1.json').read_text()) def example(): t = torch.arange(1, 4501) * .002 phase = torch.bucketize(t, torch.tensor([3., 4., 4.6, 6., 6.8]), right=True) z = torch.zeros(4500) foot = torch.stack(((t - 4.6).div(1.4).clamp(0, 1) * .025, (t - 4.6).div(1.4).clamp(0, 1) * .02, torch.where((phase == 2) | (phase == 3), .04, 0.) + .02), -1) normal = torch.stack((z, z, torch.where((phase == 0) | (phase == 5), 50., 0.)), -1) d = {'time': t, 'phase': phase, 'scenario': phase * 0, 'weight': z + 50, 'normal': normal, 'foot': foot, 'target': foot.clone(), 'gap': foot[:, 2] - .02, 'signed_height_error': z, 'tilt': z, 'external': torch.zeros(4500, 2, 6), 'guide': torch.zeros(4500, 2, 6), 'auxiliary': torch.zeros(4500, 2, 6), 'joint_excess': z, 'limit_ratio': z, 'contact_acquired': z.bool().logical_not(), 'actor_metadata_available': z.bool(), 'control_action_available': z.bool().logical_not()} return {k: v[:, None].clone() for k, v in d.items()} def test_complete_nominal_example_is_functional_not_learned_acceptance(): result = audit.evaluate(example(), CFG, 'analytic_reference') assert result['sim_functional_passed'] and result['accepted_skill'] is False @pytest.mark.parametrize('fault', ['partial', 'reset', 'airless', 'static_xy', 'no_reload', 'collapse', 'nonfinite', 'hidden_guide', 'wrong_controller', 'disturbance', 'no_unload', 'aux_horizontal', 'aux_cancellation', 'aux_free_couple', 'negative_contact']) def test_functional_gate_rejects_nonfunctional_or_out_of_scope_trace(fault): d = example() kind = 'analytic_reference' if fault == 'partial': d = {k: v[:330] for k, v in d.items()} elif fault == 'reset': d['time'][3000:] -= 6 elif fault == 'airless': d['gap'].zero_() elif fault == 'static_xy': d['foot'][:, :, :2].zero_() elif fault == 'no_reload': d['normal'][d['phase'] == 5] = 0 elif fault == 'no_unload': d['normal'][:, :, 2][d['phase'] == 1] = 50 elif fault == 'collapse': d['signed_height_error'][-1] = -.101 elif fault == 'nonfinite': d['normal'][0, 0, 2] = float('nan') elif fault == 'hidden_guide': d['guide'][:, :, 0, 2] = 50 elif fault == 'wrong_controller': kind = 'learned_policy' elif fault == 'disturbance': d['external'][:, :, 0, 0] = 5 elif fault == 'aux_horizontal': d['auxiliary'][:, :, 0, 0] = 1000 elif fault == 'aux_cancellation': mask = d['phase'][:, 0] == 3 d['auxiliary'][mask, 0, 0, 2] = 1000 d['auxiliary'][mask, 0, 1, 2] = -1000 elif fault == 'aux_free_couple': d['auxiliary'][:, :, 0, 3] = 1000 elif fault == 'negative_contact': d['normal'][:, :, 2][d['phase'] == 3] = -5 assert not audit.evaluate(d, CFG, kind)['sim_functional_passed'] def test_vertical_fixture_allows_bounded_com_reduction_moment(): d = example() mask = d['phase'][:, 0] == 3 d['auxiliary'][mask, 0, 0, 2] = 50 d['auxiliary'][mask, 0, 0, 4] = -1 # r=(.02,0,0), F=(0,0,50) -> r x F=(0,-1,0) assert audit.evaluate(d, CFG, 'analytic_reference')['sim_functional_passed']