# SPDX-License-Identifier: AGPL-3.0-or-later """Löschjournal ohne Datenbank: Einträge, Marke nach Wiederherstellung, Aufräumen.""" from __future__ import annotations import uuid from datetime import UTC, datetime, timedelta from pathlib import Path from types import SimpleNamespace from typing import Any import pytest pytest.importorskip("pydantic_settings") from surveybox import erasure_journal from surveybox.backup import backup_time def settings(tmp_path: Path) -> Any: return SimpleNamespace(data_dir=tmp_path, backup_dir=tmp_path / "backups") def test_record_read_and_prune(tmp_path: Path) -> None: s = settings(tmp_path) first = uuid.uuid4() erasure_journal.record(s, "user", first, forms={"a": ""}) erasure_journal.record(s, "guest", uuid.uuid4()) with erasure_journal.journal_path(s).open("a", encoding="utf-8") as handle: handle.write('{"at": "kaputt"') # angefangene Zeile nach einem Absturz found = erasure_journal.entries(s) assert [e.kind for e in found] == ["user", "guest"] assert found[0].id == str(first) and found[0].extra == {"forms": {"a": ""}} later = datetime.now(UTC) + timedelta(seconds=1) assert erasure_journal.entries(s, later) == [] assert erasure_journal.prune(s, later) == 3 assert erasure_journal.entries(s) == [] with pytest.raises(ValueError): erasure_journal.record(s, "unknown", first) def test_restore_marker(tmp_path: Path) -> None: s = settings(tmp_path) assert erasure_journal.pending_since(s) is None taken = datetime(2026, 11, 1, 3, 0, tzinfo=UTC) erasure_journal.mark_restore(s, taken) assert erasure_journal.pending_since(s) == taken erasure_journal.clear_marker(s) assert erasure_journal.pending_since(s) is None def test_backup_time_from_name() -> None: assert backup_time(Path("surveybox-daily-20261101T030000Z.dump")) == datetime( 2026, 11, 1, 3, 0, tzinfo=UTC) assert backup_time(Path( "surveybox-pre-upgrade-20261101T030000Z-from-0012-to-0013.dump" )) == datetime(2026, 11, 1, 3, 0, tzinfo=UTC) assert backup_time(Path("eigene.dump")) is None