"""Replay the observed timing with synthetic intra-native demands.""" import json from unittest.mock import patch def build_floor_cases(boundary, controller_replay, pp, ptz_io, config, report_path, fixed): report = json.loads(report_path.read_bytes()) observed = [ (int(camera), decision) for camera, episodes in report["episodes"].items() for episode in episodes for decision in episode["matched_decisions"] if decision["reason"] == "pending_native_floor" and decision["result"] == "short" ] class NativeFloor(boundary): def seed(self, camera): f = self.fixture(camera, "normal", 10, .34) # The observed logs record native 10. This synthetic fraction has # the same wire representation but a larger controller value. f.st["zoom_step_pending_useful_cmd"] = .106 return f def gate(self, f, command=.103, *, can=True, floor=None, timing=(.34, .40), kind="normal", observed_now=None): trace = [] value = pp._gate_zoom_in_pulse( f.st, f.wall, command, can, pulse_kind=kind, pulse_floor=command if floor is None else floor, pulse_timing=timing, optics_observed_now=f.wall if observed_now is None else observed_now, stop_trace=trace, ) return value, trace def test_observed_native10_windows(self): self.assertEqual(sorted(camera for camera, _ in observed), [19, 21]) for camera, row in observed: with self.subTest(camera=camera, frame=row["frame"]): self.assertEqual(row["native"], "10") self.assertEqual(row["minimum"], "0.238s") f = self.seed({19: 8, 21: 10}[camera]) timer = f.st["zoom_step_stop_timer"] request = f.st["zoom_step_physical_request_id"] deadline = f.st["zoom_step_until"] hard = f.st["zoom_step_stop_started_mono"] + timer.delay origin = f.st["zoom_step_put_started_mono"] f._advance(float(row["duration"].removesuffix("s"))) shaped = f._shape(.103) self.assertEqual(shaped, .103) self.assertEqual(ptz_io._zoom_speed_to_int(shaped), 10) self.assertEqual(ptz_io._zoom_speed_to_int(.106), 10) with patch.object(config, "ZOOM_STEP_PULSE_SEC", .10): self.assertEqual(pp._select_zoom_response_timing( f.st, useful=False, hfov_deg=59., wide_hfov_deg=59., center_err=.02, margin=.45, image_rate=.01, requested_native=10 ), (.10, .12)) timing = pp._select_zoom_response_timing( f.st, useful=True, hfov_deg=59., wide_hfov_deg=59., center_err=.02, margin=.45, image_rate=.01, requested_native=10 ) self.assertEqual(timing, (.34, .40)) command, trace = self.gate(f, shaped, timing=timing) self.assertEqual((command, trace), (.106, []) if fixed else (0., ["pending_native_floor"])) pan, tilt, _ = row["requested"] f._enqueue(command, pan=pan / 100, tilt=tilt / 100) f._flush() if not fixed: self.assertEqual(f._zooms()[-1], 0) self.assertEqual(f.st["zoom_step_last_delivery_result"], "short") self.assertAlmostEqual(f.st["zoom_step_last_delivered_sec"], float(row["duration"].removesuffix("s"))) continue self.assertEqual(f._zooms()[-1], 10) self.assertEqual(f.st["zoom_step_physical_request_id"], request) self.assertEqual(f.st["zoom_step_until"], deadline) self.assertEqual(f.st["zoom_step_put_started_mono"], origin) active_timer = f.st["zoom_step_stop_timer"] self.assertAlmostEqual(f.st["zoom_step_stop_started_mono"] + active_timer.delay, hard) self.assertAlmostEqual(hard - origin, .40) f._advance(hard - f.mono) active_timer.fire() self.assertEqual(f._zooms()[-1], 0) self.assertEqual(f.st["zoom_step_physical_request_id"], 0) self.assertNotEqual(f.st["zoom_step_last_delivery_result"], "short") def test_stop_vetoes_keep_zero(self): for name, state_change, gate_args, expected in ( ("below_native10", {}, {"command": .0999999999}, "pending_native_floor"), ("floor_missing", {}, {"floor": 0.}, "pending_native_floor"), ("floor_invalid", {}, {"floor": float("nan")}, "pending_native_floor"), ("shorter_timing", {}, {"timing": (.10, .12)}, "pending_timing"), ("no_timing", {}, {"timing": None}, "pending_native_floor"), ("permission", {}, {"can": False}, "permission"), ("cancel", {"zoom_step_cancel_requested": True}, {}, "cancel_requested"), ("discard", {"zoom_step_discard_on_finalize": True}, {}, "pending_native_floor"), ("no_effect", {"zoom_no_effect_blocked": True}, {}, "no_effect_limit"), ("new_owner", {"ptz_generation": 5}, {}, "pending_native_floor"), ("new_track", {"track_started_at": 91.}, {}, "pending_native_floor"), ("new_request", {"zoom_step_physical_request_id": 7}, {}, "pending_native_floor"), ("no_delivery", {"zoom_step_physical_started_at": 0.}, {}, "pending_native_floor"), ("protective_stop", {"zoom_stop_obligation_active": True}, {}, "pending_native_floor"), ("watchdog_missing", {"zoom_step_stop_timer": None}, {}, "pending_native_floor"), ("watchdog_other_source", {"zoom_step_stop_source": "acquire"}, {}, "pending_native_floor"), ("probe", {"zoom_step_pending_kind": "probe"}, {"kind": "probe"}, "pending_native_floor"), ("reached_nominal_end", {}, {"observed_now": 101.}, "pending_native_floor"), ("real_zero", {}, {"command": 0.}, None), ): with self.subTest(name=name): f = self.seed(8) f._advance(.05) f.st.update(state_change) value, trace = self.gate(f, **gate_args) self.assertEqual(value, 0.) self.assertEqual(trace, [expected] if expected else []) f = self.seed(8) f._advance(.05) with patch.object(config, "ZOOM_STEP_MAX_IN_CMD", .104): self.assertEqual(self.gate(f), (0., ["pending_native_floor"])) class FrameFloor(controller_replay): def test_track_frame_uses_native_continuation(self): self.stack.enter_context(patch.object(config, "ZOOM_PROBE_ENABLE", False)) self.stack.enter_context(patch.object(config, "ZOOM_IDENTIFY_ENABLE", False)) shape = pp._shape_useful_zoom_pulse def fraction(command, **kwargs): demand = .103 if self.st.get("zoom_step_physical_request_id") else .106 return shape(demand if command > 0. else command, **kwargs) with patch.object(pp, "_shape_useful_zoom_pulse", side_effect=fraction): for _ in range(24): row = self.tick(.1) if any(axes[2] > 0 for axes in row["sent"]): break self.assertEqual(self.st["zoom_step_pending_useful_cmd"], .106) deadline = self.st["zoom_step_until"] with patch.object(pp, "_gate_zoom_in_pulse", wraps=pp._gate_zoom_in_pulse) as gate: row = self.tick(.06) self.assertEqual(gate.call_args.args[2], .103) if fixed: self.assertEqual(self.st["zoom_step_until"], deadline) self.assertEqual(self.fixture._zooms()[-1], 10) else: self.assertEqual(row["sent"][-1], (0, 0, 0)) self.assertEqual(self.st["zoom_step_last_delivery_result"], "short") return [ NativeFloor("test_observed_native10_windows"), NativeFloor("test_stop_vetoes_keep_zero"), FrameFloor("test_track_frame_uses_native_continuation"), ]