157 lines
7.0 KiB
Python
157 lines
7.0 KiB
Python
"""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')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|