"""Focused tests for the background window used in seam-repaired composites."""
import cv2
import numpy as np
from PIL import Image
from r112_scaleup_seam_repair import render

def sample(shape):
    return {'image': Image.fromarray(np.full(shape, 128, dtype=np.uint8)),
            'weak_mask_proposals': np.zeros((3, *shape), dtype=bool)}

def test_window_is_contiguous_and_stays_inside_background():
    background = np.arange(20 * 24, dtype=np.uint16).reshape(20, 24) % 251
    background = background.astype(np.uint8)
    image, meta = render(sample((8, 9)), background, (.09, .09), 112)
    x, y = meta['offset_xy']
    assert meta['reflected_padding_bottom_right'] == [0, 0]
    np.testing.assert_array_equal(np.asarray(image), background[y:y + 8, x:x + 9])

def test_oversize_window_uses_reflection_only_for_missing_extent():
    background = np.arange(6 * 7, dtype=np.uint8).reshape(6, 7)
    image, meta = render(sample((9, 11)), background, (.09, .09), 112)
    assert meta['reflected_padding_bottom_right'] == [3, 4]
    expected = cv2.copyMakeBorder(background, 0, 3, 0, 4, cv2.BORDER_REFLECT_101)
    np.testing.assert_array_equal(np.asarray(image), expected)

if __name__ == '__main__':
    test_window_is_contiguous_and_stays_inside_background()
    test_oversize_window_uses_reflection_only_for_missing_extent()
    print('2 seam-repair tests passed')
