Add chat image upload support

This commit is contained in:
Xin Wang
2026-06-01 13:39:22 +08:00
parent a55ca37c39
commit 96d685da91
7 changed files with 951 additions and 4 deletions

View File

@@ -358,6 +358,64 @@ class TestAsyncChatClient:
assert response.status_code == 200
await client.close()
@pytest.mark.asyncio
async def test_presign_chat_file_post_url(self, api_key):
"""Test async presigned chat file upload URL creation."""
client = AsyncChatClient(api_key)
mock_response = Mock(spec=httpx.Response)
mock_response.status_code = 200
mock_response.json = Mock(return_value={
"code": 200,
"message": "",
"data": {"url": "https://s3.example/upload", "fields": {"key": "chat/file.png"}},
})
with patch.object(client, "_send_request", AsyncMock(return_value=mock_response)) as mock_send:
result = await client.presign_chat_file_post_url(
appId="app-123",
chatId="chat-123",
filename="file.png",
fileSelectConfig={"canSelectImg": True},
)
assert result["fields"]["key"] == "chat/file.png"
assert mock_send.call_args[0][1] == "/api/core/chat/presignChatFilePostUrl"
await client.close()
@pytest.mark.asyncio
async def test_upload_chat_image_with_fields_gets_preview_url(self, api_key, tmp_path):
"""Test async upload_chat_image normalizes presigned POST uploads."""
client = AsyncChatClient(api_key)
file_path = tmp_path / "file.png"
file_path.write_bytes(b"png")
with patch.object(client, "_get_chat_file_select_config", AsyncMock(return_value={"canSelectImg": True})), \
patch.object(client, "presign_chat_file_post_url", AsyncMock(return_value={
"url": "https://s3.example/upload",
"fields": {"key": "chat/file.png"},
"maxSize": 100,
})) as mock_presign, \
patch.object(client, "upload_to_presigned_url", AsyncMock()) as mock_upload, \
patch.object(client, "presign_chat_file_get_url", AsyncMock(return_value="https://s3.example/preview.png")) as mock_get:
result = await client.upload_chat_image(
appId="app-123",
chatId="chat-123",
file_path=file_path,
)
assert result == {
"type": "img",
"name": "file.png",
"key": "chat/file.png",
"url": "https://s3.example/preview.png",
"previewUrl": "https://s3.example/preview.png",
}
assert mock_presign.call_args[1]["fileSelectConfig"] == {"canSelectImg": True}
mock_upload.assert_awaited_once()
mock_get.assert_awaited_once_with(appId="app-123", key="chat/file.png", outLinkAuthData=None)
await client.close()
@pytest.mark.asyncio
async def test_get_chat_histories_basic(self, api_key, sample_chat_histories_response):
"""Test getting chat histories with basic parameters."""