SPB Git forge

spb/doc-api

Public
2commits 1branches 0releases
15.7 MBsize
maindefault branch
13 days agolast push
Python 88.3% TypeScript 7.6% Shell 4.1%
3.6 KB · 72 lines python
Raw Blame History
1"""Files API smoke tests (GA, no beta header): upload → list → retrieve → use in Messages → content 400 → delete → 404.2All Files operations are free; the Messages call costs ≈$0.0001.3"""4from __future__ import annotations56import uuid78import pytest91011def _multipart(filename: str, content: bytes, mime: str, fields: dict | None = None) -> tuple[bytes, str]:12    b = "----atlas" + uuid.uuid4().hex13    body = b""14    for k, v in (fields or {}).items():15        body += f"--{b}\r\nContent-Disposition: form-data; name=\"{k}\"\r\n\r\n{v}\r\n".encode()16    body += (f"--{b}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"{filename}\"\r\nContent-Type: {mime}\r\n\r\n").encode()17    body += content + f"\r\n--{b}--\r\n".encode()18    return body, f"multipart/form-data; boundary={b}"192021@pytest.fixture22def uploaded(anthropic):23    data, ct = _multipart("atlas-test.txt", b"The sky is blue. Grass is green.", "text/plain", {"expires_in_seconds": "3600"})24    st, body, _ = anthropic("POST", "/v1/files", data=data, content_type=ct, note="test_files upload")25    assert st == 200, body26    yield body27    st, out, _ = anthropic("DELETE", f"/v1/files/{body['id']}", note="test_files delete")28    assert st == 200 and out["type"] == "file_deleted"293031def test_upload_shape(uploaded):32    assert uploaded["type"] == "file" and uploaded["id"].startswith("file_")33    assert uploaded["mime_type"] == "text/plain" and uploaded["downloadable"] is False34    assert uploaded["expires_at"]  # set because expires_in_seconds was given353637def test_list_retrieve_and_ids_filter(anthropic, uploaded):38    st, lst, _ = anthropic("GET", "/v1/files?limit=5", note="test_files list")39    assert st == 200 and "data" in lst and "next_page" in lst40    st, one, _ = anthropic("GET", f"/v1/files?ids[]={uploaded['id']}", note="test_files ids filter")41    assert st == 200 and [f["id"] for f in one["data"]] == [uploaded["id"]] and one["next_page"] is None42    st, meta, _ = anthropic("GET", f"/v1/files/{uploaded['id']}", note="test_files retrieve")43    assert st == 200 and meta["id"] == uploaded["id"]444546def test_uploaded_file_is_not_downloadable(anthropic, uploaded):47    st, body, _ = anthropic("GET", f"/v1/files/{uploaded['id']}/content", note="test_files content of uploaded file")48    assert st == 400 and "not downloadable" in body["error"]["message"]495051def test_document_file_id_in_messages(anthropic, models, uploaded):52    content = [{"type": "document", "source": {"type": "file", "file_id": uploaded["id"]}}, {"type": "text", "text": "What color is grass? One word."}]53    st, body, _ = anthropic("POST", "/v1/messages", {"model": models["anthropic"], "max_tokens": 10, "messages": [{"role": "user", "content": content}]},54                            est_cost_usd=0.0001, note="test_files document file_id")55    assert st == 200, body56    assert "green" in body["content"][0]["text"].lower()575859def test_expires_in_seconds_range(anthropic):60    data, ct = _multipart("short.txt", b"x", "text/plain", {"expires_in_seconds": "60"})61    st, body, _ = anthropic("POST", "/v1/files", data=data, content_type=ct, note="test_files expires too short")62    assert st == 400 and "3600" in body["error"]["message"]636465def test_deleted_file_404(anthropic):66    data, ct = _multipart("gone.txt", b"bye", "text/plain")67    st, f, _ = anthropic("POST", "/v1/files", data=data, content_type=ct, note="test_files upload for 404")68    assert st == 20069    anthropic("DELETE", f"/v1/files/{f['id']}", note="test_files delete for 404")70    st, body, _ = anthropic("GET", f"/v1/files/{f['id']}", note="test_files retrieve deleted")71    assert st == 404 and body["error"]["type"] == "not_found_error"72