fix: restore main runtime sources and cache consistency

This commit is contained in:
User
2026-07-14 14:19:36 +08:00
parent e40989eeab
commit f951bc6598
15 changed files with 1664 additions and 29 deletions

View File

@@ -9,12 +9,30 @@ Phase 3 验证测试:重复上传旧切片残留修复
import os
import sys
import tempfile
import shutil
import uuid
from pathlib import Path
# 添加项目根目录到 Python 路径
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEST_TEMP_ROOT = os.path.join(PROJECT_ROOT, ".data", "test-runs")
sys.path.insert(0, PROJECT_ROOT)
def _make_test_dir():
"""在工作区内创建测试目录,兼容 Codex 的 Windows 文件权限。"""
os.makedirs(TEST_TEMP_ROOT, exist_ok=True)
path = os.path.join(TEST_TEMP_ROOT, f"upload-{uuid.uuid4().hex}")
os.makedirs(path)
return path
def _cleanup_test_dir(path):
root = os.path.realpath(TEST_TEMP_ROOT)
target = os.path.realpath(path)
if os.path.commonpath([root, target]) != root:
raise RuntimeError(f"拒绝清理测试目录之外的路径: {target}")
shutil.rmtree(target, ignore_errors=True)
def test_add_file_to_kb_dedup():
@@ -143,7 +161,7 @@ def test_upload_document_overwrite():
print("\n=== 测试 2upload_document 同名文件覆盖 ===")
# 创建临时目录
temp_dir = tempfile.mkdtemp()
temp_dir = _make_test_dir()
try:
target_dir = os.path.join(temp_dir, 'public_kb')
os.makedirs(target_dir, exist_ok=True)
@@ -184,14 +202,14 @@ def test_upload_document_overwrite():
print(f" [PASS] replaced={replaced}")
finally:
shutil.rmtree(temp_dir)
_cleanup_test_dir(temp_dir)
def test_batch_upload_overwrite():
"""测试批量上传中的同名文件覆盖逻辑"""
print("\n=== 测试 3批量上传同名文件覆盖 ===")
temp_dir = tempfile.mkdtemp()
temp_dir = _make_test_dir()
try:
target_dir = os.path.join(temp_dir, 'public_kb')
os.makedirs(target_dir, exist_ok=True)
@@ -240,14 +258,14 @@ def test_batch_upload_overwrite():
print(f" [PASS] 文件内容已更新")
finally:
shutil.rmtree(temp_dir)
_cleanup_test_dir(temp_dir)
def test_docstore_cleanup():
"""测试 DocStore 文件清理逻辑"""
print("\n=== 测试 4DocStore 文件清理 ===")
temp_dir = tempfile.mkdtemp()
temp_dir = _make_test_dir()
try:
docstore_dir = Path(temp_dir) / 'docstore'
docstore_dir.mkdir()
@@ -289,7 +307,7 @@ def test_docstore_cleanup():
print(f" [PASS] 剩余 {len(remaining)} 个无关文件未被清理")
finally:
shutil.rmtree(temp_dir)
_cleanup_test_dir(temp_dir)
def test_sync_hash_cleanup():