# 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 signing no-operation task."""

from unittest import mock

from django.test import TestCase

import debusine.worker.tags as wtags
from debusine.artifacts.models import WorkRequestResults
from debusine.signing.tasks import SigningNoop
from debusine.signing.tasks.tests.helpers import SigningTaskTestCase
from debusine.tasks import TaskConfigError


class SigningNoopTests(SigningTaskTestCase[SigningNoop], TestCase):
    """Test the SigningNoop task."""

    SAMPLE_TASK_DATA = {"result": WorkRequestResults.SUCCESS}

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

    def test_get_subject(self) -> None:
        self.assertIsNone(self.get_subject())

    def test_get_configuration_context(self) -> None:
        self.assertIsNone(self.get_configuration_context())

    def test_get_provided_worker_tags(self) -> None:
        self.assertCountEqual(
            self.task.get_provided_worker_tags(),
            [wtags.TASK_PREFIX + "signing:noop:version:1"],
        )

    def test_execute_result_success(self) -> None:
        """If result=SUCCESS, the task returns SUCCESS."""
        with mock.patch.object(
            self.task, "_upload_work_request_debug_logs", autospec=True
        ):
            self.assertEqual(self.task.execute(), WorkRequestResults.SUCCESS)

    def test_execute_result_failure(self) -> None:
        """If result=FAILURE, the task returns FAILURE."""
        self.task_data["result"] = WorkRequestResults.FAILURE
        with mock.patch.object(
            self.task, "_upload_work_request_debug_logs", autospec=True
        ):
            self.assertEqual(self.task.execute(), WorkRequestResults.FAILURE)

    def test_no_additional_properties(self) -> None:
        """Assert no additional properties."""
        error_msg = "Extra inputs are not permitted"
        with self.assertRaisesRegex(TaskConfigError, error_msg):
            SigningNoop({"input": {}})
