Spaces:
Running
Running
feat: add dream analysis service and accompanying unit tests
Browse files- app/services/dream_analyzer.py +1 -1
- tests/test_analyze.py +6 -2
- tests/test_commend.py +24 -5
app/services/dream_analyzer.py
CHANGED
|
@@ -145,7 +145,7 @@ JSON:"""
|
|
| 145 |
# from _get_client() reads "..._api_key required" (underscore), and
|
| 146 |
# Gemini's own errors tend to read "API key not valid" (space), so
|
| 147 |
# a plain substring check on either form alone misses the other.
|
| 148 |
-
if "not configured" in err:
|
| 149 |
return None, "Dream analysis is not configured"
|
| 150 |
if "api key" in err or "api_key" in err or "apikey" in err:
|
| 151 |
logger.error("Gemini refused the configured API key; replace DREAMS_GEMINI_API_KEY / COMMEND_GEMINI_API_KEY")
|
|
|
|
| 145 |
# from _get_client() reads "..._api_key required" (underscore), and
|
| 146 |
# Gemini's own errors tend to read "API key not valid" (space), so
|
| 147 |
# a plain substring check on either form alone misses the other.
|
| 148 |
+
if "not configured" in err or "required" in err:
|
| 149 |
return None, "Dream analysis is not configured"
|
| 150 |
if "api key" in err or "api_key" in err or "apikey" in err:
|
| 151 |
logger.error("Gemini refused the configured API key; replace DREAMS_GEMINI_API_KEY / COMMEND_GEMINI_API_KEY")
|
tests/test_analyze.py
CHANGED
|
@@ -265,8 +265,12 @@ def test_analyze_file_real_audio(
|
|
| 265 |
assert result["decisionSource"].startswith("auris_")
|
| 266 |
assert result["modelVersion"] == "auris-v1-fusion"
|
| 267 |
|
| 268 |
-
# Confidence
|
| 269 |
-
assert 0.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
|
| 271 |
# Features should have real values
|
| 272 |
feat = result["features"]
|
|
|
|
| 265 |
assert result["decisionSource"].startswith("auris_")
|
| 266 |
assert result["modelVersion"] == "auris-v1-fusion"
|
| 267 |
|
| 268 |
+
# Confidence is calibrated P(AI) within the schema bounds [0.03, 0.97]
|
| 269 |
+
assert 0.03 <= result["confidence"] <= 0.97
|
| 270 |
+
if result.get("xai") is not None:
|
| 271 |
+
assert result["isAIGenerated"] == (
|
| 272 |
+
result["xai"]["probability"] >= result["xai"]["threshold"]
|
| 273 |
+
)
|
| 274 |
|
| 275 |
# Features should have real values
|
| 276 |
feat = result["features"]
|
tests/test_commend.py
CHANGED
|
@@ -82,9 +82,29 @@ def test_commend_post_error_envelope_format(client: TestClient) -> None:
|
|
| 82 |
assert "message" in detail
|
| 83 |
|
| 84 |
|
| 85 |
-
def test_commend_auth_fail_closed_without_key(
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
"/api/commend/generate",
|
| 89 |
json={
|
| 90 |
"videoUrl": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
|
|
@@ -92,8 +112,7 @@ def test_commend_auth_fail_closed_without_key(client: TestClient) -> None:
|
|
| 92 |
"commentStyle": "supportive",
|
| 93 |
},
|
| 94 |
)
|
| 95 |
-
|
| 96 |
-
assert response.status_code in (429, 503)
|
| 97 |
|
| 98 |
|
| 99 |
def test_commend_rate_limit_independent_of_auth(client: TestClient) -> None:
|
|
|
|
| 82 |
assert "message" in detail
|
| 83 |
|
| 84 |
|
| 85 |
+
def test_commend_auth_fail_closed_without_key(
|
| 86 |
+
client: TestClient,
|
| 87 |
+
monkeypatch,
|
| 88 |
+
) -> None:
|
| 89 |
+
"""Posting is always fail-closed without COMMEND_API_KEY, and generate fails closed when auth is required."""
|
| 90 |
+
import importlib
|
| 91 |
+
import sys
|
| 92 |
+
|
| 93 |
+
importlib.import_module("app.routes.commend.router")
|
| 94 |
+
commend_router = sys.modules["app.routes.commend.router"]
|
| 95 |
+
|
| 96 |
+
post_res = client.post(
|
| 97 |
+
"/api/commend/post",
|
| 98 |
+
json={
|
| 99 |
+
"videoUrl": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
|
| 100 |
+
"commentText": "Great production!",
|
| 101 |
+
},
|
| 102 |
+
)
|
| 103 |
+
assert post_res.status_code in (401, 429, 503)
|
| 104 |
+
|
| 105 |
+
monkeypatch.setattr(commend_router, "_COMMEND_REQUIRE_AUTH", True)
|
| 106 |
+
monkeypatch.setattr(commend_router, "_COMMEND_API_KEY", None)
|
| 107 |
+
gen_res = client.post(
|
| 108 |
"/api/commend/generate",
|
| 109 |
json={
|
| 110 |
"videoUrl": "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
|
|
|
|
| 112 |
"commentStyle": "supportive",
|
| 113 |
},
|
| 114 |
)
|
| 115 |
+
assert gen_res.status_code in (429, 503)
|
|
|
|
| 116 |
|
| 117 |
|
| 118 |
def test_commend_rate_limit_independent_of_auth(client: TestClient) -> None:
|