|
| 1 | +# Copyright (c) 2026 ByteDance Ltd. and/or its affiliates. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +import asyncio |
| 5 | + |
| 6 | +import httpx |
| 7 | +import pytest |
| 8 | + |
| 9 | +from arkruntime import Ark, AsyncArk |
| 10 | +from arkruntime._list_response import normalize_list_response |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture(autouse=True) |
| 14 | +def synthetic_credentials(monkeypatch): |
| 15 | + # MockTransport handles every request; no real credential is needed. |
| 16 | + monkeypatch.setenv("ARK_API_KEY", "placeholder") |
| 17 | + |
| 18 | + |
| 19 | +@pytest.mark.parametrize("api", ["files", "resources"]) |
| 20 | +@pytest.mark.parametrize("payload", ["null", " \n null \t", '{"data":null}', '{"data":[]}']) |
| 21 | +def test_null_lists_sync(api, payload): |
| 22 | + transport = httpx.MockTransport( |
| 23 | + lambda r: httpx.Response(200, text=payload, headers={"content-type": "application/json"}) |
| 24 | + ) |
| 25 | + with Ark(base_url="https://example.test/custom/v3", http_client=httpx.Client(transport=transport)) as client: |
| 26 | + result = client.files.list() if api == "files" else client.sessions.resources.list("session-1") |
| 27 | + assert result is not None |
| 28 | + assert result.data == [] |
| 29 | + if api == "files": |
| 30 | + assert list(result) == [] |
| 31 | + assert not result.has_next_page() |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.parametrize("api", ["files", "resources"]) |
| 35 | +@pytest.mark.parametrize("payload", ["null", " \n null \t", '{"data":null}', '{"data":[]}']) |
| 36 | +def test_null_lists_async(api, payload): |
| 37 | + async def run(): |
| 38 | + transport = httpx.MockTransport( |
| 39 | + lambda r: httpx.Response(200, text=payload, headers={"content-type": "application/json"}) |
| 40 | + ) |
| 41 | + http_client = httpx.AsyncClient(transport=transport) |
| 42 | + async with AsyncArk(base_url="https://example.test/custom/v3", http_client=http_client) as client: |
| 43 | + result = await client.files.list() if api == "files" else await client.sessions.resources.list("session-1") |
| 44 | + assert result is not None |
| 45 | + assert result.data == [] |
| 46 | + if api == "files": |
| 47 | + assert [item async for item in result] == [] |
| 48 | + assert not result.has_next_page() |
| 49 | + |
| 50 | + asyncio.run(run()) |
| 51 | + |
| 52 | + |
| 53 | +def test_pagination_and_nonempty_items_are_preserved(): |
| 54 | + payload = { |
| 55 | + "object": "file", |
| 56 | + "data": [{"id": "file-1"}], |
| 57 | + "first_id": "file-1", |
| 58 | + "last_id": "file-1", |
| 59 | + "has_more": True, |
| 60 | + } |
| 61 | + transport = httpx.MockTransport(lambda r: httpx.Response(200, json=payload)) |
| 62 | + with Ark(http_client=httpx.Client(transport=transport)) as client: |
| 63 | + result = client.files.list() |
| 64 | + assert result.data[0].id == "file-1" |
| 65 | + assert result.first_id == result.last_id == "file-1" |
| 66 | + assert result.has_next_page() |
| 67 | + |
| 68 | + |
| 69 | +@pytest.mark.parametrize( |
| 70 | + "path,method,status", |
| 71 | + [ |
| 72 | + ("/files/file-1", "GET", 200), |
| 73 | + ("/agents", "GET", 200), |
| 74 | + ("/files", "POST", 200), |
| 75 | + ("/files", "GET", 400), |
| 76 | + ("/sessions/session-1/resources", "POST", 200), |
| 77 | + ], |
| 78 | +) |
| 79 | +def test_other_operations_and_errors_are_not_normalized(path, method, status): |
| 80 | + response = httpx.Response(status, request=httpx.Request(method, "https://example.test" + path)) |
| 81 | + assert normalize_list_response(None, response, "") is None |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.parametrize("data", [{}, {"data": "invalid"}, [], "invalid", 123]) |
| 85 | +def test_invalid_shapes_are_not_normalized(data): |
| 86 | + response = httpx.Response(200, request=httpx.Request("GET", "https://example.test/files")) |
| 87 | + assert normalize_list_response(data, response, "") is data |
| 88 | + |
| 89 | + |
| 90 | +def test_resources_keep_existing_items(): |
| 91 | + payload = {"data": [{"id": "resource-1", "type": "file", "file_id": "file-1"}]} |
| 92 | + transport = httpx.MockTransport(lambda r: httpx.Response(200, json=payload)) |
| 93 | + with Ark(http_client=httpx.Client(transport=transport)) as client: |
| 94 | + result = client.sessions.resources.list("session-1") |
| 95 | + assert result.data[0].id == "resource-1" |
| 96 | + assert result.data[0].file_id == "file-1" |
| 97 | + |
| 98 | + |
| 99 | +def test_null_data_keeps_pagination_fields(): |
| 100 | + payload = {"data": None, "first_id": "cursor", "last_id": "cursor", "has_more": True} |
| 101 | + transport = httpx.MockTransport(lambda r: httpx.Response(200, json=payload)) |
| 102 | + with Ark(http_client=httpx.Client(transport=transport)) as client: |
| 103 | + result = client.files.list() |
| 104 | + assert result.data == [] |
| 105 | + assert result.first_id == result.last_id == "cursor" |
| 106 | + assert result.has_more is True |
0 commit comments