<commit_before># coding=utf-8
# noinspection PyUnresolvedReferences
import chatcommunicate  # coverage
import chatcommands
from apigetpost import api_get_post
from parsing import to_protocol_relative
from classes._Post import Post
from globalvars import GlobalVars
from datahandling import _remove_pickle

import datetime
import os
import pytest
import regex
import types
import requests
if GlobalVars.on_windows:
    # noinspection PyPep8Naming
    from classes._Git_Windows import git
else:
    from sh.contrib import git

from fake import Fake
from unittest.mock import patch


def test_null():
    assert chatcommands.null() is None


def test_coffee():
    msg = Fake({"owner": {"name": "<NAME>"}})

    coffees = "\\*brews a cup of ({}) for ".format("|".join(chatcommands.COFFEES))
    assert regex.match(coffees + "@El'endiaStarman\\*", chatcommands.coffee(None, original_msg=msg))
    assert regex.match(coffees + "@angussidney\\*", chatcommands.coffee("angussidney"))


def test_tea():
    msg = Fake({"owner": {"name": "<NAME>"}})

    teas = "\\*brews a cup of ({}) tea for ".format("|".join(chatcommands.TEAS))
    assert regex.match(teas + "@El'endiaStarman\\*", chatcommands.tea(None, original_msg=msg))
    assert regex.match(teas + "@angussidney\\*", chatcommands.tea("angussidney"))


def test_lick():
    assert chatcommands.lick() == "*licks ice cream cone*"


def test_brownie():
    assert chatcommands.brownie() == "Brown!"


def test_wut():
    assert chatcommands.wut() == "Whaddya mean, 'wut'? Humans..."


def test_alive():
    assert chatcommands.alive() in chatcommands.ALIVE_MSG


def test_location():
    assert chatcommands.location() == GlobalVars.location


def test_version():
    assert chatcommands.version() == '{id} [{commit_name}]({repository}/commit/{commit_code})'.format(
        id=GlobalVars.location, commit_name=GlobalVars.commit_with_author,
        commit_code=GlobalVars.commit.id, repository=GlobalVars.bot_repository)


def test_bisect():
    chatcommunicate.parse_room_config("test/test_rooms.yml")
    msg = Fake({
        "owner": {
            "name": "ArtOfCode",
            "id": 121520,
            "is_moderator": True
        },
        "room": {
            "_client": {
                "host": "stackexchange.com"
            },
            "id": 11540
        },
        "content_source": None
    })
    msg.content_source = "!!/bisect :::essayssos.com:::"
    assert chatcommands.bisect(None, original_msg=msg) == r"Matched by `essayssos\.com` on [line 1 of watched_keywords.txt](https://github.com/{}/blob/{}/watched_keywords.txt#L1)".format(GlobalVars.bot_repo_slug, GlobalVars.commit.id)
    msg.content_source = "!!/bisect OoOasdfghjklOoO"
    assert chatcommands.bisect(None, original_msg=msg) == r"'OoOasdfghjklOoO' is not caught by a blacklist or watchlist item."


"""
@patch("chatcommands.datetime")
def test_hats(date):
    date.side_effect = datetime.datetime

    date.utcnow.return_value = datetime.datetime(2018, 12, 11, hour=23)
    assert chatcommands.hats() == "WE LOVE HATS! Winter Bash will begin in 0 days, 1 hour, 0 minutes, and 0 seconds."

    date.utcnow.return_value = datetime.datetime(2019, 1, 1, hour=23)
    assert chatcommands.hats() == "Winter Bash won't end for 0 days, 1 hour, 0 minutes, and 0 seconds. GO EARN SOME HATS!"
"""


def test_blame():
    msg1 = Fake({
        "_client": {
            "host": "stackexchange.com",
            "get_user": lambda id: Fake({"name": "<NAME>", "id": id})
        },

        "room": {
            "get_current_user_ids": lambda: [161943]
        }
    })

    assert chatcommands.blame(original_msg=msg1) == "It's [J F](https://chat.stackexchange.com/users/161943)'s fault."

    msg2 = Fake({
        "_client": {
            "host": "stackexchange.com",
            "get_user": lambda id: Fake({"name": "<NAME>", "id": id})
        }
    })

    assert chatcommands.blame2("\u200B\u200C\u2060\u200D\u180E\uFEFF\u2063", original_msg=msg2) == "It's [J F](https://chat.stackexchange.com/users/161943)'s fault."


def test_privileged():
    chatcommunicate.parse_room_config("test/test_rooms.yml")

    msg = Fake({
        "owner": {
            "name": "ArtOfCode",
            "id": 121520,
            "is_moderator": False
        },
        "room": {
            "_client": {
                "host": "stackexchange.com"
            },
            "id": 11540
        }
    })

    assert chatcommands.amiprivileged(original_msg=msg) == "\u2713 You are a privileged user."

    msg.owner.id = 2
    assert chatcommands.amiprivileged(original_msg=msg) == "\u2573 " + GlobalVars.not_privileged_warning

    msg.owner.is_moderator = True
    assert chatcommands.amiprivileged(original_msg=msg) == "\u2713 You are a privileged user."


def test_deprecated_blacklist():
    assert chatcommands.blacklist("").startswith("The !!/blacklist command has been deprecated.")


@pytest.mark.skipif(GlobalVars.on_branch != "master", reason="avoid branch checkout")
def test_watch(monkeypatch):
    # XXX TODO: expand<commit_msg>Fix test case from PR #3549<commit_after># coding=utf-8
# noinspection PyUnresolvedReferences
import chatcommunicate  # coverage
import chatcommands
from apigetpost import api_get_post
from parsing import to_protocol_relative
from classes._Post import Post
from globalvars import GlobalVars
from datahandling import _remove_pickle

import datetime
import os
import pytest
import regex
import types
import requests
if GlobalVars.on_windows:
    # noinspection PyPep8Naming
    from classes._Git_Windows import git
else:
    from sh.contrib import git

from fake import Fake
from unittest.mock import patch


def test_null():
    assert chatcommands.null() is None


def test_coffee():
    msg = Fake({"owner": {"name": "<NAME>"}})

    coffees = "\\*brews a cup of ({}) for ".format("|".join(chatcommands.COFFEES))
    assert regex.match(coffees + "@El'endiaStarman\\*", chatcommands.coffee(None, original_msg=msg))
    assert regex.match(coffees + "@angussidney\\*", chatcommands.coffee("angussidney"))


def test_tea():
    msg = Fake({"owner": {"name": "<NAME>"}})

    teas = "\\*brews a cup of ({}) tea for ".format("|".join(chatcommands.TEAS))
    assert regex.match(teas + "@El'endiaStarman\\*", chatcommands.tea(None, original_msg=msg))
    assert regex.match(teas + "@angussidney\\*", chatcommands.tea("angussidney"))


def test_lick():
    assert chatcommands.lick() == "*licks ice cream cone*"


def test_brownie():
    assert chatcommands.brownie() == "Brown!"


def test_wut():
    assert chatcommands.wut() == "Whaddya mean, 'wut'? Humans..."


def test_alive():
    assert chatcommands.alive() in chatcommands.ALIVE_MSG


def test_location():
    assert chatcommands.location() == GlobalVars.location


def test_version():
    assert chatcommands.version() == '{id} [{commit_name}]({repository}/commit/{commit_code})'.format(
        id=GlobalVars.location, commit_name=GlobalVars.commit_with_author,
        commit_code=GlobalVars.commit.id, repository=GlobalVars.bot_repository)


def test_bisect():
    chatcommunicate.parse_room_config("test/test_rooms.yml")
    msg = Fake({
        "owner": {
            "name": "ArtOfCode",
            "id": 121520,
            "is_moderator": True
        },
        "room": {
            "_client": {
                "host": "stackexchange.com"
            },
            "id": 11540
        },
        "content_source": None
    })
    msg.content_source = "!!/bisect :::essayssos.com:::"
    assert chatcommands.bisect(None, original_msg=msg) == r"Matched by `essayssos\.com` on [line 1 of watched_keywords.txt](https://github.com/{}/blob/{}/watched_keywords.txt#L1)".format(GlobalVars.bot_repo_slug, GlobalVars.commit.id)
    msg.content_source = "!!/bisect OoOasdfghjklOoO"
    assert chatcommands.bisect(None, original_msg=msg) == r"'OoOasdfghjklOoO' is not caught by a blacklist or watchlist item."


"""
@patch("chatcommands.datetime")
def test_hats(date):
    date.side_effect = datetime.datetime

    date.utcnow.return_value = datetime.datetime(2018, 12, 11, hour=23)
    assert chatcommands.hats() == "WE LOVE HATS! Winter Bash will begin in 0 days, 1 hour, 0 minutes, and 0 seconds."

    date.utcnow.return_value = datetime.datetime(2019, 1, 1, hour=23)
    assert chatcommands.hats() == "Winter Bash won't end for 0 days, 1 hour, 0 minutes, and 0 seconds. GO EARN SOME HATS!"
"""


def test_blame():
    msg1 = Fake({
        "_client": {
            "host": "stackexchange.com",
            "get_user": lambda id: Fake({"name": "<NAME>", "id": id})
        },

        "room": {
            "get_current_user_ids": lambda: [161943]
        }
    })

    assert chatcommands.blame(original_msg=msg1) == "It's [J F](https://chat.stackexchange.com/users/161943)'s fault."

    msg2 = Fake({
        "_client": {
            "host": "stackexchange.com",
            "get_user": lambda id: Fake({"name": "<NAME>", "id": id})
        }
    })

    assert chatcommands.blame2("\u200B\u200C\u2060\u200D\u180E\uFEFF\u2063", original_msg=msg2) == "It's [J F](https://chat.stackexchange.com/users/161943)'s fault."


def test_privileged():
    chatcommunicate.parse_room_config("test/test_rooms.yml")

    msg = Fake({
        "owner": {
            "name": "ArtOfCode",
            "id": 121520,
            "is_moderator": False
        },
        "room": {
            "_client": {
                "host": "stackexchange.com"
            },
            "id": 11540
        }
    })

    assert chatcommands.amiprivileged(original_msg=msg) == "\u2713 You are a privileged user."

    msg.owner.id = 2
    assert chatcommands.amiprivileged(original_msg=msg) == "\u2573 " + GlobalVars.not_privileged_warning

    msg.owner.is_moderator = True
    assert chatcommands.amiprivileged(original_msg=msg) == "\u2713 You are a privileged user."


def test_deprecated_blacklist():
    assert chatcommands.blacklist("").startswith("The `!!/blacklist` command has been deprecated.")


@pytest.mark.skipif(GlobalVars.on_branch != "master", reason="avoid branch checkout")
def test_watch(monkeypatch):
    # XXX TODO: expand
{'fraction_non_alphanumeric': 0.14320963284642715, 'fraction_numerical': 0.021713383339913146, 'mean_word_length': 3.315587734241908, 'pattern_counts': {'":': 66, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1521058', 'n_tokens_mistral': 3560, 'n_tokens_neox': 3321, 'n_words': 732}