import unittest from unittest.mock import Mock, patch import httpx from shop_ledger.modal_client import ModalBackendClient class ModalBackendClientTests(unittest.TestCase): def test_process_posts_to_modal_api(self): response = Mock() response.json.return_value = {"entries": [{"amount": 1200}], "model_used": "llama.cpp"} response.raise_for_status.return_value = None with patch("shop_ledger.modal_client.httpx.post", return_value=response) as post: result = ModalBackendClient("https://example.modal.run/").process("paid Ravi 1200", "LKR") self.assertEqual(result["model_used"], "llama.cpp") post.assert_called_once_with( "https://example.modal.run/api/process", json={"note": "paid Ravi 1200", "currency": "LKR", "image_urls": None}, timeout=1800.0, ) def test_process_returns_visible_backend_error_on_failure(self): with patch("shop_ledger.modal_client.httpx.post", side_effect=httpx.ConnectError("no route")): result = ModalBackendClient("https://example.modal.run").process("paid Ravi 1200", "LKR") self.assertEqual(result["entries"], []) self.assertEqual(result["model_used"], "modal backend unavailable") self.assertIn("Modal backend", result["questions"][0]) if __name__ == "__main__": unittest.main()