feat: refactor API key configuration and enhance application initialization
- Renamed `check_environment` to `check_api_key_configured` for clarity, simplifying the API key validation logic. - Removed the blocking behavior of the API key check during application startup, allowing the app to run while providing a prompt for configuration. - Updated `LocalAgentApp` to accept an `api_configured` parameter, enabling conditional messaging for API key setup. - Enhanced the `SandboxRunner` to support backup management and improved execution result handling with detailed metrics. - Integrated data governance strategies into the `HistoryManager`, ensuring compliance and improved data management. - Added privacy settings and metrics tracking across various components to enhance user experience and application safety.
This commit is contained in:
142
tests/test_task_features.py
Normal file
142
tests/test_task_features.py
Normal file
@@ -0,0 +1,142 @@
|
||||
"""
|
||||
任务特征提取与匹配的测试用例
|
||||
"""
|
||||
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
# 添加项目根目录到路径
|
||||
project_root = Path(__file__).parent.parent
|
||||
sys.path.insert(0, str(project_root))
|
||||
|
||||
from history.task_features import TaskFeatureExtractor, TaskMatcher
|
||||
|
||||
|
||||
def test_feature_extraction():
|
||||
"""测试特征提取"""
|
||||
print("=" * 60)
|
||||
print("测试 1: 特征提取")
|
||||
print("=" * 60)
|
||||
|
||||
extractor = TaskFeatureExtractor()
|
||||
|
||||
# 测试用例 1
|
||||
input1 = "将 D:/photos 目录下的所有 .jpg 图片按日期重命名"
|
||||
features1 = extractor.extract(input1)
|
||||
|
||||
print(f"\n输入: {input1}")
|
||||
print(f"文件格式: {features1.file_formats}")
|
||||
print(f"目录路径: {features1.directory_paths}")
|
||||
print(f"命名规则: {features1.naming_patterns}")
|
||||
print(f"操作类型: {features1.operations}")
|
||||
print(f"数量信息: {features1.quantities}")
|
||||
|
||||
# 测试用例 2
|
||||
input2 = "批量转换 C:/documents 下的 100 个 .docx 文件为 .pdf"
|
||||
features2 = extractor.extract(input2)
|
||||
|
||||
print(f"\n输入: {input2}")
|
||||
print(f"文件格式: {features2.file_formats}")
|
||||
print(f"目录路径: {features2.directory_paths}")
|
||||
print(f"命名规则: {features2.naming_patterns}")
|
||||
print(f"操作类型: {features2.operations}")
|
||||
print(f"数量信息: {features2.quantities}")
|
||||
|
||||
|
||||
def test_similarity_matching():
|
||||
"""测试相似度匹配"""
|
||||
print("\n" + "=" * 60)
|
||||
print("测试 2: 相似度匹配")
|
||||
print("=" * 60)
|
||||
|
||||
matcher = TaskMatcher()
|
||||
|
||||
# 测试场景 1: 高度相似(仅目录不同)
|
||||
print("\n场景 1: 高度相似任务(仅目录不同)")
|
||||
current1 = "将 D:/photos 目录下的所有 .jpg 图片按日期重命名"
|
||||
history1 = "将 C:/images 目录下的所有 .jpg 图片按日期重命名"
|
||||
|
||||
score1, diffs1 = matcher.calculate_similarity(current1, history1)
|
||||
print(f"当前任务: {current1}")
|
||||
print(f"历史任务: {history1}")
|
||||
print(f"相似度: {score1:.2%}")
|
||||
print(f"差异数量: {len(diffs1)}")
|
||||
for diff in diffs1:
|
||||
print(f" - {diff.category} [{diff.importance}]: 当前={diff.current_value}, 历史={diff.history_value}")
|
||||
|
||||
# 测试场景 2: 中等相似(格式和操作不同)
|
||||
print("\n场景 2: 中等相似任务(格式和操作不同)")
|
||||
current2 = "将 D:/photos 目录下的所有 .jpg 图片转换为 .png"
|
||||
history2 = "将 D:/photos 目录下的所有 .jpg 图片按日期重命名"
|
||||
|
||||
score2, diffs2 = matcher.calculate_similarity(current2, history2)
|
||||
print(f"当前任务: {current2}")
|
||||
print(f"历史任务: {history2}")
|
||||
print(f"相似度: {score2:.2%}")
|
||||
print(f"差异数量: {len(diffs2)}")
|
||||
for diff in diffs2:
|
||||
print(f" - {diff.category} [{diff.importance}]: 当前={diff.current_value}, 历史={diff.history_value}")
|
||||
|
||||
# 测试场景 3: 低相似度(完全不同的任务)
|
||||
print("\n场景 3: 低相似度任务(完全不同)")
|
||||
current3 = "将 D:/photos 目录下的所有 .jpg 图片按日期重命名"
|
||||
history3 = "统计 C:/documents 下所有 .txt 文件的行数"
|
||||
|
||||
score3, diffs3 = matcher.calculate_similarity(current3, history3)
|
||||
print(f"当前任务: {current3}")
|
||||
print(f"历史任务: {history3}")
|
||||
print(f"相似度: {score3:.2%}")
|
||||
print(f"差异数量: {len(diffs3)}")
|
||||
for diff in diffs3:
|
||||
print(f" - {diff.category} [{diff.importance}]: 当前={diff.current_value}, 历史={diff.history_value}")
|
||||
|
||||
# 测试场景 4: 关键参数差异(数量不同)
|
||||
print("\n场景 4: 关键参数差异(数量不同)")
|
||||
current4 = "批量转换 100 个 .docx 文件为 .pdf"
|
||||
history4 = "批量转换所有 .docx 文件为 .pdf"
|
||||
|
||||
score4, diffs4 = matcher.calculate_similarity(current4, history4)
|
||||
print(f"当前任务: {current4}")
|
||||
print(f"历史任务: {history4}")
|
||||
print(f"相似度: {score4:.2%}")
|
||||
print(f"差异数量: {len(diffs4)}")
|
||||
for diff in diffs4:
|
||||
print(f" - {diff.category} [{diff.importance}]: 当前={diff.current_value}, 历史={diff.history_value}")
|
||||
|
||||
|
||||
def test_edge_cases():
|
||||
"""测试边界情况"""
|
||||
print("\n" + "=" * 60)
|
||||
print("测试 3: 边界情况")
|
||||
print("=" * 60)
|
||||
|
||||
matcher = TaskMatcher()
|
||||
|
||||
# 空输入
|
||||
print("\n边界 1: 空输入")
|
||||
score, diffs = matcher.calculate_similarity("", "")
|
||||
print(f"相似度: {score:.2%}, 差异数: {len(diffs)}")
|
||||
|
||||
# 完全相同
|
||||
print("\n边界 2: 完全相同")
|
||||
same_input = "将 D:/photos 目录下的所有 .jpg 图片按日期重命名"
|
||||
score, diffs = matcher.calculate_similarity(same_input, same_input)
|
||||
print(f"相似度: {score:.2%}, 差异数: {len(diffs)}")
|
||||
|
||||
# 仅标点不同
|
||||
print("\n边界 3: 仅标点不同")
|
||||
input_a = "将D:/photos目录下的所有.jpg图片按日期重命名"
|
||||
input_b = "将 D:/photos 目录下的所有 .jpg 图片按日期重命名"
|
||||
score, diffs = matcher.calculate_similarity(input_a, input_b)
|
||||
print(f"相似度: {score:.2%}, 差异数: {len(diffs)}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_feature_extraction()
|
||||
test_similarity_matching()
|
||||
test_edge_cases()
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("所有测试完成!")
|
||||
print("=" * 60)
|
||||
|
||||
Reference in New Issue
Block a user