"""Word-aligned punch-in beats: the pure derivation in punch_in.emphasis_times. #2b moved beat timing from audio-envelope-only to the envelope snapped onto clip-relative word onsets. These tests pin the merge itself (no audio file needed) and the one behaviour that must not change: no word times -> exactly the old audio-only result. """ import pytest from clipwright.layouts import punch_in from clipwright.layouts.punch_in import merge_beats class TestMergeBeats: def test_no_words_is_audio_only(self): assert merge_beats([1.0, 15.0], [], min_gap=4.0) == [1.0, 15.0] assert merge_beats([1.0, 15.0], None, min_gap=4.0) == [1.0, 15.0] def test_envelope_beat_snaps_to_a_nearby_word(self): # The envelope peak sits 0.2s off the spoken word: the word is exact. assert merge_beats([10.0], [10.2], min_gap=4.0) == [10.2] def test_a_distant_word_does_not_move_the_envelope_beat(self): # 5.0 is nowhere near either peak, so neither peak is re-timed. assert merge_beats([1.0, 30.0], [5.0], min_gap=4.0) == [1.0, 30.0] def test_words_become_beats_when_audio_has_none(self): assert merge_beats([], [3.0, 40.0], min_gap=18.0) == [3.0, 40.0] def test_beats_are_thinned_to_the_minimum_gap(self): # 2 and 4 collide at gap 10; the earliest wins, 20 keeps its own slot. assert merge_beats([2.0, 4.0, 20.0], [], min_gap=10.0) == [2.0, 20.0] def test_result_is_sorted_and_rounded(self): assert merge_beats([12.3456], [], min_gap=4.0) == [12.346] assert merge_beats([], [3.0, 12.3456], min_gap=4.0) == [3.0, 12.346] class TestEmphasisTimesWithWords: def _envelope_spike(self, monkeypatch, beat_index=10, value=1.0, length=50): from clipwright.layouts import active_speaker envelope = [0.0] * length envelope[beat_index] = value monkeypatch.setattr(active_speaker, "audio_envelope", lambda *a, **k: envelope) return beat_index * 0.2 # window defaults to BEAT_WINDOW def test_silent_audio_with_words_still_yields_word_beats(self, monkeypatch): from clipwright.layouts import active_speaker monkeypatch.setattr(active_speaker, "audio_envelope", lambda *a, **k: []) assert punch_in.emphasis_times("x.mp4", 30.0, word_times=[4.0]) == [4.0] def test_no_words_keeps_the_old_audio_only_result(self, monkeypatch): beat = self._envelope_spike(monkeypatch, beat_index=10) assert punch_in.emphasis_times("x.mp4", 20.0) == [round(beat, 3)] def test_word_snaps_the_envelope_spike_onto_itself(self, monkeypatch): beat = self._envelope_spike(monkeypatch, beat_index=10) times = punch_in.emphasis_times("x.mp4", 20.0, word_times=[beat + 0.1]) assert times == [round(beat + 0.1, 3)] def test_empty_word_list_is_backward_compatible(self, monkeypatch): beat = self._envelope_spike(monkeypatch, beat_index=5) assert (punch_in.emphasis_times("x.mp4", 20.0, word_times=[]) == [round(beat, 3)]) class TestClipWordOnsets: def _onsets(self, transcript, start, end): pytest.importorskip("clipwright.pipeline.main") from clipwright.pipeline.main import clip_word_onsets return clip_word_onsets(transcript, start, end) def test_words_become_clip_relative_seconds(self): transcript = {"segments": [{"words": [ {"word": "a", "start": 100.0, "end": 100.5}, {"word": "b", "start": 102.0, "end": 102.5}, {"word": "c", "start": 111.0, "end": 111.5}, # after the clip ]}]} assert self._onsets(transcript, 100.0, 110.0) == [0.0, 2.0] def test_boundary_words_are_clamped_into_the_clip(self): transcript = {"segments": [{"words": [ {"word": "x", "start": 98.0, "end": 101.0}, # starts before {"word": "y", "start": 109.0, "end": 112.0}, # ends after ]}]} assert self._onsets(transcript, 100.0, 110.0) == [0.0, 9.0] def test_no_transcript_or_words_is_empty(self): assert self._onsets(None, 0.0, 10.0) == [] assert self._onsets({"segments": []}, 0.0, 10.0) == [] assert self._onsets({"segments": [{"text": "hi"}]}, 0.0, 10.0) == []