Spaces:
Runtime error
Runtime error
File size: 4,192 Bytes
6f423bd 8fd4e2b 6f423bd 1d60f3a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | import pytest
fastapi = pytest.importorskip("fastapi")
from fastapi.testclient import TestClient # noqa: E402
from plotscribe.web import app # noqa: E402
from tests import synth # noqa: E402
def test_upload_roundtrip(tmp_path):
img = synth.generate(9100, tmp_path, "clean", "bar")
client = TestClient(app)
assert client.get("/").status_code == 200
r = client.post("/api/extract", files={"file": (img.name, img.read_bytes(), "image/png")})
assert r.status_code == 200
d = r.json()
assert d["status"] == "ok" and d["csv"].startswith("category")
plot = d["plot"]
assert plot["x"]["kind"] == "categorical" and plot["series"]
assert all(len(p) == 2 for s in plot["series"] for p in s["points"])
assert "range" in plot["y"]
def test_rejects_non_image():
r = TestClient(app).post("/api/extract", files={"file": ("x.txt", b"hello", "text/plain")})
assert r.status_code == 400
def _mcp_call(client, method, params, id_=1):
r = client.post("/mcp", json={"jsonrpc": "2.0", "id": id_, "method": method, "params": params},
headers={"Accept": "application/json, text/event-stream"})
assert r.status_code == 200, r.text
return r.json()
def test_mcp_extract_base64(tmp_path):
pytest.importorskip("mcp")
import base64
import json
img = synth.generate(9101, tmp_path, "clean", "bar")
with TestClient(app) as client:
tools = _mcp_call(client, "tools/list", {})["result"]["tools"]
assert [t["name"] for t in tools] == ["extract_chart_data"]
res = _mcp_call(client, "tools/call", {"name": "extract_chart_data", "arguments": {
"image_base64": base64.b64encode(img.read_bytes()).decode()}}, 2)["result"]
assert not res.get("isError"), res
out = res.get("structuredContent") or json.loads(res["content"][0]["text"])
assert out["status"] == "ok" and out["csv"].startswith("category") and out["n_rows"] > 0
bad = _mcp_call(client, "tools/call", {"name": "extract_chart_data", "arguments": {
"image_base64": base64.b64encode(b"hello").decode()}}, 3)["result"]
assert bad["isError"] and "PNG or JPG" in bad["content"][0]["text"]
def test_mcp_rejects_private_url():
pytest.importorskip("mcp")
from plotscribe.service import InputError, fetch_image
for url in ("http://127.0.0.1/x.png", "http://169.254.169.254/latest", "file:///etc/passwd"):
with pytest.raises(InputError):
fetch_image(url)
def test_llms_txt():
r = TestClient(app).get("/llms.txt")
assert r.status_code == 200 and "extract_chart_data" in r.text
def test_extract_by_url(tmp_path, monkeypatch):
import plotscribe.web as web
img = synth.generate(9102, tmp_path, "clean", "bar")
monkeypatch.setattr(web, "fetch_image", lambda url: img.read_bytes())
client = TestClient(app)
r = client.get("/api/extract", params={"url": "https://example.org/chart.png"})
assert r.status_code == 200 and r.headers["content-type"].startswith("text/plain")
assert "status: ok" in r.text and "\ncategory," in r.text
d = client.get("/api/extract", params={"url": "https://example.org/chart.png", "format": "json"}).json()
assert d["status"] == "ok" and d["csv"].startswith("category")
def test_extract_by_url_errors_readable():
client = TestClient(app)
r = client.get("/api/extract", params={"url": "http://127.0.0.1/x.png"})
assert r.status_code == 200 and "status: error" in r.text and "public address" in r.text
assert client.get("/api/extract", params={"url": "http://127.0.0.1/x.png", "format": "json"}).status_code == 400
def test_page_has_ai_instructions():
from plotscribe.web import PUBLIC_URL
page = TestClient(app).get("/").text
assert f"{PUBLIC_URL}/api/extract?url=" in page and f"{PUBLIC_URL}/mcp" in page and "@PUBLIC_URL@" not in page
def test_mcp_local_file_tool(tmp_path):
pytest.importorskip("mcp")
import anyio
from plotscribe.mcp_server import extract_chart_file
img = synth.generate(9103, tmp_path, "clean", "bar")
out = anyio.run(extract_chart_file, str(img))
assert out["status"] == "ok" and out["n_rows"] > 0
|