"""Regression: the 运营洞察 reconciliation overview + drill-down drawer must dedup ``vendor_license_holders`` (and the buckets that sum to it) by product family WITHIN a software. Companion to ``test_billing_seat_family_dedup`` (which covers the workbench top stat). Here the surface is ``_compute_seat_reconciliation_overview`` (per-software table) and ``_compute_seat_reconciliation_detail`` (drawer summary). Both previously SUMmed the org-wide, family-keyed per-plan stat rows, so a software carrying two plans of the same ``product_key`` showed double the real vendor count and broke the licensed-seat identity ``vendor == matched + external + no_email + leaver + vendor_inactive``. ``off_paid_site`` is recorded as anomaly observations but is **excluded** from ``vendor_license_holders`` (Atlassian doesn't bill for sandbox / personal sites). """ from __future__ import annotations import uuid from decimal import Decimal import pytest from sqlalchemy.ext.asyncio import AsyncSession from app.domains.asset.models import ( AssetSoftwareCatalog, SoftwareLicensePlan, SoftwareSeatSyncStat, ) from app.domains.asset.router import ( _compute_seat_reconciliation_detail, _compute_seat_reconciliation_overview, ) from app.integrations.models import Integration pytestmark = pytest.mark.asyncio async def _setup_two_plans_one_family(db: AsyncSession): sw = AssetSoftwareCatalog( id=uuid.uuid4(), name=f"Confluence-{uuid.uuid4().hex[:6]}", asset_type="SaaS", status="active", ) db.add(sw) await db.flush() integ = Integration( id=uuid.uuid4(), kind="software_provisioning", provider="jira", name="Test Jira", slug=f"jira-{uuid.uuid4().hex[:6]}", enabled=True, is_primary=False, ) db.add(integ) await db.flush() # Two active plans on the SAME software, SAME product_key — each gets a # stat row carrying the identical org-wide family count + buckets. for _ in range(2): plan = SoftwareLicensePlan( id=uuid.uuid4(), software_id=sw.id, name=f"Plan-{uuid.uuid4().hex[:4]}", price=Decimal("10.00"), billing_cycle="monthly", currency="USD", status="active", max_seats=None, ) db.add(plan) await db.flush() # vendor_license_holders 106 = matched 90 + ext 7 + no_email 4 # + leaver 2 + vendor_inactive 3 (off_paid_site=1 recorded for # the drawer's audit tab but NOT counted in the headline). db.add(SoftwareSeatSyncStat( integration_id=integ.id, software_id=sw.id, plan_id=plan.id, provider="jira", product_key="confluence", vendor_license_holders=106, holders_matched=90, holders_external=7, holders_no_email=4, holders_leaver_residual=2, holders_vendor_inactive=3, holders_off_paid_site=1, )) await db.commit() return sw async def test_overview_dedups_family_within_software(db_session: AsyncSession): sw = await _setup_two_plans_one_family(db_session) overview = await _compute_seat_reconciliation_overview(db_session) row = next(r for r in overview if r["software_id"] == str(sw.id)) # Family counted once — not 212. off_paid_site is excluded from # the licensed-seat total. assert row["vendor_license_holders"] == 106 assert row["holders_matched"] == 90 assert row["holders_no_email"] == 4 async def test_detail_drawer_dedups_family_and_keeps_identity( db_session: AsyncSession, ): sw = await _setup_two_plans_one_family(db_session) detail = await _compute_seat_reconciliation_detail( db_session, sw.id, page=1, page_size=50, include_resolved=False, ) summary = detail["summary"] # Headline deduped to the single family count (off_paid_site excluded). assert summary["vendor_license_holders"] == 106 assert summary["holders_matched"] == 90 assert summary["holders_no_email"] == 4 # Identity holds against the deduped vendor total — five paid-site # buckets sum exactly; off_paid_site sits alongside as anomaly only. snapshot_sum = ( summary["holders_matched"] + summary["snapshot_holders_external"] + summary["holders_no_email"] + summary["snapshot_holders_leaver_residual"] + summary["snapshot_holders_vendor_inactive"] ) assert snapshot_sum == summary["vendor_license_holders"] # off_paid_site is still surfaced — operators need it for the audit # tab even though it's not licensed. assert summary["snapshot_holders_off_paid_site"] == 1 # The per-plan table still shows BOTH plans (not deduped). assert len(detail["per_plan"]) == 2