Improve history handling
- Try to keep at least 3 messages from each channel in the history - Use post processed messages for the history, instead of the raw messages from the openai API
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import unittest
|
||||
import tempfile
|
||||
import os
|
||||
import pickle
|
||||
from fjerkroa_bot import AIMessage, AIResponse
|
||||
from .test_main import TestBotBase
|
||||
|
||||
@@ -73,6 +76,51 @@ You always try to say something positive about the current day and the Fjærkroa
|
||||
self.assertAIResponse(response, AIResponse('test', True, 'something', None, False), scmp=lambda a, b: type(a) == str and len(a) > 5)
|
||||
print(f"\n{self.bot.airesponder.history}")
|
||||
|
||||
def test_update_history(self) -> None:
|
||||
updater = self.bot.airesponder
|
||||
updater.history = []
|
||||
updater.history_file = None
|
||||
|
||||
question = {"channel": "test_channel", "content": "What is the meaning of life?"}
|
||||
answer = {"channel": "test_channel", "content": "42"}
|
||||
|
||||
# Test case 1: Limit set to 2
|
||||
updater.update_history(question, answer, 2)
|
||||
self.assertEqual(updater.history, [question, answer])
|
||||
|
||||
# Test case 2: Limit set to 4, check limit enforcement (deletion)
|
||||
new_question = {"channel": "test_channel", "content": "What is AI?"}
|
||||
new_answer = {"channel": "test_channel", "content": "Artificial Intelligence"}
|
||||
updater.update_history(new_question, new_answer, 3)
|
||||
self.assertEqual(updater.history, [answer, new_question, new_answer])
|
||||
|
||||
# Test case 3: Limit set to 4, check limit enforcement (deletion)
|
||||
other_question = {"channel": "other_channel", "content": "What is XXX?"}
|
||||
other_answer = {"channel": "other_channel", "content": "Tripple X"}
|
||||
updater.update_history(other_question, other_answer, 4)
|
||||
self.assertEqual(updater.history, [new_question, new_answer, other_question, other_answer])
|
||||
|
||||
# Test case 4: Limit set to 4, check limit enforcement (deletion)
|
||||
next_question = {"channel": "other_channel", "content": "What is YYY?"}
|
||||
next_answer = {"channel": "other_channel", "content": "Tripple Y"}
|
||||
updater.update_history(next_question, next_answer, 4)
|
||||
self.assertEqual(updater.history, [new_answer, other_answer, next_question, next_answer])
|
||||
|
||||
# Test case 5: Limit set to 4, check limit enforcement (deletion)
|
||||
next_question2 = {"channel": "other_channel", "content": "What is ZZZ?"}
|
||||
next_answer2 = {"channel": "other_channel", "content": "Tripple Z"}
|
||||
updater.update_history(next_question2, next_answer2, 4)
|
||||
self.assertEqual(updater.history, [new_answer, next_answer, next_question2, next_answer2])
|
||||
|
||||
# Test case 5: Check history file save using mock
|
||||
with unittest.mock.patch("builtins.open", unittest.mock.mock_open()) as mock_file:
|
||||
_, temp_path = tempfile.mkstemp()
|
||||
os.remove(temp_path)
|
||||
self.bot.airesponder.history_file = temp_path
|
||||
updater.update_history(question, answer, 2)
|
||||
mock_file.assert_called_with(temp_path, 'wb')
|
||||
mock_file().write.assert_called_with(pickle.dumps([question, answer]))
|
||||
|
||||
|
||||
if __name__ == "__mait__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user