"""Capture doubles exercise collection only; no test establishes gameplay acceptance.""" import importlib import json from pathlib import Path import subprocess import sys import tempfile import unittest from unittest import mock ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(ROOT / 'tools/recon')) import fidelity_checkpoint as checkpoint import fidelity_receipts as receipts try: capture = importlib.import_module('fidelity_capture') except ModuleNotFoundError: capture = None class CaptureTests(unittest.TestCase): def setUp(self): self.tmp = tempfile.TemporaryDirectory() self.addCleanup(self.tmp.cleanup) self.root = Path(self.tmp.name) self.package = self.root / 'package' self.package.mkdir() for name in ('GamePort', 'GamePort.dll', 'GamePort.runtimeconfig.json', 'GamePort.deps.json', 'libSDL3.so.0', 'BUILD-INFO.txt', 'assets/NewGame.tsk', 'scenes/level1b/scene.json', 'scenes/VR04/scene.json', 'scenes/level1/scene.json', 'scenes/animations/jimstop/animation.json', 'audio/a.wav', 'Shaders/a.spv'): path = self.package / name path.parent.mkdir(parents=True, exist_ok=True) path.write_bytes(b'test fixture ' + name.encode()) (self.package / 'GamePort').chmod(0o755) checkpoint.seal_package(self.package, 'linux-x64', {'commit': 'fixture'}, tooling=True) self.settings = {'display_method': 'x11', 'display': ':11', 'audio_method': 'pulse', 'audio_source': 'hardware.monitor', 'input_method': 'physical', 'size': '1280x720', 'offset': '0,0'} self.run = self.root / 'test-run' self.media = {'streams': [{'codec_type': 'video', 'duration': '2'}, {'codec_type': 'audio', 'duration': '2'}], 'format': {'duration': '2'}} def api(self): self.assertIsNotNone(capture, 'bounded Linux capture API must exist') return capture def probe(self, argv, timeout=10): output = json.dumps(self.media) if 'ffprobe' in Path(argv[0]).name else 'local test tool\n' if 'sources' in argv: output = '1 hardware.monitor module-pipewire float32le 2ch 48000Hz RUNNING\n' return {'argv': argv, 'exit_code': 0, 'stdout': output, 'error': None} def spawn(self, argv, **kwargs): proc = mock.Mock() proc.pid = 99999999 proc.wait.return_value = 0 proc.poll.return_value = 0 if '-f' in argv and argv[-1].endswith('.mkv'): Path(argv[-1]).write_bytes(b'fixture recording') else: observer = Path(argv[argv.index('--fidelity-output') + 1]) observer.mkdir() (observer / 'events.jsonl').write_text('{"fixture":true}\n') return proc def execute(self, **changes): options = dict(package=self.package, output=self.run, session_id='test-launch', case_id='fresh-natural-ordinary', scope='dialogue-return', settings=self.settings, seconds=2, tooling=True, probe=self.probe, popen=self.spawn) options.update(changes) return self.api().run_capture(**options) def test_capture_probe_hash_import_roundtrip_stays_synthetic(self): self.execute() receipt = self.api().collect(self.run, probe=self.probe) self.assertEqual(receipt['status'], 'captured') self.assertEqual(receipt['evidence_kind'], 'synthetic') imported = receipts.import_receipt(self.run / 'receipt.json', self.run, self.root / 'test-imports') self.assertTrue(Path(imported['path']).exists()) self.assertFalse(receipts.validate_receipt(receipt, self.run)['accepted']) self.assertTrue(any(a['role'] == 'video' for a in receipt['artifacts'])) def test_missing_display_publishes_failed_preflight_receipt(self): self.settings['display'] = '' self.execute() receipt = self.api().collect(self.run, probe=self.probe) self.assertEqual(receipt['status'], 'failed') self.assertTrue(any('display' in s.lower() for s in receipt['missing_evidence'])) def test_null_audio_cannot_be_native_acceptance(self): self.settings['audio_source'] = 'auto_null.monitor' report = self.api().preflight(self.package, self.settings, tooling=True, probe=self.probe) self.assertFalse(report['ready']) self.assertTrue(any('null' in s for s in report['blockers'])) def test_stale_package_rejected(self): (self.package / 'GamePort.dll').write_bytes(b'changed') self.assertFalse(self.api().preflight(self.package, self.settings, tooling=True, probe=self.probe)['ready']) def test_fixture_package_rejected_in_real_mode(self): self.assertFalse(self.api().preflight(self.package, self.settings, probe=self.probe)['ready']) def test_recorder_exit_failure_remains_failed(self): def bad(argv, **kwargs): proc = self.spawn(argv, **kwargs) if '-f' in argv: proc.wait.return_value = 7 return proc self.execute(popen=bad) self.assertEqual(self.api().collect(self.run, probe=self.probe)['status'], 'failed') def test_timeout_aborts_owned_processes(self): processes = [] def hanging(argv, **kwargs): proc = self.spawn(argv, **kwargs) proc.wait.side_effect = [subprocess.TimeoutExpired(argv, 2), 0] processes.append(proc) return proc self.execute(popen=hanging) self.assertEqual(self.api().collect(self.run, probe=self.probe)['status'], 'aborted') self.assertTrue(any(p.send_signal.called for p in processes)) def test_empty_media_fails(self): self.execute() (self.run / 'raw.mkv').write_bytes(b'') self.assertEqual(self.api().collect(self.run, probe=self.probe)['status'], 'failed') def test_missing_audio_stream_fails(self): self.execute() self.media['streams'] = self.media['streams'][:1] self.assertEqual(self.api().collect(self.run, probe=self.probe)['status'], 'failed') def test_frozen_indicator_keeps_result_incomplete(self): self.execute() (self.run / 'recorder.log').write_text('lavfi.freezedetect.freeze_start: 0\n') result = self.api().collect(self.run, probe=self.probe) self.assertEqual(result['status'], 'failed') self.assertTrue(any('frozen' in x for x in result['missing_evidence'])) def test_automated_or_headless_never_physical_category(self): self.settings['input_method'] = 'automated' self.execute() result = self.api().collect(self.run, probe=self.probe) self.assertEqual(result['evidence_kind'], 'synthetic') self.assertFalse(result['review']['ordinary_input']) def test_output_reuse_and_package_overlap_rejected_before_launch(self): self.execute() with self.assertRaises(ValueError): self.execute() with self.assertRaises(ValueError): self.execute(output=self.package / 'capture') def matrix(self, records=(), **kwargs): api = self.api() self.assertTrue(hasattr(api, 'matrix'), 'Linux matrix must enumerate and preserve all forty obligations') return api.matrix(receipts.read_json(ROOT / 'docs/evidence/scenarios.json'), records, **kwargs) def test_matrix_enumerates_eight_cells_forty_open_obligations(self): result = self.matrix(dependency={'receipt_id': 'failed-preflight', 'reasons': ['No real display']}) self.assertEqual(len(result['cells']), 8) self.assertEqual(sum(len(c['obligations']) for c in result['cells']), 40) self.assertTrue(all(o['state'] == 'open' and o['dependencies'] for c in result['cells'] for o in c['obligations'])) def test_matrix_preserves_dialogue_lifetime_and_input_procedures(self): result = self.matrix() for cell in result['cells']: self.assertIn(cell['lifetime'], ('fresh', 'retained')) self.assertIn(cell['dialogue'], ('natural', 'skipped')) self.assertIn(cell['input'], ('ordinary', 'edge')) for obligation in cell['obligations']: if cell['dialogue'] == 'skipped' and obligation['id'] == 'dialogue-return': self.assertIn('dialogue_skip', obligation['required_events']) if cell['input'] == 'edge' and obligation['id'] == 'continuous': self.assertIn('held_return', obligation['required_events']) def test_continuous_route_cannot_be_replaced_by_focused_capture(self): self.execute() receipt = self.api().collect(self.run, probe=self.probe) result = self.matrix([(receipt, self.run)]) cell = next(c for c in result['cells'] if c['id'] == receipt['case_id']) continuous = next(o for o in cell['obligations'] if o['id'] == 'continuous') focused = next(o for o in cell['obligations'] if o['id'] == 'dialogue-return') self.assertFalse(continuous['attempts']) self.assertEqual(focused['attempts'][0]['receipt_id'], receipt['receipt_id']) self.assertEqual(continuous['route'], ['lab', 'VR04', 'lab', 'outside']) def test_resume_preserves_failed_history_and_requires_new_launch(self): self.settings['display'] = '' self.execute() first = self.api().collect(self.run, probe=self.probe) previous = self.matrix([(first, self.run)]) new_run = self.root / 'test-second' self.execute(output=new_run, session_id='test-second') second = self.api().collect(new_run, probe=self.probe) resumed = self.matrix([(second, new_run)], previous=previous) attempts = next(o['attempts'] for c in resumed['cells'] for o in c['obligations'] if o['attempts']) self.assertEqual([a['session_id'] for a in attempts], ['test-launch', 'test-second']) second['session_id'] = first['session_id'] with self.assertRaises(ValueError): self.matrix([(second, new_run)], previous=previous) def test_collection_exposes_shared_scenario_identity_without_claiming_continuity(self): self.execute() receipt = self.api().collect(self.run, probe=self.probe) self.assertEqual(receipt.get('obligation_id'), 'dialogue-return') self.assertEqual(receipt['clocks'].get('clock_id'), 'test-launch-monotonic') matrix = self.matrix([(receipt, self.run)]) self.assertEqual(matrix['accepted'], 0) self.assertFalse(matrix['complete']) def test_disconnected_or_missing_retained_setup_remains_open(self): self.execute(case_id='retained-skipped-edge', scope='continuous') receipt = self.api().collect(self.run, probe=self.probe) result = self.matrix([(receipt, self.run)]) attempted = next(o for c in result['cells'] for o in c['obligations'] if o['attempts']) self.assertEqual(attempted['state'], 'open') self.assertTrue(any('scenario' in x.lower() for x in attempted['dependencies'])) if __name__ == '__main__': unittest.main()