import pytest from cbb_refresh_verify.runtime_timing import RunTimings, parse_save_progress def test_phase_duration_uses_worker_monotonic_time_not_delayed_poll_time(): now = [900.0] timer = RunTimings(clock=lambda: now[0]) timer.record({'stage': 'pcb_launch', 'monotonic_seconds': 1000.0}) now[0] = 1600 timer.record({'stage': 'identity_verified', 'monotonic_seconds': 1132.0}) assert timer.snapshot()[0]['pcb_startup'] == 132.0 def test_action_and_save_subtimings_accumulate_real_durations(): timer = RunTimings(clock=lambda: 1000.0) timer.record({'stage': 'integration_action_complete', 'action': 'ForwardAnnotate', 'elapsed_seconds': 341, 'call_elapsed_seconds': 336}) timer.record({'stage': 'integration_action_complete', 'action': 'BackAnnotate', 'elapsed_seconds': 4, 'call_elapsed_seconds': 3}) timer.record({'stage': 'save_progress', 'timings_seconds': {'save': 246, 'close': 3}}) durations, unfinished = timer.snapshot() assert durations['forward_annotation'] == 341 assert durations['forward_annotation_call'] == 336 assert durations['back_annotation'] == 4 assert durations['save'] == 246 assert durations['close'] == 3 assert 'synch_ces' not in durations # skipped is not a zero-time measurement def test_timeout_reports_unfinished_phase_without_fabricating_completion(): now = [10.0] timer = RunTimings(clock=lambda: now[0]) timer.record({'stage': 'save_start', 'monotonic_seconds': 15}) now[0] = 45 durations, unfinished = timer.snapshot() assert durations['save_close'] == 30 assert unfinished == ('save_close',) assert 'save' not in durations assert durations['total'] == 35 def test_save_helper_timing_parser_keeps_completion_markers(): result = parse_save_progress('SAVE_START\nTIMING|save|246.250\nSAVE_OK\nTIMING|close|3.125\nCLOSE_OK\n') assert result == {'markers': ['SAVE_START', 'SAVE_OK', 'CLOSE_OK'], 'timings_seconds': {'save': 246.25, 'close': 3.125}} @pytest.mark.parametrize('invalid', ['TIMING|save|NaN', 'TIMING|save|-1', 'TIMING|save|bad']) def test_invalid_helper_timing_never_poison_result(invalid): assert parse_save_progress(invalid)['timings_seconds'] == {} def test_returned_but_rejected_save_is_not_reported_as_still_in_call(): now = [10.0] timer = RunTimings(clock=lambda: now[0]) timer.record({'stage': 'save_progress', 'markers': ['SAVE_START'], 'monotonic_seconds': 12}) now[0] = 30 timer.record({'stage': 'save_progress', 'markers': ['SAVE_START'], 'timings_seconds': {'save': 5}}) durations, unfinished = timer.snapshot() assert durations['save'] == 5 assert 'save' not in unfinished def test_repeat_action_and_repeated_save_snapshot_do_not_double_count(): timer = RunTimings(clock=lambda: 1000.0) for _ in range(2): timer.record({'stage': 'integration_action_complete', 'action': 'ForwardAnnotate', 'elapsed_seconds': 10, 'call_elapsed_seconds': 8}) event = {'stage': 'save_progress', 'markers': ['SAVE_START', 'SAVE_OK'], 'timings_seconds': {'save': 30}} timer.record(event) timer.record(event) values, unfinished = timer.snapshot() assert values['forward_annotation'] == 20 assert values['forward_annotation_call'] == 16 assert values['save'] == 30 assert unfinished == ()