Skip to content

Commit

Permalink
test: fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Nov 17, 2024
1 parent 1aa576a commit 7edb198
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 22 deletions.
4 changes: 2 additions & 2 deletions gptme/util/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def tokens_count(text: str | None, model: str, file: str | None):
except KeyError:
print(f"Error: Model '{model}' not supported by tiktoken.")
print("Supported models include: gpt-4, gpt-3.5-turbo, text-davinci-003")
return 1
sys.exit(1)

# Count tokens
tokens = enc.encode(text)
Expand Down Expand Up @@ -167,7 +167,7 @@ def tools_info(tool_name: str):
print(f"Tool '{tool_name}' not found. Available tools:")
for t in loaded_tools:
print(f"- {t.name}")
return
sys.exit(1)

print(f"Tool: {tool.name}")
print(f"Description: {tool.desc}")
Expand Down
19 changes: 18 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pytest-profiling = "^1.7.0"
pytest-dotenv = "^0.5.2"
pytest-timeout = "^2.2.0"
pytest-retry = "^1.6.3"
pytest-mock = "*"
greenlet = "*" # dependency of playwright, but needed for coverage

# docs
Expand Down
71 changes: 52 additions & 19 deletions tests/test_util_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,68 @@ def test_tokens_count():
assert "Token count" in result.output


def test_chats_list(mocker):
def test_chats_list(tmp_path, mocker):
"""Test the chats list command."""
runner = CliRunner()

# Test empty list
mocker.patch("gptme.util.cli.get_user_conversations", return_value=[])
# Create test conversations
logs_dir = tmp_path / "logs"
logs_dir.mkdir()

# Mock both the logs directory and the conversation listing
mocker.patch("gptme.dirs.get_logs_dir", return_value=str(logs_dir))
mocker.patch("gptme.logmanager.get_user_conversations", return_value=[])

# Test empty list (should work now since we're using our empty logs_dir)
result = runner.invoke(main, ["chats", "ls"])
assert result.exit_code == 0
assert "No conversations found" in result.output

# Test with conversations
class MockConv:
def __init__(self, name, messages, modified):
self.name = name
self.messages = messages
self.modified = modified

mock_convs = [
MockConv("test-1", 5, "2024-01-01"),
MockConv("test-2", 10, "2024-01-02"),
]
mocker.patch("gptme.util.cli.get_user_conversations", return_value=mock_convs)
# Create test conversation files with names that won't be filtered
conv1_dir = logs_dir / "2024-01-01-chat-one"
conv1_dir.mkdir()
(conv1_dir / "conversation.jsonl").write_text(
'{"role": "user", "content": "hello", "timestamp": "2024-01-01T00:00:00"}\n'
)

conv2_dir = logs_dir / "2024-01-01-chat-two"
conv2_dir.mkdir()
(conv2_dir / "conversation.jsonl").write_text(
'{"role": "user", "content": "hello", "timestamp": "2024-01-01T00:00:00"}\n'
'{"role": "assistant", "content": "hi", "timestamp": "2024-01-01T00:00:01"}\n'
)

# Create ConversationMeta objects for our test conversations
from gptme.logmanager import ConversationMeta
import time

conv1 = ConversationMeta(
name="2024-01-01-chat-one",
path=str(conv1_dir / "conversation.jsonl"),
created=time.time(),
modified=time.time(),
messages=1,
branches=1,
)
conv2 = ConversationMeta(
name="2024-01-01-chat-two",
path=str(conv2_dir / "conversation.jsonl"),
created=time.time(),
modified=time.time(),
messages=2,
branches=1,
)

# Update the mock to return our test conversations
mocker.patch("gptme.logmanager.get_user_conversations", return_value=[conv1, conv2])

# Test with conversations
result = runner.invoke(main, ["chats", "ls"])
assert result.exit_code == 0
assert "test-1" in result.output
assert "test-2" in result.output
assert "5 messages" in result.output
assert "10 messages" in result.output
assert "chat-one" in result.output
assert "chat-two" in result.output
assert "Messages: 1" in result.output # First chat has 1 message
assert "Messages: 2" in result.output # Second chat has 2 messages


@pytest.mark.skip("Waiting for context module PR")
Expand Down

0 comments on commit 7edb198

Please sign in to comment.