22# SPDX-License-Identifier: LicenseRef-GoodData-Enterprise
33import os
44import sys
5+ import types
56from unittest .mock import MagicMock , patch
67
78import pytest
@@ -25,6 +26,39 @@ def test_normalize_maql_removes_select_wrapper():
2526 assert _normalize_maql ("(SELECT {metric/abc})" ) == "{metric/abc}"
2627
2728
29+ def test_generate_simulated_response_prompt_preserves_maql_fidelity (monkeypatch ):
30+ """Regression test for a live-reproduced bug: the old prompt ("reply briefly",
31+ no instruction to cover clauses the assistant didn't ask about) let the
32+ simulating LLM silently drop a MAQL's WHERE clause or paraphrase a label id --
33+ confirmed via a 5x-repeated A/B test (1/5 vs 5/5 fidelity) that this was the
34+ prompt, not the model (gpt-4o did not fix it under the old prompt either).
35+ """
36+ monkeypatch .setenv ("OPENAI_API_KEY" , "test-key" )
37+ mock_client = MagicMock ()
38+ mock_response = MagicMock ()
39+ mock_response .choices = [MagicMock (message = MagicMock (content = "ok" ))]
40+ mock_client .chat .completions .create .return_value = mock_response
41+
42+ # `openai` is an optional [llm-judge] extra, not installed in this test env --
43+ # inject a fake module rather than patching a real one (mirrors how the source
44+ # itself does `from openai import OpenAI` as a local, guarded import).
45+ fake_openai_module = types .SimpleNamespace (OpenAI = MagicMock (return_value = mock_client ), OpenAIError = Exception )
46+ monkeypatch .setitem (sys .modules , "openai" , fake_openai_module )
47+
48+ expected_output = {"maql" : 'SELECT {metric/spend_amount_-_cutcgco} WHERE {label/ecommerce_indicator_code} = "1"' }
49+ generate_simulated_response ("Which base metric should I use?" , expected_output )
50+
51+ call_kwargs = mock_client .chat .completions .create .call_args .kwargs
52+ sent_prompt = call_kwargs ["messages" ][0 ]["content" ]
53+
54+ assert expected_output ["maql" ] in sent_prompt
55+ assert "verbatim" in sent_prompt
56+ assert "every clause" in sent_prompt
57+ assert "WHERE" in sent_prompt or "filter" in sent_prompt .lower ()
58+ assert "reply briefly" not in sent_prompt .lower ()
59+ assert call_kwargs ["max_tokens" ] >= 300
60+
61+
2862def test_metric_run_result_fields ():
2963 r = MetricRunResult (
3064 conversation_id = "c1" ,
0 commit comments