""" Automated unit and integration tests for Handwriting Synthesis Backend API. """ import os import sys import pytest import httpx # Add backend directory to sys.path sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))) from app import app from core.paper_generator import generate_paper_texture from core.photo_engine import transform_to_realistic_photo from core.mistake_generator import inject_auto_mistakes, parse_mistake_markup, generate_scratch_path from core.neural_stroke import synthesize_procedural_strokes @pytest.mark.asyncio async def test_health_endpoint(): async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://testserver") as client: response = await client.get("/api/health") assert response.status_code == 200 data = response.json() assert data["status"] == "healthy" assert "cuda_available" in data assert data["supported_papers"] >= 4 @pytest.mark.asyncio async def test_list_papers(): async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://testserver") as client: response = await client.get("/api/papers") assert response.status_code == 200 papers = response.json()["papers"] paper_ids = [p["id"] for p in papers] assert "plain_a4" in paper_ids assert "dirty_a4" in paper_ids assert "ruled_red_margin" in paper_ids assert "ruled_copy" in paper_ids def test_paper_generator_shapes(): for ptype in ["plain_a4", "dirty_a4", "ruled_copy", "ruled_red_margin"]: img = generate_paper_texture(ptype, width=600, height=800) assert img.size == (600, 800) def test_mistake_markup_parser(): text = "We have ~~theroy~~ theory and [insert:very] clear notes." tokens = parse_mistake_markup(text) scratch_tokens = [t for t in tokens if t["type"] == "scratch"] insert_tokens = [t for t in tokens if t["type"] == "insert"] assert len(scratch_tokens) == 1 assert scratch_tokens[0]["text"] == "theroy" assert len(insert_tokens) == 1 assert insert_tokens[0]["text"] == "very" @pytest.mark.asyncio async def test_inject_mistakes_api(): async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://testserver") as client: response = await client.post("/api/inject-mistakes", json={ "text": "The theory and equation is definitely verified.", "frequency": 0.5, "seed": 42 }) assert response.status_code == 200 data = response.json() assert "modified" in data assert "tokens" in data assert len(data["tokens"]) > 0 @pytest.mark.asyncio async def test_neural_stroke_api(): async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://testserver") as client: response = await client.post("/api/neural-stroke", json={ "text": "Hello World", "speed": 1.0, "wobble": 0.5, "seed": 42 }) assert response.status_code == 200 data = response.json() assert data["num_strokes"] > 0 assert len(data["strokes"]) > 0 def test_photo_realistic_transform(): img = generate_paper_texture("plain_a4", width=400, height=600) photo = transform_to_realistic_photo(img, enable_curvature=True, enable_tilt=True, enable_phone_shadow=True) assert photo.size == (400, 600) @pytest.mark.asyncio async def test_handwriting_template_api(): async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://testserver") as client: # Test JSON response res = await client.get("/api/handwriting-template") assert res.status_code == 200 data = res.json() assert data["success"] is True assert data["template_data_url"].startswith("data:image/png;base64,") # Test download response res_dl = await client.get("/api/handwriting-template?download=true") assert res_dl.status_code == 200 assert res_dl.headers["content-type"] == "image/png" assert len(res_dl.content) > 1000 @pytest.mark.asyncio async def test_extract_custom_font_api(): from core.font_extractor import generate_handwriting_template import io, base64 # Generate template image, simulate uploading it tpl = generate_handwriting_template() buf = io.BytesIO() tpl.save(buf, format="PNG") b64 = base64.b64encode(buf.getvalue()).decode("utf-8") async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://testserver") as client: res = await client.post("/api/extract-custom-font", json={ "image_base64": f"data:image/png;base64,{b64}", "font_name": "TestClonedFont" }) assert res.status_code == 200 data = res.json() assert data["success"] is True assert data["font_name"] == "TestClonedFont" assert data["num_glyphs"] > 0 assert len(data["ttf_base64"]) > 500 assert "A" in data["preview_glyphs"] @pytest.mark.asyncio async def test_clone_from_notes_api(): from tests.test_notes_cloner import create_mock_note_image import io, base64 mock_img = create_mock_note_image() buf = io.BytesIO() mock_img.save(buf, format="JPEG", quality=90) b64 = "data:image/jpeg;base64," + base64.b64encode(buf.getvalue()).decode("utf-8") async with httpx.AsyncClient(transport=httpx.ASGITransport(app=app), base_url="http://testserver") as client: res = await client.post("/api/clone-from-notes", json={ "images_base64": [b64], "font_name": "APIClonedFont" }) assert res.status_code == 200 data = res.json() assert data["success"] is True assert data["fontName"] == "APIClonedFont" assert len(data["ttfBase64"]) > 500 assert "styleProfile" in data assert "paperType" in data["styleProfile"] assert "penType" in data["styleProfile"]