import pytest
from multiplayer_crosswords.crossword_algorithm import extract_slots, slot_pattern, Slot
from multiplayer_crosswords.crossword import Orientation

def test_extract_slots_simple():
    grid = [
        ['', '', '', '#', ''],
        ['', '#', '', '', ''],
        ['', '', '', '', ''],
        ['#', '', '#', '', ''],
        ['', '', '', '', ''],
    ]
    slots = extract_slots(grid, min_length=2)
    # Should find horizontal and vertical slots of length >= 2
    assert any(s.orientation == Orientation.HORIZONTAL and s.length == 3 for s in slots)
    assert any(s.orientation == Orientation.VERTICAL and s.length == 3 for s in slots)
    # No slot should start at a '#' cell
    for slot in slots:
        r, c = slot.row, slot.col
        assert grid[r][c] != '#'

    assert len(slots) == 10  # Total slots found

def test_slot_pattern():
    grid = [
        ['a', '', '', '#', ''],
        ['', '#', '', '', ''],
        ['', '', '', '', ''],
        ['#', '', '#', '', ''],
        ['', '', '', '', ''],
    ]
    slot = Slot(0, 0, Orientation.HORIZONTAL, 3)
    pattern = slot_pattern(grid, slot)
    assert pattern == 'a**'

    slot2 = Slot(2, 1, Orientation.VERTICAL, 3)
    pattern2 = slot_pattern(grid, slot2)
    assert pattern2 == '***'  # All empty cells in this slot

def test_extract_slots_min_length():
    grid = [
        ['#', '', '', '#'],
        ['', '', '', ''],
        ['#', '', '', '#'],
    ]
    slots = extract_slots(grid, min_length=3)
    # Only slots of length 3 or more
    assert all(slot.length >= 3 for slot in slots)
    assert len(slots) == 3