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.
This commit is contained in:
parent
6a3e1e0d5a
commit
e268794564
13 changed files with 1103 additions and 19 deletions
73
tests/test_restore_preview.py
Normal file
73
tests/test_restore_preview.py
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
"""Tests for the exact-file preview shown before Pre-edit Backup restoration."""
|
||||
|
||||
import pytest
|
||||
|
||||
from hosts.core.restore_preview import RestorePreviewError, create_restore_preview
|
||||
|
||||
|
||||
def test_preview_pairs_current_and_restored_lines_in_changed_hunks(tmp_path):
|
||||
"""The current Hosts File is the left/before side of the preview."""
|
||||
backup = tmp_path / "hosts.backup"
|
||||
hosts_file = tmp_path / "hosts"
|
||||
backup.write_text("127.0.0.1 localhost\n192.0.2.1 restored.test\n")
|
||||
hosts_file.write_text("127.0.0.1 localhost\n192.0.2.2 current.test\n")
|
||||
|
||||
preview = create_restore_preview(backup, hosts_file)
|
||||
|
||||
assert preview.has_changes
|
||||
row = preview.changed_hunks[0].rows[1]
|
||||
assert row.current_line_number == 2
|
||||
assert row.restored_line_number == 2
|
||||
assert row.current_text == "192.0.2.2 current.test"
|
||||
assert row.restored_text == "192.0.2.1 restored.test"
|
||||
|
||||
|
||||
def test_preview_distinguishes_missing_final_newlines(tmp_path):
|
||||
backup = tmp_path / "hosts.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)
|
||||
|
||||
assert preview.has_changes
|
||||
assert preview.restored_has_final_newline
|
||||
assert not preview.current_has_final_newline
|
||||
|
||||
|
||||
def test_preview_rejects_files_that_cannot_be_decoded_as_hosts_text(tmp_path):
|
||||
backup = tmp_path / "hosts.backup"
|
||||
hosts_file = tmp_path / "hosts"
|
||||
backup.write_bytes(b"\xff")
|
||||
hosts_file.write_text("127.0.0.1 localhost\n")
|
||||
|
||||
with pytest.raises(RestorePreviewError, match="Pre-edit Backup"):
|
||||
create_restore_preview(backup, hosts_file)
|
||||
|
||||
|
||||
def test_preview_detects_when_either_reviewed_file_changes(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.2 current.test\n")
|
||||
preview = create_restore_preview(backup, hosts_file)
|
||||
|
||||
hosts_file.write_text("192.0.2.3 changed.test\n")
|
||||
|
||||
assert not preview.matches_files()
|
||||
|
||||
|
||||
def test_full_preview_keeps_lines_outside_changed_hunk_context(tmp_path):
|
||||
"""Full mode retains the complete files after changed hunks are calculated."""
|
||||
backup = tmp_path / "hosts.backup"
|
||||
hosts_file = tmp_path / "hosts"
|
||||
current_lines = [f"192.0.2.{index} host-{index}.test" for index in range(1, 41)]
|
||||
restored_lines = current_lines.copy()
|
||||
restored_lines[19] = "192.0.2.20 restored.test"
|
||||
hosts_file.write_text("\n".join(current_lines) + "\n")
|
||||
backup.write_text("\n".join(restored_lines) + "\n")
|
||||
|
||||
preview = create_restore_preview(backup, hosts_file)
|
||||
|
||||
assert len(preview.changed_hunks[0].rows) == 7
|
||||
assert len(preview.full_hunks[0].rows) == 40
|
||||
Loading…
Add table
Add a link
Reference in a new issue