"""构建与资源验收:通过真实生成器和资源读取接口验证发布内容。""" import json import shutil import subprocess import sys import zipfile from pathlib import Path import pytest from muse import 资源加载 from muse.共享.错误 import 资源缺失错误 仓库根 = Path(__file__).resolve().parents[2] @pytest.mark.case_id( "NC-resource-bytes", environment="离线,临时目录与真实生成器", given="非空登记和合成资源", when="打包、读取及篡改后核对", then=["资源完整时读取成功,篡改后哈希核对失败"], contract="docs/系统架构/新版设计/文件设计/工程配置与部署.md", ) def test_资源核对遍历实际字节__a02003(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: """非空资源包完整可读,篡改任一资源后核对失败。""" 工具 = tmp_path / "工具" 工具.mkdir() shutil.copyfile(仓库根 / "工具/构建资源包.py", 工具 / "构建资源包.py") shutil.copyfile(仓库根 / "工具/效果依赖.py", 工具 / "效果依赖.py") (tmp_path / "uv.lock").write_text("synthetic-test-lock\n") (tmp_path / "pyproject.toml").write_text('[project]\nversion="0.0.0-test"\n') (tmp_path / "正文方法.md").write_text("合成方法资源", encoding="utf-8") (工具 / "资源登记.json").write_text( json.dumps( { "发布身份": "test-resource", "资源": [{"源路径": "正文方法.md", "打包路径": "方法/正文.md"}], }, ensure_ascii=False, ) ) subprocess.run([sys.executable, str(工具 / "构建资源包.py")], check=True, capture_output=True) 资源目录 = tmp_path / "src/muse/资源" monkeypatch.setattr(资源加载.resources, "files", lambda _: 资源目录) assert 资源加载.安装包资源提供器().核对资源()["发布身份"] == "test-resource" with zipfile.ZipFile(资源目录 / "运行资源.zip", "w") as 包: 包.writestr("方法/正文.md", "被篡改的资源") with pytest.raises(资源缺失错误, match="哈希"): 资源加载.安装包资源提供器().核对资源() @pytest.mark.case_id( "NC-generator-no-false-success", environment="离线,临时目录与真实生成器", given="存在非空接口生成输入", when="执行尚未接入的生成器", then=["非零退出并明确未生成合同"], contract="docs/系统架构/新版设计/文件设计/工程配置与部署.md", ) def test_非空生成输入不能伪成功__a02004(tmp_path: Path) -> None: """未接入的生成器接到实际登记输入必须非零退出。""" 工具 = tmp_path / "工具" 工具.mkdir() shutil.copyfile(仓库根 / "工具/生成接口.py", 工具 / "生成接口.py") (工具 / "生成输入.json").write_text(json.dumps({"资源": [{"源路径": "缺失.py"}]})) 结果 = subprocess.run( [sys.executable, str(工具 / "生成接口.py")], capture_output=True, text=True ) assert 结果.returncode != 0 assert "未生成合同" in 结果.stderr