| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- """P1c commit 状态机测试。
- 重点不是“能不能提交”,而是**不安全时绝不提交**:
- A 片段 9–12 px 的间隙必须被 gap_px_hard_max=8 挡住;B 落位帧 3–5 px
- 必须允许提交;state=2 默认允许提交,但 allow_state2=False 时只识别、不提交。
- """
- from __future__ import annotations
- import unittest
- from portcv.config import Config
- from portcv.logic.commit import CommitParams, CommitTracker
- def tracker(**kwargs) -> CommitTracker:
- base = dict(
- gap_m_max=0.10,
- gap_px_hard_max=8.0,
- stable_frames=3,
- structure_frames=3,
- min_structure_hits=0.60,
- allow_state2=False,
- )
- base.update(kwargs)
- return CommitTracker(CommitParams(**base))
- def feed(t: CommitTracker, n: int, *, state: int, gap_m, gap_px, **kwargs):
- result = None
- for i in range(n):
- result = t.update(float(i) * 0.1, state=state, gap_m=gap_m, gap_px=gap_px, **kwargs)
- return result
- class CommitSafetyTests(unittest.TestCase):
- def test_a_like_gap_is_blocked_even_after_many_frames(self):
- t = tracker()
- result = feed(t, 10, state=1, gap_m=0.104, gap_px=12.0)
- self.assertFalse(result.committed)
- self.assertEqual(result.phase, "T1")
- self.assertIn("gap", result.reason)
- self.assertEqual(result.stable_frames, 10)
- def test_gap_px_hard_max_blocks_sub_0_10m_but_pixel_unsafe(self):
- # 0.08 m 在 115.6 px/m 下约 9.2 px;米制过门,但像素硬门不过。
- t = tracker()
- result = feed(t, 10, state=1, gap_m=0.08, gap_px=9.2)
- self.assertFalse(result.committed)
- def test_b_like_seated_gap_commits(self):
- t = tracker()
- result = feed(t, 3, state=1, gap_m=0.035, gap_px=4.0)
- self.assertTrue(result.committed)
- self.assertEqual(result.phase, "T2")
- self.assertEqual(result.reason, "commit")
- def test_state2_can_be_disabled_explicitly(self):
- t = tracker()
- result = feed(t, 10, state=2, gap_m=0.03, gap_px=3.5)
- self.assertFalse(result.committed)
- self.assertEqual(result.reason, "state2_disabled")
- def test_state2_is_enabled_by_default(self):
- t = CommitTracker()
- result = feed(t, 6, state=2, gap_m=0.03, gap_px=3.5)
- self.assertTrue(result.committed)
- self.assertEqual(result.phase, "T2")
- def test_state2_can_be_enabled_explicitly(self):
- t = tracker(allow_state2=True)
- result = feed(t, 3, state=2, gap_m=0.03, gap_px=3.5)
- self.assertTrue(result.committed)
- self.assertEqual(result.phase, "T2")
- def test_zero_contact_gap_commits_without_scale(self):
- t = CommitTracker()
- result = feed(t, 6, state=2, gap_m=None, gap_px=0.0)
- self.assertTrue(result.committed)
- self.assertEqual(result.phase, "T2")
- def test_top_level_allow_state2_false_disables_commit(self):
- cfg = Config(
- {
- "evidence_v2": {
- "allow_state2": False,
- "commit": {"allow_state2": True},
- }
- }
- )
- t = CommitTracker(CommitParams.from_config(cfg))
- result = feed(t, 6, state=2, gap_m=0.03, gap_px=3.5)
- self.assertFalse(result.committed)
- self.assertEqual(result.reason, "state2_disabled")
- def test_motion_is_t3_and_never_commits(self):
- t = tracker()
- result = feed(t, 10, state=1, gap_m=0.03, gap_px=3.0, motion_ok=False)
- self.assertFalse(result.committed)
- self.assertEqual(result.phase, "T3")
- self.assertEqual(result.reason, "motion")
- def test_plc_arrival_bypasses_gap_but_not_structure(self):
- t = tracker()
- result = feed(
- t, 3, state=1, gap_m=0.50, gap_px=60.0,
- plc_arrived=True, structure_hits=1.0,
- )
- self.assertTrue(result.committed)
- self.assertEqual(result.reason, "commit:plc_arrival")
- t2 = tracker()
- weak = feed(
- t2, 5, state=1, gap_m=0.50, gap_px=60.0,
- plc_arrived=True, structure_hits=0.2,
- )
- self.assertFalse(weak.committed)
- self.assertIn("structure", weak.reason)
- def test_weak_structure_never_commits(self):
- t = tracker()
- result = feed(t, 10, state=1, gap_m=0.03, gap_px=3.0, structure_hits=0.30)
- self.assertFalse(result.committed)
- self.assertIn("structure", result.reason)
- def test_gap_rising_is_rejected(self):
- t = tracker()
- gaps = [0.03, 0.03, 0.06, 0.09]
- result = None
- for i, gap in enumerate(gaps):
- result = t.update(float(i) * 0.1, state=1, gap_m=gap, gap_px=gap * 115.6)
- self.assertFalse(result.committed)
- self.assertIn("gap_rising", result.reason)
- def test_unknown_state_does_not_commit(self):
- t = tracker()
- result = feed(t, 10, state=255, gap_m=None, gap_px=None)
- self.assertFalse(result.committed)
- self.assertEqual(result.reason, "state_unknown")
- def test_zero_can_commit_without_gap(self):
- t = tracker()
- result = feed(t, 3, state=0, gap_m=None, gap_px=None)
- self.assertTrue(result.committed)
- def test_reset_clears_window(self):
- t = tracker()
- feed(t, 2, state=1, gap_m=0.03, gap_px=3.0)
- t.reset()
- result = t.update(5.0, state=1, gap_m=0.03, gap_px=3.0)
- self.assertFalse(result.committed)
- self.assertEqual(result.stable_frames, 1)
- def test_hoist_high_blocks_commit_and_does_not_accumulate_window(self):
- t = tracker()
- blocked = feed(t, 10, state=1, gap_m=0.03, gap_px=3.0, hoist_high=True)
- self.assertFalse(blocked.committed)
- self.assertEqual(blocked.phase, "T3")
- self.assertEqual(blocked.reason, "hoist_high")
- self.assertTrue(blocked.hoist_high)
- self.assertEqual(blocked.stable_frames, 0)
- # 高悬消失后仍要从 0 开始累计,不能复用被拦截前的窗口。
- allowed = feed(t, 3, state=1, gap_m=0.03, gap_px=3.0)
- self.assertTrue(allowed.committed)
- self.assertFalse(allowed.hoist_high)
- def test_hoist_high_blocks_state0_and_state2_too(self):
- for state in (0, 2):
- t = tracker(allow_state2=True)
- result = feed(
- t, 10, state=state, gap_m=0.0, gap_px=0.0, hoist_high=True
- )
- self.assertFalse(result.committed)
- self.assertEqual(result.reason, "hoist_high")
- def test_hoist_high_blocks_plc_arrival_override(self):
- t = tracker()
- result = feed(
- t, 3, state=1, gap_m=0.50, gap_px=60.0,
- plc_arrived=True, hoist_high=True,
- )
- self.assertFalse(result.committed)
- self.assertEqual(result.reason, "hoist_high")
- def test_params_validation(self):
- with self.assertRaises(ValueError):
- CommitParams(stable_frames=0)
- with self.assertRaises(ValueError):
- CommitParams(gap_m_max=-0.1)
- if __name__ == "__main__":
- unittest.main()
|