You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
273 lines
18 KiB
Python
273 lines
18 KiB
Python
"""Offline checks: real candidate alarm functions, fake transports; no model imports."""
|
|
import ast, asyncio, dataclasses as dc, importlib, json, logging, math
|
|
from pathlib import Path
|
|
import sys, types, unittest
|
|
from unittest.mock import patch
|
|
|
|
ROOT = Path(__file__).resolve().parent
|
|
PKG = 'cc10_offline'
|
|
pkg = types.ModuleType(PKG); pkg.__path__ = [str(ROOT/'ptz_tracker_modular')]; sys.modules[PKG]=pkg
|
|
cfg = types.ModuleType(PKG+'.config')
|
|
cfg.UNKNOWN_INSPECTION_ENABLE=True
|
|
cfg.CAMERA_CONFIG={10: {'mac':'00:11:22:33:44:55'},8: {'mac':'00:11:22:33:44:66'}}
|
|
cfg.ALARM_COUNT_ON=1;cfg.ALARM_COUNT_OFF=3;cfg.ALARM_HTTP_ENABLE=True
|
|
cfg.ALARM_HTTP_BASE='https://offline.invalid';cfg.ALARM_HTTP_PATH='/videodata'
|
|
sys.modules[PKG+'.config']=cfg
|
|
state=types.ModuleType(PKG+'.state');state.MAIN_LOOP=None;state.alarm_queue=None;state.detected_cameras={10,8}
|
|
sys.modules[PKG+'.state']=state
|
|
cams=types.ModuleType(PKG+'.cameras_io');cams.get_all=lambda:cfg.CAMERA_CONFIG
|
|
sys.modules[PKG+'.cameras_io']=cams
|
|
policy=importlib.import_module(PKG+'.alarm_eligibility')
|
|
contracts=importlib.import_module(PKG+'.model_class_contract')
|
|
boundary=importlib.import_module(PKG+'.alarm_boundary')
|
|
alarms=importlib.import_module(PKG+'.alarms')
|
|
Context,Frame,Semantic,AlarmEligibility=policy.Context,policy.Frame,policy.Semantic,policy.AlarmEligibility
|
|
A='a'*64;B='b'*64
|
|
NAMES=('multirotor_uav','fixed_wing_uav','bird','manned_aircraft')
|
|
|
|
|
|
def model(role='semantic_classifier', **kw):
|
|
data=dict(path='fake.pt',actual_sha=B if role=='semantic_classifier' else A,
|
|
expected_sha=B if role=='semantic_classifier' else A,
|
|
raw_names=NAMES if role=='semantic_classifier' else ['airborne_object'],
|
|
role=role,metadata_source='loaded_model.names',actual_task='classify' if role=='semantic_classifier' else 'detect')
|
|
data.update(kw)
|
|
return contracts.validate_contract(**data)
|
|
|
|
|
|
def scores(name='multirotor_uav'):
|
|
return tuple((n,.91 if n==name else .03) for n in NAMES)
|
|
|
|
|
|
class Base(unittest.TestCase):
|
|
def setUp(self):
|
|
cfg.UNKNOWN_INSPECTION_ENABLE=True;cfg.CAMERA_CONFIG={10:{'mac':'00:11:22:33:44:55'},8:{'mac':'00:11:22:33:44:66'}}
|
|
self.clock=10.2
|
|
self.ctx=Context(10,1,('auto',7,12),'camera21:stream_epoch=1',A,B)
|
|
self.gate=AlarmEligibility()
|
|
self.gate.begin(self.ctx,proposal_contract=model('generic_detector'),classifier_contract=model())
|
|
self.provider={10:self.ctx}
|
|
boundary.bind_guard(self.gate,lambda c:self.provider.get(c))
|
|
self.tm=patch.object(boundary,'time',types.SimpleNamespace(monotonic=lambda:self.clock));self.tm.start();self.addCleanup(self.tm.stop)
|
|
alarms._H.clear();alarms._http_queue=asyncio.Queue();state.alarm_queue=asyncio.Queue();state.MAIN_LOOP=None
|
|
self.f1=Frame(1,10.,(100,100,200,200),(720,1280))
|
|
self.f2=Frame(2,10.1,(101,100,201,200),(720,1280))
|
|
|
|
def put(self,frame,name='multirotor_uav',**kw):
|
|
self.assertTrue(self.gate.observe(self.ctx,frame,self.clock))
|
|
return self.gate.submit(self.ctx,Semantic(frame,B,scores(name),100,**kw),self.clock)
|
|
|
|
def confirm(self):
|
|
self.assertFalse(self.put(self.f1));self.assertTrue(self.put(self.f2))
|
|
|
|
|
|
class EligibilityTests(Base):
|
|
def test_unknown_record_and_tracker_score_are_not_evidence(self):
|
|
state.rec_active=True;state.tracker_score=.999
|
|
self.assertFalse(self.gate.eligible(self.ctx,self.clock))
|
|
self.assertFalse(boundary.arm_allowed(10))
|
|
def test_one_frame_denied_two_frames_allowed(self): self.confirm()
|
|
def test_duplicate_cached_frame_never_second_vote(self):
|
|
self.put(self.f1)
|
|
self.assertFalse(self.gate.submit(self.ctx,Semantic(self.f1,B,scores(),100),self.clock))
|
|
def test_reordered_frame_cannot_vote(self):
|
|
self.put(self.f2)
|
|
self.assertFalse(self.gate.observe(self.ctx,self.f1,self.clock))
|
|
self.assertFalse(self.gate.eligible(self.ctx,self.clock))
|
|
def test_result_must_match_original_frame_exactly(self):
|
|
self.gate.observe(self.ctx,self.f1,self.clock)
|
|
forged=dc.replace(self.f1,bbox=(100,100,201,200))
|
|
self.assertFalse(self.gate.submit(self.ctx,Semantic(forged,B,scores(),100),self.clock))
|
|
def test_inference_completion_cannot_replace_acquisition(self):
|
|
self.gate.observe(self.ctx,self.f1,self.clock)
|
|
forged=dc.replace(self.f1,acquired_mono=self.clock)
|
|
self.assertFalse(self.gate.submit(self.ctx,Semantic(forged,B,scores(),100),self.clock))
|
|
def test_wrong_context_dimensions(self):
|
|
self.confirm()
|
|
for field,value in [('camera',8),('inspection_id',2),('owner',('auto',8,12)),('source_id','camera21:stream_epoch=2'),('classifier_sha','c'*64),('proposal_sha','d'*64)]:
|
|
with self.subTest(field=field):self.assertFalse(self.gate.eligible(dc.replace(self.ctx,**{field:value}),self.clock))
|
|
def test_unpinned_classifier_does_not_vote(self):
|
|
self.put(self.f1);self.gate.observe(self.ctx,self.f2,self.clock)
|
|
self.assertFalse(self.gate.submit(self.ctx,Semantic(self.f2,'c'*64,scores(),100),self.clock))
|
|
def test_both_semantic_frames_must_remain_fresh(self):
|
|
self.confirm();self.assertFalse(self.gate.eligible(self.ctx,10.66))
|
|
def test_future_and_nan_time_rejected(self):
|
|
for value in (11.,float('nan'),float('inf')):
|
|
with self.subTest(value=value):self.assertFalse(self.gate.observe(self.ctx,dc.replace(self.f1,acquired_mono=value),self.clock))
|
|
def test_duplicate_timestamp_not_independent(self):
|
|
self.put(self.f1);self.assertFalse(self.gate.observe(self.ctx,dc.replace(self.f2,acquired_mono=10.),self.clock))
|
|
def test_semantic_class_must_be_consistent(self):
|
|
self.put(self.f1);self.assertFalse(self.put(self.f2,'fixed_wing_uav'))
|
|
def test_bird_or_manned_aircraft_never_alarm(self):
|
|
for name in ('bird','manned_aircraft'):
|
|
with self.subTest(name=name):
|
|
self.setUp();self.assertFalse(self.put(self.f1,name));self.assertFalse(self.put(self.f2,name))
|
|
def test_bad_details_revoke_previously_confirmed_target(self):
|
|
for kwargs in ({'clipped':True},{'occluded':True},{'sharpness':0},{'scores':(('multirotor_uav',1.),)}, {'scores':scores('bird')}):
|
|
with self.subTest(kwargs=kwargs):
|
|
self.setUp();self.confirm();f=dc.replace(self.f2,seq=3,acquired_mono=10.2)
|
|
self.gate.observe(self.ctx,f,self.clock)
|
|
result=dc.replace(Semantic(f,B,scores(),100),**kwargs)
|
|
self.assertFalse(self.gate.submit(self.ctx,result,self.clock));self.assertFalse(self.gate.eligible(self.ctx,self.clock))
|
|
def test_small_original_box_cannot_be_enlarged_crop_proof(self):
|
|
f=dc.replace(self.f1,bbox=(100,100,110,110));self.gate.observe(self.ctx,f,self.clock)
|
|
self.assertFalse(self.gate.submit(self.ctx,Semantic(f,B,scores(),100),self.clock))
|
|
def test_geometry_discontinuity_revokes(self):
|
|
self.confirm();f=dc.replace(self.f2,seq=3,acquired_mono=10.2,bbox=(600,100,700,200))
|
|
self.assertFalse(self.gate.observe(self.ctx,f,self.clock));self.assertFalse(self.gate.eligible(self.ctx,self.clock))
|
|
def test_shape_change_revokes(self):
|
|
self.confirm();f=dc.replace(self.f2,seq=3,acquired_mono=10.2,shape=(1080,1920))
|
|
self.assertFalse(self.gate.observe(self.ctx,f,self.clock));self.assertFalse(self.gate.eligible(self.ctx,self.clock))
|
|
def test_revoke_lost_owner_fullstop_blocks_queued_proof(self):
|
|
self.confirm();self.gate.revoke(10);self.assertFalse(self.gate.eligible(self.ctx,self.clock))
|
|
def test_actual_visual_control_owner_tuple_accepts_finite_float(self):
|
|
context=dc.replace(self.ctx,owner=(147852,7,1793000000.125,2))
|
|
self.gate.begin(context,proposal_contract=model('generic_detector'),classifier_contract=model())
|
|
self.assertTrue(context.valid())
|
|
for value in (True,float('nan'),float('inf')):
|
|
with self.subTest(value=value):self.assertFalse(dc.replace(context,owner=(147852,7,value,2)).valid())
|
|
|
|
def test_begin_requires_verified_two_roles(self):
|
|
with self.assertRaises(ValueError):self.gate.begin(self.ctx,proposal_contract=model(),classifier_contract=model())
|
|
def test_frame_ledger_is_bounded(self):
|
|
for seq in range(1,101):self.gate.observe(self.ctx,Frame(seq,10+seq/1000,(100,100,200,200),(720,1280)),self.clock)
|
|
self.assertEqual(len(self.gate._states[10].frames),16)
|
|
|
|
|
|
class ModelTests(unittest.TestCase):
|
|
def test_preserves_legacy_numeric_names_no_fixed_wing_alias(self):
|
|
c=model('legacy_display',raw_names={0:'Fighter',1:'drone'},actual_sha=A,expected_sha=A)
|
|
self.assertEqual(c.names,('Fighter','drone'));self.assertEqual(c.role,'legacy_display')
|
|
with self.assertRaises(ValueError):model(raw_names=c.names)
|
|
def test_task_cannot_be_inferred_from_label_names(self):
|
|
for task in (None,'detect','segment'):
|
|
with self.subTest(task=task),self.assertRaises(ValueError):model(actual_task=task)
|
|
with self.assertRaises(ValueError):model('generic_detector',actual_task='classify')
|
|
|
|
def test_generic_class_zero_is_not_uav_class_zero(self):
|
|
self.assertEqual(model('generic_detector').names,('airborne_object',))
|
|
with self.assertRaises(ValueError):model('generic_detector',raw_names=['drone'])
|
|
def test_four_semantic_labels_exact_no_silent_alias(self):
|
|
for names in [('multirotor_uav','fixedwing_uav','bird','manned_aircraft'),('multirotor_uav','fixed_wing_uav','bird','civil_aircraft')]:
|
|
with self.subTest(names=names),self.assertRaises(ValueError):model(raw_names=names)
|
|
def test_format_metadata_sources_follow_checkout(self):
|
|
for path,source in [('fake.pt','loaded_model.names'),('fake.pth','result.names'),('fake.onnx','onnx.metadata.names'),('fake.onnx','result.names'),('fake.engine','result.names')]:
|
|
with self.subTest(path=path,source=source):self.assertEqual(model(path=path,metadata_source=source).names,NAMES)
|
|
def test_unsupported_format_or_metadata_fails(self):
|
|
for path,source in [('fake.tflite','result.names'),('fake.engine','loaded_model.names'),('fake.pt','config.CLASS_NAMES'),('fake.onnx','sidecar-unpinned')]:
|
|
with self.subTest(path=path,source=source),self.assertRaises(ValueError):model(path=path,metadata_source=source)
|
|
def test_duplicate_literal_keys_are_not_silently_overwritten(self):
|
|
with self.assertRaises(ValueError):contracts.normalize_names("{0: 'Fighter', 0: 'drone'}")
|
|
|
|
def test_model_hash_must_match_pin(self):
|
|
with self.assertRaises(ValueError):model(actual_sha=A)
|
|
def test_no_fallback_when_names_missing_or_ambiguous(self):
|
|
for raw in (None,{},[],{True:'x'},{0:'x','0':'y'},{'01':'x'},['bird','bird'],{0:'x',2:'y'}, {0:' x '}):
|
|
with self.subTest(raw=raw),self.assertRaises(ValueError):contracts.normalize_names(raw)
|
|
def test_onnx_literal_names_are_data_only(self):
|
|
self.assertEqual(contracts.normalize_names("{0: 'Fighter', 1: 'drone'}"),('Fighter','drone'))
|
|
with self.assertRaises(ValueError):contracts.normalize_names("__import__('os').system('false')")
|
|
|
|
|
|
class BoundaryTests(Base):
|
|
def test_unknown_false_allows_recording_but_blocks_alarm_ingress(self):
|
|
alarms.ingest_detection(10,True)
|
|
self.assertFalse(alarms.is_armed(10));self.assertTrue(state.alarm_queue.empty())
|
|
def test_verified_alarm_ingress_and_snapshot(self):
|
|
self.confirm();alarms.ingest_detection(10,True)
|
|
self.assertTrue(alarms.is_armed(10));self.assertTrue(state.alarm_queue.get_nowait()['data'])
|
|
self.assertTrue(alarms._http_queue.get_nowait()['json']['cameras'][1]['alarm'])
|
|
def test_raw_queued_boolean_or_untokened_on_denied(self):
|
|
self.confirm()
|
|
for payload in (True,'true',{'type':'video_alarm','data':True}):self.assertFalse(boundary.filter_ws(payload)['data'])
|
|
def test_smoke_cannot_arm_unknown_camera(self):
|
|
alarms.queue_alarm_mac('00:11:22:33:44:55',True)
|
|
self.assertFalse(state.alarm_queue.get_nowait()['data'])
|
|
def test_unmapped_smoke_mac_denied_even_with_other_target(self):
|
|
self.confirm();alarms.queue_alarm_mac('00:11:22:33:44:77',True)
|
|
self.assertFalse(state.alarm_queue.get_nowait()['data'])
|
|
def test_rec_active_alone_and_legacy_events_api_denied(self):
|
|
tree=ast.parse((ROOT/'ptz_tracker_modular/events.py').read_text(encoding='utf8'))
|
|
fn=next(n for n in tree.body if isinstance(n,ast.FunctionDef) and n.name=='queue_alarm')
|
|
queued=[];env={'_dispatch_alarm':queued.append,'_boundary':boundary}
|
|
exec(compile(ast.Module(body=[fn],type_ignores=[]),'events-queue','exec'),env)
|
|
env['queue_alarm'](10,True);self.assertFalse(queued[0]['data'])
|
|
def test_buffered_on_rechecked_after_expiry(self):
|
|
self.confirm();alarms.ingest_detection(10,True);queued=state.alarm_queue.get_nowait();self.clock=10.9
|
|
self.assertFalse(boundary.filter_ws(queued)['data']);self.assertFalse(alarms.is_armed(10))
|
|
def test_owner_or_stream_changed_before_send_denied(self):
|
|
self.confirm();alarms.ingest_detection(10,True);queued=state.alarm_queue.get_nowait()
|
|
self.provider[10]=dc.replace(self.ctx,owner=('manual',8,12));self.assertFalse(boundary.filter_ws(queued)['data'])
|
|
def test_old_token_cannot_use_new_confirmed_session(self):
|
|
self.confirm();alarms.ingest_detection(10,True);queued=state.alarm_queue.get_nowait()
|
|
self.ctx=dc.replace(self.ctx,inspection_id=2);self.provider[10]=self.ctx
|
|
self.gate.begin(self.ctx,proposal_contract=model('generic_detector'),classifier_contract=model());self.confirm()
|
|
self.assertFalse(boundary.filter_ws(queued)['data'])
|
|
def test_http_retry_rechecks_expiry_and_preserves_body(self):
|
|
self.confirm();alarms.ingest_detection(10,True);item=alarms._http_queue.get_nowait();self.clock=10.9
|
|
clean=boundary.filter_http(item['json'],item['_cc10_contexts'],alarms._camera_for_mac)
|
|
self.assertFalse(any(c['alarm'] for c in clean['cameras']));self.assertTrue(any(c['alarm'] for c in item['json']['cameras']))
|
|
def test_heartbeat_drops_stale_armed_state(self):
|
|
self.confirm();alarms.ingest_detection(10,True);self.clock=10.9
|
|
_,body=alarms._bulk_snapshot_for_all_cams();self.assertFalse(any(c['alarm'] for c in body['cameras']))
|
|
def test_provider_failure_and_missing_binding_fail_closed(self):
|
|
self.confirm();boundary._context_provider=lambda c:1/0
|
|
self.assertFalse(boundary.arm_allowed(10));boundary._gate=None;self.assertFalse(boundary.arm_allowed(10))
|
|
def test_off_always_passes_as_off(self):
|
|
alarms.queue_alarm_mac('00:11:22:33:44:55',False)
|
|
self.assertFalse(state.alarm_queue.get_nowait()['data'])
|
|
def test_disabled_mode_preserves_legacy(self):
|
|
cfg.UNKNOWN_INSPECTION_ENABLE=False;alarms.ingest_detection(10,True);self.assertTrue(alarms.is_armed(10))
|
|
def test_smoke_startup_is_disabled_under_unknown_mode(self):
|
|
tree=ast.parse((ROOT/'ptz_tracker_modular/main.py').read_text(encoding='utf8'))
|
|
node=next(n for n in ast.walk(tree) if isinstance(n,ast.If) and 'ALARM_SMOKE' in ast.unparse(n.test))
|
|
fake_os=types.SimpleNamespace(getenv=lambda *a:'1')
|
|
self.assertFalse(eval(compile(ast.Expression(node.test),'smoke-test','eval'),{'config':cfg,'os':fake_os}))
|
|
def run_sender(self, coroutine):
|
|
# Windows Proactor creates a TCP socketpair even for fake transports.
|
|
# Step this coroutine with a deterministic fake loop instead; no sockets.
|
|
class Loop:
|
|
tick=0.
|
|
def time(self):self.tick+=.02;return self.tick
|
|
async def run_in_executor(self, executor, fn):return fn()
|
|
async def sleep(*a):return None
|
|
loop=Loop();state.MAIN_LOOP=loop
|
|
with patch.object(asyncio,'get_running_loop',return_value=loop),patch.object(asyncio,'sleep',sleep):
|
|
with self.assertRaises(asyncio.CancelledError):coroutine.send(None)
|
|
|
|
def test_heartbeat_internal_transport_retries_disabled_in_unknown_mode(self):
|
|
tree=ast.parse((ROOT/'ptz_tracker_modular/alarms.py').read_text(encoding='utf8'))
|
|
fn=next(n for n in tree.body if isinstance(n,ast.AsyncFunctionDef) and n.name=='periodic_status_sender')
|
|
condition=next(n.test for n in ast.walk(fn) if isinstance(n,ast.If) and 'Retry is not None' in ast.unparse(n.test))
|
|
self.assertFalse(eval(compile(ast.Expression(condition),'retry-policy','eval'),{'Retry':object(),'_boundary':boundary}))
|
|
|
|
def test_actual_ws_sender_filters_queued_on(self):
|
|
self.confirm();alarms.ingest_detection(10,True);self.clock=10.9
|
|
sent=[]
|
|
class WS:
|
|
async def __aenter__(self):return self
|
|
async def __aexit__(self,*a):return False
|
|
async def send(self,payload):sent.append(json.loads(payload));raise asyncio.CancelledError
|
|
fake=types.ModuleType('websockets');fake.connect=lambda *a,**k:WS()
|
|
with patch.dict(sys.modules,{'websockets':fake}):self.run_sender(alarms.alarm_sender())
|
|
self.assertEqual(sent,[{'type':'video_alarm','data':False}])
|
|
def test_actual_http_sender_filters_queued_on(self):
|
|
self.confirm();alarms.ingest_detection(10,True);self.clock=10.9;sent=[]
|
|
class Session:
|
|
def mount(self,*a):pass
|
|
def post(self,url,**kw):sent.append(kw['json']);raise asyncio.CancelledError
|
|
req=types.ModuleType('requests');req.Session=Session
|
|
adapters=types.ModuleType('requests.adapters');adapters.HTTPAdapter=lambda **kw:object()
|
|
with patch.dict(sys.modules,{'requests':req,'requests.adapters':adapters}):self.run_sender(alarms.alarm_http_sender())
|
|
self.assertEqual(len(sent),1);self.assertFalse(any(c['alarm'] for c in sent[0]['cameras']))
|
|
|
|
|
|
if __name__=='__main__':
|
|
import socket
|
|
# Any accidental real socket attempt fails this run. Fake senders above do not
|
|
# instantiate model.py/main.py/streaming.py or contact actual services.
|
|
with patch.object(socket,'create_connection',side_effect=AssertionError('network forbidden')):
|
|
suite=unittest.defaultTestLoader.loadTestsFromModule(sys.modules[__name__])
|
|
result=unittest.TextTestRunner(verbosity=2).run(suite)
|
|
raise SystemExit(not result.wasSuccessful())
|