"""Unit tests for app.py endpoints.""" import os import sys import unittest sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from fastapi.testclient import TestClient from app import app class TestAPIEndpoints(unittest.TestCase): @classmethod def setUpClass(cls): cls.client = TestClient(app) # Trigger lifespan cls.client.__enter__() @classmethod def tearDownClass(cls): cls.client.__exit__(None, None, None) def test_health_endpoint(self): """Health check should return ok status.""" response = self.client.get("/health") self.assertEqual(response.status_code, 200) data = response.json() self.assertEqual(data["status"], "ok") self.assertIn("model", data) self.assertIn(data["model"], ["qwen2.5-3b-lora", "fallback"]) def test_humanize_endpoint_fallback(self): """Humanize endpoint should work in fallback mode.""" response = self.client.post( "/humanize", json={"text": "In order to provide you with results, it is important to note that data was collected."} ) self.assertEqual(response.status_code, 200) data = response.json() self.assertIn("original", data) self.assertIn("humanized", data) self.assertIn("model", data) self.assertEqual(data["model"], "fallback") self.assertNotEqual(data["original"], data["humanized"]) def test_humanize_empty_text(self): """Empty text should return 400.""" response = self.client.post("/humanize", json={"text": " "}) self.assertEqual(response.status_code, 400) def test_humanize_missing_text(self): """Missing text field should return 422 validation error.""" response = self.client.post("/humanize", json={}) self.assertEqual(response.status_code, 422) def test_root_endpoint(self): """Root should serve HTML UI.""" response = self.client.get("/") self.assertEqual(response.status_code, 200) self.assertIn("text/html", response.headers["content-type"]) self.assertIn("humanize-e", response.text) if __name__ == '__main__': unittest.main()