from types import SimpleNamespace
from django.core.files.uploadedfile import SimpleUploadedFile

import atlas.api as atlas_api
from atlas.models import DuplicateDicom, SeriesImage


def test_upload_dicom_duplicate_series_image_without_series_does_not_500(
    monkeypatch,
):
    request = SimpleNamespace(user=SimpleNamespace(is_authenticated=True))

    class DummyUncategorisedDicom:
        def __init__(self, image, user):
            self.image = image
            self.user = user
            self.image_blake3_hash = "hash123"

        def save(self, *args, **kwargs):
            raise DuplicateDicom(SeriesImage(series=None))

    monkeypatch.setattr(atlas_api, "UncategorisedDicom", DummyUncategorisedDicom)

    upload = SimpleUploadedFile(
        "duplicate-no-series.dcm",
        b"dicom-bytes",
        content_type="application/dicom",
    )
    payload = atlas_api.upload_dicom(request, files=[upload])

    assert payload["uploaded"] == []
    assert payload["failed"] == []
    assert payload["duplicate_series"] == []
    assert payload["duplicates"] == [("duplicate-no-series.dcm", "hash123")]
