shinkan-jinkendo/backend/tests/test_planning_llm_usage.py
Lars 9cee862c32
All checks were successful
Deploy Development / deploy (push) Successful in 47s
Test Suite / pytest-backend (push) Successful in 49s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 15s
Test Suite / k6 /health Baseline (push) Successful in 34s
Test Suite / playwright-tests (push) Successful in 1m26s
Implement Planning Prompt Enhancements and LLM Usage Tracking
- Added new fields for goal query, user notes, max steps, and search query in the AiPromptPreviewBody to support planning prompts.
- Integrated planning prompt handling in the preview_ai_prompt function, allowing for distinct processing of planning and exercise prompts.
- Introduced LLM usage tracking in openrouter_chat_completion and planning_exercise_suggest functions to monitor AI call metrics.
- Updated frontend components to accommodate new input fields for planning prompts, enhancing user experience and functionality.
2026-06-15 07:50:49 +02:00

95 lines
3.0 KiB
Python

"""LLM-Zählung für Planungs-APIs (P1-C2)."""
from unittest.mock import MagicMock, patch
import pytest
from planning_llm_usage import (
current_planning_llm_call_counter,
planning_llm_call_meter,
record_planning_llm_call,
)
def test_meter_inactive_by_default():
assert current_planning_llm_call_counter() is None
record_planning_llm_call(3)
assert current_planning_llm_call_counter() is None
def test_meter_counts_within_scope():
with planning_llm_call_meter() as meter:
record_planning_llm_call(1)
record_planning_llm_call(2)
assert meter.count == 3
def test_openrouter_increments_active_meter():
from openrouter_chat import openrouter_chat_completion
fake_resp = MagicMock()
fake_resp.status_code = 200
fake_resp.json.return_value = {
"choices": [{"message": {"content": "ok"}, "finish_reason": "stop"}],
}
with planning_llm_call_meter() as meter:
with patch("openrouter_chat.httpx.Client") as client_cls:
client = MagicMock()
client.__enter__.return_value = client
client.post.return_value = fake_resp
client_cls.return_value = client
out = openrouter_chat_completion(
api_key="test-key",
model="test/model",
user_content="hello",
)
assert out == "ok"
assert meter.count == 1
def test_openrouter_skips_meter_on_http_error():
from openrouter_chat import OpenRouterError, openrouter_chat_completion
fake_resp = MagicMock()
fake_resp.status_code = 500
fake_resp.json.return_value = {"error": {"message": "fail"}}
fake_resp.text = "fail"
with planning_llm_call_meter() as meter:
with patch("openrouter_chat.httpx.Client") as client_cls:
client = MagicMock()
client.__enter__.return_value = client
client.post.return_value = fake_resp
client_cls.return_value = client
with pytest.raises(OpenRouterError):
openrouter_chat_completion(
api_key="test-key",
model="test/model",
user_content="hello",
)
assert meter.count == 0
def test_uses_ai_gap_fill_not_counted_without_openrouter():
"""Regression: Gap-Fill-Flag allein löst keinen OpenRouter-Aufruf aus."""
from planning_exercise_path_builder import ProgressionPathSuggestRequest
body = ProgressionPathSuggestRequest(
query="Mae Geri Progression",
include_llm_intent=False,
include_llm_path_qa=False,
include_llm_roadmap=False,
include_llm_start_target=False,
include_ai_gap_fill=True,
evaluate_only=True,
evaluate_steps=[],
)
uses_ai = (
body.include_llm_intent
or body.include_llm_path_qa
or body.include_llm_roadmap
or body.include_llm_start_target
or (body.start_target_only and body.include_llm_start_target)
)
assert uses_ai is False