from __future__ import annotations from yt_scraper.chapters import align_chapters, chapters_from_info, Chapter, Section from yt_scraper.parse import Segment def test_align_no_chapters(): segs = [Segment(0.0, 1.0, "a"), Segment(1.0, 2.0, "b")] sections = align_chapters(segs, []) assert len(sections) == 1 assert sections[0].title == "Transcripcion" assert len(sections[0].segments) == 2 def test_align_with_chapters(): segs = [ Segment(0.0, 1.0, "intro"), Segment(5.0, 6.0, "mid"), Segment(20.0, 21.0, "end"), ] chapters = [ Chapter(title="Intro", start_time=0.0, end_time=10.0), Chapter(title="Desarrollo", start_time=10.0, end_time=30.0), ] sections = align_chapters(segs, chapters) assert len(sections) == 2 assert sections[0].title == "Intro" assert len(sections[0].segments) == 2 assert sections[1].title == "Desarrollo" assert len(sections[1].segments) == 1 assert sections[1].segments[0].text == "end" def test_align_pre_chapter_segments(): segs = [Segment(0.0, 1.0, "antes")] chapters = [Chapter(title="Cap 1", start_time=5.0, end_time=10.0)] sections = align_chapters(segs, chapters) assert len(sections) == 2 assert sections[0].title == "Inicio" assert sections[0].segments[0].text == "antes" def test_chapters_from_info(): info = { "chapters": [ {"title": "Cap 1", "start_time": 0, "end_time": 60}, {"title": "Cap 2", "start_time": 60, "end_time": 120}, ] } chapters = chapters_from_info(info) assert len(chapters) == 2 assert chapters[0].title == "Cap 1" assert chapters[1].start_time == 60.0 def test_chapters_from_info_empty(): assert chapters_from_info({}) == [] assert chapters_from_info({"chapters": []}) == []