from types import SimpleNamespace import pytest from app.ai_tasks.prompts import ( get_effective_prompt, list_effective_prompts, reset_prompt_override, save_prompt_override, ) class _ScalarResult: def __init__(self, value): self._value = value def scalar_one_or_none(self): return self._value class _PromptSettingsDB: def __init__(self, payload=None): self.record = SimpleNamespace(category="ai_prompts", payload=payload) if payload is not None else None self.added = None self.commits = 0 async def execute(self, _statement): return _ScalarResult(self.record) def add(self, record): self.record = record self.added = record async def commit(self): self.commits += 1 @pytest.mark.asyncio async def test_prompt_defaults_are_loaded_without_override(): db = _PromptSettingsDB() prompt = await get_effective_prompt(db, "earth.news.enrich") assert prompt.key == "earth.news.enrich" assert prompt.is_custom is False assert "strict JSON" in prompt.prompt @pytest.mark.asyncio async def test_prompt_override_save_and_reset(): db = _PromptSettingsDB() saved = await save_prompt_override( db, "alerts.brief", system_prompt="system custom", prompt="prompt custom", ) assert saved.is_custom is True assert saved.system_prompt == "system custom" assert saved.prompt == "prompt custom" assert db.commits == 1 effective = await get_effective_prompt(db, "alerts.brief") assert effective.prompt == "prompt custom" reset = await reset_prompt_override(db, "alerts.brief") assert reset.is_custom is False assert reset.prompt != "prompt custom" @pytest.mark.asyncio async def test_prompt_list_marks_custom_items(): db = _PromptSettingsDB( { "overrides": { "bgp.brief": { "system_prompt": "", "prompt": "custom bgp prompt", "updated_at": "2026-05-16T00:00:00Z", } } } ) prompts = await list_effective_prompts(db) by_key = {prompt.key: prompt for prompt in prompts} assert by_key["bgp.brief"].is_custom is True assert by_key["bgp.brief"].prompt == "custom bgp prompt" assert by_key["earth.news.enrich"].is_custom is False