hosts/tests/test_backup_restore_modal.py
phg e268794564 Fix #13: Expose Pre-edit Backup restoration
Preserve full diff opcodes before grouping, route modal navigation to a two-axis viewport-sized scroller, and calculate side-by-side layout using terminal cell widths so tabbed Hosts File lines keep a stable divider.
2026-09-04 21:37:51 +02:00

70 lines
2.6 KiB
Python

"""Tests for the Pre-edit Backup restoration confirmation."""
import asyncio
from unittest.mock import Mock
from textual.widgets import Button
from hosts.tui.backup_restore_modal import BackupRestoreModal
from hosts.core.restore_preview import create_restore_preview
async def unchanged_preview(preview):
"""Return the already reviewed files for modal unit tests."""
return preview
class TestBackupRestoreModal:
"""The modal identifies both paths and requires an explicit choice."""
def test_modal_exposes_backup_and_hosts_file_paths(self, tmp_path):
backup = tmp_path / "hosts.backup"
hosts_file = tmp_path / "hosts"
backup.write_text("127.0.0.1 localhost\n")
hosts_file.write_text("192.0.2.1 current.test\n")
preview = create_restore_preview(backup, hosts_file)
modal = BackupRestoreModal(preview, lambda: unchanged_preview(preview))
assert modal.preview.backup_path == backup
assert modal.preview.hosts_file_path == hosts_file
def test_confirm_dismisses_true_after_freshness_check(self, tmp_path):
backup = tmp_path / "backup"
hosts_file = tmp_path / "hosts"
backup.write_text("127.0.0.1 restored.test\n")
hosts_file.write_text("127.0.0.1 current.test\n")
preview = create_restore_preview(backup, hosts_file)
modal = BackupRestoreModal(preview, lambda: unchanged_preview(preview))
modal.dismiss = Mock()
modal._set_status = Mock()
asyncio.run(modal._confirm())
modal.dismiss.assert_called_once_with(True)
def test_cancel_is_focused_and_dismisses_false(self):
preview = Mock()
modal = BackupRestoreModal(preview, Mock())
modal.dismiss = Mock()
cancel = Mock()
modal.query_one = Mock(return_value=cancel)
modal._render_preview = Mock()
modal.on_mount()
modal.action_cancel()
modal.query_one.assert_called_once_with("#cancel-button", Button)
cancel.focus.assert_called_once_with()
modal.dismiss.assert_called_once_with(False)
def test_final_newline_difference_is_visible_without_changed_lines(self, tmp_path):
backup = tmp_path / "backup"
hosts_file = tmp_path / "hosts"
backup.write_bytes(b"127.0.0.1 localhost\n")
hosts_file.write_bytes(b"127.0.0.1 localhost")
preview = create_restore_preview(backup, hosts_file)
modal = BackupRestoreModal(preview, lambda: unchanged_preview(preview))
rendered = modal._render_diff()
assert "No newline at end of current Hosts File" in str(rendered)