muse-agent-example/tests/集成/test_规则启用接入.py
zizi d909d1bd1b 后端实现与用例身份:19 包集成落地并修复收尾缺陷
实现侧:
- 上下文:任务范围拆分为 范围校验/范围授权;索引按可发现口径重建、索引新鲜度改对称差;依赖校验统一快照漂移说明。
- 知识方法:方法与材料读取口径统一;超限方法材料按可选省略,核对路径不再二次计费;删除无合同的读时重算。
- 任务运行:新增 context.usage/tool.denied 事件类型;连接池常驻并在装配生命周期内开关;调用结算与核对分列。
- 效果评测/审校修订/交付连载/作者经验/作品规划:凭据冻结、标定消费、导出补证、事实引文核对等收尾修复。
- 资源加载:能力正文不再夹带索引用的导航注记(该注记此前进入角色与技能的模型提示)。
- 元数据:受保护骨架与代码保护属性对齐;字段校验与内置结构口径同步。
- 基础设施:环境预检进入装配生命周期;数据库连接运行期字段不参与相等比较;索引指纹归一化 jsonb 浮点。
- 删除被替代实现:7 份旧提示词模板与空壳 资料来源 读取器。

用例侧:
- 用例身份与导航元信息迁移;夹具补生命周期、同库暴露与模板封存;
- 本轮定向修复:方法材料省略、事实引文、迁移回执、额度与暂停用例、慢用例超时预算等。
2026-09-18 01:15:00 +08:00

279 lines
11 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.

"""规则资格的真实接入;测试中的owned声明只验证工程链,不认证真实语料来源。"""
import json
import os
import socket
import subprocess
import sys
import time
from dataclasses import replace
from datetime import UTC, datetime, timedelta
from pathlib import Path
from threading import Thread
import httpx
import pytest
import test_生产评测权限隔离 as 接入测试
import test_规则诊断资格与消费 as 规则测试
import uvicorn
from fastapi.testclient import TestClient
from muse.共享.调用身份 import 用途
from muse.接入.http.应用 import 创建应用
from muse.效果评测.接口 import 规则诊断数据集发布
from muse.资源加载 import 加载清单
from muse.配置 import 读取配置
pytestmark = pytest.mark.数据库
规则资格环境 = 规则测试.规则资格环境
def _配置(env, tmp_path):
configs = {}
for purpose in 用途:
path = 接入测试._配置文件(env["pools"][purpose], tmp_path)
path.write_text(
path.read_text()
.replace("eval-author", env["author"].作者)
.replace(
'"发布身份"="test"',
'"发布身份"=' + json.dumps(加载清单()["构建身份"]),
)
)
configs[purpose] = path
return configs
def _cli(tmp_path, config, command, action, target, *args):
process = subprocess.run(
[
sys.executable,
"-I",
"-m",
"muse",
command,
str(config),
action,
str(target),
*map(str, args),
],
cwd=tmp_path,
capture_output=True,
text=True,
timeout=30,
)
assert process.returncode == 0, process.stderr
return json.loads(process.stdout)
def _登录(client):
response = client.post("/api/v1/session", json={"password": "synthetic-evaluation-only"})
assert response.status_code == 200, response.text
@pytest.mark.case_id(
"NC-w25-25d104",
environment="隔离PG/合成HTTP及浏览器",
given="固定请求、来源与作者;按用例环境准备独立测试输入",
when="通过实际公开入口执行并核对拒绝、状态及持久结果",
then=["规则验证真实CLI到默认HTTP审阅启停及读回"],
contract="docs/系统架构/新版设计/模块设计/B10-效果评测.md",
)
def test_规则验证真实CLI到默认HTTP审阅启停及读回__25d104(规则资格环境, tmp_path):
env = 规则资格环境
configs = _配置(env, tmp_path)
request = 规则诊断数据集发布(
dataset_id="rule-entry",
revision=1,
rule_id="l002",
rule_version=1,
approval_ref="test-author-declared-fixture",
expires_at=datetime.now(UTC) + timedelta(hours=2),
samples=规则测试._samples("owned", "entry"),
)
source_file = tmp_path / "samples.json"
source_file.write_text(request.model_dump_json())
dataset = _cli(
tmp_path,
configs[用途.维护],
"评测",
"发布规则样本",
source_file,
"--来源配置",
configs[用途.生产],
)
run = _cli(tmp_path, configs[用途.评测], "评测", "验证规则", dataset["version_id"])
assert run["assessment"]["status"] == "passed"
replay = _cli(tmp_path, configs[用途.评测], "评测", "规则验证报告", run["run_id"])
assert replay["assessment"] == run["assessment"]
export_file = tmp_path / "export.json"
export_file.write_text(
json.dumps(
{
"run_id": run["run_id"],
"approval_ref": "fixture-owner",
"valid_until": (datetime.now(UTC) + timedelta(hours=1)).isoformat(),
}
)
)
proof = _cli(tmp_path, configs[用途.维护], "评测", "导出规则凭据", export_file)
change = {
"rule_id": "l002",
"version": 1,
"action": "enable",
"expected_activation_revision": 0,
"credential_id": proof["receipt_id"],
}
config = 读取配置(configs[用途.生产])
with TestClient(创建应用(config), headers={"Origin": "http://testserver"}) as client:
_登录(client)
before = client.get("/api/v1/rules/l002/activation")
assert before.status_code == 200 and before.json()["current"] is None
malformed = {**change, "version": True}
assert (
client.post(
"/api/v1/rules/l002/activation/review", json={"request": malformed}
).status_code
== 422
)
opened = client.post("/api/v1/rules/l002/activation/review", json={"request": change})
assert opened.status_code == 200, opened.text
assert client.get("/api/v1/rules/l002/activation").json()["current"] is None
decision = {
"command_id": "rule-entry-adopt",
"request": change,
"action": "adopt",
"review_id": opened.json()["review_id"],
"review_hash": opened.json()["review_hash"],
}
response = client.post("/api/v1/rules/l002/activation/decisions", json=decision)
assert response.status_code == 200, response.text
assert (
client.post("/api/v1/rules/l002/activation/decisions", json=decision).json()
== response.json()
)
current = client.get("/api/v1/rules/l002/activation").json()
assert current["consumable"] and current["current"]["rule_version"] == 1
diagnosis = client.post("/api/v1/diagnoses", json={"target": env["target"]})
assert diagnosis.status_code == 201, diagnosis.text
assert diagnosis.json()["report"]["findings"][0]["rule_id"] == "l002"
assert client.get("/api/v1/evaluation/rule-diagnostics/" + run["run_id"]).status_code == 403
state = _cli(tmp_path, configs[用途.生产], "审校", "规则状态", "l002")
assert state["current"]["activation_revision"] == 1 and state["consumable"]
with TestClient(
创建应用(读取配置(configs[用途.评测])), headers={"Origin": "http://testserver"}
) as client:
_登录(client)
result = client.post(
"/api/v1/evaluation/rule-diagnostics",
json={"dataset_version_id": dataset["version_id"]},
)
assert result.status_code == 201 and result.json()["run_id"] == run["run_id"]
stopped = client.post(
"/api/v1/evaluation/rule-diagnostics/" + run["run_id"] + "/stop",
json={"command_id": "stop-rule-test"},
)
assert stopped.status_code == 200, stopped.text
state = _cli(tmp_path, configs[用途.生产], "审校", "规则状态", "l002")
assert state["current"]["state"] == "active" and not state["consumable"]
change.update(action="disable", credential_id="", expected_activation_revision=1)
review_file = tmp_path / "review.json"
review_file.write_text(json.dumps({"request": change}))
review = _cli(tmp_path, configs[用途.生产], "审校", "规则审阅", review_file)
decision_file = tmp_path / "decision.json"
decision_file.write_text(
json.dumps(
{
"request": change,
"command_id": "rule-entry-disable",
"review_id": review["review_id"],
"review_hash": review["review_hash"],
"action": "adopt",
}
)
)
_cli(tmp_path, configs[用途.生产], "审校", "规则决定", decision_file)
state = _cli(tmp_path, configs[用途.生产], "审校", "规则状态", "l002")
assert state["current"]["state"] == "disabled" and len(state["history"]) == 2
assert env["app"].要求正文().读取正文(env["author"], "rule-ch")["revision"] == 1
@pytest.mark.case_id(
"NC-w25-25d105",
environment="隔离PG/合成HTTP及浏览器",
given="固定请求、来源与作者;按用例环境准备独立测试输入",
when="通过实际公开入口执行并核对拒绝、状态及持久结果",
then=["规则凭据经真实浏览器审阅启用刷新停用"],
contract="docs/系统架构/新版设计/模块设计/B10-效果评测.md",
)
@pytest.mark.浏览器
def test_规则凭据经真实浏览器审阅启用刷新停用__25d105(规则资格环境, tmp_path):
if os.environ.get("MUSE_RUN_BROWSER") != "1":
pytest.skip("需显式启用浏览器")
env = 规则资格环境
configs = _配置(env, tmp_path)
published = 规则测试._publish(env, "browser-rule-fixture", "owned")
run = env["evaluate"].执行规则诊断验证(env["evaluation_identity"], published["version_id"])
proof = env["maintain"].导出规则诊断凭据(
env["maintenance_identity"],
run["run_id"],
批准引用="fixture-browser-proof",
有效期=datetime.now(UTC) + timedelta(hours=1),
)
with socket.socket() as sock:
sock.bind(("127.0.0.1", 0))
port = sock.getsockname()[1]
url = f"http://127.0.0.1:{port}"
config = 读取配置(configs[用途.生产])
assert config.HTTP is not None
config = replace(config, HTTP=replace(config.HTTP, 公开地址=url, 允许来源=(url,)))
server = uvicorn.Server(
uvicorn.Config(创建应用(config), host="127.0.0.1", port=port, log_level="warning")
)
thread = Thread(target=server.run, daemon=True)
thread.start()
output = Path(os.environ.get("MUSE_BROWSER_ARTIFACTS", str(tmp_path / "browser"))).resolve()
output.mkdir(parents=True, exist_ok=True)
try:
for _ in range(100):
if server.started:
break
time.sleep(0.05)
assert server.started and httpx.get(url + "/ready", trust_env=False).status_code == 200
environment = {
**os.environ,
"MUSE_WORKBENCH_URL": url,
"MUSE_AUTHOR_PASSWORD_FILE": config.HTTP.口令文件,
"MUSE_RULE_CREDENTIAL": proof["receipt_id"],
"MUSE_BROWSER_EXECUTABLE": (
"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
),
}
with (output / "浏览器.json").open("w") as report:
process = subprocess.run(
[
"pnpm",
"exec",
"playwright",
"test",
"规则启用.spec.ts",
"--reporter=json",
"--output",
str(output / "产物"),
],
cwd=Path(__file__).resolve().parents[2] / "web",
env=environment,
stdout=report,
stderr=subprocess.STDOUT,
timeout=90,
)
assert process.returncode == 0, "浏览器未通过,见浏览器.json"
state = env["review"].读取规则状态(env["author"], "l002")
assert state["current"]["state"] == "disabled"
assert [r["action"] for r in state["history"]] == ["enable", "disable"]
assert env["app"].要求正文().读取正文(env["author"], "rule-ch")["revision"] == 1
finally:
server.should_exit = True
thread.join(10)
assert not thread.is_alive()