-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor chat loop and command handling in cli.py
The chat loop and command handling in cli.py have been refactored into a new class, Chat, in input.py. This change improves code organization and readability by encapsulating chat-related functionality in a dedicated class. The Chat class handles parsing of human input, command execution, and file context display. The cli.py file has been updated to use this new class, resulting in a significant reduction in its complexity and size. A test suite for the new Chat class has also been added.
- Loading branch information
1 parent
a2c5162
commit a14c2ea
Showing
3 changed files
with
167 additions
and
90 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
from aicodebot.helpers import create_and_write_file | ||
from aicodebot.input import Chat | ||
from tests.conftest import in_temp_directory | ||
import pytest | ||
|
||
|
||
class MockConsole: | ||
def __init__(self): | ||
self.output = [] | ||
|
||
def print(self, message, style=None): # noqa: A003 | ||
self.output.append(message) | ||
|
||
|
||
@pytest.fixture | ||
def chat(): | ||
console = MockConsole() | ||
files = () # Initial argument from click is a tuple | ||
return Chat(console, files) | ||
|
||
|
||
def test_parse_human_input(chat): | ||
# Test with a normal input | ||
input_data = "Hello, world!" | ||
assert chat.parse_human_input(input_data) == input_data | ||
|
||
# Test with an empty input | ||
input_data = "" | ||
assert chat.parse_human_input(input_data) == chat.CONTINUE | ||
|
||
|
||
def test_parse_human_input_files(chat, tmp_path): | ||
with in_temp_directory(tmp_path): | ||
create_and_write_file(tmp_path / "file.txt", "text") | ||
|
||
assert chat.parse_human_input("/add file.txt") == chat.CONTINUE | ||
assert chat.files == set(["file.txt"]) | ||
assert "✅ Added 'file.txt' to the list of files." in chat.console.output | ||
|
||
assert chat.parse_human_input("/files") == chat.CONTINUE | ||
assert "file.txt" in "".join(chat.console.output) | ||
|
||
assert chat.parse_human_input("/drop file.txt") == chat.CONTINUE | ||
assert chat.files == set() | ||
|
||
|
||
def test_parse_human_input_commands(chat): | ||
# Test /sh command | ||
assert chat.parse_human_input("/sh ls") == chat.CONTINUE | ||
|
||
# Test /quit command | ||
assert chat.parse_human_input("/quit") == chat.BREAK |