-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load command and some refactoring (#14)
- Loading branch information
1 parent
ec926b7
commit 1ac6fb0
Showing
9 changed files
with
227 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import contextlib | ||
|
||
import pytest | ||
from openai import ChatCompletion | ||
|
||
from oregpt.chat_bot import ChatBot | ||
from oregpt.stdinout import StdInOut | ||
|
||
|
||
def pytest_configure(): | ||
pytest.DUMMY_CONTENT = "Yep" | ||
|
||
|
||
# Use this trick | ||
# https://stackoverflow.com/a/42156088/3926333 | ||
class Helpers: | ||
@staticmethod | ||
def make_std_in_out(): | ||
return StdInOut({}, lambda: "Dummy") | ||
|
||
@staticmethod | ||
def make_chat_bot(name: str): | ||
return ChatBot(name, Helpers.make_std_in_out()) | ||
|
||
|
||
@pytest.fixture | ||
def helpers(): | ||
return Helpers | ||
|
||
|
||
@pytest.fixture | ||
def patched_bot(monkeypatch, helpers): | ||
def _create(*args, **kwargs): | ||
return [{"choices": [{"delta": {"content": pytest.DUMMY_CONTENT}}]}] | ||
|
||
# Set monkey patch to avoid this error: https://github.com/prompt-toolkit/python-prompt-toolkit/issues/406 | ||
def _print(*args, **kwargs): | ||
pass | ||
|
||
@contextlib.contextmanager | ||
def _print_as_contextmanager(*args, **kwargs): | ||
yield | ||
|
||
monkeypatch.setattr(ChatCompletion, "create", _create) | ||
monkeypatch.setattr(StdInOut, "_print", _print) | ||
monkeypatch.setattr(StdInOut, "print_assistant_thinking", _print_as_contextmanager) | ||
return helpers.make_chat_bot("Yes") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import pytest | ||
|
||
from oregpt.chat_bot import ChatBot | ||
from oregpt.command import ( | ||
ClearCommand, | ||
CommandBuilder, | ||
ExitCommand, | ||
HelpCommand, | ||
HistoryCommand, | ||
LoadCommand, | ||
SaveCommand, | ||
) | ||
|
||
|
||
def test_command_builder(helpers): | ||
command_builder = CommandBuilder({}, helpers.make_chat_bot("Yahoo")) | ||
for command_type in [ExitCommand, ClearCommand, HistoryCommand, SaveCommand, LoadCommand, HelpCommand]: | ||
for representation in command_type.representations: | ||
assert isinstance(command_builder.build(f"/{representation}"), command_type) | ||
|
||
|
||
def test_exit_command(patched_bot): | ||
command = ExitCommand({}, patched_bot, []) | ||
with pytest.raises(SystemExit): | ||
command.execute() | ||
|
||
|
||
def test_clear_command(patched_bot): | ||
cl = ClearCommand({}, patched_bot, []) | ||
patched_bot.respond("Hi, bot-san") | ||
cl.execute() | ||
patched_bot.log == ChatBot.SYSTEM_ROLE |