# Path: botnet/modules/builtin/sed.py
# def parse_message(message_text):
#     pattern = re.compile('^([^:, ]*)[:, ]*s/([^/]*)/([^/]*)(/|/[a-z]+)?$')
#     result = pattern.search(message_text) 
#     if result is None:
#         raise ValueError
#     groups = list(result.groups())
# 
#     # Nickname
#     if groups[0] == '':
#         groups[0] = None
# 
#     # Flags
#     if groups[3] is not None:
#         groups[3] = groups[3].lstrip('/')
#     else:
#         groups[3] = ''
#     groups[3] = list(groups[3])
# 
#     return tuple(groups)
# 
# def make_msg_entry(author, message):
#     return {
#         'author': author,
#         'message': message,
#     }
# 
# def replace(messages, nick, a, b, flags):
#     for stored_msg in messages:
#         if a in stored_msg['message'] and stored_msg['author'] == nick:
#             if 'g' in flags:
#                 return stored_msg['message'].replace(a, b)
#             else:
#                 return stored_msg['message'].replace(a, b, 1)
#     return None


import pytest
from botnet.modules.builtin.sed import parse_message, make_msg_entry, replace
    assert parse_message('s/a/b/gi') == (None, 'a', 'b', ['g', 'i'])
    assert parse_message('nick:s/a/b/gi') == ('nick', 'a', 'b', ['g', 'i'])
    assert parse_message('nick: s/a/b/gi') == ('nick', 'a', 'b', ['g', 'i'])
    assert parse_message('nick: s/lorem ipsum/something else/gi') == ('nick', 'lorem ipsum', 'something else', ['g', 'i'])


def test_parse_message_invalid():
    with pytest.raises(ValueError):
        parse_message('lorem ipsum')


def test_replace_single():
    messages = [
        make_msg_entry('nick', 'lorem ipsum lorem'),
    ]
    assert replace(messages, *parse_message('nick: s/lorem/test')) == 'test ipsum lorem'


def test_replace_multiple():
    messages = [
        make_msg_entry('nick', 'lorem ipsum one'),
        make_msg_entry('nick', 'lorem ipsum two'),
    ]
    assert replace(messages, *parse_message('nick: s/lorem/test')) == 'test ipsum one'


def test_replace_global():
    messages = [
        make_msg_entry('nick', 'lorem ipsum lorem'),
    ]

# Path: botnet/modules/builtin/sed.py
# def parse_message(message_text):
#     pattern = re.compile('^([^:, ]*)[:, ]*s/([^/]*)/([^/]*)(/|/[a-z]+)?$')
#     result = pattern.search(message_text) 
#     if result is None:
#         raise ValueError
#     groups = list(result.groups())
# 
#     # Nickname
#     if groups[0] == '':
#         groups[0] = None
# 
#     # Flags
#     if groups[3] is not None:
#         groups[3] = groups[3].lstrip('/')
#     else:
#         groups[3] = ''
#     groups[3] = list(groups[3])
# 
#     return tuple(groups)
# 
# def make_msg_entry(author, message):
#     return {
#         'author': author,
#         'message': message,
#     }
# 
# def replace(messages, nick, a, b, flags):
#     for stored_msg in messages:
#         if a in stored_msg['message'] and stored_msg['author'] == nick:
#             if 'g' in flags:
#                 return stored_msg['message'].replace(a, b)
#             else:
#                 return stored_msg['message'].replace(a, b, 1)
#     return None


# Path: tests/modules/builtin/test_sed.py
import pytest
from botnet.modules.builtin.sed import parse_message, make_msg_entry, replace

    assert parse_message('s/a/b/gi') == (None, 'a', 'b', ['g', 'i'])
    assert parse_message('nick:s/a/b/gi') == ('nick', 'a', 'b', ['g', 'i'])
    assert parse_message('nick: s/a/b/gi') == ('nick', 'a', 'b', ['g', 'i'])
    assert parse_message('nick: s/lorem ipsum/something else/gi') == ('nick', 'lorem ipsum', 'something else', ['g', 'i'])


def test_parse_message_invalid():
    with pytest.raises(ValueError):
        parse_message('lorem ipsum')


def test_replace_single():
    messages = [
        make_msg_entry('nick', 'lorem ipsum lorem'),
    ]
    assert replace(messages, *parse_message('nick: s/lorem/test')) == 'test ipsum lorem'


def test_replace_multiple():
    messages = [
        make_msg_entry('nick', 'lorem ipsum one'),
        make_msg_entry('nick', 'lorem ipsum two'),
    ]
    assert replace(messages, *parse_message('nick: s/lorem/test')) == 'test ipsum one'


def test_replace_global():
    messages = [
        make_msg_entry('nick', 'lorem ipsum lorem'),
    ]

    assert replace(messages, *parse_message('nick: s/lorem/test/g')) == 'test ipsum test'