from cargo2rpm.utils import load_metadata_from_resource, short_repr

import pytest


@pytest.mark.parametrize(
    "filename,optional,expected",
    [
        ("ahash-0.8.3.json", False, {"cfg-if", "once_cell"}),
        ("ahash-0.8.3.json", True, {"atomic-polyfill", "const-random", "getrandom", "serde"}),
        ("aho-corasick-1.0.2.json", False, set()),
        ("aho-corasick-1.0.2.json", True, {"log", "memchr"}),
        ("assert_cmd-2.0.8.json", False, {"bstr", "doc-comment", "predicates", "predicates-core", "predicates-tree", "wait-timeout"}),
        ("assert_cmd-2.0.8.json", True, {"concolor", "yansi"}),
        ("assert_fs-1.0.10.json", False, {"doc-comment", "globwalk", "predicates", "predicates-core", "predicates-tree", "tempfile"}),
        ("assert_fs-1.0.10.json", True, {"concolor", "yansi"}),
        ("autocfg-1.1.0.json", False, set()),
        ("autocfg-1.1.0.json", True, set()),
        ("bstr-1.2.0.json", False, {"memchr"}),
        ("bstr-1.2.0.json", True, {"once_cell", "regex-automata", "serde"}),
        ("cfg-if-1.0.0.json", False, set()),
        ("cfg-if-1.0.0.json", True, {"compiler_builtins", "core"}),
        ("clap-4.1.4.json", False, {"bitflags", "clap_lex"}),
        (
            "clap-4.1.4.json",
            True,
            {"backtrace", "clap_derive", "is-terminal", "once_cell", "strsim", "termcolor", "terminal_size", "unicase", "unicode-width"},
        ),
        (
            "gstreamer-0.19.7.json",
            False,
            {
                "bitflags",
                "cfg-if",
                "ffi",
                "futures-channel",
                "futures-core",
                "futures-util",
                "glib",
                "libc",
                "muldiv",
                "num-integer",
                "num-rational",
                "once_cell",
                "opt-ops",
                "paste",
                "pretty-hex",
                "thiserror",
            },
        ),
        ("gstreamer-0.19.7.json", True, {"serde", "serde_bytes"}),
        ("human-panic-1.1.0.json", False, {"backtrace", "os_info", "serde", "serde_derive", "toml", "uuid"}),
        ("human-panic-1.1.0.json", True, {"concolor", "termcolor"}),
        (
            "hyperfine-1.15.0.json",
            False,
            {
                "anyhow",
                "atty",
                "clap",
                "colored",
                "csv",
                "indicatif",
                "rand",
                "rust_decimal",
                "serde",
                "serde_json",
                "shell-words",
                "statistical",
                "thiserror",
                "once_cell",
                "libc",
                "nix",
                "winapi",
            },
        ),
        ("hyperfine-1.15.0.json", True, set()),
        ("iri-string-0.7.0.json", False, set()),
        ("iri-string-0.7.0.json", True, {"memchr", "serde"}),
        ("libc-0.2.139.json", False, set()),
        ("libc-0.2.139.json", True, {"rustc-std-workspace-core"}),
        ("predicates-2.1.5.json", False, {"itertools", "predicates-core"}),
        ("predicates-2.1.5.json", True, {"concolor", "difflib", "float-cmp", "normalize-line-endings", "regex", "yansi"}),
        ("proc-macro2-1.0.50.json", False, {"unicode-ident"}),
        ("proc-macro2-1.0.50.json", True, set()),
        ("quote-1.0.23.json", False, {"proc-macro2"}),
        ("quote-1.0.23.json", True, set()),
        ("rand-0.8.5.json", False, {"rand_core"}),
        ("rand-0.8.5.json", True, {"log", "packed_simd", "rand_chacha", "serde", "libc"}),
        ("rand_core-0.6.4.json", False, set()),
        ("rand_core-0.6.4.json", True, {"getrandom", "serde"}),
        ("regex-1.8.4.json", False, {"regex-syntax"}),
        ("regex-1.8.4.json", True, {"aho-corasick", "memchr"}),
        ("regex-syntax-0.7.2.json", False, set()),
        ("regex-syntax-0.7.2.json", True, set()),
        ("rust_decimal-1.28.0.json", False, {"arrayvec", "num-traits"}),
        (
            "rust_decimal-1.28.0.json",
            True,
            {
                "arbitrary",
                "borsh",
                "bytecheck",
                "byteorder",
                "bytes",
                "diesel1",
                "diesel2",
                "postgres",
                "rand",
                "rkyv",
                "rocket",
                "serde",
                "serde_json",
                "tokio-postgres",
            },
        ),
        ("rustix-0.36.8.json", False, {"bitflags", "linux-raw-sys", "libc", "libc_errno", "windows-sys"}),
        ("rustix-0.36.8.json", True, {"alloc", "compiler_builtins", "core", "io-lifetimes", "itoa", "libc", "libc_errno", "once_cell"}),
        ("serde-1.0.152.json", False, set()),
        ("serde-1.0.152.json", True, {"serde_derive"}),
        ("serde_derive-1.0.152.json", False, {"proc-macro2", "quote", "syn"}),
        ("serde_derive-1.0.152.json", True, set()),
        ("sha1collisiondetection-0.3.1.json", False, {"generic-array"}),
        ("sha1collisiondetection-0.3.1.json", True, {"clap", "clap_mangen", "const-oid", "digest"}),
        ("syn-1.0.107.json", False, {"proc-macro2", "unicode-ident"}),
        ("syn-1.0.107.json", True, {"quote"}),
        ("time-0.3.17.json", False, {"time-core"}),
        ("time-0.3.17.json", True, {"itoa", "quickcheck", "rand", "serde", "time-macros", "js-sys", "libc", "num_threads"}),
        ("tokio-1.25.0.json", False, {"pin-project-lite", "windows-sys"}),
        (
            "tokio-1.25.0.json",
            True,
            {
                "bytes",
                "memchr",
                "mio",
                "num_cpus",
                "parking_lot",
                "tokio-macros",
                "socket2",
                "tracing",
                "libc",
                "signal-hook-registry",
                "windows-sys",
            },
        ),
        ("unicode-xid-0.2.4.json", False, set()),
        ("unicode-xid-0.2.4.json", True, set()),
        (
            "zbus-3.8.0.json",
            False,
            {
                "async-broadcast",
                "async-recursion",
                "async-trait",
                "byteorder",
                "derivative",
                "dirs",
                "enumflags2",
                "event-listener",
                "futures-core",
                "futures-sink",
                "futures-util",
                "hex",
                "nix",
                "once_cell",
                "ordered-stream",
                "rand",
                "serde",
                "serde_repr",
                "sha1",
                "static_assertions",
                "tracing",
                "zbus_macros",
                "zbus_names",
                "zvariant",
                "uds_windows",
                "winapi",
            },
        ),
        (
            "zbus-3.8.0.json",
            True,
            {
                "async-executor",
                "async-io",
                "async-lock",
                "async-task",
                "lazy_static",
                "quick-xml",
                "serde-xml-rs",
                "tokio",
                "tokio-vsock",
                "vsock",
            },
        ),
        (
            "zoxide-0.9.0.json",
            False,
            {"anyhow", "askama", "bincode", "clap", "dirs", "dunce", "fastrand", "glob", "ouroboros", "serde", "nix", "which"},
        ),
        ("zoxide-0.9.0.json", True, set()),
    ],
    ids=short_repr,
)
def test_package_get_normal_dependencies(filename: str, optional: bool, expected: set[str]):
    data = load_metadata_from_resource(filename)
    assert set(data.packages[0].get_normal_dependencies(optional).keys()) == expected


@pytest.mark.parametrize(
    "filename,expected",
    [
        (
            "ahash-0.8.3.json",
            {
                "criterion",
                "fnv",
                "fxhash",
                "hashbrown",
                "hex",
                "no-panic",
                "rand",
                "seahash",
                "serde_json",
            },
        ),
        ("aho-corasick-1.0.2.json", {"doc-comment"}),
        ("assert_cmd-2.0.8.json", {"escargot"}),
        ("assert_fs-1.0.10.json", set()),
        ("autocfg-1.1.0.json", set()),
        ("bstr-1.2.0.json", {"quickcheck", "ucd-parse", "unicode-segmentation"}),
        ("cfg-if-1.0.0.json", set()),
        ("clap-4.1.4.json", {"humantime", "rustversion", "shlex", "snapbox", "static_assertions", "trybuild", "trycmd", "unic-emoji-char"}),
        ("gstreamer-0.19.7.json", {"futures-executor", "gir-format-check", "ron", "serde_json"}),
        ("human-panic-1.1.0.json", set()),
        ("hyperfine-1.15.0.json", {"approx", "assert_cmd", "predicates", "tempfile"}),
        ("iri-string-0.7.0.json", {"serde_test"}),
        ("libc-0.2.139.json", set()),
        ("predicates-2.1.5.json", {"predicates-tree"}),
        ("proc-macro2-1.0.50.json", {"quote"}),
        ("quote-1.0.23.json", {"rustversion", "trybuild"}),
        ("rand-0.8.5.json", {"bincode", "rand_pcg"}),
        ("rand_core-0.6.4.json", set()),
        ("regex-1.8.4.json", {"lazy_static", "quickcheck", "rand"}),
        ("regex-syntax-0.7.2.json", set()),
        (
            "rust_decimal-1.28.0.json",
            {"bincode", "bytes", "criterion", "csv", "futures", "rand", "serde", "serde_json", "tokio", "version-sync"},
        ),
        (
            "rustix-0.36.8.json",
            {"flate2", "io-lifetimes", "libc", "libc_errno", "memoffset", "serial_test", "tempfile", "criterion", "ctor"},
        ),
        ("serde-1.0.152.json", {"serde_derive"}),
        ("serde_derive-1.0.152.json", {"serde"}),
        ("sha1collisiondetection-0.3.1.json", {"getrandom", "hex-literal", "sha1"}),
        (
            "syn-1.0.107.json",
            {
                "anyhow",
                "automod",
                "flate2",
                "insta",
                "rayon",
                "ref-cast",
                "regex",
                "reqwest",
                "syn-test-suite",
                "tar",
                "termcolor",
                "walkdir",
            },
        ),
        ("time-0.3.17.json", {"quickcheck_macros", "rand", "serde", "serde_json", "serde_test", "time-macros", "trybuild", "criterion"}),
        (
            "tokio-1.25.0.json",
            {
                "async-stream",
                "futures",
                "mockall",
                "tempfile",
                "tokio-stream",
                "tokio-test",
                "wasm-bindgen-test",
                "loom",
                "rand",
                "proptest",
                "socket2",
                "mio-aio",
                "libc",
                "nix",
                "ntapi",
            },
        ),
        ("unicode-xid-0.2.4.json", {"criterion"}),
        ("zbus-3.8.0.json", {"async-std", "doc-comment", "futures-util", "ntest", "tempfile", "test-log", "tokio", "tracing-subscriber"}),
        ("zoxide-0.9.0.json", {"assert_cmd", "rstest", "rstest_reuse", "tempfile"}),
    ],
    ids=short_repr,
)
def test_package_get_dev_dependencies(filename: str, expected: set[str]):
    data = load_metadata_from_resource(filename)
    assert set(data.packages[0].get_dev_dependencies().keys()) == expected


@pytest.mark.parametrize(
    "filename,optional,expected",
    [
        ("ahash-0.8.3.json", False, {"version_check"}),
        ("ahash-0.8.3.json", True, set()),
        ("aho-corasick-1.0.2.json", False, set()),
        ("aho-corasick-1.0.2.json", True, set()),
        ("assert_cmd-2.0.8.json", False, set()),
        ("assert_cmd-2.0.8.json", True, set()),
        ("assert_fs-1.0.10.json", False, set()),
        ("assert_fs-1.0.10.json", True, set()),
        ("autocfg-1.1.0.json", True, set()),
        ("autocfg-1.1.0.json", False, set()),
        ("bstr-1.2.0.json", False, set()),
        ("bstr-1.2.0.json", True, set()),
        ("cfg-if-1.0.0.json", False, set()),
        ("cfg-if-1.0.0.json", True, set()),
        ("clap-4.1.4.json", False, set()),
        ("clap-4.1.4.json", True, set()),
        ("gstreamer-0.19.7.json", False, set()),
        ("gstreamer-0.19.7.json", True, set()),
        ("human-panic-1.1.0.json", False, set()),
        ("human-panic-1.1.0.json", True, set()),
        ("hyperfine-1.15.0.json", False, {"atty", "clap", "clap_complete"}),
        ("hyperfine-1.15.0.json", True, set()),
        ("iri-string-0.7.0.json", False, set()),
        ("iri-string-0.7.0.json", True, set()),
        ("libc-0.2.139.json", False, set()),
        ("libc-0.2.139.json", True, set()),
        ("predicates-2.1.5.json", False, set()),
        ("predicates-2.1.5.json", True, set()),
        ("proc-macro2-1.0.50.json", False, set()),
        ("proc-macro2-1.0.50.json", True, set()),
        ("quote-1.0.23.json", False, set()),
        ("quote-1.0.23.json", True, set()),
        ("rand-0.8.5.json", False, set()),
        ("rand-0.8.5.json", True, set()),
        ("rand_core-0.6.4.json", False, set()),
        ("rand_core-0.6.4.json", True, set()),
        ("regex-1.8.4.json", False, set()),
        ("regex-1.8.4.json", True, set()),
        ("regex-syntax-0.7.2.json", False, set()),
        ("regex-syntax-0.7.2.json", True, set()),
        ("rust_decimal-1.28.0.json", False, set()),
        ("rust_decimal-1.28.0.json", True, set()),
        ("rustix-0.36.8.json", False, set()),
        ("rustix-0.36.8.json", True, {"cc"}),
        ("serde-1.0.152.json", False, set()),
        ("serde-1.0.152.json", True, set()),
        ("serde_derive-1.0.152.json", False, set()),
        ("serde_derive-1.0.152.json", True, set()),
        ("sha1collisiondetection-0.3.1.json", False, set()),
        ("sha1collisiondetection-0.3.1.json", True, {"clap", "clap_mangen"}),
        ("syn-1.0.107.json", False, set()),
        ("syn-1.0.107.json", True, set()),
        ("time-0.3.17.json", False, set()),
        ("time-0.3.17.json", True, set()),
        ("tokio-1.25.0.json", False, {"autocfg"}),
        ("tokio-1.25.0.json", True, set()),
        ("unicode-xid-0.2.4.json", False, set()),
        ("unicode-xid-0.2.4.json", True, set()),
        ("zbus-3.8.0.json", False, set()),
        ("zbus-3.8.0.json", True, set()),
        ("zoxide-0.9.0.json", False, {"clap", "clap_complete", "clap_complete_fig"}),
        ("zoxide-0.9.0.json", True, set()),
    ],
    ids=short_repr,
)
def test_package_get_build_dependencies(filename: str, optional: bool, expected: set[str]):
    data = load_metadata_from_resource(filename)
    assert set(data.packages[0].get_build_dependencies(optional).keys()) == expected
