"""Sample tests for PA2 Part A (file_utils)."""

from file_utils import (
    parse_cell,
    read_file,
    write_log,
    print_log,
    valid_border,
    check_grid,
)

import os
LOG_PATH = os.path.join(os.path.dirname(__file__), "log.txt")


def test_parse_cell():
    assert parse_cell("[A]") == ["A", "GREEN"]
    assert parse_cell("D") == ["*", "INVALID"]


def test_read_file():
    grid = read_file("starter.txt")
    assert grid == [
        ["M", "GREEN"],
        ["E", "RED"],
        ["V", "YELLOW"],
        ["A", "YELLOW"],
        ["E", "GREEN"],
        ["R", "RED"],
        ["_", "RED"],
        ["R", "YELLOW"],
        ["_", "RED"],
        ["S", "YELLOW"],
        ["E", "YELLOW"],
        ["P", "RED"],
        ["U", "GREEN"],
        ["T", "YELLOW"],
        ["U", "RED"],
        ["S", "RED"],
        ["_", "RED"],
        ["A", "RED"],
        ["_", "RED"],
        ["A", "YELLOW"],
        ["L", "GREEN"],
        ["A", "GREEN"],
        ["E", "YELLOW"],
        ["S", "RED"],
        ["L", "GREEN"],
    ]


def test_write_log():
    success = write_log(("a", "w"))
    assert success
    with open(LOG_PATH) as file:
        lines = file.readlines()
        assert lines[-1].rstrip() == "a,w"


def test_print_log(capsys):
    with open(LOG_PATH, "w") as file:
        file.write("a,w\n")
        file.write("b,x\n")
    print_log()
    captured = capsys.readouterr()
    assert captured.out == ("\n"
        "============================================================\n"
        "MOVE LOG (from log.txt)\n"
        "============================================================\n"
        "Move 1: a,w\n"
        "Move 2: b,x\n"
        "============================================================\n"
        "\n"
    )


def test_valid_border():
    assert valid_border("[A]")
    assert not valid_border("D")


def test_check_grid():
    grid = [["A", "GREEN"]] * 25
    assert check_grid(grid) == "valid"
    grid = []
    assert check_grid(grid) == "size"
