Skip to content

Commit

Permalink
feat(test_simplebot)✨: Add deterministic mock response generation for…
Browse files Browse the repository at this point in the history
… tests

- Import hashlib to use for generating hash-based mock responses.
- Create a new function generate_mock_response to produce unique responses.
- Modify test_simple_bot_stream_stdout to use generate_mock_response.
  • Loading branch information
ericmjl committed Sep 10, 2024
1 parent 8b56779 commit 4a49925
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions tests/bot/test_simplebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
- test_simple_bot_call
"""

import hashlib

from hypothesis import given, settings, strategies as st

from llamabot.bot.simplebot import SimpleBot
Expand Down Expand Up @@ -46,13 +48,29 @@ def test_simple_bot_init(
assert bot.json_mode == json_mode


def generate_mock_response(system_prompt, human_message):
"""Generate a unique mock response based on system_prompt and human_message.
This function creates a deterministic mock response by hashing the combination
of the system prompt and human message.
:param system_prompt: The system prompt used in the bot.
:param human_message: The message input by the human.
:return: A unique mock response string.
"""
combined = f"{system_prompt}:{human_message}"
hash_object = hashlib.md5(combined.encode())
return f"Mock response for {hash_object.hexdigest()}"


@given(st.data())
@settings(deadline=None)
def test_simple_bot_stream_stdout(data):
"""Test that SimpleBot stream API exists and returns agenerator."""
system_prompt, human_message, mock_response = data.draw(
st.tuples(st.text(min_size=1), st.text(min_size=1), st.text(min_size=1))
system_prompt, human_message = data.draw(
st.tuples(st.text(min_size=1), st.text(min_size=1))
)
mock_response = generate_mock_response(system_prompt, human_message)
bot = SimpleBot(system_prompt, stream_target="stdout", mock_response=mock_response)
result = bot(human_message)
assert isinstance(result, AIMessage)
Expand Down

0 comments on commit 4a49925

Please sign in to comment.