<commit_before>import secrets
import time

from irctest import cases
from irctest.irc_utils.junkdrawer import random_name
from irctest.patma import ANYSTR

CHATHISTORY_CAP = "draft/chathistory"
EVENT_PLAYBACK_CAP = "draft/event-playback"


MYSQL_PASSWORD = ""


def validate_chathistory_batch(msgs):
    batch_tag = None
    closed_batch_tag = None
    result = []
    for msg in msgs:
        if msg.command == "BATCH":
            batch_param = msg.params[0]
            if batch_tag is None and batch_param[0] == "+":
                batch_tag = batch_param[1:]
            elif batch_param[0] == "-":
                closed_batch_tag = batch_param[1:]
        elif (
            msg.command == "PRIVMSG"
            and batch_tag is not None
            and msg.tags.get("batch") == batch_tag
        ):
            result.append(msg.to_history_message())
    assert batch_tag == closed_batch_tag
    return result


@cases.mark_services
class ChathistoryTestCase(cases.BaseServerTestCase):
    @staticmethod
    def config() -> cases.TestCaseControllerConfig:
        return cases.TestCaseControllerConfig(chathistory=True)

    @cases.mark_specifications("Ergo")
    def testInvalidTargets(self):
        bar, pw = random_name("bar"), random_name("pw")
        self.controller.registerUser(self, bar, pw)
        self.connectClient(
            bar,
            name=bar,
            capabilities=[
                "batch",
                "labeled-response",
                "message-tags",
                "server-time",
                "sasl",
                CHATHISTORY_CAP,
                EVENT_PLAYBACK_CAP,
            ],
            password=pw,
        )
        self.getMessages(bar)

        qux = random_name("qux")
        real_chname = random_name("#real_channel")
        self.connectClient(qux, name=qux)
        self.joinChannel(qux, real_chname)
        self.getMessages(qux)

        # test a nonexistent channel
        self.sendLine(bar, "CHATHISTORY LATEST #nonexistent_channel * 10")
        self.assertMessageMatch(
            self.getMessages(bar)[0],
            command="FAIL",
            params=["CHATHISTORY", "INVALID_TARGET", ANYSTR, ANYSTR],
        )

        # as should a real channel to which one is not joined:
        self.sendLine(bar, "CHATHISTORY LATEST %s * 10" % (real_chname,))
        self.assertMessageMatch(
            self.getMessages(bar)[0],
            command="FAIL",
            params=["CHATHISTORY", "INVALID_TARGET", ANYSTR, ANYSTR],
        )<commit_msg>fix chathistory spec violation in INVALID_TARGET<commit_after>import secrets
import time

from irctest import cases
from irctest.irc_utils.junkdrawer import random_name
from irctest.patma import ANYSTR

CHATHISTORY_CAP = "draft/chathistory"
EVENT_PLAYBACK_CAP = "draft/event-playback"


MYSQL_PASSWORD = ""


def validate_chathistory_batch(msgs):
    batch_tag = None
    closed_batch_tag = None
    result = []
    for msg in msgs:
        if msg.command == "BATCH":
            batch_param = msg.params[0]
            if batch_tag is None and batch_param[0] == "+":
                batch_tag = batch_param[1:]
            elif batch_param[0] == "-":
                closed_batch_tag = batch_param[1:]
        elif (
            msg.command == "PRIVMSG"
            and batch_tag is not None
            and msg.tags.get("batch") == batch_tag
        ):
            result.append(msg.to_history_message())
    assert batch_tag == closed_batch_tag
    return result


@cases.mark_services
class ChathistoryTestCase(cases.BaseServerTestCase):
    @staticmethod
    def config() -> cases.TestCaseControllerConfig:
        return cases.TestCaseControllerConfig(chathistory=True)

    @cases.mark_specifications("Ergo")
    def testInvalidTargets(self):
        bar, pw = random_name("bar"), random_name("pw")
        self.controller.registerUser(self, bar, pw)
        self.connectClient(
            bar,
            name=bar,
            capabilities=[
                "batch",
                "labeled-response",
                "message-tags",
                "server-time",
                "sasl",
                CHATHISTORY_CAP,
                EVENT_PLAYBACK_CAP,
            ],
            password=pw,
        )
        self.getMessages(bar)

        qux = random_name("qux")
        real_chname = random_name("#real_channel")
        self.connectClient(qux, name=qux)
        self.joinChannel(qux, real_chname)
        self.getMessages(qux)

        # test a nonexistent channel
        self.sendLine(bar, "CHATHISTORY LATEST #nonexistent_channel * 10")
        self.assertMessageMatch(
            self.getMessages(bar)[0],
            command="FAIL",
            params=["CHATHISTORY", "INVALID_TARGET", "LATEST", ANYSTR, ANYSTR],
        )

        # as should a real channel to which one is not joined:
        self.sendLine(bar, "CHATHISTORY LATEST %s * 10" % (real_chname,))
        self.assertMessageMatch(
            self.getMessages(bar)[0],
            command="FAIL",
            params=["CHATHISTORY", "INVALID_TARGET", "LATEST", ANYSTR, ANYSTR],
        )
{'fraction_non_alphanumeric': 0.10198078054520494, 'fraction_numerical': 0.004314571484604825, 'mean_word_length': 2.155940594059406, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '4726482', 'n_tokens_mistral': 1544, 'n_tokens_neox': 1489, 'n_words': 301}