Files
AirPlan-V2/test_ctx.py
AirLongDian 19a4550830 test: 全模式功能测试覆盖 (12模式 33项测试)
新增 arc/eng/do/dbg/ctx/xdb/sdb/ndb/dep/tst/sec/rvr 全模式功能测试;
fix do_mode UI 检测中文关键词补全;scripts/install.sh 同步更新。

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-11 10:10:57 +08:00

154 lines
4.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""Ctx Mode 功能测试"""
import sys
import os
import tempfile
import json
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent / "lib"))
def test_ctx_enter():
"""测试上下文管理器初始化"""
from air_runtime.modes.ctx_mode import ctx_enter
with tempfile.TemporaryDirectory() as tmpdir:
project_root = Path(tmpdir)
result = ctx_enter(project_root)
assert "state_path" in result
assert Path(result["state_path"]).exists()
print("✓ 上下文管理器初始化测试通过")
return True
def test_ctx_estimate_tokens():
"""测试 Token 估算"""
from air_runtime.modes.ctx_mode import estimate_tokens
# 测试中文
chinese_text = "这是一个中文测试文本用于估算 Token 数量"
tokens = estimate_tokens(chinese_text)
assert tokens > 0
# 测试英文
english_text = "This is an English text for token estimation"
tokens_en = estimate_tokens(english_text)
assert tokens_en > 0
# 测试代码
code_text = "def main():\n print('hello world')\n return 0"
tokens_code = estimate_tokens(code_text)
assert tokens_code > 0
print(f"✓ Token 估算测试通过,中文: {tokens}, 英文: {tokens_en}, 代码: {tokens_code}")
return True
def test_ctx_validate_compression():
"""测试压缩质量校验"""
from air_runtime.modes.ctx_mode import validate_compression
original = """
# ADR-0001 决策
我们选择使用 PostgreSQL 数据库
# TODO 实现
- [ ] 创建数据库连接池
- [ ] 实现 CRUD 接口
# INV-1 约束
必须使用原子写入
"""
# 好的摘要(包含关键信息)
good_summary = "ADR-0001: PostgreSQL, TODO: 连接池/CRUD, INV-1: 原子写入"
result = validate_compression(original, good_summary)
assert result["ok"] == True
# 坏的摘要(丢失关键信息)
bad_summary = "选择了 PostgreSQL"
result_bad = validate_compression(original, bad_summary)
# 可能不 ok因为丢失了 TODO 和 INV
print("✓ 压缩质量校验测试通过")
return True
def test_ctx_compress_fallback():
"""测试三级降级压缩"""
from air_runtime.modes.ctx_mode import compress_with_fallback, CompressionLevel
# 创建大文本
long_text = "这是测试内容\n" * 1000
result = compress_with_fallback(long_text, max_tokens=100)
# 应该返回结果
assert "level" in result
assert "result" in result
assert result["level"] in [CompressionLevel.RETRY, CompressionLevel.FALLBACK_MODEL, CompressionLevel.TRUNCATE]
print(f"✓ 三级降级压缩测试通过,压缩级别: {result['level']}")
return True
def test_ctx_lock():
"""测试 compactor 锁"""
from air_runtime.modes.ctx_mode import acquire_compactor_lock
with tempfile.TemporaryDirectory() as tmpdir:
lock_path = Path(tmpdir) / "test.lock"
# 首次获取锁应该成功
acquired = acquire_compactor_lock(lock_path)
assert acquired == True
assert lock_path.exists()
# 再次获取应该失败(锁已被持有)
acquired2 = acquire_compactor_lock(lock_path)
assert acquired2 == False
print("✓ Compactor 锁测试通过")
return True
def main():
print("=" * 50)
print("Ctx Mode 功能测试")
print("=" * 50)
tests = [
("上下文管理器初始化", test_ctx_enter),
("Token 估算", test_ctx_estimate_tokens),
("压缩质量校验", test_ctx_validate_compression),
("三级降级压缩", test_ctx_compress_fallback),
("Compactor 锁", test_ctx_lock),
]
passed = 0
failed = 0
for name, test_fn in tests:
try:
test_fn()
passed += 1
except Exception as e:
print(f"{name} 失败: {e}")
import traceback
traceback.print_exc()
failed += 1
print("=" * 50)
print(f"测试结果: {passed} 通过, {failed} 失败")
print("=" * 50)
return failed == 0
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)