|
| 1 | +import json |
| 2 | + |
| 3 | +from main import LOGOS_BASE_URL, LOGOS_MODEL, OPENAI_MODEL |
| 4 | + |
| 5 | +from tests.conftest import _fake_response |
| 6 | + |
| 7 | + |
| 8 | +def _set_llm_content(mock_openai_client, content): |
| 9 | + mock_openai_client.return_value.chat.completions.create.return_value = ( |
| 10 | + _fake_response(content) |
| 11 | + ) |
| 12 | + |
| 13 | + |
| 14 | +def test_happy_path_returns_ingredients(client, mock_openai_client, sample_ingredient_json): |
| 15 | + _set_llm_content(mock_openai_client, sample_ingredient_json) |
| 16 | + response = client.post("/api/ai/parse", json={"dish": "Pancakes"}) |
| 17 | + assert response.status_code == 200 |
| 18 | + body = response.json() |
| 19 | + assert body["dish"] == "Pancakes" |
| 20 | + assert len(body["ingredients"]) == 2 |
| 21 | + |
| 22 | + |
| 23 | +def test_restricted_and_alternative_fields_roundtrip( |
| 24 | + client, mock_openai_client, sample_ingredient_json |
| 25 | +): |
| 26 | + _set_llm_content(mock_openai_client, sample_ingredient_json) |
| 27 | + response = client.post("/api/ai/parse", json={"dish": "Pancakes"}) |
| 28 | + milk = next(i for i in response.json()["ingredients"] if i["name"] == "milk") |
| 29 | + assert milk["restricted"] is True |
| 30 | + assert milk["alternative"] == "oat milk" |
| 31 | + |
| 32 | + |
| 33 | +def test_malformed_llm_json_returns_500(client, mock_openai_client): |
| 34 | + _set_llm_content(mock_openai_client, "not json at all") |
| 35 | + response = client.post("/api/ai/parse", json={"dish": "Pancakes"}) |
| 36 | + assert response.status_code == 500 |
| 37 | + assert "Failed to parse LLM response" in response.json()["detail"] |
| 38 | + |
| 39 | + |
| 40 | +def test_missing_ingredients_key_returns_500(client, mock_openai_client): |
| 41 | + _set_llm_content(mock_openai_client, json.dumps({"foo": "bar"})) |
| 42 | + response = client.post("/api/ai/parse", json={"dish": "Pancakes"}) |
| 43 | + assert response.status_code == 500 |
| 44 | + assert "Failed to parse LLM response" in response.json()["detail"] |
| 45 | + |
| 46 | + |
| 47 | +def test_openai_error_returns_502(client, mock_openai_client): |
| 48 | + from openai import OpenAIError |
| 49 | + |
| 50 | + mock_openai_client.return_value.chat.completions.create.side_effect = OpenAIError( |
| 51 | + "boom" |
| 52 | + ) |
| 53 | + response = client.post( |
| 54 | + "/api/ai/parse", json={"dish": "Pancakes", "llm_provider": "openai"} |
| 55 | + ) |
| 56 | + assert response.status_code == 502 |
| 57 | + assert "openai error" in response.json()["detail"] |
| 58 | + |
| 59 | + |
| 60 | +def test_missing_api_key_returns_500(client, monkeypatch): |
| 61 | + monkeypatch.delenv("OPENAI_API_KEY", raising=False) |
| 62 | + response = client.post( |
| 63 | + "/api/ai/parse", json={"dish": "Pancakes", "llm_provider": "openai"} |
| 64 | + ) |
| 65 | + assert response.status_code == 500 |
| 66 | + assert "OPENAI_API_KEY" in response.json()["detail"] |
| 67 | + |
| 68 | + |
| 69 | +def test_default_provider_is_logos(client, mock_openai_client, sample_ingredient_json): |
| 70 | + _set_llm_content(mock_openai_client, sample_ingredient_json) |
| 71 | + client.post("/api/ai/parse", json={"dish": "Pancakes"}) |
| 72 | + _, kwargs = mock_openai_client.return_value.chat.completions.create.call_args |
| 73 | + assert kwargs["model"] == LOGOS_MODEL |
| 74 | + assert "response_format" not in kwargs |
| 75 | + _, client_kwargs = mock_openai_client.call_args |
| 76 | + assert client_kwargs["base_url"] == LOGOS_BASE_URL |
| 77 | + |
| 78 | + |
| 79 | +def test_openai_provider_selected(client, mock_openai_client, sample_ingredient_json): |
| 80 | + _set_llm_content(mock_openai_client, sample_ingredient_json) |
| 81 | + client.post( |
| 82 | + "/api/ai/parse", json={"dish": "Pancakes", "llm_provider": "openai"} |
| 83 | + ) |
| 84 | + _, kwargs = mock_openai_client.return_value.chat.completions.create.call_args |
| 85 | + assert kwargs["model"] == OPENAI_MODEL |
| 86 | + assert kwargs["response_format"] == {"type": "json_object"} |
| 87 | + |
| 88 | + |
| 89 | +def test_dietary_restrictions_passed_into_system_prompt( |
| 90 | + client, mock_openai_client, sample_ingredient_json |
| 91 | +): |
| 92 | + _set_llm_content(mock_openai_client, sample_ingredient_json) |
| 93 | + client.post( |
| 94 | + "/api/ai/parse", |
| 95 | + json={"dish": "Pancakes", "dietary_restrictions": ["Vegan"]}, |
| 96 | + ) |
| 97 | + _, kwargs = mock_openai_client.return_value.chat.completions.create.call_args |
| 98 | + system_message = next(m for m in kwargs["messages"] if m["role"] == "system") |
| 99 | + assert "animal product" in system_message["content"] |
0 commit comments