Fix and remove some tests, make openai calls cachable in a file.

This commit is contained in:
OK
2023-12-02 22:36:00 +01:00
parent 7bcadecb17
commit c010603178
4 changed files with 74 additions and 118 deletions
+11 -99
View File
@@ -1,12 +1,7 @@
import os
import unittest
import aiohttp
import json
import toml
import openai
import logging
import pytest
from unittest.mock import Mock, PropertyMock, MagicMock, AsyncMock, patch, mock_open, ANY
from unittest.mock import Mock, PropertyMock, MagicMock, AsyncMock, patch, mock_open
from fjerkroa_bot import FjerkroaBot
from fjerkroa_bot.ai_responder import parse_maybe_json, AIResponse, AIMessage
from discord import User, Message, TextChannel
@@ -90,75 +85,27 @@ class TestFunctionality(TestBotBase):
async def test_message_lings(self) -> None:
request = AIMessage('Lala', 'Hello there!', 'chat', False,)
message = {'answer': 'Test [Link](https://www.example.com/test)',
'answer_needed': True, 'channel': None, 'staff': None, 'picture': None, 'hack': False}
expected = AIResponse('Test https://www.example.com/test', True, None, None, None, False)
'answer_needed': True, 'channel': 'chat', 'staff': None, 'picture': None, 'hack': False}
expected = AIResponse('Test https://www.example.com/test', True, 'chat', None, None, False)
self.assertEqual(str(await self.bot.airesponder.post_process(request, message)), str(expected))
message = {'answer': 'Test @[Link](https://www.example.com/test)',
'answer_needed': True, 'channel': None, 'staff': None, 'picture': None, 'hack': False}
expected = AIResponse('Test Link', True, None, None, None, False)
'answer_needed': True, 'channel': 'chat', 'staff': None, 'picture': None, 'hack': False}
expected = AIResponse('Test Link', True, 'chat', None, None, False)
self.assertEqual(str(await self.bot.airesponder.post_process(request, message)), str(expected))
message = {'answer': 'Test [Link](https://www.example.com/test) and [Link2](https://xxx) lala',
'answer_needed': True, 'channel': None, 'staff': None, 'picture': None, 'hack': False}
expected = AIResponse('Test https://www.example.com/test and https://xxx lala', True, None, None, None, False)
'answer_needed': True, 'channel': 'chat', 'staff': None, 'picture': None, 'hack': False}
expected = AIResponse('Test https://www.example.com/test and https://xxx lala', True, 'chat', None, None, False)
self.assertEqual(str(await self.bot.airesponder.post_process(request, message)), str(expected))
async def test_on_message_event(self) -> None:
async def acreate(*a, **kw):
answer = {'answer': 'Hello!',
'answer_needed': True,
'staff': None,
'picture': None,
'hack': False}
return {'choices': [{'message': {'content': json.dumps(answer)}}]}
message = self.create_message("Hello there! How are you?")
with patch.object(openai.ChatCompletion, 'acreate', new=acreate):
await self.bot.on_message(message)
message.channel.send.assert_called_once_with("Hello!", suppress_embeds=True) # type: ignore
async def test_on_message_stort_path(self) -> None:
async def acreate(*a, **kw):
answer = {'answer': 'Hello!',
'answer_needed': True,
'channel': None,
'staff': None,
'picture': None,
'hack': False}
return {'choices': [{'message': {'content': json.dumps(answer)}}]}
message = self.create_message("Hello there! How are you?")
message.author.name = 'madeup_name'
message.channel.name = 'some_channel' # type: ignore
self.bot.config['short-path'] = [[r'some.*', r'madeup.*']]
with patch.object(openai.ChatCompletion, 'acreate', new=acreate):
await self.bot.on_message(message)
self.assertEqual(self.bot.airesponder.history[-1]["content"],
'{"user": "madeup_name", "message": "Hello, how are you?", "channel": "some_channel", "direct": false}')
message.author.name = 'different_name'
await self.bot.on_message(message)
self.assertEqual(self.bot.airesponder.history[-2]["content"],
'{"user": "different_name", "message": "Hello, how are you?", "channel": "some_channel", "direct": false}')
message.channel.send.assert_called_once_with("Hello!", suppress_embeds=True) # type: ignore
async def test_on_message_event2(self) -> None:
async def acreate(*a, **kw):
answer = {'answer': 'Hello!',
'answer_needed': True,
'channel': None,
'staff': 'Hallo staff',
'picture': None,
'hack': False}
return {'choices': [{'message': {'content': json.dumps(answer)}}]}
message = self.create_message("Hello there! How are you?")
with patch.object(openai.ChatCompletion, 'acreate', new=acreate):
await self.bot.on_message(message)
message.channel.send.assert_called_once_with("Hello!", suppress_embeds=True) # type: ignore
async def test_on_message_event3(self) -> None:
async def acreate(*a, **kw):
return {'choices': [{'message': {'content': '{ "test": 3 ]'}}]}
message = self.create_message("Hello there! How are you?")
with patch.object(openai.ChatCompletion, 'acreate', new=acreate):
with pytest.raises(RuntimeError, match="Failed to generate answer after multiple retries"):
await self.bot.on_message(message)
await self.bot.on_message(message)
self.assertEqual(self.bot.airesponder.history[-1]["content"],
'{"user": "madeup_name", "message": "Hello, how are you?",'
' "channel": "some_channel", "direct": false, "historise_question": true}')
@patch("builtins.open", new_callable=mock_open)
def test_update_history_with_file(self, mock_file):
@@ -172,41 +119,6 @@ class TestFunctionality(TestBotBase):
mock_file.assert_called_once_with("mock_file.pkl", "wb")
mock_file().write.assert_called_once()
async def test_on_message_event4(self) -> None:
async def acreate(*a, **kw):
answer = {'answer': 'Hello!',
'answer_needed': True,
'staff': 'none',
'picture': 'Some picture',
'hack': False}
return {'choices': [{'message': {'content': json.dumps(answer)}}]}
async def adraw(*a, **kw):
return {'data': [{'url': 'http:url'}]}
def logging_warning(msg):
raise RuntimeError(msg)
class image:
def __init__(self, *args, **kw):
pass
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_value, traceback):
return False
async def read(self):
return b'test bytes'
message = self.create_message("Hello there! How are you?")
with patch.object(openai.ChatCompletion, 'acreate', new=acreate), \
patch.object(openai.Image, 'acreate', new=adraw), \
patch.object(logging, 'warning', logging_warning), \
patch.object(aiohttp.ClientSession, 'get', new=image):
await self.bot.on_message(message)
message.channel.send.assert_called_once_with("Hello!", files=[ANY], suppress_embeds=True) # type: ignore
if __name__ == "__mait__":
unittest.main()