litellm/tests/unit/llms/manus/files/test_transformation.py
Mateo Wang d80f8c28ca
refactor(types): replace Any with proven types in 137 files (#44478)
* refactor(types): prove runtime types at harness, search, rag and client boundaries

Replace Any with adapter-validated types in the litellm.agent() harness, the
search provider transformations, RAG ingestion and query, the vector store
pre-call hook and registry, the galileo and opik logging integrations and the
proxy client CLI. Each boundary gets unit tests for well-formed and malformed
payloads.

* chore(typing): prove types at more provider boundaries and restore search transformations

Second pass over a2a, embedding, rerank, image, audio and small provider
modules. Search transformations go back to their previous form because
validating their response bodies would change the proxy status for
malformed upstream bodies from 400 to 500.

* refactor(types): prove types at logging, files, rerank, image, audio and management boundaries

Replace Any with validated or annotation-only types in 37 more files: logging
integrations, token counters, provider files/rerank/image generation/audio
transcription transformations, pass-through logging handlers and management
endpoints. No proxy HTTP status or error type changes.

* refactor(types): prove types at repository, spend, files and router boundaries

Replace Any with repository table accessors, validated mappings and
annotation-only types in 33 more files: Prisma repositories, the enterprise
batch and responses cost checkers, budget reservation, files endpoints,
management endpoints, the policy registry, the adaptive and complexity routers
and the secret managers. No proxy HTTP status or error type changes.

* test(types): run the aiohttp transformation test in-process and cover repository row conversion

The aiohttp chat transformation test no longer starts a server. It feeds the
transformation a response whose json() returns the body under test.

The proxy unit shards now exercise stored model rows whose params are JSON
strings and the object permission create and update paths.
2026-10-05 11:01:45 -07:00

128 lines
4.3 KiB
Python

import time
from unittest.mock import Mock
import httpx
import pytest
from pydantic import ValidationError
from litellm.llms.manus.files.transformation import ManusFilesConfig
from litellm.types.llms.openai import OpenAIFileObject
def _list_files(body: object) -> list[OpenAIFileObject]:
return ManusFilesConfig().transform_list_files_response(
raw_response=httpx.Response(200, json=body), logging_obj=Mock(), litellm_params={}
)
def test_delete_file_response_is_read_into_a_file_deleted_object():
deleted = ManusFilesConfig().transform_delete_file_response(
raw_response=httpx.Response(200, json={"id": "file-1", "deleted": True, "object": "file", "region": "eu"}),
logging_obj=Mock(),
litellm_params={},
)
assert deleted.model_dump() == {"id": "file-1", "deleted": True, "object": "file", "region": "eu"}
@pytest.mark.parametrize("body", [b'["secret-file"]', b'"secret-file"', b"7", b"null"])
def test_delete_file_response_rejects_a_body_that_is_not_an_object_without_echoing_it(body: bytes):
with pytest.raises(ValidationError) as exc_info:
ManusFilesConfig().transform_delete_file_response(
raw_response=httpx.Response(200, content=body), logging_obj=Mock(), litellm_params={}
)
assert "secret-file" not in str(exc_info.value)
def test_delete_file_response_requires_the_file_deleted_fields():
with pytest.raises(ValidationError, match="FileDeleted"):
ManusFilesConfig().transform_delete_file_response(
raw_response=httpx.Response(200, json={"id": "file-1"}), logging_obj=Mock(), litellm_params={}
)
def test_list_files_response_maps_every_listed_file():
files = _list_files(
{
"object": "list",
"data": [
{
"id": "file-1",
"bytes": 12,
"filename": "a.pdf",
"purpose": "batch",
"status": "processed",
"status_details": "done",
"created_at": "2024-01-02T03:04:05Z",
},
{"id": "file-2", "created_at": "2024-01-02T03:04:05.123456+00:00"},
],
}
)
created_at = int(time.mktime(time.strptime("2024-01-02T03:04:05", "%Y-%m-%dT%H:%M:%S")))
assert [file.model_dump() for file in files] == [
{
"id": "file-1",
"bytes": 12,
"created_at": created_at,
"filename": "a.pdf",
"object": "file",
"purpose": "batch",
"status": "processed",
"expires_at": None,
"status_details": "done",
},
{
"id": "file-2",
"bytes": 0,
"created_at": created_at,
"filename": "",
"object": "file",
"purpose": "assistants",
"status": "uploaded",
"expires_at": None,
"status_details": None,
},
]
@pytest.mark.parametrize("created_at", ["not-a-date", "", None, 0])
def test_list_files_response_falls_back_to_now_for_an_unusable_created_at(created_at: object):
before = int(time.time())
(file,) = _list_files({"data": [{"id": "file-1", "created_at": created_at}]})
assert before <= file.created_at <= int(time.time())
@pytest.mark.parametrize("body", [{}, {"data": []}])
def test_list_files_response_is_empty_without_listed_files(body: dict[str, object]):
assert _list_files(body) == []
@pytest.mark.parametrize(
"body",
[
["secret-file"],
"secret-file",
{"data": None},
{"data": 7},
{"data": "secret-file"},
{"data": ["secret-file"]},
{"data": [{"id": "file-1"}, ["secret-file"]]},
{"data": [{"id": "file-1", "created_at": ["secret-file"]}]},
{"data": [{"id": "file-1", "created_at": 1700000000}]},
],
)
def test_list_files_response_rejects_malformed_listings_without_echoing_them(body: object):
with pytest.raises(ValidationError) as exc_info:
_list_files(body)
assert "secret-file" not in str(exc_info.value)
def test_list_files_response_rejects_a_file_the_file_object_cannot_hold():
with pytest.raises(ValidationError, match="OpenAIFileObject"):
_list_files({"data": [{"id": "file-1", "purpose": "not-a-purpose"}]})