import pytest from chatbug import tool_helper from unittest import mock from tests import helper import re def test_tool_dummy(): with mock.patch("tests.helper.tool_dummy") as mock_dummy: helper.tool_dummy() mock_dummy.assert_called_once() # this will check if the mocked function on the context was called. def test_tool_parse_no_exec(): with mock.patch("tests.helper.tool_dummy") as mock_dummy: tool_helper.parse_and_execute_tool_call("something else", [helper.tool_dummy, helper.tool_dummy2]) assert mock_dummy.call_count == 0 def test_match_and_extract_no_match(): result = tool_helper._match_and_extract("something else", r"(.*)<\/tool_call>") assert result == None def test_match_and_extract_matching(): result = tool_helper._match_and_extract("asdfsdfas {json content} adfafsd", r"(.*)<\/tool_call>") assert result == "{json content}" def test_match_and_extract_matching2(): result = tool_helper._match_and_extract("{json content}", r"(.*)<\/tool_call>") assert result == "{json content}" def test_match_and_extract_matching3_with_newline(): result = tool_helper._match_and_extract("\n{json content}\n", r"(.*)<\/tool_call>") assert result == "\n{json content}\n" def test_string_malformed_faulty(): with mock.patch("chatbug.utils.print_error") as print_error_mock: result = tool_helper._execute_tool_call_str("{json_content}", []) assert result == None print_error_mock.assert_called_once() # this will check if the mocked function on the context was called. def test_tool_call_json_1(): with mock.patch("chatbug.utils.print_error") as print_error_mock: result = tool_helper._execute_tool_call_json({"name": "tool_dummy", "arguments": {"a": 1, "b": "zwei"}}, [helper.tool_dummy, helper.tool_dummy2]) assert result == "result_1_zwei" assert print_error_mock.call_count == 0 def test_tool_call_json_2(): with mock.patch("chatbug.utils.print_error") as print_error_mock: result = tool_helper._execute_tool_call_json({"name": "tool_dummy2", "arguments": {"text": "some_text"}}, [helper.tool_dummy, helper.tool_dummy2]) assert result == "SOME_TEXT" assert print_error_mock.call_count == 0 def test_tool_call_json_non_existing_call_check(): with mock.patch("chatbug.utils.print_error") as print_error_mock: result = tool_helper._execute_tool_call_json({"name": "tool_dummy_which_is_not_existing", "arguments": {"text": "some_text"}}, [helper.tool_dummy, helper.tool_dummy2]) assert result == None assert print_error_mock.call_count == 1 # this will check if the mocked function on the context was called. def test_tool_call_json_wrong_arguments_check(): with mock.patch("chatbug.utils.print_error") as print_error_mock: result = tool_helper._execute_tool_call_json({"name": "tool_dummy", "arguments": {"a": "must_be_an_int_but_is_string", "b": "zwei"}}, [helper.tool_dummy, helper.tool_dummy2]) assert result == None assert print_error_mock.call_count == 1 # this will check if the mocked function on the context was called. def test_regex_multiline(): pattern = r"(.*)" # The text to search (spanning multiple lines) text = """ {json} """ # Use re.search with re.DOTALL to match across newlines match = re.search(pattern, text, re.DOTALL) assert match.group(1).find("{json}") != -1