from pathlib import Path

import tomli

from licensecheck import types
from licensecheck.resolvers import native as get_deps

THISDIR = Path(__file__).resolve().parent


def test_doGetReqs_PEP631() -> None:
	extras = ["socks"]
	pyproject = tomli.loads((THISDIR / "data/pep631_socks.toml").read_text(encoding="utf-8"))
	requirementsPaths = [(THISDIR / "data/pep631_socks.toml")]
	skipDependencies = [types.ucstr("TOSKIP")]

	assert get_deps.get_reqs(skipDependencies, extras, requirementsPaths, pyproject) == {
		"DOCKERPTY",
		"PACKAGING",
		"ATTRS",
		"JSONSCHEMA",
		"PYYAML",
		"PYSOCKS",
		"CERTIFI",
		"ENUM34",
		"DOCKER",
		"TEXTTABLE",
		"PYWIN32",
		"JSONSCHEMA-SPECIFICATIONS",
		"IPADDRESS",
		"PKGUTIL-RESOLVE-NAME",
		"DOCOPT",
		"BACKPORTS-SSL-MATCH-HOSTNAME",
		"PARAMIKO",
		"IDNA",
		"COLORAMA",
		"IMPORTLIB-RESOURCES",
		"CACHED-PROPERTY",
		"DISTRO",
		"BACKPORTS-SHUTIL-GET-TERMINAL-SIZE",
		"CHARSET-NORMALIZER",
		"URLLIB3",
		"WEBSOCKET-CLIENT",
		"RPDS-PY",
		"SUBPROCESS32",
		"REQUESTS",
		"REFERENCING",
		"PYTHON-DOTENV",
	}


def test_doGetReqs_requirements() -> None:
	extras = [f"{THISDIR}/data/test_requirements.txt"]
	pyproject = {}
	requirementsPaths = [Path(f"{THISDIR}/data/test_requirements.txt")]
	skipDependencies = [types.ucstr("TOSKIP")]

	deps = get_deps.get_reqs(skipDependencies, extras, requirementsPaths, pyproject)
	assert deps == {
		"NUMPY",
		"ODFPY",
		"OPENPYXL",
		"PANDAS",
		"PYTHON-CALAMINE",
		"PYTHON-DATEUTIL",
		"PYTZ",
		"PYXLSB",
		"TZDATA",
		"XLRD",
		"XLSXWRITER",
	}
	assert "OPENPYXL" in deps
	assert (
		"XARRAY" not in deps
	)  # xarray is an optional dependency of pandas associated with 'computation' key that is not
	# tracked in test_requirements.txt


def test_doGetReqs_requirements_with_hashes() -> None:
	extras = []
	pyproject = {}
	requirementsPaths = [Path(f"{THISDIR}/data/test_requirements_hash.txt")]
	skipDependencies = [types.ucstr("TOSKIP")]

	deps = get_deps.get_reqs(skipDependencies, extras, requirementsPaths, pyproject)
	assert deps == {"PACKAGING"}
	assert "TOSKIP" not in deps


def test_issue_81() -> None:
	extras = []
	pyproject = {}
	requirementsPaths = [Path(f"{THISDIR}/data/issue_81.txt")]
	skipDependencies = []

	deps = get_deps.get_reqs(skipDependencies, extras, requirementsPaths, pyproject)
	assert deps == {
		"DATACLASSES",
		"LANGSMITH",
		"MURMURHASH",
		"CLOUDPICKLE",
		"LANGDETECT",
		"ZIPP",
		"CRYPTOGRAPHY",
		"FROZENLIST",
		"NVIDIA-CURAND-CU12",
		"PYTHON-ISO639",
		"TYPING-INSPECT",
		"WATCHFILES",
		"SPACY-LEGACY",
		"PYYAML",
		"BACKOFF",
		"DILL",
		"GRPCIO-TOOLS",
		"BLIS",
		"LARK",
		"SMART-OPEN",
		"PYZMQ",
		"FILETYPE",
		"CLICK",
		"DISTRO",
		"PLATFORMDIRS",
		"HPACK",
		"PYDANTIC",
		"DATACLASSES-JSON",
		"NVIDIA-NCCL-CU12",
		"LANGFUSE",
		"STARLETTE",
		"CHARSET-NORMALIZER",
		"MULTIPROCESS",
		"COLOREDLOGS",
		"NVIDIA-CUDA-CUPTI-CU12",
		"IMPORTLIB-RESOURCES",
		"CFFI",
		"QDRANT-CLIENT",
		"HTTPTOOLS",
		"ELASTICSEARCH",
		"PYWIN32",
		"FASTRLOCK",
		"OPENAI",
		"MMH3",
		"MULTIDICT",
		"ATTRS",
		"ONNX",
		"LANGUAGE-DATA",
		"MISTUNE",
		"SQLALCHEMY",
		"JSONPATCH",
		"IMPORTLIB-METADATA",
		"NUMPY",
		"DATACLASSES-JSON-SPEAKEASY",
		"DISKCACHE",
		"NUMBA",
		"UNSTRUCTURED-CLIENT",
		"PYTHON-DATEUTIL",
		"CUPY-CUDA12X",
		"PACKAGING",
		"LANGCHAIN-TEXT-SPLITTERS",
		"TENACITY",
		"YARL",
		"LANGCHAIN-CORE",
		"REQUESTS",
		"BEAUTIFULSOUP4",
		"CYMEM",
		"SPACY-LOGGERS",
		"PYARROW-HOTFIX",
		"ORJSON",
		"PSUTIL",
		"JINJA2",
		"PRESHED",
		"OAUTHLIB",
		"BLEACH",
		"WRAPT",
		"SENTENCEPIECE",
		"DATASETS",
		"WEBSOCKETS",
		"TYPER",
		"PYTHON-DOTENV",
		"PYARROW",
		"PYCPARSER",
		"LANGCHAIN",
		"PYDANTIC-CORE",
		"SNIFFIO",
		"SETUPTOOLS",
		"FILELOCK",
		"TORNADO",
		"UVLOOP",
		"JSONPATH-PYTHON",
		"RAPIDFUZZ",
		"TOKENIZERS",
		"TZDATA",
		"RICH",
		"PYREADLINE",
		"CATALOGUE",
		"PYTHON-MAGIC",
		"NINJA",
		"TINYCSS2",
		"TRITON",
		"NVIDIA-CUDNN-CU12",
		"HTTPCORE",
		"SLACK-LOG-HANDLER",
		"SENTENCE-TRANSFORMERS",
		"RPDS-PY",
		"H11",
		"GREENLET",
		"SAFETENSORS",
		"WEASEL",
		"FLATBUFFERS",
		"INTEREGULAR",
		"MARKDOWN",
		"LANGCODES",
		"FAISS-CPU",
		"SCIKIT-LEARN",
		"PROTOBUF",
		"COLORAMA",
		"FLASHRANK",
		"NBCLIENT",
		"LLVMLITE",
		"PYCOUNTRY",
		"TOMLI",
		"MYPY-EXTENSIONS",
		"TYPING",
		"SIX",
		"ONNXRUNTIME",
		"URLLIB3",
		"PYREADLINE3",
		"SCIPY",
		"OUTLINES",
		"ANYIO",
		"CERTIFI",
		"JMESPATH",
		"NETWORKX",
		"LANGCHAIN-ELASTICSEARCH",
		"NLTK",
		"AIOSIGNAL",
		"LOGURU",
		"NVIDIA-NVTX-CU12",
		"TYPING-EXTENSIONS",
		"XXHASH",
		"PANDOCFILTERS",
		"SOUPSIEVE",
		"AIOHAPPYEYEBALLS",
		"INICONFIG",
		"NVIDIA-CUSOLVER-CU12",
		"PYNVML",
		"FASTEMBED",
		"MARSHMALLOW",
		"SHELLINGHAM",
		"REGEX",
		"ASYNC-TIMEOUT",
		"ANNOTATED-TYPES",
		"UNSTRUCTURED",
		"PYSTEMMER",
		"JSONSCHEMA-SPECIFICATIONS",
		"JSONPOINTER",
		"CLOUDPATHLIB",
		"EXECUTING",
		"TQDM",
		"MARKDOWNIFY",
		"NBFORMAT",
		"RAY",
		"JUPYTERLAB-PYGMENTS",
		"PILLOW",
		"PYGMENTS",
		"MONOTONIC",
		"SYMPY",
		"UVICORN",
		"PYPDF",
		"IDNA",
		"NVIDIA-CUFFT-CU12",
		"REFERENCING",
		"XFORMERS",
		"EXCEPTIONGROUP",
		"ELASTIC-TRANSPORT",
		"NVIDIA-CUBLAS-CU12",
		"SRSLY",
		"THINC",
		"LXML",
		"HYPERFRAME",
		"HTTPX",
		"PYAIRPORTS",
		"EMOJI",
		"WEBENCODINGS",
		"CHEVRON",
		"GRPCIO",
		"THREADPOOLCTL",
		"AIOCONTEXTVARS",
		"NVIDIA-NVJITLINK-CU12",
		"PROMETHEUS-CLIENT",
		"FASTAPI",
		"JITER",
		"PLUGGY",
		"PYTZ",
		"DEPRECATED",
		"MSGPACK",
		"CHARDET",
		"FASTJSONSCHEMA",
		"JUPYTER-CLIENT",
		"SNOWBALLSTEMMER",
		"PYTEST",
		"NVIDIA-CUDA-RUNTIME-CU12",
		"NVIDIA-CUDA-NVRTC-CU12",
		"SPACY",
		"NEST-ASYNCIO",
		"NVIDIA-CUSPARSE-CU12",
		"DEEPDIFF",
		"REQUESTS-TOOLBELT",
		"TRAITLETS",
		"FSSPEC",
		"HUMANFRIENDLY",
		"JOBLIB",
		"PANDAS",
		"PYTHON-MULTIPART",
		"RANK-BM25",
		"TIKTOKEN",
		"HUGGINGFACE-HUB",
		"LANGCHAIN-COMMUNITY",
		"MARKUPSAFE",
		"REQUESTS-OAUTHLIB",
		"H2",
		"TABULATE",
		"MPMATH",
		"ATLASSIAN-PYTHON-API",
		"NBCONVERT",
		"TORCH",
		"CACHED-PROPERTY",
		"DEFUSEDXML",
		"JUPYTER-CORE",
		"AIOHTTP",
		"PORTALOCKER",
		"NATSORT",
		"TRANSFORMERS",
		"CONFECTION",
		"PKGUTIL-RESOLVE-NAME",
		"WASABI",
		"WIN32-SETCTIME",
		"LANGCHAIN-OPENAI",
		"JSONSCHEMA",
	}
