mirror of
https://github.com/Second-Hand-Friends/kleinanzeigen-bot.git
synced 2026-07-09 20:51:05 +02:00
## ℹ️ Description Extract post-publish persistence from `KleinanzeigenBot` into a new `publishing_flow` module, establishing the first publishing-domain extraction following the pattern used for `delete_flow`, `extend_flow`, and `download_flow`. ## 📋 Changes Summary - **New module `publishing_flow.py`** (~140 lines) with two functions: - `persist_published_ad()` — public boundary: YAML field updates, content hash, timestamps, repost/price-reduction counters, save, rollback, and local path renaming - `\_log\_local\_path\_rename\_result()` — module-private logging helper - **`__init__.py`**: added `publishing_flow` import, replaced `await self.\_persist\_published\_ad(...)` with `publishing\_flow.persist\_published\_ad(...)` (sync call, no await), removed both methods (~120 lines) - Cleaned up unused imports in `__init__.py`: `AdPartial`, `replace`, `_local_path_renaming`, `_dicts` - Translation sections moved from `__init__.py` to `publishing_flow.py` in `translations.de.yaml` ### ⚙️ Type of Change - [ ] 🐞 Bug fix (non-breaking change which fixes an issue) - [ ] ✨ New feature (adds new functionality without breaking existing usage) - [ ] 💥 Breaking change (changes that might break existing user setups, scripts, or configurations) Internal refactoring only — no user-facing behavior or CLI changes. ## ✅ Checklist - [x] I have reviewed my changes to ensure they meet the project's standards. - [x] I have tested my changes and ensured that all tests pass (`pdm run test`). - [x] I have formatted the code (`pdm run format`). - [x] I have verified that linting passes (`pdm run lint`). - [x] I have updated documentation where necessary. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Post-publication persistence moved into a dedicated publishing flow to centralize ad update and rename handling. * **Improvements** * Enhanced logging and handling of local file/folder renames after publication. * Updated persisted ad metadata after publish (IDs, content hashes, timestamps, repost and price-reduction counts). * **Bug Fixes** * Publishing now catches and logs persistence failures instead of letting them propagate. * **Tests** * Added unit tests for rename logging, persistence behavior, rollback on save failure, and resilience to persistence errors. * **Chores** * Updated file copyright headers. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
204 lines
8.0 KiB
Python
204 lines
8.0 KiB
Python
# SPDX-FileCopyrightText: © Jens Bergmann and contributors
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
# SPDX-ArtifactOfProjectHomePage: https://github.com/Second-Hand-Friends/kleinanzeigen-bot/
|
|
from pathlib import Path
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
|
|
import pytest
|
|
|
|
from kleinanzeigen_bot.utils import diagnostics as diagnostics_module
|
|
from kleinanzeigen_bot.utils.diagnostics import capture_diagnostics
|
|
|
|
|
|
@pytest.mark.unit
|
|
class TestDiagnosticsCapture:
|
|
"""Tests for diagnostics capture functionality."""
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_capture_diagnostics_creates_output_dir(self, tmp_path:Path) -> None:
|
|
"""Test that capture_diagnostics creates output directory."""
|
|
mock_page = AsyncMock()
|
|
|
|
output_dir = tmp_path / "diagnostics"
|
|
_ = await capture_diagnostics(
|
|
output_dir = output_dir,
|
|
base_prefix = "test",
|
|
page = mock_page,
|
|
)
|
|
|
|
# Verify directory was created
|
|
assert output_dir.exists()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_capture_diagnostics_creates_screenshot(self, tmp_path:Path) -> None:
|
|
"""Test that capture_diagnostics creates screenshot file."""
|
|
mock_page = AsyncMock()
|
|
mock_page.save_screenshot = AsyncMock()
|
|
|
|
output_dir = tmp_path / "diagnostics"
|
|
result = await capture_diagnostics(
|
|
output_dir = output_dir,
|
|
base_prefix = "test",
|
|
page = mock_page,
|
|
)
|
|
|
|
# Verify screenshot file was created and page method was called
|
|
assert len(result.saved_artifacts) == 1
|
|
assert result.saved_artifacts[0].suffix == ".png"
|
|
mock_page.save_screenshot.assert_awaited_once()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_capture_diagnostics_creates_html(self, tmp_path:Path) -> None:
|
|
"""Test that capture_diagnostics creates HTML file."""
|
|
mock_page = AsyncMock()
|
|
mock_page.get_content = AsyncMock(return_value = "<html></html>")
|
|
|
|
output_dir = tmp_path / "diagnostics"
|
|
result = await capture_diagnostics(
|
|
output_dir = output_dir,
|
|
base_prefix = "test",
|
|
page = mock_page,
|
|
)
|
|
|
|
# Verify HTML file was created along with screenshot
|
|
assert len(result.saved_artifacts) == 2
|
|
assert any(a.suffix == ".html" for a in result.saved_artifacts)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_capture_diagnostics_creates_json(self, tmp_path:Path) -> None:
|
|
"""Test that capture_diagnostics creates JSON file."""
|
|
mock_page = AsyncMock()
|
|
mock_page.get_content = AsyncMock(return_value = "<html></html>")
|
|
|
|
output_dir = tmp_path / "diagnostics"
|
|
result = await capture_diagnostics(
|
|
output_dir = output_dir,
|
|
base_prefix = "test",
|
|
page = mock_page,
|
|
json_payload = {"test": "data"},
|
|
)
|
|
|
|
# Verify JSON file was created along with HTML and screenshot
|
|
assert len(result.saved_artifacts) == 3
|
|
assert any(a.suffix == ".json" for a in result.saved_artifacts)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_capture_diagnostics_copies_log_file(self, tmp_path:Path) -> None:
|
|
"""Test that capture_diagnostics copies log file when enabled."""
|
|
log_file = tmp_path / "test.log"
|
|
log_file.write_text("test log content")
|
|
|
|
output_dir = tmp_path / "diagnostics"
|
|
result = await capture_diagnostics(
|
|
output_dir = output_dir,
|
|
base_prefix = "test",
|
|
page = None, # No page to avoid screenshot
|
|
log_file_path = str(log_file),
|
|
copy_log = True,
|
|
)
|
|
|
|
# Verify log was copied
|
|
assert len(result.saved_artifacts) == 1
|
|
assert result.saved_artifacts[0].suffix == ".log"
|
|
|
|
def test_copy_log_sync_returns_false_when_file_not_found(self, tmp_path:Path) -> None:
|
|
"""Test _copy_log_sync returns False when log file does not exist."""
|
|
non_existent_log = tmp_path / "non_existent.log"
|
|
log_path = tmp_path / "output.log"
|
|
|
|
result = diagnostics_module._copy_log_sync(str(non_existent_log), log_path)
|
|
|
|
assert result is False
|
|
assert not log_path.exists()
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_capture_diagnostics_handles_screenshot_exception(self, tmp_path:Path) -> None:
|
|
"""Test that capture_diagnostics handles screenshot capture exceptions gracefully."""
|
|
mock_page = AsyncMock()
|
|
mock_page.save_screenshot = AsyncMock(side_effect = Exception("Screenshot failed"))
|
|
|
|
output_dir = tmp_path / "diagnostics"
|
|
result = await capture_diagnostics(
|
|
output_dir = output_dir,
|
|
base_prefix = "test",
|
|
page = mock_page,
|
|
)
|
|
|
|
# Verify no artifacts were saved due to exception
|
|
assert len(result.saved_artifacts) == 0
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_capture_diagnostics_handles_json_exception(self, tmp_path:Path, monkeypatch:pytest.MonkeyPatch) -> None:
|
|
"""Test that capture_diagnostics handles JSON write exceptions gracefully."""
|
|
mock_page = AsyncMock()
|
|
mock_page.get_content = AsyncMock(return_value = "<html></html>")
|
|
|
|
output_dir = tmp_path / "diagnostics"
|
|
|
|
# Mock _write_json_sync to raise an exception
|
|
monkeypatch.setattr(diagnostics_module, "_write_json_sync", MagicMock(side_effect = Exception("JSON write failed")))
|
|
|
|
result = await capture_diagnostics(
|
|
output_dir = output_dir,
|
|
base_prefix = "test",
|
|
page = mock_page,
|
|
json_payload = {"test": "data"},
|
|
)
|
|
|
|
# Verify screenshot and HTML were saved, but JSON failed
|
|
assert len(result.saved_artifacts) == 2
|
|
assert any(a.suffix == ".png" for a in result.saved_artifacts)
|
|
assert any(a.suffix == ".html" for a in result.saved_artifacts)
|
|
assert not any(a.suffix == ".json" for a in result.saved_artifacts)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_capture_diagnostics_handles_log_copy_exception(self, tmp_path:Path, monkeypatch:pytest.MonkeyPatch) -> None:
|
|
"""Test that capture_diagnostics handles log copy exceptions gracefully."""
|
|
# Create a log file
|
|
log_file = tmp_path / "test.log"
|
|
log_file.write_text("test log content")
|
|
|
|
output_dir = tmp_path / "diagnostics"
|
|
|
|
# Mock _copy_log_sync to raise an exception
|
|
original_copy_log = diagnostics_module._copy_log_sync
|
|
monkeypatch.setattr(diagnostics_module, "_copy_log_sync", MagicMock(side_effect = Exception("Copy failed")))
|
|
|
|
try:
|
|
result = await capture_diagnostics(
|
|
output_dir = output_dir,
|
|
base_prefix = "test",
|
|
page = None,
|
|
log_file_path = str(log_file),
|
|
copy_log = True,
|
|
)
|
|
|
|
# Verify no artifacts were saved due to exception
|
|
assert len(result.saved_artifacts) == 0
|
|
finally:
|
|
monkeypatch.setattr(diagnostics_module, "_copy_log_sync", original_copy_log)
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_capture_diagnostics_logs_warning_when_all_captures_fail(
|
|
self, tmp_path:Path, caplog:pytest.LogCaptureFixture, monkeypatch:pytest.MonkeyPatch
|
|
) -> None:
|
|
"""Test warning is logged when capture is requested but all fail."""
|
|
mock_page = AsyncMock()
|
|
mock_page.save_screenshot = AsyncMock(side_effect = Exception("Screenshot failed"))
|
|
mock_page.get_content = AsyncMock(side_effect = Exception("HTML failed"))
|
|
|
|
# Mock JSON write to also fail
|
|
monkeypatch.setattr(diagnostics_module, "_write_json_sync", MagicMock(side_effect = Exception("JSON write failed")))
|
|
|
|
output_dir = tmp_path / "diagnostics"
|
|
result = await capture_diagnostics(
|
|
output_dir = output_dir,
|
|
base_prefix = "test",
|
|
page = mock_page,
|
|
json_payload = {"test": "data"},
|
|
)
|
|
|
|
# Verify no artifacts were saved
|
|
assert len(result.saved_artifacts) == 0
|
|
assert "Diagnostics capture attempted but no artifacts were saved" in caplog.text
|