test_default_features.py 4.02 KB
#!/usr/bin/env python3
"""
测试默认功能是否正确开启
"""

import sys
import os

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

def test_default_features():
    """测试默认功能是否正确开启"""
    print("🧪 测试默认功能开启状态")
    print("=" * 60)

    try:
        from config.config_loader import ConfigLoader
        from search.searcher import Searcher
        from utils.es_client import ESClient
        from context.request_context import create_request_context

        # 加载配置
        print("📝 加载配置...")
        loader = ConfigLoader()
        config = loader.load_customer_config("customer1")

        print(f"✅ 配置文件设置:")
        print(f"  - enable_translation: {config.query_config.enable_translation}")
        print(f"  - enable_text_embedding: {config.query_config.enable_text_embedding}")

        # 创建搜索器(模拟没有ES连接的情况)
        print(f"\n🔍 创建搜索器...")

        # 创建一个模拟的ES客户端用于测试
        class MockESClient:
            def search(self, **kwargs):
                return {
                    "hits": {"hits": [], "total": {"value": 0}, "max_score": 0.0},
                    "took": 10
                }

        es_client = MockESClient()
        searcher = Searcher(config, es_client)

        # 测试不同参数组合
        test_cases = [
            {"name": "不传递任何参数", "params": {}},
            {"name": "显式传递None", "params": {"enable_translation": None, "enable_embedding": None}},
            {"name": "显式传递False", "params": {"enable_translation": False, "enable_embedding": False}},
            {"name": "显式传递True", "params": {"enable_translation": True, "enable_embedding": True}},
        ]

        print(f"\n🧪 测试不同参数组合:")
        for test_case in test_cases:
            print(f"\n  📋 {test_case['name']}:")

            try:
                # 执行搜索
                result = searcher.search(
                    query="推车",
                    context=create_request_context("test_features", "test_user"),
                    **test_case['params']
                )

                # 检查上下文中的功能标志
                context_summary = create_request_context("test_features", "test_user").get_summary()
                # 由于我们无法直接获取内部的context,我们检查配置
                print(f"    ✅ 搜索执行成功")

            except Exception as e:
                print(f"    ❌ 搜索失败: {e}")

        # 测试配置驱动的默认行为
        print(f"\n🔧 配置驱动的默认行为测试:")

        # 模拟API调用(不传递参数,应该使用配置默认值)
        context = create_request_context("config_default_test", "config_user")

        print(f"  配置默认值:")
        print(f"    - 翻译功能: {'启用' if config.query_config.enable_translation else '禁用'}")
        print(f"    - 向量功能: {'启用' if config.query_config.enable_text_embedding else '禁用'}")

        # 验证配置逻辑
        expected_translation = config.query_config.enable_translation
        expected_embedding = config.query_config.enable_text_embedding

        print(f"\n✅ 预期行为:")
        print(f"  当API调用不传递enable_translation参数时,应该: {'启用翻译' if expected_translation else '禁用翻译'}")
        print(f"  当API调用不传递enable_embedding参数时,应该: {'启用向量' if expected_embedding else '禁用向量'}")

        if expected_translation and expected_embedding:
            print(f"\n🎉 配置正确!系统默认启用翻译和向量功能。")
            return True
        else:
            print(f"\n⚠️  配置可能需要调整。")
            return False

    except Exception as e:
        print(f"❌ 测试失败: {e}")
        import traceback
        traceback.print_exc()
        return False

if __name__ == "__main__":
    success = test_default_features()
    sys.exit(0 if success else 1)