# Copyright © The Debusine Developers
# See the AUTHORS file at the top-level directory of this distribution
#
# This file is part of Debusine. It is subject to the license terms
# in the LICENSE file found in the top-level directory of this
# distribution. No part of Debusine, including this file, may be copied,
# modified, propagated, or distributed except according to the terms
# contained in the LICENSE file.

"""Unit tests for the ExternalDebsign task."""

from debusine.artifacts.models import ArtifactCategory, DebianUpload
from debusine.server.tasks.wait import ExternalDebsign
from debusine.server.tasks.wait.models import ExternalDebsignDynamicData
from debusine.server.tasks.wait.tests.helpers import WaitTaskTestCase


class ExternalDebsignTaskTests(WaitTaskTestCase[ExternalDebsign]):
    """Test the ExternalDebsign task."""

    SAMPLE_TASK_DATA = {"unsigned": 1}

    def test_compute_scheduler_tags(self) -> None:
        self.assertComputesSchedulerTags()

    def test_compute_dynamic_data(self) -> None:
        """Dynamic data receives relevant artifact IDs."""
        [artifact, _] = self.playground.create_artifact(
            category=ArtifactCategory.UPLOAD,
            data=DebianUpload(
                type="dpkg",
                changes_fields={
                    "Architecture": "source",
                    "Files": [{"name": "hello_1.0.dsc"}],
                },
            ),
        )
        unsigned_lookup = f"{artifact.id}@artifacts"
        self.task_data["unsigned"] = unsigned_lookup

        self.assertEqual(
            self.compute_dynamic_data(self.task),
            ExternalDebsignDynamicData(unsigned_id=artifact.id),
        )

    def test_get_input_artifacts_ids(self) -> None:
        """Test get_input_artifacts_ids."""
        self.assertEqual(self.task.get_input_artifacts_ids(), [])

        self.task.dynamic_data = ExternalDebsignDynamicData(unsigned_id=1)
        self.assertEqual(self.task.get_input_artifacts_ids(), [1])

    def test_label(self) -> None:
        """Test get_label."""
        self.task_data["unsigned"] = "internal@collections/unsigned-upload"
        self.task.dynamic_data = ExternalDebsignDynamicData(unsigned_id=1)
        self.assertEqual(
            self.task.get_label(),
            "wait for external debsign for "
            "internal@collections/unsigned-upload",
        )
