|
| 1 | +""" |
| 2 | +Smoke test for the LLM_FEATURES_DISABLED env var. |
| 3 | +
|
| 4 | +The env var is intended to hide every LLM/AI surface (settings tab, edit tab, |
| 5 | +base-template AI toggle/modal) for hosted deployments. This test renders the |
| 6 | +three primary pages with the env var set and verifies that none of the |
| 7 | +LLM-related markers leak through. |
| 8 | +""" |
| 9 | +from flask import url_for |
| 10 | + |
| 11 | + |
| 12 | +def _llm_markers_absent(body: bytes, where: str = ''): |
| 13 | + """All of these strings appear in LLM UI surfaces — none should render.""" |
| 14 | + for marker in (b'AI / LLM', b'toggle-ai-mode', b'llm-not-configured-modal', |
| 15 | + b'id="ai-llm"', b'#ai-llm', b'href="#ai"'): |
| 16 | + if marker in body: |
| 17 | + idx = body.find(marker) |
| 18 | + context = body[max(0, idx - 80):idx + len(marker) + 80].decode('utf-8', 'replace') |
| 19 | + raise AssertionError(f"[{where}] {marker!r} found in body, context: ...{context}...") |
| 20 | + |
| 21 | + |
| 22 | +def test_llm_features_disabled_hides_ui(client, live_server, monkeypatch): |
| 23 | + monkeypatch.setenv('LLM_FEATURES_DISABLED', 'true') |
| 24 | + |
| 25 | + # Sanity: helper reports the env var is in effect |
| 26 | + from changedetectionio.llm.evaluator import is_llm_features_disabled, get_llm_config |
| 27 | + assert is_llm_features_disabled() is True |
| 28 | + # get_llm_config() must return None so every `if llm_configured` template hides |
| 29 | + datastore = client.application.config.get('DATASTORE') |
| 30 | + assert get_llm_config(datastore) is None |
| 31 | + |
| 32 | + # 1. Watch list (base.html + menu.html surface) |
| 33 | + res = client.get(url_for('watchlist.index')) |
| 34 | + assert res.status_code == 200 |
| 35 | + _llm_markers_absent(res.data, where='watchlist') |
| 36 | + |
| 37 | + # 2. Settings page (should not have an AI / LLM tab or the LLM tab body) |
| 38 | + res = client.get(url_for('settings.settings_page')) |
| 39 | + assert res.status_code == 200 |
| 40 | + _llm_markers_absent(res.data, where='settings') |
| 41 | + |
| 42 | + # 3. Edit page for a watch (should not have an AI / LLM tab or include_llm_intent body) |
| 43 | + uuid = datastore.add_watch(url='http://example.com', extras={'title': 'Disabled LLM watch'}) |
| 44 | + res = client.get(url_for('ui.ui_edit.edit_page', uuid=uuid)) |
| 45 | + assert res.status_code == 200 |
| 46 | + _llm_markers_absent(res.data, where='edit') |
| 47 | + # The watch-edit-only intent textarea should also be absent |
| 48 | + assert b'name="llm_intent"' not in res.data |
| 49 | + assert b'name="llm_change_summary"' not in res.data |
| 50 | + |
| 51 | + |
| 52 | +def test_llm_features_enabled_by_default(client, live_server, monkeypatch): |
| 53 | + """When LLM_FEATURES_DISABLED is unset, the AI / LLM surfaces are still rendered.""" |
| 54 | + monkeypatch.delenv('LLM_FEATURES_DISABLED', raising=False) |
| 55 | + |
| 56 | + from changedetectionio.llm.evaluator import is_llm_features_disabled |
| 57 | + assert is_llm_features_disabled() is False |
| 58 | + |
| 59 | + res = client.get(url_for('settings.settings_page')) |
| 60 | + assert res.status_code == 200 |
| 61 | + # The AI / LLM settings tab anchor should be present when not disabled |
| 62 | + assert b'href="#ai"' in res.data |
0 commit comments