import numpy as np
import pytest
from tests.node_tests._shared import make_field


def test_same_image():
    from backend.nodes.mutual_crop import MutualCrop

    node = MutualCrop()
    field = make_field()
    cropped_a, cropped_b = node.process(field, field)
    assert cropped_a.data.shape == cropped_b.data.shape


def test_output_shapes_match():
    from backend.nodes.mutual_crop import MutualCrop

    node = MutualCrop()
    rng = np.random.default_rng(10)
    field_a = make_field(data=rng.standard_normal((48, 64)))
    field_b = make_field(data=rng.standard_normal((64, 48)))
    cropped_a, cropped_b = node.process(field_a, field_b)
    assert cropped_a.data.shape == cropped_b.data.shape, (
        f"Shapes should match: {cropped_a.data.shape} vs {cropped_b.data.shape}"
    )


def test_identical_fields():
    from backend.nodes.mutual_crop import MutualCrop

    node = MutualCrop()
    data = np.random.default_rng(99).standard_normal((32, 32))
    field_a = make_field(data=data.copy())
    field_b = make_field(data=data.copy())
    cropped_a, cropped_b = node.process(field_a, field_b)
    # Identical fields should be fully overlapping, so cropped output ~ original
    assert cropped_a.data.shape == (32, 32)
    assert np.allclose(cropped_a.data, data)
