refactor(sync-run): consolidate SyncRunResult via composition
SyncRunResult now wraps OrchestratorResult instead of duplicating its 6 fields. Delegating properties preserve the public interface, eliminating field-by-field copy code in the bridge between orchestrator and sync_run. Closes #45
This commit is contained in:
1 parent
5cc3fce0a3
commit
1fcf8c6644
5 files changed
+149
-47
No files matched your search
+8
-4
@@ -14,7 +14,8 @@ class TestCLIExitCodeTranslation:
|
||||
"""Successful sync (no errors, no interrupt) → exit 0."""
|
||||
with patch("librofm_downloader.cli.sync_run") as mock_sr:
|
||||
from librofm_downloader.sync_run import SyncRunResult
|
||||
mock_sr.return_value = SyncRunResult(downloaded_count=2)
|
||||
from librofm_downloader.orchestrator import OrchestratorResult
|
||||
mock_sr.return_value = SyncRunResult(orchestrator_result=OrchestratorResult(downloaded_count=2))
|
||||
assert run() == 0
|
||||
|
||||
def test_fatal_error_returns_one(self):
|
||||
@@ -28,21 +29,24 @@ class TestCLIExitCodeTranslation:
|
||||
"""Ctrl+C interrupt → exit 130."""
|
||||
with patch("librofm_downloader.cli.sync_run") as mock_sr:
|
||||
from librofm_downloader.sync_run import SyncRunResult
|
||||
mock_sr.return_value = SyncRunResult(interrupted=True)
|
||||
from librofm_downloader.orchestrator import OrchestratorResult
|
||||
mock_sr.return_value = SyncRunResult(orchestrator_result=OrchestratorResult(interrupted=True))
|
||||
assert run() == 130
|
||||
|
||||
def test_all_books_fail_still_returns_zero(self):
|
||||
"""All downloads failed but no fatal error → exit 0 (not an error exit)."""
|
||||
with patch("librofm_downloader.cli.sync_run") as mock_sr:
|
||||
from librofm_downloader.sync_run import SyncRunResult
|
||||
mock_sr.return_value = SyncRunResult(failed_count=3)
|
||||
from librofm_downloader.orchestrator import OrchestratorResult
|
||||
mock_sr.return_value = SyncRunResult(orchestrator_result=OrchestratorResult(failed_count=3))
|
||||
assert run() == 0
|
||||
|
||||
def test_interrupted_takes_priority_over_fatal_error(self):
|
||||
"""If both interrupted and fatal_error, interrupted wins (exit 130)."""
|
||||
with patch("librofm_downloader.cli.sync_run") as mock_sr:
|
||||
from librofm_downloader.sync_run import SyncRunResult
|
||||
mock_sr.return_value = SyncRunResult(interrupted=True, fatal_error="something")
|
||||
from librofm_downloader.orchestrator import OrchestratorResult
|
||||
mock_sr.return_value = SyncRunResult(orchestrator_result=OrchestratorResult(interrupted=True), fatal_error="something")
|
||||
assert run() == 130
|
||||
|
||||
|
||||
|
||||
+44
-1
@@ -2155,4 +2155,47 @@ class TestMakeDownloadFnExplicitParams:
|
||||
assert call_args[0][1] is fake_session # client (injected session)
|
||||
assert call_args[0][3] is fake_reporter # reporter (injected)
|
||||
# rename_chapters=True was passed through closure
|
||||
assert call_args.kwargs["rename_chapters"] is True
|
||||
assert call_args.kwargs["rename_chapters"] is True
|
||||
|
||||
|
||||
class TestSyncRunResultComposition:
|
||||
"""SyncRunResult wraps OrchestratorResult — composition, not duplication."""
|
||||
|
||||
def test_delegates_to_wrapped_orchestrator_result(self):
|
||||
"""All download-count fields read from wrapped OrchestratorResult."""
|
||||
from librofm_downloader.book import Book
|
||||
from librofm_downloader.orchestrator import OrchestratorResult
|
||||
from librofm_downloader.sync_run import SyncRunResult
|
||||
|
||||
book = Book(title="Test", authors=["Author"], narrators=["Narrator"], isbn="123")
|
||||
orch = OrchestratorResult(
|
||||
downloaded_count=2,
|
||||
skipped_count=1,
|
||||
failed_count=3,
|
||||
failed_books=[(book, "error")],
|
||||
skipped_books=[book],
|
||||
interrupted=True,
|
||||
)
|
||||
result = SyncRunResult(orchestrator_result=orch, fatal_error="boom")
|
||||
|
||||
assert result.downloaded_count == 2
|
||||
assert result.skipped_count == 1
|
||||
assert result.failed_count == 3
|
||||
assert result.failed_books == [(book, "error")]
|
||||
assert result.skipped_books == [book]
|
||||
assert result.interrupted is True
|
||||
assert result.fatal_error == "boom"
|
||||
|
||||
def test_default_wraps_empty_orchestrator_result(self):
|
||||
"""SyncRunResult with only fatal_error defaults to empty OrchestratorResult."""
|
||||
from librofm_downloader.sync_run import SyncRunResult
|
||||
|
||||
result = SyncRunResult(fatal_error="Config error: bad yaml")
|
||||
|
||||
assert result.downloaded_count == 0
|
||||
assert result.skipped_count == 0
|
||||
assert result.failed_count == 0
|
||||
assert result.failed_books == []
|
||||
assert result.skipped_books == []
|
||||
assert result.interrupted is False
|
||||
assert result.fatal_error == "Config error: bad yaml"
|
||||
Reference in new issue
Block a user