mirror of
https://github.com/mealie-recipes/mealie.git
synced 2026-01-31 04:53:11 -05:00
Co-authored-by: Michael Genson <71845777+michael-genson@users.noreply.github.com> Co-authored-by: Michael Genson <genson.michael@gmail.com>
This commit is contained in:
77
tests/unit_tests/services_tests/test_openai_service.py
Normal file
77
tests/unit_tests/services_tests/test_openai_service.py
Normal file
@@ -0,0 +1,77 @@
|
||||
import pytest
|
||||
|
||||
import mealie.services.openai.openai as openai_module
|
||||
from mealie.services.openai.openai import OpenAIService
|
||||
|
||||
|
||||
class _SettingsStub:
|
||||
OPENAI_ENABLED = True
|
||||
OPENAI_MODEL = "gpt-4o"
|
||||
OPENAI_WORKERS = 1
|
||||
OPENAI_SEND_DATABASE_DATA = False
|
||||
OPENAI_ENABLE_IMAGE_SERVICES = True
|
||||
OPENAI_CUSTOM_PROMPT_DIR = None
|
||||
OPENAI_BASE_URL = None
|
||||
OPENAI_API_KEY = "dummy"
|
||||
OPENAI_REQUEST_TIMEOUT = 30
|
||||
OPENAI_CUSTOM_HEADERS = {}
|
||||
OPENAI_CUSTOM_PARAMS = {}
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def settings_stub(tmp_path, monkeypatch):
|
||||
s = _SettingsStub()
|
||||
|
||||
prompts_dir = tmp_path / "prompts"
|
||||
(prompts_dir / "recipes").mkdir(parents=True)
|
||||
default_prompt = prompts_dir / "recipes" / "parse-recipe-ingredients.txt"
|
||||
default_prompt.write_text("DEFAULT PROMPT")
|
||||
|
||||
monkeypatch.setattr(OpenAIService, "PROMPTS_DIR", prompts_dir)
|
||||
|
||||
def _fake_get_app_settings():
|
||||
return s
|
||||
|
||||
monkeypatch.setattr(openai_module, "get_app_settings", _fake_get_app_settings)
|
||||
return s
|
||||
|
||||
|
||||
def test_get_prompt_default_only(settings_stub):
|
||||
svc = OpenAIService()
|
||||
out = svc.get_prompt("recipes.parse-recipe-ingredients")
|
||||
assert out == "DEFAULT PROMPT"
|
||||
|
||||
|
||||
def test_get_prompt_custom_dir_used(settings_stub, tmp_path):
|
||||
custom_dir = tmp_path / "custom"
|
||||
(custom_dir / "recipes").mkdir(parents=True)
|
||||
(custom_dir / "recipes" / "parse-recipe-ingredients.txt").write_text("CUSTOM PROMPT")
|
||||
|
||||
settings_stub.OPENAI_CUSTOM_PROMPT_DIR = str(custom_dir)
|
||||
|
||||
svc = OpenAIService()
|
||||
out = svc.get_prompt("recipes.parse-recipe-ingredients")
|
||||
assert out == "CUSTOM PROMPT"
|
||||
|
||||
|
||||
def test_get_prompt_custom_empty_falls_back_to_default(settings_stub, tmp_path):
|
||||
custom_dir = tmp_path / "custom"
|
||||
(custom_dir / "recipes").mkdir(parents=True)
|
||||
(custom_dir / "recipes" / "parse-recipe-ingredients.txt").write_text("")
|
||||
|
||||
settings_stub.OPENAI_CUSTOM_PROMPT_DIR = str(custom_dir)
|
||||
svc = OpenAIService()
|
||||
out = svc.get_prompt("recipes.parse-recipe-ingredients")
|
||||
assert out == "DEFAULT PROMPT"
|
||||
|
||||
|
||||
def test_get_prompt_raises_when_no_files(settings_stub, monkeypatch):
|
||||
# Point PROMPTS_DIR to an empty temp folder (already done in fixture) but remove default file
|
||||
prompts_dir = OpenAIService.PROMPTS_DIR
|
||||
for p in prompts_dir.rglob("*.txt"):
|
||||
p.unlink()
|
||||
|
||||
svc = OpenAIService()
|
||||
with pytest.raises(OSError) as ei:
|
||||
svc.get_prompt("recipes.parse-recipe-ingredients")
|
||||
assert "Unable to load prompt" in str(ei.value)
|
||||
Reference in New Issue
Block a user