Enhance Club Feature Consumption Logic and Update Versioning
Deploy Development / deploy (push) Successful in 42s
Test Suite / pytest-backend (push) Successful in 47s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 14s
Test Suite / k6 /health Baseline (push) Successful in 34s
Test Suite / playwright-tests (push) Successful in 1m40s
Deploy Development / deploy (push) Successful in 42s
Test Suite / pytest-backend (push) Successful in 47s
Test Suite / lint-backend (push) Successful in 0s
Test Suite / build-frontend (push) Successful in 14s
Test Suite / k6 /health Baseline (push) Successful in 34s
Test Suite / playwright-tests (push) Successful in 1m40s
- Introduced the `consume_club_feature_with_usage` function to standardize feature consumption across endpoints, improving code reusability and clarity. - Implemented `merge_feature_usage_into_response` to embed feature usage data in API responses, streamlining frontend integration. - Updated various backend routers to utilize the new consumption logic, ensuring consistent feature usage tracking during AI-related actions. - Enhanced tests to validate the new consumption and logging behavior. - Incremented application version to 0.8.199 and updated module version for 'club_features' to 1.6.0 to reflect these changes.
This commit is contained in:
1 parent
40641594ac
commit
b68185842e
9 files changed
+323
-16
No files matched your search
@@ -5,6 +5,8 @@ from fastapi import HTTPException
|
||||
from club_features import (
|
||||
club_feature_enforcement_enabled,
|
||||
consume_club_feature,
|
||||
consume_club_feature_with_usage,
|
||||
merge_feature_usage_into_response,
|
||||
probe_club_feature_access,
|
||||
)
|
||||
|
||||
@@ -94,6 +96,45 @@ def test_consume_skips_without_club_id(monkeypatch):
|
||||
assert calls == []
|
||||
|
||||
|
||||
def test_consume_logs_usage_after_increment(monkeypatch):
|
||||
logs = []
|
||||
|
||||
monkeypatch.setattr("club_features.get_cursor", lambda conn: _fake_cur())
|
||||
monkeypatch.setattr(
|
||||
"club_quota_bypass.is_club_feature_quota_bypassed",
|
||||
lambda *a, **k: False,
|
||||
)
|
||||
monkeypatch.setattr("club_features.increment_club_feature_usage", lambda *a, **k: None)
|
||||
monkeypatch.setattr(
|
||||
"club_features.check_club_feature_access",
|
||||
lambda club_id, feature_id, conn=None: {
|
||||
"allowed": True,
|
||||
"used": 1,
|
||||
"limit": 30,
|
||||
"remaining": 29,
|
||||
"plan_id": "club",
|
||||
"reason": "within_limit",
|
||||
},
|
||||
)
|
||||
monkeypatch.setattr(
|
||||
"club_feature_logger.log_club_feature_usage",
|
||||
lambda **kwargs: logs.append(kwargs),
|
||||
)
|
||||
|
||||
consume_club_feature(
|
||||
feature_id="ai_calls",
|
||||
club_id=5,
|
||||
profile_id=9,
|
||||
portal_role="trainer",
|
||||
action="suggest",
|
||||
conn=object(),
|
||||
)
|
||||
assert len(logs) == 1
|
||||
assert logs[0]["phase"] == "consume"
|
||||
assert logs[0]["feature_id"] == "ai_calls"
|
||||
assert logs[0]["club_id"] == 5
|
||||
|
||||
|
||||
def test_consume_increments_once_per_call(monkeypatch):
|
||||
calls = []
|
||||
|
||||
@@ -106,6 +147,18 @@ def test_consume_increments_once_per_call(monkeypatch):
|
||||
lambda *a, **k: False,
|
||||
)
|
||||
monkeypatch.setattr("club_features.increment_club_feature_usage", _inc)
|
||||
monkeypatch.setattr(
|
||||
"club_features.check_club_feature_access",
|
||||
lambda club_id, feature_id, conn=None: {
|
||||
"allowed": True,
|
||||
"used": 1,
|
||||
"limit": 30,
|
||||
"remaining": 29,
|
||||
"plan_id": "club",
|
||||
"reason": "within_limit",
|
||||
},
|
||||
)
|
||||
monkeypatch.setattr("club_feature_logger.log_club_feature_usage", lambda **kwargs: None)
|
||||
consume_club_feature(
|
||||
feature_id="ai_calls",
|
||||
club_id=5,
|
||||
@@ -117,6 +170,40 @@ def test_consume_increments_once_per_call(monkeypatch):
|
||||
assert calls == [(5, "ai_calls", "suggest")]
|
||||
|
||||
|
||||
def test_merge_feature_usage_into_response():
|
||||
out = merge_feature_usage_into_response(
|
||||
{"ok": True},
|
||||
{"ai_calls": {"used": 3, "limit": 30}},
|
||||
)
|
||||
assert out["ok"] is True
|
||||
assert out["feature_usage"]["ai_calls"]["used"] == 3
|
||||
assert merge_feature_usage_into_response({"x": 1}, None) == {"x": 1}
|
||||
|
||||
|
||||
def test_consume_with_usage_returns_snapshot(monkeypatch):
|
||||
monkeypatch.setattr("club_features.get_cursor", lambda conn: _fake_cur())
|
||||
monkeypatch.setattr(
|
||||
"club_quota_bypass.is_club_feature_quota_bypassed",
|
||||
lambda *a, **k: False,
|
||||
)
|
||||
monkeypatch.setattr("club_features.consume_club_feature", lambda **kwargs: None)
|
||||
monkeypatch.setattr(
|
||||
"club_features.club_feature_usage_for_api",
|
||||
lambda cur, **kwargs: {"used": 4, "limit": 30, "allowed": True},
|
||||
)
|
||||
|
||||
usage = consume_club_feature_with_usage(
|
||||
feature_id="ai_calls",
|
||||
club_id=7,
|
||||
profile_id=1,
|
||||
portal_role="trainer",
|
||||
action="suggest",
|
||||
cur=_fake_cur(),
|
||||
conn=object(),
|
||||
)
|
||||
assert usage["ai_calls"]["used"] == 4
|
||||
|
||||
|
||||
def test_club_feature_enforcement_env_default_off(monkeypatch):
|
||||
monkeypatch.delenv("CLUB_FEATURE_ENFORCE", raising=False)
|
||||
assert club_feature_enforcement_enabled() is False
|
||||
Reference in new issue
Block a user