feat: auto save sessions before next user input (#94)
This commit is contained in:
@@ -1,8 +1,16 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
from exchange import Message
|
||||
from goose.utils.session_file import list_sorted_session_files, read_from_file, session_file_exists, write_to_file
|
||||
from goose.utils.session_file import (
|
||||
list_sorted_session_files,
|
||||
read_from_file,
|
||||
read_or_create_file,
|
||||
save_latest_session,
|
||||
session_file_exists,
|
||||
write_to_file,
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -32,6 +40,23 @@ def test_read_from_file_non_jsonl_file(file_path):
|
||||
read_from_file(file_path)
|
||||
|
||||
|
||||
def test_read_or_create_file_when_file_not_exist(tmp_path):
|
||||
file_path = tmp_path / "no_existing.json"
|
||||
|
||||
assert read_or_create_file(file_path) == []
|
||||
assert os.path.exists(file_path)
|
||||
|
||||
|
||||
def test_read_or_create_file_when_file_exists(file_path):
|
||||
messages = [
|
||||
Message.user("prompt1"),
|
||||
]
|
||||
write_to_file(file_path, messages)
|
||||
|
||||
assert file_path.exists()
|
||||
assert read_from_file(file_path) == messages
|
||||
|
||||
|
||||
def test_list_sorted_session_files(tmp_path):
|
||||
session_files_directory = tmp_path / "session_files_dir"
|
||||
session_files_directory.mkdir()
|
||||
@@ -71,6 +96,21 @@ def test_session_file_exists_return_true_when_session_file_exists(tmp_path):
|
||||
assert session_file_exists(session_files_directory)
|
||||
|
||||
|
||||
def test_save_latest_session(file_path, tmp_path):
|
||||
messages = [
|
||||
Message.user("prompt1"),
|
||||
Message.user("prompt2"),
|
||||
]
|
||||
write_to_file(file_path, messages)
|
||||
|
||||
messages.append(Message.user("prompt3"))
|
||||
save_latest_session(file_path, messages)
|
||||
|
||||
messages_in_file = read_from_file(file_path)
|
||||
assert messages_in_file == messages
|
||||
assert len(messages_in_file) == 3
|
||||
|
||||
|
||||
def create_session_file(file_path, file_name) -> Path:
|
||||
file = file_path / f"{file_name}.jsonl"
|
||||
file.touch()
|
||||
|
||||
Reference in New Issue
Block a user