"""CPU presentation-contract tests; real Kit visibility is separate desktop evidence.""" import ast import json from pathlib import Path from types import ModuleType, SimpleNamespace as NS import sys import pytest ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / 'scripts')) import go2_fl_skill as public import skill_launch_v4 as launch GUI = ROOT / 'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/skill_gui_v041.py' def gui_namespace(): # Compile the actual class/functions, omitting only native imports. This is # NOT a physical/Kit substitute; it tests close/hold ownership and fail gates. tree = ast.parse(GUI.read_text()) tree.body = [n for n in tree.body if not isinstance(n, (ast.Import, ast.ImportFrom))] value = {'__name__': '_gui_cpu', '__package__': '_gui_cpu'} exec(compile(tree, str(GUI), 'exec'), value) return value @pytest.mark.parametrize('value', [0, 1, None, 'yes', [], {}]) def test_gui_requires_exact_boolean_before_files_or_process(value, tmp_path): with pytest.raises(ValueError, match='boolean'): public.start(tmp_path / 'run', tmp_path / 'registration', sequence='S1', gui=value) assert list(tmp_path.iterdir()) == [] def test_gui_explicit_public_spec_and_supervised_command(tmp_path, monkeypatch): assert public.make_spec(sequence='S1')['gui'] is False spec = public.make_spec(sequence='S1', gui=True) assert spec['gui'] is True and spec['cycles'] == 6 captured = [] monkeypatch.setattr(launch, 'registration', lambda *_: ({}, 9999)) monkeypatch.setattr(public.subprocess, 'Popen', lambda command, **kw: captured.append((command, kw))) public.start(tmp_path / 'run', tmp_path / 'registration', sequence='S1', gui=True) command, kwargs = captured[0] assert json.loads(command[command.index('--spec-json') + 1]) == spec assert kwargs['cwd'] == ROOT assert not (tmp_path / 'run').exists() assert launch.limits(spec)['max_physics_calls'] == 27250 @pytest.mark.parametrize('mode,skill,gui,expected', [ ('prepare', True, True, False), ('run', True, True, True), ('run', True, False, False), ('run', False, True, False)]) def test_gui_never_constructed_in_nonphysical_preparation(mode, skill, gui, expected): assert launch.gui_enabled({'mode': mode, 'skill_v4': skill, 'spec': {'gui': gui}}) == expected def test_gui_dep_preimports_and_no_physical_control_source_changes(): text = (ROOT / 'scripts/evaluate_cycle_functional_flow.py').read_text() assert "'omni.replicator.core'" in text assert "importlib.import_module('isaaclab_physx.renderers.kit_viewport_utils')" in text assert "importlib.import_module('isaaclab_physx.renderers.isaac_rtx_renderer_utils')" in text tree = ast.parse(GUI.read_text()) assert not any(isinstance(n, ast.Call) and isinstance(n.func, ast.Attribute) and n.func.attr in ('step', 'reset', 'write_root_pose_to_sim', 'write_joint_state_to_sim') for n in ast.walk(tree)) import hashlib # Recorded immutable 0.4.0 hashes, no dependency on its external directory. frozen = { 'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_env.py': '04e98455035e347633b7ea6c1fcf5372b4e6ac33645805f1beac8f7784e424c6', 'source/SkillFactory/SkillFactory/tasks/manager_based/contact_cycle_sim/evaluation_mdp.py': '89c46b2038733a21c97cb72a4112ff16b48ed0e15b5070ebdebcd3ce0cd8f7ac', 'source/SkillFactory/SkillFactory/legs/contact_cycle_sim/skill_v4.py': '1cfa556a44ff749677bf030dd61792fbe85d60e424646844bdace05794fd102c', 'configs/contact_cycle/functional_flow_v2.json': '16c064228b30f36bbd420b7c2290602bacae6601cac2cec5deb16440f7600625', } for relative, digest in frozen.items(): assert hashlib.sha256((ROOT / relative).read_bytes()).hexdigest() == digest def test_headless_can_import_presentation_source_without_native_ui(): import subprocess code = ('import importlib.util,sys; s=importlib.util.spec_from_file_location("gui",'+repr(str(GUI))+'); ' 'm=importlib.util.module_from_spec(s); s.loader.exec_module(m); ' 'assert not any(n.startswith(("omni", "isaaclab", "pxr")) for n in sys.modules)') subprocess.run(['/usr/bin/python3.12', '-c', code], check=True) def test_actual_installed_launcher_requires_explicit_kit_selection(): import os installed = Path('/home/cen/IsaacLab/source/isaaclab/isaaclab/app/app_launcher.py') tree = ast.parse(installed.read_text()) cls = next(n for n in tree.body if isinstance(n, ast.ClassDef) and n.name == 'AppLauncher') methods = {'_parse_visualizer_csv', '_normalize_visualizer_intent', '_resolve_visualizer_settings', '_resolve_headless_settings'} cls.body = [n for n in cls.body if isinstance(n, ast.FunctionDef) and n.name in methods] ns = {'Any': object, 'os': os, 'logger': NS(info=lambda *_: None, debug=lambda *_: None, warning=lambda *_: None)} exec(compile(ast.Module(body=[cls], type_ignores=[]), str(installed), 'exec'), ns) launcher = ns['AppLauncher'] launcher._APPLAUNCHER_CFG_INFO = {'headless': (bool, False)} for visualizer, expected_headless in ((None, True), ('none', True), ('kit', False)): app = launcher.__new__(launcher) app._livestream = 0 args = {'headless': False} if visualizer is not None: args['visualizer'] = visualizer app._resolve_visualizer_settings(args) app._resolve_headless_settings(args, 0, 0) assert app._headless is expected_headless source = ast.parse((ROOT / 'scripts/evaluate_cycle_functional_flow.py').read_text()) call = next(n for n in ast.walk(source) if isinstance(n, ast.Call) and isinstance(n.func, ast.Name) and n.func.id == 'AppLauncher') selected = next(k.value for k in call.keywords if k.arg == 'visualizer') assert isinstance(selected, ast.IfExp) and selected.body.value == 'kit' and selected.orelse.value == 'none' @pytest.mark.parametrize('changed', [False, True]) def test_final_hold_pumps_no_physics_and_checks_carried_state(monkeypatch, changed): n = gui_namespace() obj = n['LivePresentation'].__new__(n['LivePresentation']) obj.closed = False obj.app = NS(is_running=lambda: True) obj.render_updates = 0 obj.hold_no_physics = False obj.message = NS(text='') published = [] obj.publish = published.append obj.update = lambda *_: None obj.report = lambda: {'protocol_completed': obj.protocol_completed} obj.guard = lambda: None records = ModuleType('_gui_cpu.functional_flow_recording_v2') records.counters = lambda *_: {'physics': 27000} carries = iter([{'root': 'same'}, {'root': 'different' if changed else 'same'}]) records.carry_snapshot = lambda *_: next(carries) monkeypatch.setitem(sys.modules, records.__name__, records) n['time'] = NS(monotonic=lambda: 10., sleep=lambda *_: None) pumps = [] def render(): pumps.append('render-only') obj.request_close() env = NS(runtime_hooks=object(), evidence_callback=NS(capture=NS(state=object())), sim=NS(render=render)) if changed: with pytest.raises(RuntimeError, match='carried_physical_state'): obj.hold(env, True) else: obj.hold(env, True) assert pumps == ['render-only'] assert obj.hold_no_physics is (not changed) assert obj.close_reason == 'window_close' @pytest.mark.parametrize('headless,visualizers', [(True, []), (False, [])]) def test_attach_requires_real_app_and_concrete_kit_not_legacy_flag(headless, visualizers): n = gui_namespace() obj = n['LivePresentation'].__new__(n['LivePresentation']) obj.app = NS(config={'headless': headless}) with pytest.raises(RuntimeError, match='non_headless_app_and_live_Kit'): obj.attach(NS(sim=NS(visualizers=visualizers))) def test_abort_never_enters_final_frame_hold(): n = gui_namespace() obj = n['LivePresentation'].__new__(n['LivePresentation']) published = [] obj.publish = published.append obj.report = lambda: {'protocol_completed': obj.protocol_completed} obj.hold(None, False) assert published == [{'protocol_completed': False}] def test_os_close_is_deferred_to_evidence_owner(): n = gui_namespace() canceled = [] n['omni'] = NS(kit=NS(app=NS(get_app=lambda: NS(try_cancel_shutdown=canceled.append)))) obj = n['LivePresentation'].__new__(n['LivePresentation']) obj._on_quit(None) assert obj.closed and obj.close_reason == 'window_close' and canceled def test_gui_safe_stop_remains_usable_not_full_matrix_proof(): from test_contact_cycle_skill_v4 import worker_receipt spec = public.make_spec(cycles=3, gui=True) receipt = worker_receipt(spec, stopped=True) receipt['gui'] = {'enabled': True, 'protocol_completed': False, 'render_updates': 10, 'hold_no_physics': True, 'close_reason': 'window_close'} assert launch.complete_receipt(receipt, spec) assert not launch.complete_analytic_prerequisite(receipt, spec) def test_complete_receipt_requires_gui_proof_not_only_physical_pass(): from test_contact_cycle_skill_v4 import worker_receipt spec = public.make_spec(cycles=3, gui=True) receipt = worker_receipt(spec) assert not launch.complete_receipt(receipt, spec) receipt['gui'] = {'enabled': True, 'protocol_completed': True, 'render_updates': 10, 'hold_no_physics': True, 'close_reason': 'window_close'} assert launch.complete_receipt(receipt, spec) for key in ('protocol_completed', 'hold_no_physics'): receipt['gui'][key] = False assert not launch.complete_receipt(receipt, spec) receipt['gui'][key] = True