Files

126 lines
5.5 KiB
Python

"""Batch lifecycle tests — FCP batch-FSM parity."""
from src.batch import BatchLifecycleManager
from src.interfaces import Detection
from src.pipeline import compute_sacks_in_truck_area, update_batch_state
from shapely.geometry import Polygon
def _counting_manager() -> BatchLifecycleManager:
"""Manager already in COUNTING_SACKS (stabilize_seconds=0 to skip the ramp)."""
m = BatchLifecycleManager(stabilize_seconds=0.0)
m.update_truck(True, (100.0, 100.0), 1000.0)
assert m.state == "COUNTING_SACKS"
return m
class _RecordingManager(BatchLifecycleManager):
"""Batch manager that records the arguments it was driven with."""
def __init__(self) -> None:
super().__init__(stabilize_seconds=0.0)
self.sacks_calls: list[dict] = []
def update_sacks(self, has_crossing_event, sacks_in_area_count, timestamp,
loading_count=0, unloading_count=0):
self.sacks_calls.append({
"has_crossing_event": has_crossing_event,
"sacks_in_area_count": sacks_in_area_count,
"loading_count": loading_count,
"unloading_count": unloading_count,
})
super().update_sacks(has_crossing_event, sacks_in_area_count, timestamp,
loading_count, unloading_count)
def test_update_batch_state_passes_real_crossing_flag():
"""A crossing event reaches the manager as has_crossing_event=True."""
m = _RecordingManager()
update_batch_state(m, events=[{"direction": "loading"}], tracked_sacks=[],
truck_polygon=None, timestamp=1000.0,
loading_count=1, unloading_count=0)
assert m.sacks_calls[-1]["has_crossing_event"] is True
def test_update_batch_state_counts_sacks_in_truck_area():
"""sacks_in_area_count comes from the truck polygon, not from truck presence."""
poly = Polygon([(0, 0), (100, 0), (100, 100), (0, 100)])
dets = [Detection(bbox=(10.0, 80.0, 20.0, 95.0), confidence=0.9,
class_id=0, class_name="sack", track_id=1)]
m = _RecordingManager()
update_batch_state(m, events=[], tracked_sacks=dets, truck_polygon=poly,
timestamp=1000.0, loading_count=0, unloading_count=0)
assert m.sacks_calls[-1]["sacks_in_area_count"] == 1
def test_compute_sacks_in_truck_area_applies_bottom_cutoff():
"""Only the bottom 85% of the truck area counts (FCP predict.py:950-958)."""
poly = Polygon([(0, 0), (100, 0), (100, 100), (0, 100)])
top = [Detection(bbox=(10.0, 5.0, 20.0, 20.0), confidence=0.9,
class_id=0, class_name="sack", track_id=1)]
bottom = [Detection(bbox=(10.0, 80.0, 20.0, 95.0), confidence=0.9,
class_id=0, class_name="sack", track_id=2)]
assert compute_sacks_in_truck_area(top, poly) == 0
assert compute_sacks_in_truck_area(bottom, poly) == 1
def test_idle_timeout_without_activity_enters_waiting():
"""No crossing and no sack in area for sack_idle_timeout -> WAITING."""
m = _counting_manager()
m.update_sacks(False, 0, 1001.0, 1, 0)
m.update_sacks(False, 0, 1045.0, 1, 0) # 45s after batch start
assert m.state == "WAITING_FOR_ACTIVITY"
def test_pending_counts_follow_latest_crossing():
"""_pending_loading tracks the newest count, not a frozen snapshot."""
m = _counting_manager()
m.update_sacks(True, 1, 1001.0, 7, 2)
assert m._pending_loading == 7
assert m._pending_unloading == 2
def test_update_batch_state_counts_sacks_only_in_truck_area():
"""Boxes in the truck area are not sacks (FCP keeps them in a separate list)."""
poly = Polygon([(0, 0), (100, 0), (100, 100), (0, 100)])
box = Detection(bbox=(10.0, 80.0, 20.0, 95.0), confidence=0.9,
class_id=1, class_name="box", track_id=1)
m = _RecordingManager()
update_batch_state(m, events=[], tracked_sacks=[box], truck_polygon=poly,
timestamp=1000.0, loading_count=0, unloading_count=0)
assert m.sacks_calls[-1]["sacks_in_area_count"] == 0
def test_update_batch_state_counts_the_sack_next_to_a_box():
"""Positive control: a sack and a box in the same area count as one sack."""
poly = Polygon([(0, 0), (100, 0), (100, 100), (0, 100)])
sack = Detection(bbox=(40.0, 80.0, 50.0, 95.0), confidence=0.9,
class_id=0, class_name="sack", track_id=1)
box = Detection(bbox=(10.0, 80.0, 20.0, 95.0), confidence=0.9,
class_id=1, class_name="box", track_id=2)
m = _RecordingManager()
update_batch_state(m, events=[], tracked_sacks=[sack, box], truck_polygon=poly,
timestamp=1000.0, loading_count=0, unloading_count=0)
assert m.sacks_calls[-1]["sacks_in_area_count"] == 1
def test_on_batch_start_fires_once_when_the_batch_opens():
"""The REAL manager fires the batch-start callback with (batch_id, ts).
The pipeline hook that clears the per-track state depends on this
callback, so it is exercised here against the real manager — the
pipeline test stubs it out.
"""
m = BatchLifecycleManager(stabilize_seconds=0.0)
calls: list[tuple[int, float]] = []
m.on_batch_start(lambda batch_id, timestamp: calls.append((batch_id, timestamp)))
m.update_truck(True, (100.0, 100.0), 1000.0) # truck seen -> batch opens
assert m.state == "COUNTING_SACKS"
assert calls == [(1, 1000.0)]
# later frames must not fire it again
m.update_sacks(True, 1, 1001.0, 1, 0)
m.update_truck(True, (100.0, 100.0), 1002.0)
assert calls == [(1, 1000.0)]