import os import unittest from unittest.mock import patch from fastapi import HTTPException from services.word_scan_service import word_scan_service class WordScanServiceTest(unittest.TestCase): def tearDown(self): get_settings = __import__("config", fromlist=["get_settings"]).get_settings get_settings.cache_clear() def test_heuristic_parse_tab_separated_pairs(self): with patch.dict(os.environ, {"DEEPSEEK_API_KEY": ""}, clear=False): __import__("config", fromlist=["get_settings"]).get_settings.cache_clear() items = word_scan_service.parse_scan_text("apple\t苹果\nbanana\t香蕉") self.assertEqual(len(items), 2) self.assertEqual(items[0]["source_text"], "apple") self.assertEqual(items[0]["target_text"], "苹果") def test_empty_text_raises(self): with self.assertRaises(HTTPException) as ctx: word_scan_service.parse_scan_text(" ") self.assertEqual(ctx.exception.status_code, 400) def test_unparseable_text_raises(self): with patch.dict(os.environ, {"DEEPSEEK_API_KEY": ""}, clear=False): __import__("config", fromlist=["get_settings"]).get_settings.cache_clear() with self.assertRaises(HTTPException) as ctx: word_scan_service.parse_scan_text("hello world only english") self.assertEqual(ctx.exception.status_code, 422) if __name__ == "__main__": unittest.main()