# 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 noop task."""

from tempfile import TemporaryDirectory
from unittest import TestCase, mock

import debusine.worker.tags as wtags
from debusine.artifacts.models import WorkRequestResults
from debusine.tasks import Noop, TaskConfigError


class NoopTests(TestCase):
    """Test the Noop class."""

    def test_get_provided_worker_tags(self) -> None:
        task = Noop({"result": WorkRequestResults.SUCCESS})
        self.assertCountEqual(
            task.get_provided_worker_tags(),
            [
                wtags.TASK_PREFIX + "worker:noop:version:1",
            ],
        )

    def test_compute_system_required_tags(self) -> None:
        task = Noop({"result": WorkRequestResults.SUCCESS})
        self.assertCountEqual(task.compute_system_required_tags(), [])

    def test_execute_true(self) -> None:
        """Assert execute() return SUCCESS (result is SUCCESS)."""
        noop = Noop({"result": WorkRequestResults.SUCCESS})
        noop._debug_log_files_directory = TemporaryDirectory(
            prefix="debusine-tests-"
        )
        self.addCleanup(noop._debug_log_files_directory.cleanup)
        with mock.patch.object(
            noop, "_upload_work_request_debug_logs", autospec=True
        ):
            self.assertEqual(noop.execute(), WorkRequestResults.SUCCESS)

    def test_execute_false(self) -> None:
        """Assert execute() return FAILURE (result is FAILURE)."""
        noop = Noop({"result": WorkRequestResults.FAILURE})
        noop._debug_log_files_directory = TemporaryDirectory(
            prefix="debusine-tests-"
        )
        self.addCleanup(noop._debug_log_files_directory.cleanup)
        with mock.patch.object(
            noop, "_upload_work_request_debug_logs", autospec=True
        ):
            self.assertEqual(noop.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):
            Noop({"input": {}})
