"""Autodesk release-year versioning — run: python tests/test_autodesk_year.py

Autodesk versions its products by release YEAR and NVD lists them the same way
(autodesk:autocad:2018). The inventory reports the internal build instead —
AutoCAD LT 2026 installs as 25.1.60.0 — and comparing a build number against a
year matches nothing, which is why these were invisible.

The year is taken from the NAME. Where there is none, nothing is scanned:
guessing a year would be worse than missing the product.
"""
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from app.services.app_cve_scanner_service import resolve_product, _effective_version


def _ver(name, version="25.1.60.0"):
    e = resolve_product(name)
    return (e or {}).get("key"), (_effective_version(name, version, e) if e else None)


def demo():
    # Verbatim inventory rows from the tester's estate.
    assert _ver("AutoCAD LT 2026 - Deutsch (German)") == \
        ("cpe:autodesk:autocad_lt", "2026")
    assert _ver("Autodesk AutoCAD LT 2026 - Deutsch (German)") == \
        ("cpe:autodesk:autocad_lt", "2026")
    # LT and full AutoCAD are separate CPE products.
    assert _ver("AutoCAD 2025")[0] == "cpe:autodesk:autocad"
    assert _ver("AutoCAD LT 2026")[0] == "cpe:autodesk:autocad_lt"

    # No year in the name → no version to scan with; the caller skips it.
    assert _ver("AutoCAD LT Private")[1] == ""

    # Companions ship alongside and are not the application. Each carries its
    # own unrelated version (Autodesk Access 2.21.0.559, CER 7.2.4.927, …).
    for helper in ("AutoCAD Open in Desktop", "Autodesk Access", "Autodesk CER",
                   "Autodesk Genuine Service", "Autodesk Identity Manager"):
        assert resolve_product(helper) is None, helper

    print("autodesk year OK")


def demo_family():
    """The wider Autodesk family, from the tester's real inventory.

    Two AutoCAD LT generations sit on one host, and every product ships
    language packs as separate entries with the SAME version as the
    application — Navisworks Freedom 2025 has twelve. Each would collect the
    product's full CVE set, exactly like Adobe's dictionary pack did.
    """
    assert _ver("AutoCAD LT 2022 - Deutsch (German)", "24.1.51.0") == \
        ("cpe:autodesk:autocad_lt", "2022")
    assert _ver("AutoCAD LT 2025 - Deutsch (German)", "25.0.58.0") == \
        ("cpe:autodesk:autocad_lt", "2025")
    # The viewers, both blind spots until now.
    assert _ver("Autodesk DWG TrueView 2024 - English", "24.3.119.0") == \
        ("cpe:autodesk:dwg_trueview", "2024")
    assert _ver("Autodesk Navisworks Freedom 2025", "22.0.1411.23") == \
        ("cpe:autodesk:navisworks_freedom", "2025")

    # Language packs and shared components are not the applications.
    for other in ("AutoCAD LT 2022 Language Pack - Deutsch (German)",
                  "Autodesk Navisworks Freedom 2025 - Language Pack Deutsch (German)",
                  "Autodesk Navisworks Freedom 2025 - English Language Pack",
                  "Autodesk Navisworks Freedom 2025 - Module linguistique Fran\u00e7ais (French)",
                  "Autodesk Material Library 2022",
                  "Autodesk Material Library Base Resolution Image Library 2022",
                  "Autodesk Single Sign On Component"):
        assert resolve_product(other) is None, other

    print("autodesk family OK")


if __name__ == "__main__":
    demo()
    demo_family()
