diff --git a/tests/test_fidelity_scenarios.py b/tests/test_fidelity_scenarios.py new file mode 100644 index 0000000..bd59e26 --- /dev/null +++ b/tests/test_fidelity_scenarios.py @@ -0,0 +1,200 @@ +"""Scenario completeness uses temporary metadata fixtures, never real captures.""" +import copy +import importlib +import itertools +import json +from pathlib import Path +import subprocess +import sys +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tools/recon')) +api = importlib.import_module('fidelity_receipts') +PROTOCOL = ROOT / 'docs/evidence/scenarios.json' + + +class FidelityScenarios(unittest.TestCase): + def setUp(self): + self.tmp = tempfile.TemporaryDirectory() + self.addCleanup(self.tmp.cleanup) + self.base = Path(self.tmp.name) + + def protocol(self): + self.assertTrue(PROTOCOL.is_file(), 'eight-cell executable protocol must exist') + return json.loads(PROTOCOL.read_text()) + + def test_eight_explicit_cells(self): + p = self.protocol() + self.assertEqual(len(p['cells']), 8) + expected = set(itertools.product(('fresh', 'retained'), ('natural', 'skipped'), ('ordinary', 'edge'))) + self.assertEqual({(c['lifetime'], c['dialogue'], c['input']) for c in p['cells']}, expected) + self.assertEqual(len({c['id'] for c in p['cells']}), 8) + + def test_stable_ids_and_order(self): + p = self.protocol() + self.assertEqual([c['id'] for c in p['cells']], sorted(c['id'] for c in p['cells'])) + for c in p['cells']: + self.assertEqual(c['id'], '-'.join((c['lifetime'], c['dialogue'], c['input']))) + api.validate_scenario_contract(p) + + def test_every_cell_has_continuous_and_focused_obligations(self): + p = self.protocol() + for cell in p['cells']: + self.assertEqual(cell['obligations'], ['continuous', 'dialogue-return', 'bank-left', 'bank-right', 'landing-idle']) + self.assertEqual(p['obligations']['continuous']['route'], ['lab', 'VR04', 'lab', 'outside']) + for o in p['obligations'].values(): + for field in ('setup', 'actions', 'end_condition', 'required_events', 'required_artifact_roles'): + self.assertTrue(o[field]) + + def test_missing_dimension_even_equal_count_rejected(self): + p = self.protocol() + p['cells'][-1] = copy.deepcopy(p['cells'][0]) + with self.assertRaises(ValueError): api.validate_scenario_contract(p) + + def test_natural_skipped_are_equal_obligations(self): + p = self.protocol() + for lifetime, inputs in itertools.product(('fresh', 'retained'), ('ordinary', 'edge')): + cells = [c for c in p['cells'] if c['lifetime'] == lifetime and c['input'] == inputs] + self.assertEqual(cells[0]['obligations'], cells[1]['obligations']) + self.assertIn('dialogue_end', p['dialogue_events']['natural']) + self.assertIn('dialogue_skip', p['dialogue_events']['skipped']) + + def test_edge_procedure_includes_held_input_both_banks_landing_idle(self): + p = self.protocol() + self.assertEqual(set(p['input_events']['edge']), {'resumed_held_input', 'held_dialogue', 'held_return'}) + self.assertIn('first_neutral_input', p['obligations']['dialogue-return']['required_events']) + self.assertIn('landing', p['obligations']['landing-idle']['required_events']) + self.assertIn('idle', p['obligations']['landing-idle']['required_events']) + + def test_empty_receipts_structural_readiness_is_not_acceptance(self): + result = api.audit_scenarios(self.protocol(), []) + self.assertTrue(result['structurally_ready']) + self.assertFalse(result['complete']) + self.assertEqual(result['required'], 120) + self.assertEqual(result['accepted'], 0) + self.assertEqual(len(result['missing_case_ids']), 8) + + def receipt(self, cell='retained-skipped-edge', obligation='continuous', kind='native_port', os_name='linux'): + p = self.protocol() + c = next(c for c in p['cells'] if c['id'] == cell) + d = self.base / (cell + '-' + obligation + '-' + kind + '-' + os_name) + d.mkdir(exist_ok=True) + artifacts = [] + for name, role in [('video.txt', 'video'), ('audio.txt', 'audio'), + ('events.txt', 'input_events'), ('setup.txt', 'prerequisite')]: + (d / name).write_text('temporary metadata fixture; not native media') + artifacts.append(dict(api.identify_file(d / name), path=name, role=role, + duration_seconds=100)) + identity = {} + for key in ('executable', 'package', 'resources', 'launcher', 'source', 'original_pre', + 'original_post', 'copy_pre', 'copy_post', 'dxwnd', 'dxwnd_config'): + identity[key] = dict(api.identify_file(d / 'setup.txt'), path='setup.txt') + identity['source'].update(commit='a' * 40, dirty=False, diff_sha256='b' * 64) + required = api.required_events(p, c, obligation) + events = [dict(name=name, artifact='video.txt', media_seconds=index, + elapsed_seconds=index, method='manual', uncertainty_seconds=.1) + for index, name in enumerate(required)] + value = {'schema_version': 1, 'receipt_id': d.name, 'session_id': d.name, + 'case_id': cell, 'obligation_id': obligation, 'evidence_kind': kind, + 'status': 'reviewed', 'historical': False, 'scope': obligation, + 'platform': {'os': os_name, 'execution': {'native_port': 'native', + 'original_dxwnd': 'dxwnd'}.get(kind, kind), 'version': 'unit-fixture', + 'gpu': 'fixture', 'driver': 'fixture', 'vulkan': 'fixture', 'sdl': 'fixture'}, + 'identity': identity, 'invocation': {'argv': ['fixture'], 'cwd': '.'}, + 'clocks': {'wall_start': '2026-01-01T00:00:00Z', 'wall_end': '2026-01-01T00:01:40Z', + 'monotonic_start': 0, 'monotonic_end': 100, 'clock_id': d.name}, + 'artifacts': artifacts, 'derivatives': [], 'events': events, + 'limitations': ['Unit fixture, not imported as runtime proof'], 'missing_evidence': [], + 'acceptance': {'state': 'accepted', 'remaining': []}, + 'review': dict(visible=True, audible=True, ordinary_input=True, human_reviewed=True), + 'wrapper_settings': {'description': 'unit fixture only'}, + 'continuity': {'lifetime_id': d.name, 'departure_session_id': d.name, + 'return_session_id': d.name, 'prerequisite_artifact': 'setup.txt', + 'copy_pre_sha256': 'a' * 64, 'retained_copy_sha256': 'b' * 64}} + return value, d + + def test_identified_metadata_cell_can_cover_one_obligation(self): + value, root = self.receipt() + result = api.audit_scenarios(self.protocol(), [(value, root)]) + self.assertEqual(result['accepted'], 1) + self.assertFalse(result['complete']) + + def test_disconnected_route_snapshots_fail(self): + value, root = self.receipt() + value['events'][-1]['artifact'] = 'audio.txt' + result = api.audit_scenarios(self.protocol(), [(value, root)]) + self.assertEqual(result['accepted'], 0) + self.assertTrue(any('continuous' in error for error in result['invalid'])) + + def test_retained_departure_return_must_share_session(self): + value, root = self.receipt() + value['continuity']['return_session_id'] = 'different-launch' + self.assertEqual(api.audit_scenarios(self.protocol(), [(value, root)])['accepted'], 0) + + def test_retained_requires_prerequisite_and_copy_state(self): + value, root = self.receipt() + del value['continuity']['retained_copy_sha256'] + self.assertEqual(api.audit_scenarios(self.protocol(), [(value, root)])['accepted'], 0) + + def test_reset_timestamps_fail(self): + value, root = self.receipt() + value['events'][-1]['elapsed_seconds'] = 0 + self.assertEqual(api.audit_scenarios(self.protocol(), [(value, root)])['accepted'], 0) + + def test_fresh_sessions_are_independent(self): + a, ar = self.receipt('fresh-natural-ordinary') + b, br = self.receipt('fresh-natural-edge') + b['session_id'] = a['session_id'] + b['continuity'] = copy.deepcopy(a['continuity']) + result = api.audit_scenarios(self.protocol(), [(a, ar), (b, br)]) + self.assertLess(result['accepted'], 2) + self.assertTrue(any('fresh' in error for error in result['invalid'])) + + def test_synthetic_wine_component_cannot_substitute_native(self): + for kind in ('synthetic', 'wine', 'component', 'cross_publish'): + value, root = self.receipt(kind=kind) + value['acceptance'] = {'state': 'open', 'remaining': ['not native']} + self.assertEqual(api.audit_scenarios(self.protocol(), [(value, root)])['accepted'], 0) + + def test_skipped_aborted_failed_missing_remain_open(self): + for status in ('skipped', 'aborted', 'failed', 'missing'): + value, root = self.receipt() + value['status'] = status + value['acceptance'] = {'state': 'open', 'remaining': [status]} + self.assertEqual(api.audit_scenarios(self.protocol(), [(value, root)])['accepted'], 0) + + def test_missing_event_and_media_out_of_range_rejected(self): + value, root = self.receipt() + value['events'].pop() + self.assertEqual(api.audit_scenarios(self.protocol(), [(value, root)])['accepted'], 0) + value, root = self.receipt() + value['events'][-1]['media_seconds'] = 101 + self.assertEqual(api.audit_scenarios(self.protocol(), [(value, root)])['accepted'], 0) + + def test_cli_readiness_and_strict_verdicts_differ(self): + p = self.protocol() + path = self.base / 'scenarios.json' + path.write_text(json.dumps(p)) + index = self.base / 'receipts' + index.mkdir() + args = [sys.executable, str(ROOT / 'tools/recon/fidelity.py'), 'validate', + '--scenarios', str(path), '--receipts', str(index)] + ready = subprocess.run([*args, '--allow-open'], capture_output=True, text=True, timeout=15) + strict = subprocess.run([*args, '--require-complete'], capture_output=True, text=True, timeout=15) + self.assertEqual(ready.returncode, 0, ready.stderr) + self.assertNotEqual(strict.returncode, 0) + self.assertIn('0/120', ready.stdout) + self.assertIn('OPEN', strict.stdout) + self.assertIn('fresh-natural-edge', strict.stdout) + + def test_protected_observations_and_clock_semantics_recorded(self): + p = self.protocol() + self.assertEqual(set(p['protected_observations']), {'tsk_retained_order', 'fresh_direct_scene_order', 'legitimate_landing_bank'}) + self.assertEqual(p['event_contract']['simulation_ticks'], 'separate_port_only') + self.assertEqual(p['event_contract']['original_method'], ['manual', 'procedural']) + + +if __name__ == '__main__': + unittest.main()