"""Unit tests for launch_pisces script."""
import os
import sys

import pytest

from mined_metric.builder.metrics_impl.pisces.errors import (
    PiscesFileNotFoundError,
    PiscesInvalidShaError,
    PiscesInvalidSlackTargetError,
    PiscesSameLabelError,
    PiscesSameShaError,
)
from mined_metric.builder.metrics_impl.pisces.launch_pisces import (
    check_path_exists,
    validate_sdl_pipedream_paths,
    is_email,
    validate_notification_target,
    validate_shas,
    validate_labels,
    split_arg,
    sanitize_label,
)


def test_none_path_raises_error():
    """Tests that checking None as path raises an error."""
    with pytest.raises(PiscesFileNotFoundError):
        check_path_exists(None)


def test_foo_path_raises_error():
    """Tests that checking a non-existant path raises an error."""
    with pytest.raises(PiscesFileNotFoundError):
        check_path_exists("foo")


def test_none_sdl_pipedream_paths():
    """Tests that both sdl pipedream paths being None is OK."""
    assert validate_sdl_pipedream_paths(None, None) is None


def test_foo_sdl_pipedream_control_path():
    """Tests that candidate sdl pipedream path being None is not OK."""
    with pytest.raises(PiscesFileNotFoundError):
        validate_sdl_pipedream_paths("foo", None)


def test_foo_sdl_pipedream_candidate_path():
    """Tests that control sdl pipedream path being None is not OK."""
    with pytest.raises(PiscesFileNotFoundError):
        validate_sdl_pipedream_paths(None, "foo")


def test_valid_email():
    """Tests checking a valid email."""
    assert is_email("foo@zoox.com") is not None


def test_invalid_email():
    """Tests checking an invalid email."""
    assert is_email("foo") is None


def test_valid_notification_email():
    """Tests validating a valid email notification target."""
    assert validate_notification_target("foo@zoox.com") is None


def test_valid_notification_slack():
    """Tests validating a valid slack notification target."""
    assert validate_notification_target("#foo") is None


def test_invalid_notification_target():
    """Tests that an invalid notification target raises an error."""
    with pytest.raises(PiscesInvalidSlackTargetError):
        validate_notification_target("foo")


def test_same_shas_and_labels():
    """Tests that SHAs and labels cannot be the same."""
    with pytest.raises(PiscesSameShaError):
        validate_shas("DEADBEEF", "bar", "DEADBEEF", "bar")


def test_short_long_hashes_and_labels():
    """Tests invalidating short and long hashes with matching labels."""
    short_hash = "DEADBEEF"
    long_hash = short_hash + "D00D"
    with pytest.raises(PiscesSameShaError):
        validate_shas(short_hash, short_hash, long_hash, long_hash)


def test_sha_too_short():
    """Tests invalidating SHA1 less than 8 characters."""
    with pytest.raises(PiscesInvalidShaError):
        validate_shas("foo", "bar", "DEADBEEF", "baz")


def test_different_labels():
    """Tests different labels are OK."""
    assert validate_labels("foo", "bar") is None


def test_same_labels():
    """Tests same labels are not OK."""
    with pytest.raises(PiscesSameLabelError):
        validate_labels("foo", "foo")


def test_split_none():
    """Tests splitting None returns empty list."""
    arg = split_arg(None)
    assert isinstance(arg, list)
    assert not arg


def test_split_single_arg():
    """Tests splitting a single arg with no commas."""
    assert split_arg("foo") == ["foo"]


def test_split_multiple_args():
    """Tests splitting multiple comma-separated args."""
    assert split_arg("foo,bar,baz") == ["foo", "bar", "baz"]


def test_sanitize_none_label():
    """Tests replacing a None label with the SHA."""
    assert sanitize_label(None, "foo") == "foo"


def test_sanitize_label_characters():
    """Tests replacing certain characters with underscores."""
    assert sanitize_label("foo bar", "baz") == "foo_bar"
    assert sanitize_label("foo,bar", "baz") == "foo_bar"
    assert sanitize_label("foo/bar", "baz") == "foo_bar"


def test_unmodified_label():
    """Tests passing through a label without special characters."""
    assert sanitize_label("foo_bar", "baz") == "foo_bar"


if __name__ == "__main__":
    args = [os.path.dirname(__file__), "--color=yes", "--verbose", "--verbose"]
    code = pytest.main(args)
    sys.exit(code)
