feat: add localization tests and improve locale handling in tools

This commit is contained in:
OhMyApps
2025-06-23 23:35:02 +02:00
parent 3368830f05
commit a46f8c2fad
4 changed files with 144 additions and 6 deletions

View File

@@ -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.")

View File

@@ -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()

View File

@@ -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)

View File

@@ -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 ===