From a46f8c2fad927e81acb004d6093404b90f14bf8e Mon Sep 17 00:00:00 2001 From: OhMyApps <74984020+GiGiDKR@users.noreply.github.com> Date: Mon, 23 Jun 2025 23:35:02 +0200 Subject: [PATCH] feat: add localization tests and improve locale handling in tools --- test_localization_debug.py | 35 +++++++++++++ test_simple_localization.py | 92 +++++++++++++++++++++++++++++++++ tests/test_utf8_localization.py | 13 +++-- tools/shared/base_tool.py | 10 ++-- 4 files changed, 144 insertions(+), 6 deletions(-) create mode 100644 test_localization_debug.py create mode 100644 test_simple_localization.py diff --git a/test_localization_debug.py b/test_localization_debug.py new file mode 100644 index 0000000..a3c12e9 --- /dev/null +++ b/test_localization_debug.py @@ -0,0 +1,35 @@ +import os +import sys + +sys.path.append(".") + +from tests.test_utf8_localization import TestTool + +# Test the language instruction generation +tool = TestTool() + +# Test French locale +print("Testing French locale...") +os.environ["LOCALE"] = "fr-FR" +instruction_fr = tool.get_language_instruction() +print(f'French instruction: "{instruction_fr}"') + +# Test English locale +print("Testing English locale...") +os.environ["LOCALE"] = "en-US" +instruction_en = tool.get_language_instruction() +print(f'English instruction: "{instruction_en}"') + +# Test empty locale +print("Testing empty locale...") +os.environ["LOCALE"] = "" +instruction_empty = tool.get_language_instruction() +print(f'Empty instruction: "{instruction_empty}"') + +# Test no locale +print("Testing no locale...") +os.environ.pop("LOCALE", None) +instruction_none = tool.get_language_instruction() +print(f'None instruction: "{instruction_none}"') + +print("Test completed.") diff --git a/test_simple_localization.py b/test_simple_localization.py new file mode 100644 index 0000000..3ee81e6 --- /dev/null +++ b/test_simple_localization.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python3 +""" +Simple test script to verify that the localization fix works correctly. +""" +import os +import sys + +# Set up path +sys.path.insert(0, ".") + + +# Simple test implementation that doesn't depend on heavy imports +class SimpleBaseTool: + def get_language_instruction(self) -> str: + """ + Generate language instruction based on LOCALE configuration. + This is the FIXED version that reads directly from environment. + """ + locale = os.getenv("LOCALE", "").strip() + if not locale: + return "" + return f"Always respond in {locale}.\n\n" + + +def test_localization(): + """Test the localization functionality.""" + tool = SimpleBaseTool() + + # Save original locale + original = os.environ.get("LOCALE") + + try: + print("=== Testing Localization Fix ===") + + # Test 1: French locale + print("\n1. Testing French locale...") + os.environ["LOCALE"] = "fr-FR" + instruction = tool.get_language_instruction() + expected = "Always respond in fr-FR.\n\n" + print(f" Expected: {repr(expected)}") + print(f" Got: {repr(instruction)}") + print(f" Result: {'✅ PASS' if instruction == expected else '❌ FAIL'}") + + # Test 2: English locale + print("\n2. Testing English locale...") + os.environ["LOCALE"] = "en-US" + instruction = tool.get_language_instruction() + expected = "Always respond in en-US.\n\n" + print(f" Expected: {repr(expected)}") + print(f" Got: {repr(instruction)}") + print(f" Result: {'✅ PASS' if instruction == expected else '❌ FAIL'}") + + # Test 3: Empty locale + print("\n3. Testing empty locale...") + os.environ["LOCALE"] = "" + instruction = tool.get_language_instruction() + expected = "" + print(f" Expected: {repr(expected)}") + print(f" Got: {repr(instruction)}") + print(f" Result: {'✅ PASS' if instruction == expected else '❌ FAIL'}") + + # Test 4: No locale (unset) + print("\n4. Testing unset locale...") + if "LOCALE" in os.environ: + del os.environ["LOCALE"] + instruction = tool.get_language_instruction() + expected = "" + print(f" Expected: {repr(expected)}") + print(f" Got: {repr(instruction)}") + print(f" Result: {'✅ PASS' if instruction == expected else '❌ FAIL'}") + + # Test 5: Locale with spaces + print("\n5. Testing locale with spaces...") + os.environ["LOCALE"] = " zh-CN " + instruction = tool.get_language_instruction() + expected = "Always respond in zh-CN.\n\n" + print(f" Expected: {repr(expected)}") + print(f" Got: {repr(instruction)}") + print(f" Result: {'✅ PASS' if instruction == expected else '❌ FAIL'}") + + finally: + # Restore original locale + if original is not None: + os.environ["LOCALE"] = original + else: + os.environ.pop("LOCALE", None) + + print("\n=== Test Complete ===") + + +if __name__ == "__main__": + test_localization() diff --git a/tests/test_utf8_localization.py b/tests/test_utf8_localization.py index 1d918b0..38e68e3 100644 --- a/tests/test_utf8_localization.py +++ b/tests/test_utf8_localization.py @@ -207,7 +207,8 @@ class TestUTF8Localization(unittest.TestCase): # Created by: Lead Developer # Creation date: December 15, 2024 -def process_data(preferences, parameters): ''' +def process_data(preferences, parameters): + ''' Processes data according to user preferences. Args: @@ -358,7 +359,9 @@ class TestLocalizationIntegration(unittest.TestCase): "relevant_files": ["/test/example.py"], "model": "test-model", } - ) # Checks + ) + + # Checks self.assertIsNotNone(result) self.assertEqual(len(result), 1) @@ -385,8 +388,12 @@ class TestLocalizationIntegration(unittest.TestCase): # Spanish os.environ["LOCALE"] = "es-ES" + instruction_es = tool.get_language_instruction() # Spanish + os.environ["LOCALE"] = "es-ES" instruction_es = tool.get_language_instruction() - self.assertIn("es-ES", instruction_es) # Chinese + self.assertIn("es-ES", instruction_es) + + # Chinese os.environ["LOCALE"] = "zh-CN" instruction_zh = tool.get_language_instruction() self.assertIn("zh-CN", instruction_zh) diff --git a/tools/shared/base_tool.py b/tools/shared/base_tool.py index ca04e91..c832875 100644 --- a/tools/shared/base_tool.py +++ b/tools/shared/base_tool.py @@ -1079,13 +1079,17 @@ When recommending searches, be specific about what information you need and why str: Language instruction to prepend to prompt, or empty string if no locale set """ - from config import LOCALE + # Read LOCALE directly from environment to support dynamic changes + # This allows tests to modify os.environ["LOCALE"] and see the changes + import os - if not LOCALE or not LOCALE.strip(): + locale = os.getenv("LOCALE", "").strip() + + if not locale: return "" # Simple language instruction - return f"Always respond in {LOCALE.strip()}.\n\n" + return f"Always respond in {locale}.\n\n" # === ABSTRACT METHODS FOR SIMPLE TOOLS ===