mirror of
https://github.com/denizsafak/abogen.git
synced 2026-09-20 11:40:57 +02:00
refactor: move calculate_text_length to domain, add build_chapter_payload to app-layer
- Create domain/text_utils.py with canonical calculate_text_length (strips chapter markers, voice markers, metadata tags) - Remove calculate_text_length from utils.py and subtitle_utils.py - Update all imports to use domain.text_utils directly - Create application/chapter_selection.py with build_chapter_payload() (orchestrates preselection + character count + safety net) - Replace manual chapter logic in form.py with build_chapter_payload() - 22 new tests for text_utils and chapter_selection (1550 total)
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
"""Tests for application/chapter_selection.py."""
|
||||
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||
|
||||
from dataclasses import dataclass
|
||||
from abogen.application.chapter_selection import build_chapter_payload
|
||||
|
||||
|
||||
@dataclass
|
||||
class FakeChapter:
|
||||
title: str
|
||||
text: str
|
||||
|
||||
|
||||
class TestBuildChapterPayload:
|
||||
def test_empty_chapters(self):
|
||||
result = build_chapter_payload([], source_name="book.txt")
|
||||
assert len(result) == 1
|
||||
assert result[0]["id"] == "0000"
|
||||
assert result[0]["title"] == "book.txt"
|
||||
assert result[0]["text"] == ""
|
||||
assert result[0]["characters"] == 0
|
||||
assert result[0]["enabled"] is True
|
||||
|
||||
def test_single_chapter_always_enabled(self):
|
||||
chapters = [FakeChapter("Chapter 1", "Once upon a time.")]
|
||||
result = build_chapter_payload(chapters)
|
||||
assert len(result) == 1
|
||||
assert result[0]["title"] == "Chapter 1"
|
||||
assert result[0]["enabled"] is True
|
||||
assert result[0]["index"] == 0
|
||||
assert result[0]["id"] == "0000"
|
||||
|
||||
def test_content_chapters_preselected(self):
|
||||
chapters = [
|
||||
FakeChapter("Chapter 1", "The story begins with a long enough text to pass the threshold."),
|
||||
FakeChapter("Chapter 2", "The story continues with another substantial chunk of text."),
|
||||
]
|
||||
result = build_chapter_payload(chapters)
|
||||
assert all(ch["enabled"] for ch in result)
|
||||
|
||||
def test_supplement_not_preselected(self):
|
||||
chapters = [
|
||||
FakeChapter("Chapter 1", "The story begins with a long enough text to pass the threshold."),
|
||||
FakeChapter("Title Page", ""),
|
||||
FakeChapter("Copyright", "All rights reserved."),
|
||||
FakeChapter("Table of Contents", ""),
|
||||
FakeChapter("Chapter 2", "The story continues with another substantial chunk of text."),
|
||||
]
|
||||
result = build_chapter_payload(chapters)
|
||||
titles_enabled = {ch["title"]: ch["enabled"] for ch in result}
|
||||
assert titles_enabled["Chapter 1"] is True
|
||||
assert titles_enabled["Chapter 2"] is True
|
||||
assert titles_enabled["Title Page"] is False
|
||||
assert titles_enabled["Copyright"] is False
|
||||
assert titles_enabled["Table of Contents"] is False
|
||||
|
||||
def test_at_least_one_enabled(self):
|
||||
chapters = [
|
||||
FakeChapter("Title Page", ""),
|
||||
FakeChapter("Copyright", "All rights reserved."),
|
||||
]
|
||||
result = build_chapter_payload(chapters)
|
||||
assert any(ch["enabled"] for ch in result)
|
||||
|
||||
def test_characters_calculated(self):
|
||||
chapters = [FakeChapter("Ch1", "Hello world")]
|
||||
result = build_chapter_payload(chapters)
|
||||
assert result[0]["characters"] == 11
|
||||
|
||||
def test_ids_are_zero_padded(self):
|
||||
chapters = [FakeChapter(f"Ch{i}", f"text {i}") for i in range(5)]
|
||||
result = build_chapter_payload(chapters)
|
||||
ids = [ch["id"] for ch in result]
|
||||
assert ids == ["0000", "0001", "0002", "0003", "0004"]
|
||||
|
||||
def test_indices_are_sequential(self):
|
||||
chapters = [FakeChapter(f"Ch{i}", f"text {i}") for i in range(3)]
|
||||
result = build_chapter_payload(chapters)
|
||||
indices = [ch["index"] for ch in result]
|
||||
assert indices == [0, 1, 2]
|
||||
|
||||
def test_source_name_used_for_empty(self):
|
||||
result = build_chapter_payload([], source_name="mybook.epub")
|
||||
assert result[0]["title"] == "mybook.epub"
|
||||
|
||||
def test_default_source_name(self):
|
||||
result = build_chapter_payload([])
|
||||
assert result[0]["title"] == ""
|
||||
|
||||
def test_none_title_and_text(self):
|
||||
class BadChapter:
|
||||
def __init__(self):
|
||||
self.title = None
|
||||
self.text = None
|
||||
|
||||
result = build_chapter_payload([BadChapter()])
|
||||
assert result[0]["title"] == ""
|
||||
assert result[0]["text"] == ""
|
||||
assert result[0]["enabled"] is True # single chapter always enabled
|
||||
@@ -0,0 +1,44 @@
|
||||
"""Tests for domain/text_utils.py."""
|
||||
|
||||
import sys
|
||||
import os
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
|
||||
|
||||
from abogen.domain.text_utils import calculate_text_length
|
||||
|
||||
|
||||
class TestCalculateTextUtilsLength:
|
||||
def test_empty(self):
|
||||
assert calculate_text_length("") == 0
|
||||
|
||||
def test_plain_text(self):
|
||||
assert calculate_text_length("Hello world") == 11
|
||||
|
||||
def test_strips_newlines(self):
|
||||
assert calculate_text_length("Hello\nworld") == 10
|
||||
|
||||
def test_strips_leading_trailing_spaces(self):
|
||||
assert calculate_text_length(" Hello ") == 5
|
||||
|
||||
def test_strips_chapter_markers(self):
|
||||
assert calculate_text_length("Hello<<CHAPTER_MARKER:intro>>world") == 10
|
||||
|
||||
def test_strips_voice_markers(self):
|
||||
assert calculate_text_length("Hello<<VOICE:M1>>world") == 10
|
||||
|
||||
def test_strips_metadata_tags(self):
|
||||
assert calculate_text_length("Hello<<METADATA_TITLE:My Book>>world") == 10
|
||||
|
||||
def test_strips_multiple_markers(self):
|
||||
text = "<<CHAPTER_MARKER:ch1>>Hello<<VOICE:M1>> <<METADATA_TITLE:Book>>world"
|
||||
assert calculate_text_length(text) == 11
|
||||
|
||||
def test_strips_mixed_content(self):
|
||||
text = "<<CHAPTER_MARKER:ch1>>\nHello\n<<VOICE:M1>>\nworld\n"
|
||||
assert calculate_text_length(text) == 10
|
||||
|
||||
def test_preserves_internal_spaces(self):
|
||||
assert calculate_text_length("Hello world") == 11
|
||||
|
||||
def test_only_markers(self):
|
||||
assert calculate_text_length("<<CHAPTER_MARKER:x>><<VOICE:y>>") == 0
|
||||
@@ -6,7 +6,7 @@ from pathlib import Path
|
||||
from ebooklib import epub
|
||||
|
||||
from abogen.text_extractor import extract_from_path
|
||||
from abogen.utils import calculate_text_length
|
||||
from abogen.domain.text_utils import calculate_text_length
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
||||
Reference in New Issue
Block a user