#!/usr/bin/env python3
# Copyright: 2026, Maximiliano Curia <maxy@debian.org>
#
# License: ISC
#  Permission to use, copy, modify, and/or distribute this software for any
#  purpose with or without fee is hereby granted, provided that the above
#  copyright notice and this permission notice appear in all copies.
#  .
#  THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
#  REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
#  AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
#  INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
#  LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
#  OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
#  PERFORMANCE OF THIS SOFTWARE.

"""Tests for the DEP-5 name -> common-licenses file map."""

from __future__ import annotations

from decopy.licensing.data.common_licenses import (
    COMMON_LICENSES,
    common_license_for,
)
from decopy.licensing.data.loader import common_licenses_index
from decopy.licensing.spdx import canonical_license_name


def test_map_targets_in_index():
    index = common_licenses_index()
    for key, entry in COMMON_LICENSES.items():
        assert entry.file in index, (key, entry.file)


def test_map_keys_canonical():
    # Keys are the canonical DEP-5 names decopy emits, so a lookup by a freshly
    # detected name hits directly. Idempotence under canonical_license_name is
    # the invariant that guarantees it.
    for key in COMMON_LICENSES:
        assert canonical_license_name(key) == key


def test_or_later_shares_file():
    # The +/only distinction changes the header, not the referenced text file.
    assert common_license_for("GPL-2") is not None
    assert common_license_for("GPL-2+") == common_license_for("GPL-2")


def test_gfdl_niv_shares_text_file():
    # GFDL-NIV (no-invariants) is a usage choice stated in the notice; the
    # referenced GFDL text file is the same as the invariants-allowing name.
    niv = common_license_for("GFDL-NIV-1.2+")
    plain = common_license_for("GFDL-1.2")
    assert niv is not None
    assert plain is not None
    assert niv.file == "GFDL-1.2"
    assert niv.file == plain.file


def test_unknown_returns_none():
    assert common_license_for("Zlib") is None
