refactor(history): relocate _write_history() function (#40)

Move the `_write_history()` function from `downloader.py` to `history.py`. This refactoring separates persistence logic from download orchestration, ensuring `downloader.py` focuses solely on its core responsibility.
This commit is contained in:
AlexKucera committed 2026-06-05 18:52:50 +02:00
1 parent 2930d163b2
commit bcc1f7efee
7 files changed
+109 -24

No files matched your search

+30 -1
View File
@@ -8,6 +8,9 @@ import pytest
from librofm_downloader.history import DownloadHistory, HistoryEntry
from librofm_downloader.history import _write_history
from librofm_downloader.book import Book
class TestWriteAndRead:
"""Tracer bullet: write an entry and read it back by ISBN."""
@@ -243,4 +246,30 @@ class TestThreadSafety:
found = history.find("111")
assert found is not None
assert found.title == "First"
assert found.title == "First"
class TestWriteHistoryRelocated:
"""Issue #40: _write_history() lives in history.py, not downloader.py."""
def test_write_history_creates_entry_via_history_module(self):
"""_write_history() imported from history.py writes a correct HistoryEntry."""
with tempfile.TemporaryDirectory() as tmpdir:
history_path = Path(tmpdir) / "download_history.json"
history = DownloadHistory(history_path)
book = Book(
title="Test Relocation",
authors=["Author"],
narrators=["Narrator"],
isbn="9780000000001",
)
_write_history(history, book, "m4b", "/audiobooks/Test/Test.m4b")
entry = history.find("9780000000001")
assert entry is not None
assert entry.title == "Test Relocation"
assert entry.format == "m4b"
assert entry.path == "/audiobooks/Test/Test.m4b"
assert entry.downloaded_at # non-empty ISO timestamp