#!/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.

"""Contract of canonical_license_name(): the single licence-name normaliser.

It maps any SPDX-or-DEP-5 single licence id to decopy's canonical DEP-5 name,
is idempotent, and leaves names it does not recognise unchanged. The read path
and the SPDX-License-Identifier path both normalise through it, so these
properties are what lets every entry point agree.
"""

import unittest

from decopy.licensing.spdx import canonical_license_name

# (input, canonical DEP-5 name) for the recognised mappings the read path relies
# on. SPDX -only / -or-later / .0 spellings collapse onto decopy's short names.
_RECOGNISED = (
    ("GPL-2.0-only", "GPL-2"),
    ("GPL-2.0", "GPL-2"),
    ("GPL-2", "GPL-2"),
    ("GPL-2.0-or-later", "GPL-2+"),
    ("LGPL-2.1-only", "LGPL-2.1"),
    ("AGPL-3.0", "AGPL-3"),
    ("Apache-2.0", "Apache-2"),
    ("MIT", "Expat"),
    ("CC0-1.0", "CC0"),
    ("BSD-3-Clause", "BSD-3-clause"),
)

# Names canonical_license_name() must pass through untouched: decopy's own
# canonical spellings (already normalised) and ids it does not recognise. The
# identity-on-unknown property is the safety guarantee -- it must never mangle a
# bespoke name a maintainer wrote by hand.
_PASSTHROUGH = (
    "Expat",
    "GPL-2",
    "GPL-2+",
    "MIT/X11",
    "ISC",
    "Some-Bespoke-1.2",
    "Totally-Made-Up-License",
)


class TestCanonicalLicenseName(unittest.TestCase):
    def test_recognised_mappings(self):
        for raw, expected in _RECOGNISED:
            with self.subTest(name=raw):
                self.assertEqual(canonical_license_name(raw), expected)

    def test_identity_on_unknown_and_already_canonical(self):
        for name in _PASSTHROUGH:
            with self.subTest(name=name):
                self.assertEqual(canonical_license_name(name), name)

    def test_idempotent(self):
        names = [raw for raw, _ in _RECOGNISED] + list(_PASSTHROUGH)
        for name in names:
            with self.subTest(name=name):
                once = canonical_license_name(name)
                self.assertEqual(canonical_license_name(once), once)


if __name__ == "__main__":
    unittest.main()
