# Path: px/px_loginhistory.py
# LOG = logging.getLogger(__name__)
# LAST_USERNAME = "([^ ]+)"
# LAST_DEVICE = "([^ ]+)"
# LAST_ADDRESS = "([^ ]+)?"
# LAST_PID = r"( \[[0-9]+\])?"
# LAST_FROM = "(... ... .. ..:..)"
# LAST_DASH = " [- ] "
# LAST_TO = "[^(]*"
# LAST_DURATION = "([0-9+:]+)"
# LAST_RE = re.compile(
#     LAST_USERNAME
#     + " +"
#     + LAST_DEVICE
#     + " +"
#     + LAST_ADDRESS
#     + LAST_PID
#     + " +"
#     + LAST_FROM
#     + LAST_DASH
#     + LAST_TO
#     + r" *(\("
#     + LAST_DURATION
#     + r"\))?"
# )
# TIMEDELTA_RE = re.compile(r"(([0-9]+)\+)?([0-9][0-9]):([0-9][0-9])")
# MONTHS = {
#     "Jan": 1,
#     "Feb": 2,
#     "Mar": 3,
#     "Apr": 4,
#     "May": 5,
#     "Jun": 6,
#     "Jul": 7,
#     "Aug": 8,
#     "Sep": 9,
#     "Oct": 10,
#     "Nov": 11,
#     "Dec": 12,
# }
# def get_users_at(
#     timestamp,  # type: datetime.datetime
#     last_output=None,  # type: Optional[str]
#     now=None,  # type: Optional[datetime.datetime]
# ):
# def call_last():
# def _to_timestamp(string, now):
# def _to_timedelta(string):


import datetime
import pytest
import dateutil.tz
from px import px_loginhistory
    from typing import Set  # NOQA

    # Note trailing space in test string, we get that from last on OS X 10.11.3
    assert not get_users_at("wtmp begins Thu Oct  1 22:54 ", now, now)


def test_get_users_at_unexpected_last_output(caplog):
    UNEXPECTED = "glasskiosk"

    now = datetime.datetime(2016, 4, 7, 12, 8, tzinfo=dateutil.tz.tzlocal())
    assert not get_users_at(UNEXPECTED, now, now)

    assert UNEXPECTED in caplog.text


def test_get_users_at_just_run_it(check_output):
    # Just tyre kick it live wherever we happen to be. This shouldn't crash.
    px_loginhistory.get_users_at(datetime.datetime.now(dateutil.tz.tzlocal()))


def test_to_timestamp(check_output):
    now = datetime.datetime(2016, 4, 3, 12, 8, tzinfo=dateutil.tz.tzlocal())
    expected = datetime.datetime(2016, 3, 5, 11, 19, tzinfo=dateutil.tz.tzlocal())
    assert px_loginhistory._to_timestamp("Thu Mar  5 11:19", now) == expected

    now = datetime.datetime(2016, 4, 3, 12, 8, tzinfo=dateutil.tz.tzlocal())
    expected = datetime.datetime(2016, 2, 29, 13, 19, tzinfo=dateutil.tz.tzlocal())
    assert px_loginhistory._to_timestamp("Mon Feb 29 13:19", now) == expected

    now = datetime.datetime(2017, 1, 3, 12, 8, tzinfo=dateutil.tz.tzlocal())
    expected = datetime.datetime(2016, 2, 29, 13, 19, tzinfo=dateutil.tz.tzlocal())

# Path: px/px_loginhistory.py
# LOG = logging.getLogger(__name__)
# LAST_USERNAME = "([^ ]+)"
# LAST_DEVICE = "([^ ]+)"
# LAST_ADDRESS = "([^ ]+)?"
# LAST_PID = r"( \[[0-9]+\])?"
# LAST_FROM = "(... ... .. ..:..)"
# LAST_DASH = " [- ] "
# LAST_TO = "[^(]*"
# LAST_DURATION = "([0-9+:]+)"
# LAST_RE = re.compile(
#     LAST_USERNAME
#     + " +"
#     + LAST_DEVICE
#     + " +"
#     + LAST_ADDRESS
#     + LAST_PID
#     + " +"
#     + LAST_FROM
#     + LAST_DASH
#     + LAST_TO
#     + r" *(\("
#     + LAST_DURATION
#     + r"\))?"
# )
# TIMEDELTA_RE = re.compile(r"(([0-9]+)\+)?([0-9][0-9]):([0-9][0-9])")
# MONTHS = {
#     "Jan": 1,
#     "Feb": 2,
#     "Mar": 3,
#     "Apr": 4,
#     "May": 5,
#     "Jun": 6,
#     "Jul": 7,
#     "Aug": 8,
#     "Sep": 9,
#     "Oct": 10,
#     "Nov": 11,
#     "Dec": 12,
# }
# def get_users_at(
#     timestamp,  # type: datetime.datetime
#     last_output=None,  # type: Optional[str]
#     now=None,  # type: Optional[datetime.datetime]
# ):
# def call_last():
# def _to_timestamp(string, now):
# def _to_timedelta(string):


# Path: tests/px_loginhistory_test.py
import datetime
import pytest
import dateutil.tz
from px import px_loginhistory
    from typing import Set  # NOQA


    # Note trailing space in test string, we get that from last on OS X 10.11.3
    assert not get_users_at("wtmp begins Thu Oct  1 22:54 ", now, now)


def test_get_users_at_unexpected_last_output(caplog):
    UNEXPECTED = "glasskiosk"

    now = datetime.datetime(2016, 4, 7, 12, 8, tzinfo=dateutil.tz.tzlocal())
    assert not get_users_at(UNEXPECTED, now, now)

    assert UNEXPECTED in caplog.text


def test_get_users_at_just_run_it(check_output):
    # Just tyre kick it live wherever we happen to be. This shouldn't crash.
    px_loginhistory.get_users_at(datetime.datetime.now(dateutil.tz.tzlocal()))


def test_to_timestamp(check_output):
    now = datetime.datetime(2016, 4, 3, 12, 8, tzinfo=dateutil.tz.tzlocal())
    expected = datetime.datetime(2016, 3, 5, 11, 19, tzinfo=dateutil.tz.tzlocal())
    assert px_loginhistory._to_timestamp("Thu Mar  5 11:19", now) == expected

    now = datetime.datetime(2016, 4, 3, 12, 8, tzinfo=dateutil.tz.tzlocal())
    expected = datetime.datetime(2016, 2, 29, 13, 19, tzinfo=dateutil.tz.tzlocal())
    assert px_loginhistory._to_timestamp("Mon Feb 29 13:19", now) == expected

    now = datetime.datetime(2017, 1, 3, 12, 8, tzinfo=dateutil.tz.tzlocal())
    expected = datetime.datetime(2016, 2, 29, 13, 19, tzinfo=dateutil.tz.tzlocal())

    assert px_loginhistory._to_timestamp("Mon Feb 29 13:19", now) == expected