diff --git a/tests/test_chapters.py b/tests/test_chapters.py deleted file mode 100644 index 8bc8150..0000000 --- a/tests/test_chapters.py +++ /dev/null @@ -1,163 +0,0 @@ -import os -import pytest -from unittest.mock import patch, MagicMock -from storycraftr.agent.agents import create_message, create_or_get_assistant, get_thread -from storycraftr.utils.core import load_book_config -from storycraftr.agent.chapters import ( - generate_chapter, - generate_cover, - generate_back_cover, - generate_epilogue, - save_to_markdown, -) - - -# Test for generate_chapter -@patch("storycraftr.chapters.create_message") -@patch("storycraftr.chapters.get_thread") -@patch("storycraftr.chapters.create_or_get_assistant") -@patch("storycraftr.chapters.load_book_config") -@patch("os.path.exists") -@patch("storycraftr.chapters.save_to_markdown") -def test_generate_chapter( - mock_save, - mock_exists, - mock_load_book_config, - mock_assistant, - mock_thread, - mock_message, -): - # Mocks - mock_exists.return_value = False - mock_load_book_config.return_value = MagicMock(primary_language="en") - mock_message.return_value = "Generated Chapter Content" - mock_thread.return_value.id = "thread_id" - mock_assistant.return_value = "assistant_object" - - # Call function - result = generate_chapter("my_book", "A great chapter", 1) - - # Assertions - mock_exists.assert_called_once_with( - os.path.join("my_book", "chapters", "chapter-1.md") - ) - mock_message.assert_called_once_with( - thread_id="thread_id", - content="Write a detailed chapter for the following book premise: A great chapter. Write it in en.", - assistant="assistant_object", - ) - mock_save.assert_called_once_with( - "my_book", "chapter-1.md", "Chapter 1", "Generated Chapter Content" - ) - assert result == "Generated Chapter Content" - - -# Test for generate_cover -@patch("storycraftr.chapters.create_message") -@patch("storycraftr.chapters.get_thread") -@patch("storycraftr.chapters.create_or_get_assistant") -@patch("storycraftr.chapters.load_book_config") -@patch("storycraftr.chapters.save_to_markdown") -def test_generate_cover( - mock_save, mock_load_book_config, mock_assistant, mock_thread, mock_message -): - # Mocks - mock_load_book_config.return_value = MagicMock( - primary_language="en", - book_path="My Book", - default_author="Author Name", - genre="Science Fiction", - alternate_languages=["es"], - ) - mock_message.return_value = "Generated Cover Content" - mock_thread.return_value.id = "thread_id" - mock_assistant.return_value = "assistant_object" - - # Call function - result = generate_cover("my_book", "A beautiful cover") - - # Assertions - expected_prompt = ( - "Create a professional book cover in markdown format for the book titled 'My Book'. " - "Include the title, author (which is 'Author Name'), genre ('Science Fiction'), " - "and alternate languages ('es'). Use this information to format a typical " - "book cover with a detailed description. Use this prompt as additional context: A beautiful cover. " - "Write the content in en." - ) - mock_message.assert_called_once_with( - thread_id="thread_id", content=expected_prompt, assistant="assistant_object" - ) - mock_save.assert_called_once_with( - "my_book", "cover.md", "Cover", "Generated Cover Content" - ) - assert result == "Generated Cover Content" - - -# Test for generate_back_cover -@patch("storycraftr.chapters.create_message") -@patch("storycraftr.chapters.get_thread") -@patch("storycraftr.chapters.create_or_get_assistant") -@patch("storycraftr.chapters.load_book_config") -@patch("storycraftr.chapters.save_to_markdown") -def test_generate_back_cover( - mock_save, mock_load_book_config, mock_assistant, mock_thread, mock_message -): - # Mocks - mock_load_book_config.return_value = MagicMock(primary_language="en") - mock_message.return_value = "Generated Back Cover Content" - mock_thread.return_value.id = "thread_id" - mock_assistant.return_value = "assistant_object" - - # Call function - result = generate_back_cover("my_book", "A short synopsis") - - # Assertions - mock_message.assert_called_once_with( - thread_id="thread_id", - content="Generate a detailed synopsis for the back cover of the book based on this prompt: A short synopsis. Write it in en.", - assistant="assistant_object", - ) - mock_save.assert_called_once_with( - "my_book", "back_cover.md", "Back Cover", "Generated Back Cover Content" - ) - assert result == "Generated Back Cover Content" - - -# Test for generate_epilogue -@patch("storycraftr.chapters.create_message") -@patch("storycraftr.chapters.get_thread") -@patch("storycraftr.chapters.create_or_get_assistant") -@patch("storycraftr.chapters.load_book_config") -@patch("os.path.exists") -@patch("storycraftr.chapters.save_to_markdown") -def test_generate_epilogue( - mock_save, - mock_exists, - mock_load_book_config, - mock_assistant, - mock_thread, - mock_message, -): - # Mocks - mock_exists.return_value = False - mock_load_book_config.return_value = MagicMock(primary_language="en") - mock_message.return_value = "Generated Epilogue Content" - mock_thread.return_value.id = "thread_id" - mock_assistant.return_value = "assistant_object" - - # Call function - result = generate_epilogue("my_book", "A thrilling epilogue") - - # Assertions - mock_exists.assert_called_once_with( - os.path.join("my_book", "chapters", "epilogue.md") - ) - mock_message.assert_called_once_with( - thread_id="thread_id", - content="Generate the epilogue for the book based on this prompt: A thrilling epilogue. Write it in en.", - assistant="assistant_object", - ) - mock_save.assert_called_once_with( - "my_book", "epilogue.md", "Epilogue", "Generated Epilogue Content" - ) - assert result == "Generated Epilogue Content" diff --git a/tests/test_cli.py b/tests/test_cli.py new file mode 100644 index 0000000..5684a74 --- /dev/null +++ b/tests/test_cli.py @@ -0,0 +1,87 @@ +import os +import sys +import pytest +import requests +from unittest import mock +from pathlib import Path +from click import ClickException +from storycraftr.cli import ( + load_openai_api_key, + download_file, + verify_book_path, + is_initialized, + project_not_initialized_error, + init_structure, +) + + +# Mock the console object to avoid printing to the console during tests +@pytest.fixture +def mock_console(): + with mock.patch("storycraftr.cli.console") as mock_console: + yield mock_console + + +# Test download_file function +@mock.patch("requests.get") +def test_download_file_success(mock_get, mock_console): + mock_response = mock.Mock() + mock_response.text = "file content" + mock_response.raise_for_status = mock.Mock() + mock_get.return_value = mock_response + + save_dir = "test_dir" + filename = "test_file.txt" + + with mock.patch("pathlib.Path.mkdir") as mock_mkdir, mock.patch( + "pathlib.Path.write_text" + ) as mock_write: + download_file("http://example.com", save_dir, filename) + + mock_mkdir.assert_called_once_with(parents=True, exist_ok=True) + mock_write.assert_called_once_with("file content", encoding="utf-8") + mock_console.print.assert_called_with( + "[green]File downloaded successfully from http://example.com[/green]" + ) + + +@mock.patch("requests.get", side_effect=requests.exceptions.RequestException("Error")) +def test_download_file_failure(mock_get, mock_console): + with pytest.raises(SystemExit): + download_file("http://example.com", "test_dir", "test_file.txt") + + mock_console.print.assert_called_with( + "[red]Error downloading the file from http://example.com: Error[/red]" + ) + + +# Test verify_book_path function +@mock.patch("pathlib.Path.exists", return_value=True) +def test_verify_book_path_success(mock_console): # Se agrega el parámetro mock_console + assert verify_book_path("test_path") == "test_path" + + +@mock.patch("pathlib.Path.exists", return_value=False) +def test_verify_book_path_failure(mock_console): # Se agrega el parámetro mock_console + with pytest.raises(ClickException): + verify_book_path("invalid_path") + + +# Test is_initialized function +@mock.patch("pathlib.Path.exists", return_value=True) +def test_is_initialized_true(mock_console): # Se agrega el parámetro mock_console + assert is_initialized("test_path") + + +@mock.patch("pathlib.Path.exists", return_value=False) +def test_is_initialized_false(mock_console): # Se agrega el parámetro mock_console + assert not is_initialized("test_path") + + +# Test project_not_initialized_error function +def test_project_not_initialized_error(mock_console): + project_not_initialized_error("test_path") + + mock_console.print.assert_called_with( + "[red]✖ Project 'test_path' is not initialized. Run 'storycraftr init {book_path}' first.[/red]" + ) diff --git a/tests/test_markdown.py b/tests/test_markdown.py new file mode 100644 index 0000000..4506ef0 --- /dev/null +++ b/tests/test_markdown.py @@ -0,0 +1,110 @@ +import os +import pytest +import shutil +from unittest import mock +from storycraftr.utils.markdown import ( + save_to_markdown, + append_to_markdown, + read_from_markdown, + consolidate_book_md, +) + + +# Mocks comunes para todos los tests +@pytest.fixture +def mock_console(): + with mock.patch("storycraftr.cli.console") as mock_console: + yield mock_console + + +# Test para save_to_markdown +@mock.patch("shutil.copyfile") +@mock.patch("os.path.exists", return_value=True) +@mock.patch("builtins.open", new_callable=mock.mock_open) +def test_save_to_markdown_backup(mock_open, mock_exists, mock_copy, mock_console): + book_path = "test_book" + file_name = "test.md" + header = "Test Header" + content = "Test content" + + save_to_markdown(book_path, file_name, header, content) + + # Verificar que se realizó una copia de seguridad + mock_copy.assert_called_with( + os.path.join(book_path, file_name), os.path.join(book_path, file_name) + ".back" + ) + mock_open.assert_called_with( + os.path.join(book_path, file_name), "w", encoding="utf-8" + ) + mock_open().write.assert_called_with(f"# {header}\n\n{content}") + + +@mock.patch("os.path.exists", return_value=False) +@mock.patch("builtins.open", new_callable=mock.mock_open) +def test_save_to_markdown_no_backup(mock_open, mock_exists, mock_console): + book_path = "test_book" + file_name = "test.md" + header = "Test Header" + content = "Test content" + + save_to_markdown(book_path, file_name, header, content) + + # Verificar que no se hizo copia de seguridad + mock_open.assert_called_with( + os.path.join(book_path, file_name), "w", encoding="utf-8" + ) + mock_open().write.assert_called_with(f"# {header}\n\n{content}") + + +# Test para append_to_markdown +@mock.patch("os.path.exists", return_value=True) +@mock.patch("builtins.open", new_callable=mock.mock_open) +def test_append_to_markdown_success(mock_open, mock_exists, mock_console): + book_path = "test_book" + folder_name = "test_folder" + file_name = "test.md" + content = "Appended content" + + append_to_markdown(book_path, folder_name, file_name, content) + + mock_open.assert_called_with( + os.path.join(book_path, folder_name, file_name), "a", encoding="utf-8" + ) + mock_open().write.assert_called_with(f"\n\n{content}") + + +@mock.patch("os.path.exists", return_value=False) +def test_append_to_markdown_file_not_found(mock_console): + book_path = "test_book" + folder_name = "test_folder" + file_name = "test.md" + content = "Appended content" + + with pytest.raises(FileNotFoundError): + append_to_markdown(book_path, folder_name, file_name, content) + + +# Test para read_from_markdown +@mock.patch("os.path.exists", return_value=True) +@mock.patch("builtins.open", new_callable=mock.mock_open, read_data="File content") +def test_read_from_markdown_success(mock_open, mock_exists, mock_console): + book_path = "test_book" + folder_name = "test_folder" + file_name = "test.md" + + content = read_from_markdown(book_path, folder_name, file_name) + + mock_open.assert_called_with( + os.path.join(book_path, folder_name, file_name), "r", encoding="utf-8" + ) + assert content == "File content" + + +@mock.patch("os.path.exists", return_value=False) +def test_read_from_markdown_file_not_found(mock_console): + book_path = "test_book" + folder_name = "test_folder" + file_name = "test.md" + + with pytest.raises(FileNotFoundError): + read_from_markdown(book_path, folder_name, file_name) diff --git a/tests/test_outline.py b/tests/test_outline.py deleted file mode 100644 index b78b66b..0000000 --- a/tests/test_outline.py +++ /dev/null @@ -1,200 +0,0 @@ -import os -import pytest -from unittest.mock import patch, MagicMock -from storycraftr.agent.outline import ( - generate_general_outline, - generate_character_summary, - generate_plot_points, - generate_chapter_synopsis, - save_to_markdown, -) -from storycraftr.utils.core import load_book_config, file_has_more_than_three_lines - - -# Test for generate_general_outline -@patch("storycraftr.outline.create_message") -@patch("storycraftr.outline.get_thread") -@patch("storycraftr.outline.create_or_get_assistant") -@patch("storycraftr.outline.load_book_config") -@patch("storycraftr.outline.file_has_more_than_three_lines") -@patch("os.path.exists") -@patch("storycraftr.outline.save_to_markdown") -def test_generate_general_outline( - mock_save, - mock_exists, - mock_file_lines, - mock_load_book_config, - mock_assistant, - mock_thread, - mock_message, -): - # Mocks - mock_exists.return_value = False - mock_file_lines.return_value = False - mock_load_book_config.return_value = MagicMock(primary_language="en") - mock_message.return_value = "Generated General Outline Content" - mock_thread.return_value.id = "thread_id" - mock_assistant.return_value = "assistant_object" - - # Call function - result = generate_general_outline("my_book", "Outline for a new book") - - # Assertions - mock_exists.assert_called_once_with( - os.path.join("my_book", "outline", "general_outline.md") - ) - mock_message.assert_called_once_with( - thread_id="thread_id", - content="Create a general outline for a book based on this prompt: Outline for a new book. Write it in en.", - assistant="assistant_object", - ) - mock_save.assert_called_once_with( - "my_book", - "general_outline.md", - "General Outline", - "Generated General Outline Content", - ) - assert result == "Generated General Outline Content" - - -# Test for generate_character_summary -@patch("storycraftr.outline.create_message") -@patch("storycraftr.outline.get_thread") -@patch("storycraftr.outline.create_or_get_assistant") -@patch("storycraftr.outline.load_book_config") -@patch("storycraftr.outline.file_has_more_than_three_lines") -@patch("os.path.exists") -@patch("storycraftr.outline.save_to_markdown") -def test_generate_character_summary( - mock_save, - mock_exists, - mock_file_lines, - mock_load_book_config, - mock_assistant, - mock_thread, - mock_message, -): - # Mocks - mock_exists.return_value = True - mock_file_lines.return_value = True - mock_load_book_config.return_value = MagicMock(primary_language="en") - mock_message.return_value = "Generated Character Summary Content" - mock_thread.return_value.id = "thread_id" - mock_assistant.return_value = "assistant_object" - - # Call function - result = generate_character_summary("my_book", "Summary of main characters") - - # Assertions - mock_exists.assert_called_once_with( - os.path.join("my_book", "outline", "character_summary.md") - ) - mock_file_lines.assert_called_once_with( - os.path.join("my_book", "outline", "character_summary.md") - ) - mock_message.assert_called_once_with( - thread_id="thread_id", - content="Use the attached character summary file to evolve the content based on this prompt: Summary of main characters. Write it in en.", - assistant="assistant_object", - file_path=os.path.join("my_book", "outline", "character_summary.md"), - ) - mock_save.assert_called_once_with( - "my_book", - "character_summary.md", - "Character Summary", - "Generated Character Summary Content", - ) - assert result == "Generated Character Summary Content" - - -# Test for generate_plot_points -@patch("storycraftr.outline.create_message") -@patch("storycraftr.outline.get_thread") -@patch("storycraftr.outline.create_or_get_assistant") -@patch("storycraftr.outline.load_book_config") -@patch("storycraftr.outline.file_has_more_than_three_lines") -@patch("os.path.exists") -@patch("storycraftr.outline.save_to_markdown") -def test_generate_plot_points( - mock_save, - mock_exists, - mock_file_lines, - mock_load_book_config, - mock_assistant, - mock_thread, - mock_message, -): - # Mocks - mock_exists.return_value = False - mock_file_lines.return_value = False - mock_load_book_config.return_value = MagicMock(primary_language="en") - mock_message.return_value = "Generated Plot Points Content" - mock_thread.return_value.id = "thread_id" - mock_assistant.return_value = "assistant_object" - - # Call function - result = generate_plot_points("my_book", "Plot points for the story") - - # Assertions - mock_exists.assert_called_once_with( - os.path.join("my_book", "outline", "plot_points.md") - ) - mock_message.assert_called_once_with( - thread_id="thread_id", - content="Generate the main plot points for the book based on this prompt: Plot points for the story. Write it in en.", - assistant="assistant_object", - ) - mock_save.assert_called_once_with( - "my_book", "plot_points.md", "Main Plot Points", "Generated Plot Points Content" - ) - assert result == "Generated Plot Points Content" - - -# Test for generate_chapter_synopsis -@patch("storycraftr.outline.create_message") -@patch("storycraftr.outline.get_thread") -@patch("storycraftr.outline.create_or_get_assistant") -@patch("storycraftr.outline.load_book_config") -@patch("storycraftr.outline.file_has_more_than_three_lines") -@patch("os.path.exists") -@patch("storycraftr.outline.save_to_markdown") -def test_generate_chapter_synopsis( - mock_save, - mock_exists, - mock_file_lines, - mock_load_book_config, - mock_assistant, - mock_thread, - mock_message, -): - # Mocks - mock_exists.return_value = True - mock_file_lines.return_value = True - mock_load_book_config.return_value = MagicMock(primary_language="en") - mock_message.return_value = "Generated Chapter Synopsis Content" - mock_thread.return_value.id = "thread_id" - mock_assistant.return_value = "assistant_object" - - # Call function - result = generate_chapter_synopsis("my_book", "Chapter by chapter synopsis") - - # Assertions - mock_exists.assert_called_once_with( - os.path.join("my_book", "outline", "chapter_synopsis.md") - ) - mock_file_lines.assert_called_once_with( - os.path.join("my_book", "outline", "chapter_synopsis.md") - ) - mock_message.assert_called_once_with( - thread_id="thread_id", - content="Use the attached chapter-by-chapter synopsis file to evolve the content based on this prompt: Chapter by chapter synopsis. Write it in en.", - assistant="assistant_object", - file_path=os.path.join("my_book", "outline", "chapter_synopsis.md"), - ) - mock_save.assert_called_once_with( - "my_book", - "chapter_synopsis.md", - "Chapter Synopsis", - "Generated Chapter Synopsis Content", - ) - assert result == "Generated Chapter Synopsis Content" diff --git a/tests/test_storycraftr.py b/tests/test_state.py similarity index 100% rename from tests/test_storycraftr.py rename to tests/test_state.py diff --git a/tests/test_worldbuilding.py b/tests/test_worldbuilding.py deleted file mode 100644 index 20555cd..0000000 --- a/tests/test_worldbuilding.py +++ /dev/null @@ -1,238 +0,0 @@ -import os -import pytest -from unittest.mock import patch, MagicMock -from storycraftr.agent.worldbuilding import ( - generate_geography, - generate_history, - generate_culture, - generate_magic_system, - generate_technology, - save_to_markdown, -) -from storycraftr.utils.core import load_book_config, file_has_more_than_three_lines - - -# Test for generate_geography -@patch("storycraftr.worldbuilding.create_message") -@patch("storycraftr.worldbuilding.get_thread") -@patch("storycraftr.worldbuilding.create_or_get_assistant") -@patch("storycraftr.worldbuilding.load_book_config") -@patch("storycraftr.worldbuilding.file_has_more_than_three_lines") -@patch("os.path.exists") -@patch("storycraftr.worldbuilding.save_to_markdown") -def test_generate_geography( - mock_save, - mock_exists, - mock_file_lines, - mock_load_book_config, - mock_assistant, - mock_thread, - mock_message, -): - # Mocks - mock_exists.return_value = False - mock_file_lines.return_value = False - mock_load_book_config.return_value = MagicMock(primary_language="en") - mock_message.return_value = "Generated Geography Content" - mock_thread.return_value.id = "thread_id" - mock_assistant.return_value = "assistant_object" - - # Call function - result = generate_geography("my_book", "Geography of a fantastic world") - - # Assertions - mock_exists.assert_called_once_with( - os.path.join("my_book", "worldbuilding", "geography.md") - ) - mock_message.assert_called_once_with( - thread_id="thread_id", - content="Generate the geography details for the book's world based on this prompt: Geography of a fantastic world. Write it in en.", - assistant="assistant_object", - ) - mock_save.assert_called_once_with( - "my_book", "geography.md", "Geography", "Generated Geography Content" - ) - assert result == "Generated Geography Content" - - -# Test for generate_history -@patch("storycraftr.worldbuilding.create_message") -@patch("storycraftr.worldbuilding.get_thread") -@patch("storycraftr.worldbuilding.create_or_get_assistant") -@patch("storycraftr.worldbuilding.load_book_config") -@patch("storycraftr.worldbuilding.file_has_more_than_three_lines") -@patch("os.path.exists") -@patch("storycraftr.worldbuilding.save_to_markdown") -def test_generate_history( - mock_save, - mock_exists, - mock_file_lines, - mock_load_book_config, - mock_assistant, - mock_thread, - mock_message, -): - # Mocks - mock_exists.return_value = True - mock_file_lines.return_value = True - mock_load_book_config.return_value = MagicMock(primary_language="en") - mock_message.return_value = "Generated History Content" - mock_thread.return_value.id = "thread_id" - mock_assistant.return_value = "assistant_object" - - # Call function - result = generate_history("my_book", "Ancient history of the world") - - # Assertions - mock_exists.assert_called_once_with( - os.path.join("my_book", "worldbuilding", "history.md") - ) - mock_file_lines.assert_called_once_with( - os.path.join("my_book", "worldbuilding", "history.md") - ) - mock_message.assert_called_once_with( - thread_id="thread_id", - content="Use the attached history file to evolve the content based on this prompt: Ancient history of the world. Write it in en.", - assistant="assistant_object", - file_path=os.path.join("my_book", "worldbuilding", "history.md"), - ) - mock_save.assert_called_once_with( - "my_book", "history.md", "History", "Generated History Content" - ) - assert result == "Generated History Content" - - -# Test for generate_culture -@patch("storycraftr.worldbuilding.create_message") -@patch("storycraftr.worldbuilding.get_thread") -@patch("storycraftr.worldbuilding.create_or_get_assistant") -@patch("storycraftr.worldbuilding.load_book_config") -@patch("storycraftr.worldbuilding.file_has_more_than_three_lines") -@patch("os.path.exists") -@patch("storycraftr.worldbuilding.save_to_markdown") -def test_generate_culture( - mock_save, - mock_exists, - mock_file_lines, - mock_load_book_config, - mock_assistant, - mock_thread, - mock_message, -): - # Mocks - mock_exists.return_value = False - mock_file_lines.return_value = False - mock_load_book_config.return_value = MagicMock(primary_language="en") - mock_message.return_value = "Generated Culture Content" - mock_thread.return_value.id = "thread_id" - mock_assistant.return_value = "assistant_object" - - # Call function - result = generate_culture("my_book", "Culture of the people") - - # Assertions - mock_exists.assert_called_once_with( - os.path.join("my_book", "worldbuilding", "culture.md") - ) - mock_message.assert_called_once_with( - thread_id="thread_id", - content="Generate the culture details for the book's world based on this prompt: Culture of the people. Write it in en.", - assistant="assistant_object", - ) - mock_save.assert_called_once_with( - "my_book", "culture.md", "Culture", "Generated Culture Content" - ) - assert result == "Generated Culture Content" - - -# Test for generate_magic_system -@patch("storycraftr.worldbuilding.create_message") -@patch("storycraftr.worldbuilding.get_thread") -@patch("storycraftr.worldbuilding.create_or_get_assistant") -@patch("storycraftr.worldbuilding.load_book_config") -@patch("storycraftr.worldbuilding.file_has_more_than_three_lines") -@patch("os.path.exists") -@patch("storycraftr.worldbuilding.save_to_markdown") -def test_generate_magic_system( - mock_save, - mock_exists, - mock_file_lines, - mock_load_book_config, - mock_assistant, - mock_thread, - mock_message, -): - # Mocks - mock_exists.return_value = True - mock_file_lines.return_value = True - mock_load_book_config.return_value = MagicMock(primary_language="en") - mock_message.return_value = "Generated Magic System Content" - mock_thread.return_value.id = "thread_id" - mock_assistant.return_value = "assistant_object" - - # Call function - result = generate_magic_system("my_book", "Complex magic system") - - # Assertions - mock_exists.assert_called_once_with( - os.path.join("my_book", "worldbuilding", "magic_system.md") - ) - mock_file_lines.assert_called_once_with( - os.path.join("my_book", "worldbuilding", "magic_system.md") - ) - mock_message.assert_called_once_with( - thread_id="thread_id", - content="Use the attached magic/science system file to evolve the content based on this prompt: Complex magic system. Write it in en.", - assistant="assistant_object", - file_path=os.path.join("my_book", "worldbuilding", "magic_system.md"), - ) - mock_save.assert_called_once_with( - "my_book", - "magic_system.md", - "Magic/Science System", - "Generated Magic System Content", - ) - assert result == "Generated Magic System Content" - - -# Test for generate_technology -@patch("storycraftr.worldbuilding.create_message") -@patch("storycraftr.worldbuilding.get_thread") -@patch("storycraftr.worldbuilding.create_or_get_assistant") -@patch("storycraftr.worldbuilding.load_book_config") -@patch("storycraftr.worldbuilding.file_has_more_than_three_lines") -@patch("os.path.exists") -@patch("storycraftr.worldbuilding.save_to_markdown") -def test_generate_technology( - mock_save, - mock_exists, - mock_file_lines, - mock_load_book_config, - mock_assistant, - mock_thread, - mock_message, -): - # Mocks - mock_exists.return_value = False - mock_file_lines.return_value = False - mock_load_book_config.return_value = MagicMock(primary_language="en") - mock_message.return_value = "Generated Technology Content" - mock_thread.return_value.id = "thread_id" - mock_assistant.return_value = "assistant_object" - - # Call function - result = generate_technology("my_book", "Advanced technology of the world") - - # Assertions - mock_exists.assert_called_once_with( - os.path.join("my_book", "worldbuilding", "technology.md") - ) - mock_message.assert_called_once_with( - thread_id="thread_id", - content="Generate the technology details for the book's world based on this prompt: Advanced technology of the world. Write it in en.", - assistant="assistant_object", - ) - mock_save.assert_called_once_with( - "my_book", "technology.md", "Technology", "Generated Technology Content" - ) - assert result == "Generated Technology Content"