import hashlib
import importlib
import json
from pathlib import Path
import sys
import unittest

modules=['app.byte_tracking','app.stream_settings','app.api.streams','app.stream_manager','app.det_result_path']
files={name:Path(importlib.import_module(name).__file__) for name in modules}
expected='/opt/contextra/' if 'image' in sys.argv[1] else '/source/'
assert all(str(f).startswith(expected) for f in files.values()),files
print({k:dict(file=str(f),sha256=hashlib.sha256(f.read_bytes()).hexdigest()) for k,f in files.items()})
suite=unittest.TestSuite()
def add(tests):
    for t in tests:
        if isinstance(t,unittest.TestSuite):add(t)
        elif not t.id().endswith(('test_entrypoint_scopes_blas_to_application', 'test_manifest_exactness')):suite.addTest(t)
for pattern in ['test_tracking_disabled.py','test_tracking_kernel.py','test_tracking_empty_update.py','test_det_result_path.py','test_fire_path.py']:
    add(unittest.defaultTestLoader.discover('/tests',pattern=pattern))
add(unittest.defaultTestLoader.loadTestsFromName('test_intrusion_connection.ScenarioConnectionTest'))
r=unittest.TextTestRunner(verbosity=2).run(suite)
out=dict(tests=r.testsRun,success=r.wasSuccessful(),errors=len(r.errors),failures=len(r.failures))
Path('/evidence',sys.argv[1]+'.json').write_text(json.dumps(out));print(json.dumps(out))
raise SystemExit(0 if r.wasSuccessful() else 1)
