import pytest
from src.fuzzy_anchor import FuzzyAnchor


class TestFuzzyAnchor:
 def test_create_slice_basic(self):
  text = "line0\nline1\nline2\nline3\nline4\n"
  result = FuzzyAnchor.create_slice(text, 2, 4)
  assert "start_line" in result
  assert "end_line" in result
  assert "content_hash" in result
  assert "start_context" in result
  assert "end_context" in result
  assert result["start_line"] == 2
  assert result["end_line"] == 4
  assert result["start_context"] == result["end_context"]

 def test_resolve_slice_exact_match(self):
  text = "line0\nline1\nline2\nline3\nline4\n"
  slc = FuzzyAnchor.create_slice(text, 2, 4)
  result = FuzzyAnchor.resolve_slice(text, slc)
  assert result is not None
  start, end = result
  assert start == 2
  assert end == 4

 def test_resolve_slice_line_inserted_before(self):
  original = "line0\nline1\nline2\nline3\nline4\n"
  modified = "NEW\nline0\nline1\nline2\nline3\nline4\n"
  slc = FuzzyAnchor.create_slice(original, 2, 4)
  result = FuzzyAnchor.resolve_slice(modified, slc)
  assert result is not None
  start, end = result
  assert start == 3
  assert end == 5

 def test_resolve_slice_line_deleted_before_returns_none(self):
  original = "line0\nline1\nline2\nline3\nline4\n"
  modified = "line0\nline2\nline3\nline4\n"
  slc = FuzzyAnchor.create_slice(original, 2, 4)
  result = FuzzyAnchor.resolve_slice(modified, slc)
  assert result is None

 def test_resolve_slice_multiple_lines_changed(self):
  original = "line0\nline1\nline2\nline3\nline4\n"
  modified = "a\nb\nc\nd\ne\nline0\nline1\nline2\nline3\nline4\n"
  slc = FuzzyAnchor.create_slice(original, 1, 2)
  result = FuzzyAnchor.resolve_slice(modified, slc)
  assert result is not None
  start, end = result
  assert start == 6
  assert end == 7

 def test_resolve_slice_anchor_mismatch_returns_none(self):
  original = "alpha\nbeta\ngamma\ndelta\nepsilon\n"
  modified = "foo\nbar\nbaz\ndelta\nepsilon\n"
  slc = FuzzyAnchor.create_slice(original, 2, 3)
  result = FuzzyAnchor.resolve_slice(modified, slc)
  assert result is None
