"""Multipart field name for POST /api/sync/upload."""

from types import SimpleNamespace

from server.api.sync_v2 import _collect_multipart_upload_files


class _FakeForm:
    def __init__(self, data: dict[str, list]):
        self._data = data

    def getlist(self, key: str) -> list:
        return list(self._data.get(key, []))


def test_collect_file_field():
    form = _FakeForm({
        "file": [SimpleNamespace(filename="a.txt")],
    })
    items = _collect_multipart_upload_files(form)
    assert len(items) == 1
    assert items[0].filename == "a.txt"


def test_collect_legacy_files_field():
    form = _FakeForm({
        "files": [
            SimpleNamespace(filename="one.bin"),
            SimpleNamespace(filename="two.bin"),
        ],
    })
    items = _collect_multipart_upload_files(form)
    assert len(items) == 2


def test_collect_prefers_file_over_files():
    form = _FakeForm({
        "file": [SimpleNamespace(filename="primary.txt")],
        "files": [SimpleNamespace(filename="legacy.txt")],
    })
    items = _collect_multipart_upload_files(form)
    assert len(items) == 1
    assert items[0].filename == "primary.txt"


def test_collect_empty():
    assert _collect_multipart_upload_files(_FakeForm({})) == []
