|
1 | 1 | """Exercise client contracts without network access or real credentials.""" |
2 | 2 |
|
3 | 3 | import json |
| 4 | +from pathlib import Path |
4 | 5 | from typing import Any, Type |
5 | 6 |
|
6 | 7 | import httpx |
@@ -141,6 +142,37 @@ def handle(request: httpx.Request) -> httpx.Response: |
141 | 142 | assert seen[-1][2:] == ({"version": "v1"}, {"inputs": {"value": 3}}) |
142 | 143 |
|
143 | 144 |
|
| 145 | +def test_graph_media_upload_uses_multipart(tmp_path: Path) -> None: |
| 146 | + """Upload graph media as a typed multipart file without retrying.""" |
| 147 | + image = tmp_path / "thumbnail.jpg" |
| 148 | + image.write_bytes(b"jpeg-bytes") |
| 149 | + seen = [] |
| 150 | + |
| 151 | + def handle(request: httpx.Request) -> httpx.Response: |
| 152 | + seen.append(request) |
| 153 | + return httpx.Response(201, json={"artifact_id": "art_thumb"}) |
| 154 | + |
| 155 | + with Subfork(KEY, transport=httpx.MockTransport(handle)) as client: |
| 156 | + result = client.graphs.upload_media("g_test", "thumbnail", image) |
| 157 | + replacement = client.graphs.upload_media( |
| 158 | + "g_test", "screenshot", image, replace_id="art_old" |
| 159 | + ) |
| 160 | + with pytest.raises(ValueError, match="purpose"): |
| 161 | + client.graphs.upload_media("g_test", "unknown", image) |
| 162 | + |
| 163 | + assert result == replacement == {"artifact_id": "art_thumb"} |
| 164 | + assert len(seen) == 2 |
| 165 | + assert seen[0].method == "POST" |
| 166 | + assert seen[0].url.path == "/api/v1/graphs/g_test/media/thumbnail" |
| 167 | + assert str(seen[0].url.params) == "" |
| 168 | + assert seen[1].url.path == "/api/v1/graphs/g_test/media/screenshot" |
| 169 | + assert str(seen[1].url.params) == "replace_id=art_old" |
| 170 | + assert seen[0].headers["content-type"].startswith("multipart/form-data;") |
| 171 | + assert b'filename="thumbnail.jpg"' in seen[0].content |
| 172 | + assert b"image/jpeg" in seen[0].content |
| 173 | + assert b"jpeg-bytes" in seen[0].content |
| 174 | + |
| 175 | + |
144 | 176 | def test_transport_failure_is_not_retried() -> None: |
145 | 177 | """Verify transport failure is not retried.""" |
146 | 178 | seen = [] |
|
0 commit comments