import uuid import pytest from sqlalchemy import select from app.models.subscription import Subscription from app.models.feature_flag import FeatureFlag, PlanFeatureDefault, AccountFeatureOverride @pytest.mark.asyncio async def test_billing_state_returns_subscription_plan_features( client, test_db, test_user, auth_headers ): """Subscription is already seeded by test_user fixture (pro/active). Add a feature flag default for `pro` and verify it shows up in the response.""" flag = FeatureFlag(flag_key="psa_integration", display_name="PSA Integration") test_db.add(flag) await test_db.flush() test_db.add(PlanFeatureDefault(plan="pro", flag_id=flag.id, enabled=True)) await test_db.commit() response = await client.get("/api/v1/billing/state", headers=auth_headers) assert response.status_code == 200, response.json() body = response.json() assert body["subscription"]["status"] == "active" assert body["subscription"]["plan"] == "pro" assert body["subscription"]["has_pro_entitlement"] is True assert body["subscription"]["is_paid"] is True assert body["enabled_features"]["psa_integration"] is True # plan_limits should be a dict with the seeded pro limits from conftest assert body["plan_limits"]["plan"] == "pro" assert body["plan_limits"]["max_trees"] == 25 @pytest.mark.asyncio async def test_billing_state_account_override_beats_plan_default( client, test_db, test_user, auth_headers ): account_id = uuid.UUID(test_user["user_data"]["account_id"]) flag = FeatureFlag(flag_key="escalation_mode", display_name="Escalation Mode") test_db.add(flag) await test_db.flush() test_db.add(PlanFeatureDefault(plan="pro", flag_id=flag.id, enabled=False)) test_db.add(AccountFeatureOverride( account_id=account_id, flag_id=flag.id, enabled=True, )) await test_db.commit() response = await client.get("/api/v1/billing/state", headers=auth_headers) assert response.status_code == 200 assert response.json()["enabled_features"]["escalation_mode"] is True @pytest.mark.asyncio async def test_billing_state_404_when_no_subscription( client, test_db, test_user, auth_headers ): """Wipe the seeded subscription and verify the endpoint surfaces 404.""" from sqlalchemy import delete account_id = uuid.UUID(test_user["user_data"]["account_id"]) await test_db.execute(delete(Subscription).where(Subscription.account_id == account_id)) await test_db.commit() response = await client.get("/api/v1/billing/state", headers=auth_headers) assert response.status_code == 404