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