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

from debusine.artifacts.models import WorkRequestResults
from debusine.tasks import Noop
from debusine.tasks.tests.helpers import ExternalTaskHelperMixin
from debusine.test import TestCase


class HelpersTests(ExternalTaskHelperMixin[Noop], TestCase):
    """Test the external task test helpers."""

    def test_set_current_does_not_manage_twice(self) -> None:
        # Task is managed on creation
        task1 = self.task
        assert task1 in self.tasks
        self.task_reset_current()

        # A new task is also managed on creation
        self.task_data["result"] = WorkRequestResults.FAILURE
        task2 = self.task
        assert task2 in self.tasks

        assert task1 != task2

        with self.spy(
            "debusine.tasks.tests.helpers.TaskTestCase.task_manage"
        ) as task_manage:
            self.task_set_current(task1)

        self.assertIs(self.task, task1)
        task_manage.assert_not_called()
