"""SDK public BufferOperator False -> drop; CPU finite fixture, not live inference."""
import glob
import json
from pyservicemaker import Pipeline, Probe, BufferOperator
from app.multi_pipeline import _DecodedFrameObserver
from app.stream_lifecycle import RoleRunHandle

assert not glob.glob('/dev/nvidia*')
h = RoleRunHandle('det', 1)
source = _DecodedFrameObserver(h, drop_on_stop=True)
counts = {'input': 0, 'output': 0}
class Before(BufferOperator):
    def handle_buffer(self, buffer):
        counts['input'] += 1
        if counts['input'] == 4:
            h.stop()
        return source.handle_buffer(buffer)
class After(BufferOperator):
    def handle_buffer(self, buffer):
        counts['output'] += 1
        return True
p = Pipeline('stop-drop-probe')
p.add('fakesrc', 'source', {'num-buffers': 12})
p.add('identity', 'tail')
p.add('fakesink', 'sink', {'sync': False})
p.link('source', 'tail', 'sink')
before, after = Before(), After()
p.attach('source', Probe('before', before))
p.attach('tail', Probe('after', after))
p.start().wait()
p.stop()
assert counts == {'input': 12, 'output': 3}, counts
print(json.dumps({'status': 'PASS', **counts, 'dropped': 9, 'gpu_nodes': 0, 'live_inference': False}))
