180 lines
8.6 KiB
Python
180 lines
8.6 KiB
Python
"""HEL-902 统一审核:后台重点故障注入补核(总工独立新增).
|
|
|
|
针对总管列出的统一审核重点,用可控假服务注入故障,零真实调用:
|
|
|
|
1. 8090 已登记、CyberGril 本地元数据落盘失败 —— 是否留下无法恢复或重复注册的半状态。
|
|
2. 登记上传成功后面板响应断连(结果已生效、响应丢失)+ 用户重试 —— 共享注册表里出现什么。
|
|
3. 通话开启时 worker 音色状态写不进 —— 诚实拒绝且不开房。
|
|
4. worker 状态文件可读时其他键保留;文件损坏(读不出 JSON)时其他键是否被丢弃。
|
|
5. 试听流水写不进 —— 「应用前有可听试听」凭据缺失时应用是否守门(fail-closed)。
|
|
6. 选择状态落盘失败 —— 整体回滚,worker 文件不被写。
|
|
|
|
时间与真实性边界:worker 实际在会话入口读音色文件、与真实 worker 的时序一致性
|
|
属真机窗口验证范围(见 docs/rework/hel902/),本文件只锁 API 侧可离线证明的事实。
|
|
"""
|
|
|
|
import json
|
|
|
|
import httpx
|
|
import pytest
|
|
from test_voice_custom import ( # noqa: F401 (复用作者既有桩;同目录根式导入)
|
|
BUILTIN,
|
|
CREATE,
|
|
FakeVoiceStack,
|
|
build_app,
|
|
make_wav,
|
|
)
|
|
|
|
from cybergril.voice.custom import CustomVoiceService
|
|
|
|
|
|
def _register(client, **params):
|
|
return client.post("/api/voice/voices", params={"prompt_text": "参考文字", **params},
|
|
content=make_wav(3), headers={"Content-Type": "audio/wav"})
|
|
|
|
|
|
@pytest.fixture
|
|
def stack(tmp_path):
|
|
return FakeVoiceStack(tmp_path)
|
|
|
|
|
|
@pytest.fixture
|
|
def instance(tmp_path, stack):
|
|
"""同作者桩,但 raise_server_exceptions=False:故障路径要按产品语义看 500."""
|
|
from fastapi.testclient import TestClient
|
|
|
|
app, custom, worker_state = build_app(tmp_path, stack)
|
|
with TestClient(app, raise_server_exceptions=False) as client:
|
|
client.post("/api/companion", json=CREATE)
|
|
yield client, stack, custom, worker_state, tmp_path
|
|
|
|
|
|
def _swap_panel(custom, stack, handler):
|
|
transport = httpx.MockTransport(handler)
|
|
stack.transport = transport
|
|
custom.http = httpx.AsyncClient(transport=transport, verify=False)
|
|
|
|
|
|
def test_panel_ok_local_metadata_write_fails(instance):
|
|
"""8090 登记成功、本地元数据写不进:500(未映射合同错误);状态仍可恢复、不逼重复注册."""
|
|
client, stack, custom, worker_state, tmp_path = instance
|
|
blocked = tmp_path / "blocked-custom.json"
|
|
blocked.mkdir()
|
|
custom.registry_path = blocked
|
|
|
|
response = _register(client)
|
|
assert response.status_code == 500 # OSError 未被映射为 503/422(产品语义)
|
|
assert len(stack.uploads) == 1
|
|
voice_id = stack.uploads[0]["name"]
|
|
# 面板已登记;进程内记录仍在:本进程里看得到、可试听可应用,不是死条目
|
|
listing = client.get("/api/voice/voices").json()
|
|
assert voice_id in {v["id"] for v in listing["voices"]}
|
|
assert client.post("/api/voice/preview", json={"voice": voice_id}).status_code == 200
|
|
assert client.post("/api/voice/selection", json={"voice": voice_id}).status_code == 200
|
|
# 「重启」后元数据确实丢失(无 label/时间),但共享注册表仍在,声音仍可列出使用
|
|
revived = CustomVoiceService(
|
|
config=custom.config, http=custom.http,
|
|
selection_path=tmp_path / "sel.json", registry_path=tmp_path / "custom2.json",
|
|
journal_path=tmp_path / "usage.jsonl")
|
|
assert voice_id in set(stack.ids)
|
|
assert voice_id not in revived.custom_voices() # 元数据丢了,声音没丢
|
|
|
|
|
|
def test_upload_response_lost_then_retry_leaves_orphan(instance):
|
|
"""上传在面板侧已生效但响应断连 → 503;重试另立新 ID,共享注册表留下无元数据孤儿条目."""
|
|
client, stack, custom, worker_state, tmp_path = instance
|
|
original = stack._handle
|
|
def flaky(request: httpx.Request) -> httpx.Response:
|
|
if request.url.host == "panel.test" and request.url.path == "/api/voices/upload":
|
|
original(request) # 面板真的登记了(写进共享注册表与上传账)
|
|
raise httpx.ConnectError("response lost after panel committed")
|
|
return original(request)
|
|
|
|
_swap_panel(custom, stack, flaky)
|
|
first = _register(client)
|
|
assert first.status_code == 503 # 诚实说「登记服务不在」,但面板侧已成立
|
|
committed = [u["name"] for u in stack.uploads]
|
|
assert len(committed) == 1
|
|
|
|
_swap_panel(custom, stack, original) # 用户看到失败后重试
|
|
second = _register(client)
|
|
assert second.status_code == 201
|
|
orphan, live = committed[0], second.json()["id"]
|
|
registry = json.loads(stack.registry_file.read_text())
|
|
assert orphan in registry and live in registry
|
|
listing = {v["id"]: v for v in client.get("/api/voice/voices").json()["voices"]}
|
|
assert orphan in listing and listing[orphan]["label"] == orphan # 孤儿以 ID 为名仍可见
|
|
assert listing[live]["label"] == live # 本次登记未起名,同样以 ID 为名(一致行为)
|
|
# 既有 11 支与通用助手状态零改写;孤儿条目未被 CyberGril 覆盖或删除
|
|
assert set(BUILTIN) <= set(listing)
|
|
assert stack.generic_voice_writes == 0
|
|
assert stack.deletes == []
|
|
|
|
|
|
def test_session_refuses_when_worker_state_unwritable(instance):
|
|
"""通话开启时 worker 音色写不进:诚实 503;恢复后正常开启,选择不受牵连."""
|
|
client, stack, custom, worker_state, tmp_path = instance
|
|
registered = _register(client).json()
|
|
assert client.post("/api/voice/preview", json={"voice": registered["id"]}).status_code == 200
|
|
client.post("/api/voice/selection", json={"voice": registered["id"]})
|
|
|
|
blocked = tmp_path / "blocked-worker.json"
|
|
blocked.mkdir()
|
|
custom.config.worker_state_file = str(blocked)
|
|
response = client.post("/api/voice/session")
|
|
assert response.status_code == 503
|
|
assert "写不进" in response.json()["detail"]
|
|
|
|
custom.config.worker_state_file = str(worker_state)
|
|
assert client.post("/api/voice/session").status_code == 201
|
|
|
|
|
|
def test_worker_state_other_keys_and_corruption(instance):
|
|
"""可读的 worker 文件其他键保留;文件损坏(非 JSON)时重写只剩 voice —— 如实记录行为."""
|
|
client, stack, custom, worker_state, tmp_path = instance
|
|
registered = _register(client).json()
|
|
client.post("/api/voice/preview", json={"voice": registered["id"]})
|
|
worker_state.write_text(json.dumps(
|
|
{"voice": "default", "persona": "keep-me", "volume": 7}), encoding="utf-8")
|
|
client.post("/api/voice/selection", json={"voice": registered["id"]})
|
|
state = json.loads(worker_state.read_text())
|
|
assert state["voice"] == registered["id"]
|
|
assert state["persona"] == "keep-me" and state["volume"] == 7
|
|
|
|
worker_state.write_text("{not json", encoding="utf-8")
|
|
other = _register(client).json()
|
|
client.post("/api/voice/preview", json={"voice": other["id"]})
|
|
client.post("/api/voice/selection", json={"voice": other["id"]})
|
|
state = json.loads(worker_state.read_text())
|
|
# 损坏文件读不出 → 整体重写仅含 voice,旧键丢失(该文件由专属 worker 维护;
|
|
# 损坏属异常现场。记录事实,不构成合同违约)
|
|
assert state == {"voice": other["id"]}
|
|
|
|
|
|
def test_journal_unwritable_keeps_apply_gated(instance):
|
|
"""试听流水写不进:试听仍可成功(尽力而为),但应用凭据缺失 → 409 守门不放松."""
|
|
client, stack, custom, worker_state, tmp_path = instance
|
|
blocked = tmp_path / "blocked-usage.jsonl"
|
|
blocked.mkdir()
|
|
custom.journal_path = blocked
|
|
registered = _register(client).json()
|
|
assert client.post("/api/voice/preview", json={"voice": registered["id"]}).status_code == 200
|
|
refused = client.post("/api/voice/selection", json={"voice": registered["id"]})
|
|
assert refused.status_code == 409 # fail-closed:宁可再听一次,不放过未试听
|
|
|
|
|
|
def test_apply_rolls_back_when_selection_file_unwritable(instance):
|
|
"""本地选择状态写不进:整体回滚,worker 文件不动,错误如实 503."""
|
|
client, stack, custom, worker_state, tmp_path = instance
|
|
registered = _register(client).json()
|
|
client.post("/api/voice/preview", json={"voice": registered["id"]})
|
|
worker_state.write_text(json.dumps({"voice": "rain"}), encoding="utf-8")
|
|
blocked = tmp_path / "blocked-sel.json"
|
|
blocked.mkdir()
|
|
custom.selection_path = blocked
|
|
response = client.post("/api/voice/selection", json={"voice": registered["id"]})
|
|
assert response.status_code == 503
|
|
assert "写不进" in response.json()["detail"]
|
|
assert json.loads(worker_state.read_text())["voice"] == "rain"
|
|
assert custom.selection().voice is None
|