muse-agent-example/tests/单元/test_受控文件CAS与恢复.py
zizi d82581daee W08 原文证据、临时租约与归档:原文证据生命周期、临时租约、调用授权与归档恢复。
按 R2 串行阶段整理提交;包内文件为该阶段交付(含后续小增量),状态以工作包清单为准。
2026-09-10 19:25:41 +08:00

44 lines
1.7 KiB
Python

"""合成私人暂存的字节、权限与清理边界。"""
import hashlib
import os
import uuid
import pytest
from muse.基础设施.受控文件 import 受控文件, 受控文件错误
def test_内容哈希排他发布和重复写入保持字节__a80001(tmp_path) -> None:
管理器 = 受控文件(tmp_path / "私人暂存")
身份 = str(uuid.uuid4())
原文 = '中文"引号"\n换行\\反斜杠'.encode()
哈希 = hashlib.sha256(原文).hexdigest()
管理器.写入(身份, 哈希, 原文)
管理器.写入(身份, 哈希, 原文)
assert 管理器.读取(身份, 哈希) == 原文
assert (管理器.根 / 身份 / (哈希 + ".bin")).stat().st_mode & 0o777 == 0o600
with pytest.raises(受控文件错误):
管理器.写入(身份, 哈希, b"different")
管理器.删除对象(身份)
assert not (管理器.根 / 身份).exists()
def test_软链接和未知资料不会被清理或跟随__a80002(tmp_path) -> None:
管理器 = 受控文件(tmp_path / "私人暂存")
身份 = str(uuid.uuid4())
原文 = b"synthetic"
哈希 = hashlib.sha256(原文).hexdigest()
管理器.写入(身份, 哈希, 原文)
外部 = tmp_path / "其他资料"
外部.write_bytes(b"preserve")
(管理器.根 / 身份 / (哈希 + ".bin")).unlink()
os.symlink(外部, 管理器.根 / 身份 / (哈希 + ".bin"))
with pytest.raises(受控文件错误):
管理器.读取(身份, 哈希)
with pytest.raises(受控文件错误):
管理器.删除对象(身份)
assert 外部.read_bytes() == b"preserve"
with pytest.raises(受控文件错误):
管理器.读取("../其他资料", 哈希)