"""기존 SDK image의 network-none/GPU0 환경에서 별도 process timeout과 함께 실행한다.

실제 SDK message -> production callback 경계만 확인하며 추론/영상 정확도 검증은 아니다.
"""
import glob
import json
import os

assert not glob.glob('/dev/nvidia*')
os.environ['DATABASE_URL'] = 'postgresql://u:p' + chr(64) + 'localhost:1/db'
os.environ['REDIS_STREAMS_ENABLED'] = 'false'
from app.multi_pipeline import _pipeline_message_callback
from app.stream_lifecycle import RoleRunHandle
from pyservicemaker import Pipeline

events = []
handle = RoleRunHandle('det', 1, lambda *args: events.append(args))
pipeline = Pipeline('callback-contract-probe')
pipeline.add('fakesrc', 'source', {'num-buffers': 1})
pipeline.add('fakesink', 'sink', {'sync': False})
pipeline.link('source', 'sink')
handle.bind_pipeline(pipeline)
try:
    handle.start_pipeline(_pipeline_message_callback(handle, finite=True)).wait()
finally:
    failure = handle.stop()
assert failure is None and handle.failure is None
assert any(event[2] == 'source_stage' for event in events), 'no SDK state message observed'
print(json.dumps({'status': 'PASS', 'sdk_state_messages': sum(e[2] == 'source_stage' for e in events),
                  'gpu_nodes': 0, 'model_inference': False}))
