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

"""find_licenses() returns atomic licence names, never a compound expression.

A detected disjunction like "Expat or Apache-2" must come back as its atoms
["Expat", "Apache-2"] so each gets its own standalone License stanza (with text),
rather than a single opaque paragraph keyed by the whole expression string.
"""

import unittest

from decopy.licensing import licenses


def _detect(text: str) -> list[str]:
    return list(licenses.find_licenses(licenses.clean_comments(text)))


class TestFindLicensesAtoms(unittest.TestCase):
    def test_spdx_disjunction_split_into_atoms(self):
        result = _detect("# SPDX-License-Identifier: MIT or Apache-2.0\n")
        self.assertEqual(result, ["Expat", "Apache-2"])

    def test_spdx_conjunction_split_into_atoms(self):
        result = _detect("# SPDX-License-Identifier: MIT AND Apache-2.0\n")
        self.assertEqual(sorted(result), ["Apache-2", "Expat"])

    def test_with_clause_stays_one_atom(self):
        # A `with` exception is a single grouping key, not two atoms.
        result = _detect(
            "# SPDX-License-Identifier: GPL-2.0-or-later WITH Font-exception-2.0\n"
        )
        self.assertEqual(result, ["GPL-2+ with Font exception"])

    def test_atoms_are_deduplicated(self):
        result = _detect("# SPDX-License-Identifier: MIT or MIT\n")
        self.assertEqual(result, ["Expat"])


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