Skip to content

Commit

Permalink
Fix pyright and test
Browse files Browse the repository at this point in the history
  • Loading branch information
r0x0d committed Dec 10, 2024
1 parent 47c1240 commit 5fdc2f7
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 30 deletions.
3 changes: 2 additions & 1 deletion command_line_assistant/commands/history.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from argparse import Namespace
from pathlib import Path

from command_line_assistant.history import handle_history_write
from command_line_assistant.utils.cli import BaseCLICommand, SubParsersAction
Expand All @@ -16,7 +17,7 @@ def run(self) -> None:
if self._clear:
logger.info("Clearing history of conversation")
# TODO(r0x0d): Rewrite this.
handle_history_write("/tmp/test_history.json", [], "")
handle_history_write(Path("/tmp/test_history.json"), [], "")


def register_subcommand(parser: SubParsersAction):
Expand Down
15 changes: 6 additions & 9 deletions command_line_assistant/history.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
from pathlib import Path

from command_line_assistant.config import Config

Expand Down Expand Up @@ -30,22 +31,18 @@ def handle_history_read(config: Config) -> list:
return history[:max_size]


def handle_history_write(config: Config, history: list, response: str) -> None:
def handle_history_write(history_file: Path, history: list, response: str) -> None:
"""
Writes the history to a file.
"""
if not config.history.enabled:
return

filepath = config.history.file

if not filepath.exists():
filepath.parent.mkdir(mode=0o755)
if not history_file.exists():
history_file.parent.mkdir(mode=0o755)

history.append({"role": "assistant", "content": response})

try:
data = json.dumps(history)
filepath.write_text(data)
history_file.write_text(data)
except json.JSONDecodeError as e:
logging.error("Failed to write history file %s: %s", filepath, e)
logging.error("Failed to write history file %s: %s", history_file, e)

Check warning on line 48 in command_line_assistant/history.py

View check run for this annotation

Codecov / codecov/patch

command_line_assistant/history.py#L48

Added line #L48 was not covered by tests
8 changes: 0 additions & 8 deletions data/systemd/clad@.socket

This file was deleted.

5 changes: 4 additions & 1 deletion tests/commands/test_history.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from argparse import ArgumentParser, Namespace
from pathlib import Path
from unittest.mock import patch

import pytest
Expand Down Expand Up @@ -31,7 +32,9 @@ def test_init(self, history_command):
def test_run_with_clear(self, mock_history_write, history_command):
"""Test run() method when clear=True"""
history_command.run()
mock_history_write.assert_called_once_with("/tmp/test_history.json", [], "")
mock_history_write.assert_called_once_with(
Path("/tmp/test_history.json"), [], ""
)

@patch("command_line_assistant.commands.history.handle_history_write")
def test_run_without_clear(self, mock_history_write, history_command_no_clear):
Expand Down
14 changes: 3 additions & 11 deletions tests/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,16 @@ def test_history_over_max_size(self, tmp_path, multiply, max_size):


class TestHistoryWrite:
def test_not_enabled(self):
config = Config(history=HistorySchema(enabled=False))
assert not history.handle_history_write(config, [], "")

def test_history_file_missing(self, tmp_path):
history_file = tmp_path / "history" / "non-existing-file.json"
config = Config(history=HistorySchema(file=history_file))

history.handle_history_write(config, [], "test")
history.handle_history_write(history_file, [], "test")
assert Path(history_file).exists()

def test_history_write(self, tmp_path):
expected = [{"role": "assistant", "content": "test"}]
history_file = tmp_path / "history" / "non-existing-file.json"
config = Config(history=HistorySchema(file=history_file))

history.handle_history_write(config, [], "test")
history.handle_history_write(history_file, [], "test")

raw_history = Path(history_file).read_text()
assert json.loads(raw_history) == expected
Expand All @@ -94,9 +87,8 @@ def test_history_append(self, tmp_path):
expected.append({"role": "assistant", "content": "test"})

history_file = tmp_path / "history" / "non-existing-file.json"
config = Config(history=HistorySchema(file=history_file))

history.handle_history_write(config, MOCK_HISTORY_CONVERSATION, "test")
history.handle_history_write(history_file, MOCK_HISTORY_CONVERSATION, "test")

raw_history = Path(history_file).read_text()
assert json.loads(raw_history) == expected

0 comments on commit 5fdc2f7

Please sign in to comment.