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

"""Helpers for testing signing tasks."""

from pathlib import Path
from typing import Any, override

from django.utils import timezone

from debusine.artifacts import LocalArtifact
from debusine.client.models import (
    ArtifactResponse,
    FileResponse,
    FilesResponseType,
)
from debusine.signing.tasks import BaseSigningTask
from debusine.tasks.tests.helpers import ExternalTaskTestCase
from debusine.test.test_utils import create_artifact_response
from debusine.utils import calculate_hash


class SigningTaskTestCase[TaskClass: BaseSigningTask[Any, Any]](
    ExternalTaskTestCase[TaskClass]
):
    """Helper mixin for Task tests."""

    @override
    def setUp(self) -> None:
        super().setUp()
        self.example_url = "http://example.org/"

    def make_files_response(self, files: dict[str, Path]) -> FilesResponseType:
        """Make a fake files response."""
        return FilesResponseType(
            {
                name: FileResponse(
                    size=path.stat().st_size,
                    checksums={"sha256": calculate_hash(path, "sha256").hex()},
                    type="file",
                    url=self.example_url,
                )
                for name, path in files.items()
            }
        )

    def make_artifact_response(
        self, artifact: LocalArtifact[Any], artifact_id: int
    ) -> ArtifactResponse:
        """Make a fake artifact response."""
        return create_artifact_response(
            id=artifact_id,
            workspace="System",
            category=artifact.category,
            created_at=timezone.now(),
            data=artifact.data.model_dump(),
            download_tar_gz_url=self.example_url,
            files_to_upload=[],
            files=self.make_files_response(artifact.files),
        )
