"""Custom voice tests (HEL-899): reference-audio registration, preview, selection persistence, call-time capture — all against the real router and real services with fake panel/TTS transports. Zero network, zero real LLM/embedding/ASR/TTS calls.""" from __future__ import annotations import io import json import sys import wave from pathlib import Path import httpx import pytest sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src")) sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tests")) from test_r2 import R2LLM # noqa: E402 from cybergril.api.app import create_app # noqa: E402 from cybergril.life.runtime import LifeConfig # noqa: E402 from cybergril.think.router import build_router # noqa: E402 from cybergril.think.scenes import Tier # noqa: E402 from cybergril.voice.custom import CustomVoiceConfig, CustomVoiceService # noqa: E402 from cybergril.voice.session import VoiceConfig, VoiceSessionService # noqa: E402 CREATE = {"name": "语音测试", "birth": "2000-10-18", "hour": "亥时", "relation": "朋友"} BUILTIN = ["bailu", "bajie", "default", "female_soft", "haimianbaobao", "jiaojiao", "male_warm", "nezha", "rain", "sunwukong", "xiaoyun"] def make_wav(seconds: float = 3.0, rate: int = 16000, channels: int = 1) -> bytes: buffer = io.BytesIO() with wave.open(buffer, "wb") as wav: wav.setnchannels(channels) wav.setsampwidth(2) wav.setframerate(rate) wav.writeframes(b"\x00\x01" * int(rate * channels * seconds)) return buffer.getvalue() class FakeVoiceStack: """.12 语音面板(8090) + CosyVoice(9880) 的只读桩:记录每一次调用.""" def __init__(self, tmp: Path) -> None: self.ids: list[str] = list(BUILTIN) self.registry_file = tmp / "voices.json" self.uploads: list[dict] = [] self.tts_calls: list[dict] = [] self.panel_down = False self.tts_down = False self.reject_upload = "" # 面板 400 detail(如"自动转写失败") self.swallow = False # 谎报成功:回 ok 但不进列表(测登记后核验) self.generic_voice_writes = 0 # POST /api/voice —— 必须永远是 0 self.deletes: list[str] = [] self.transport = httpx.MockTransport(self._handle) def _registry(self) -> dict: return json.loads(self.registry_file.read_text(encoding="utf-8")) \ if self.registry_file.exists() else {} def _write_registry(self, data: dict) -> None: self.registry_file.write_text(json.dumps(data, ensure_ascii=False, indent=1), encoding="utf-8") def _handle(self, request: httpx.Request) -> httpx.Response: host, path = request.url.host, request.url.path if host == "panel.test": if self.panel_down: raise httpx.ConnectError("panel down") if path == "/api/voices" and request.method == "GET": return httpx.Response(200, json={"voices": sorted(self.ids)}) if path == "/api/voices/upload": name = self._form_name(request) prompt_text = self._form_text(request) if self.reject_upload: return httpx.Response(400, json={"detail": self.reject_upload}) if not prompt_text: # 面板在参考文本为空时走 FunASR(桩:固定转写) prompt_text = "这是自动转写出的参考文本,内容与音频一致。" registry = self._registry() registry[name] = {"prompt_text": prompt_text, "prompt_wav": f"/opt/voice-agent/cosyvoice/voice_samples/{name}.wav"} self._write_registry(registry) self.uploads.append({"name": name, "prompt_text": prompt_text, "bytes": len(request.content)}) if not self.swallow: self.ids.append(name) return httpx.Response(200, json={"ok": True, "voice": name}) if path.startswith("/api/voices/") and request.method == "DELETE": self.deletes.append(path.rsplit("/", 1)[-1]) return httpx.Response(200, json={"ok": True}) if path == "/api/voice": # 通用助手自己的状态 —— CyberGril 绝不写 self.generic_voice_writes += 1 return httpx.Response(200, json={"ok": True}) return httpx.Response(404) if host == "tts.test": if path == "/health": if self.tts_down: return httpx.Response(503) return httpx.Response(200, json={"ok": True, "voices": sorted(self.ids)}) if path == "/tts": if self.tts_down: return httpx.Response(503) body = json.loads(request.content) self.tts_calls.append(body) marker = f"{body['voice']}|{body['text']}".encode() return httpx.Response(200, content=marker + b"\x01\x00" * 240) return httpx.Response(404) return httpx.Response(404) @staticmethod def _form_name(request: httpx.Request) -> str: return _multipart_field(request.content, b"name") or "" @staticmethod def _form_text(request: httpx.Request) -> str: return _multipart_field(request.content, b"prompt_text") or "" def _multipart_field(body: bytes, field: bytes) -> str | None: """极简 multipart 取字段(只为桩服务;产品代码不解析 multipart).""" for part in body.split(b"--"): header, _, value = part.partition(b"\r\n\r\n") if field + b'"' in header: return value.strip(b"\r\n-").decode() return None class FakeLiveKit: def __init__(self) -> None: self.dispatches: list[str] = [] self.deleted_rooms: list[str] = [] self.transport = httpx.MockTransport(self._handle) def _handle(self, request: httpx.Request) -> httpx.Response: if request.url.path == "/twirp/livekit.AgentDispatchService/CreateDispatch": self.dispatches.append(request.url.path) return httpx.Response(200, json={"id": "AD_1", "room": "r"}) if request.url.path == "/twirp/livekit.RoomService/DeleteRoom": self.deleted_rooms.append(request.url.path) return httpx.Response(200, content=b"") return httpx.Response(404) def build_app(tmp: Path, stack: FakeVoiceStack, livekit: FakeLiveKit | None = None): livekit = livekit or FakeLiveKit() voice = VoiceSessionService( config=VoiceConfig( livekit_url="ws://voice.test:7880", livekit_internal="http://livekit.test", api_key="devkey", api_secret="test-secret-not-real", tts_health_url="http://tts.test/health"), http=httpx.AsyncClient(transport=httpx.MockTransport( lambda request: stack._handle(request) if request.url.host == "tts.test" else livekit._handle(request)))) worker_state = tmp / "voice_state_cybergril.json" custom = CustomVoiceService( config=CustomVoiceConfig( panel_url="https://panel.test", tts_url="http://tts.test", registry_file=str(stack.registry_file), worker_state_file=str(worker_state)), http=httpx.AsyncClient(transport=stack.transport, verify=False), selection_path=tmp / "sel.json", registry_path=tmp / "custom.json", journal_path=tmp / "usage.jsonl") provider = R2LLM() router = build_router(providers={t: provider for t in (Tier.HIGH, Tier.STANDARD, Tier.LOW)}) app = create_app(db_path=":memory:", router=router, life_config=LifeConfig(enabled=True, poll_seconds=3600), semantic_mode="stub", voice_service=voice, custom_voice_service=custom) return app, custom, worker_state @pytest.fixture def stack(tmp_path): return FakeVoiceStack(tmp_path) @pytest.fixture def instance(tmp_path, stack): from fastapi.testclient import TestClient app, custom, worker_state = build_app(tmp_path, stack) with TestClient(app) as client: client.post("/api/companion", json=CREATE) yield client, stack, custom, worker_state def test_register_list_and_selection_flow(instance): client, stack, custom, worker_state = instance audio = make_wav(3) registered = client.post( "/api/voice/voices", params={"label": "她的声音", "prompt_text": "这是参考文字"}, content=audio, headers={"Content-Type": "audio/wav"}) assert registered.status_code == 201 record = registered.json() assert record["id"].startswith("cg-") and len(record["id"]) == 11 assert record["label"] == "她的声音" assert record["prompt_text"] == "这是参考文字" assert record["prompt_text_source"] == "user" # 上传到达面板的是服务端生成的 cg- 专属 ID,不带用户文件名 assert stack.uploads[-1]["name"] == record["id"] assert stack.uploads[-1]["bytes"] > 0 listing = client.get("/api/voice/voices").json() by_id = {v["id"]: v for v in listing["voices"]} assert set(by_id) == set(BUILTIN) | {record["id"]} assert by_id[record["id"]]["source"] == "custom" assert by_id[record["id"]]["label"] == "她的声音" assert by_id["rain"]["source"] == "builtin" assert listing["selection"]["voice"] is None preview = client.post("/api/voice/preview", json={"voice": record["id"]}) assert preview.status_code == 200 assert preview.headers["content-type"] == "audio/wav" with wave.open(io.BytesIO(preview.content)) as wav: assert wav.getframerate() == 24000 and wav.getnchannels() == 1 assert stack.tts_calls[-1] == {"text": "你好,这是我的声音,以后就用它陪你说话。", "voice": record["id"]} applied = client.post("/api/voice/selection", json={"voice": record["id"]}) assert applied.status_code == 200 and applied.json()["voice"] == record["id"] assert json.loads(worker_state.read_text())["voice"] == record["id"] assert client.get("/api/voice/selection").json()["voice"] == record["id"] listing = client.get("/api/voice/voices").json() assert {v["id"] for v in listing["voices"] if v["selected"]} == {record["id"]} # 备份清单字段在列表里(CG-009 承接) entry = [v for v in listing["voices"] if v["id"] == record["id"]][0] assert entry["prompt_wav"].endswith(record["id"] + ".wav") def test_register_requires_explicit_asr_consent(instance): client, stack, _, _ = instance refusal = client.post("/api/voice/voices", content=make_wav(3)) assert refusal.status_code == 422 assert "转写" in refusal.json()["detail"] or "参考文字" in refusal.json()["detail"] assert stack.uploads == [] # 未获明确准许,不触发任何 ASR/上传 allowed = client.post("/api/voice/voices", params={"allow_asr": "true"}, content=make_wav(3)) assert allowed.status_code == 201 assert allowed.json()["prompt_text_source"] == "asr" journal = [json.loads(ln) for ln in Path(instance[3].parent, "usage.jsonl").read_text().splitlines()] assert any(e["kind"] == "register_asr" and e["voice"] == allowed.json()["id"] for e in journal) def test_register_rejects_bad_formats(instance): client, stack, _, _ = instance assert client.post("/api/voice/voices", params={"prompt_text": "x"}, content=b"ID3-not-a-wav").status_code == 422 assert client.post("/api/voice/voices", params={"prompt_text": "x"}, content=make_wav(1.0)).status_code == 422 # <2s header_only = b"RIFF" + (9999).to_bytes(4, "little") + b"WAVE" + b"junk" * 10 assert client.post("/api/voice/voices", params={"prompt_text": "x"}, content=header_only).status_code == 422 # WAV magic 但头不完整 too_big = make_wav(2) + b"\x00" * (15 * 1024 * 1024 + 1) response = client.post("/api/voice/voices", params={"prompt_text": "x"}, content=too_big) assert response.status_code == 422 and "15MB" in response.json()["detail"] assert stack.uploads == [] def test_register_ids_never_collide_with_builtin_or_each_other(instance, monkeypatch): client, stack, _, _ = instance first = client.post("/api/voice/voices", params={"prompt_text": "一"}, content=make_wav(2)).json() second = client.post("/api/voice/voices", params={"prompt_text": "二"}, content=make_wav(2)).json() assert first["id"] != second["id"] assert not any(v.startswith("cg-") for v in BUILTIN) # 强制撞名:第一次生成的 ID 已在注册表里 → 必须换新 ID,而不是覆盖 registry = stack._registry() registry["cg-deadbeef"] = {"prompt_text": "旧条目", "prompt_wav": "/old.wav"} stack._write_registry(registry) stack.ids.append("cg-deadbeef") # 让首撞成立 calls = iter(["deadbeef", "cafef00d"]) monkeypatch.setattr("secrets.token_hex", lambda n: next(calls)) forced = client.post("/api/voice/voices", params={"prompt_text": "三"}, content=make_wav(2)).json() assert forced["id"] == "cg-cafef00d" assert stack._registry()["cg-deadbeef"]["prompt_text"] == "旧条目" # 未被覆盖 async def test_concurrent_registers_are_serialized_and_distinct(tmp_path, stack): """并发登记同名同音频:锁串行化 + 随机 ID,各自成立、互不覆盖.""" import asyncio service = CustomVoiceService( config=CustomVoiceConfig(panel_url="https://panel.test", tts_url="http://tts.test", registry_file=str(stack.registry_file), worker_state_file=str(tmp_path / "w.json")), http=httpx.AsyncClient(transport=stack.transport, verify=False), selection_path=tmp_path / "sel.json", registry_path=tmp_path / "custom.json", journal_path=tmp_path / "usage.jsonl") try: records = await asyncio.gather(*[ service.register(data=make_wav(2), label="", prompt_text="并发", allow_asr=False) for _ in range(4)]) ids = [record["id"] for record in records] assert len(set(ids)) == 4 and all(v.startswith("cg-") for v in ids) assert len(stack.uploads) == 4 assert set(service.custom_voices()) == set(ids) finally: await service.aclose() def test_register_panel_down_or_rejecting_leaves_no_half_option(instance): client, stack, custom, _ = instance stack.panel_down = True assert client.post("/api/voice/voices", params={"prompt_text": "x"}, content=make_wav(2)).status_code == 503 stack.panel_down = False stack.reject_upload = "自动转写失败(boom), 请手动填写参考文本" assert client.post("/api/voice/voices", params={"allow_asr": "true"}, content=make_wav(2)).status_code == 422 assert custom.custom_voices() == {} stack.reject_upload = "" # 面板谎报成功(列表里没出现)也要拒绝,不登记元数据 stack.swallow = True lying = client.post("/api/voice/voices", params={"prompt_text": "x"}, content=make_wav(2)) assert lying.status_code in (422, 503) assert custom.custom_voices() == {} def test_preview_unknown_voice_never_reaches_tts(instance): client, stack, _, _ = instance response = client.post("/api/voice/preview", json={"voice": "ghost"}) assert response.status_code == 404 assert stack.tts_calls == [] # CosyVoice 对未知音色静默回落 default,必须前置拦住 assert client.post("/api/voice/preview", json={}).status_code == 422 def test_preview_tts_down_honest(instance): client, stack, _, _ = instance registered = client.post("/api/voice/voices", params={"prompt_text": "x"}, content=make_wav(2)).json() stack.tts_down = True response = client.post("/api/voice/preview", json={"voice": registered["id"]}) assert response.status_code == 503 assert "声音服务" in response.json()["detail"] def test_preview_writes_no_chat_memory_or_receipts(instance): client, stack, _, _ = instance registered = client.post("/api/voice/voices", params={"prompt_text": "x"}, content=make_wav(2)).json() app = client.app counts = {table: app.state.store.query_one(f"SELECT COUNT(*) c FROM {table}")["c"] for table in ("chat_message", "mem_episodic", "sharing_attempt", "life_event")} assert client.post("/api/voice/preview", json={"voice": registered["id"], "text": "自定义试听句"}).status_code == 200 assert stack.tts_calls[-1]["text"] == "自定义试听句" after = {table: app.state.store.query_one(f"SELECT COUNT(*) c FROM {table}")["c"] for table in counts} assert after == counts def test_apply_rules(instance): client, stack, _, worker_state = instance registered = client.post("/api/voice/voices", params={"prompt_text": "x"}, content=make_wav(2)).json() # 自定义音色未试听 → 拒绝 unpreviewed = client.post("/api/voice/selection", json={"voice": registered["id"]}) assert unpreviewed.status_code == 409 and "试听" in unpreviewed.json()["detail"] # 未知音色 → 404 assert client.post("/api/voice/selection", json={"voice": "ghost"}).status_code == 404 # 内置音色(D1 候选)不强制试听 builtin = client.post("/api/voice/selection", json={"voice": "rain"}) assert builtin.status_code == 200 assert json.loads(worker_state.read_text())["voice"] == "rain" # 试听后自定义音色可应用 assert client.post("/api/voice/preview", json={"voice": registered["id"]}).status_code == 200 applied = client.post("/api/voice/selection", json={"voice": registered["id"]}) assert applied.status_code == 200 and applied.json()["label"] == registered["id"] def test_apply_rollback_on_state_write_failure(tmp_path, stack): from fastapi.testclient import TestClient (tmp_path / "blocker").write_text("not a dir") app, custom, _ = build_app(tmp_path, stack) custom.config.worker_state_file = str(tmp_path / "blocker" / "worker.json") with TestClient(app) as client: client.post("/api/companion", json=CREATE) assert client.post("/api/voice/selection", json={"voice": "rain"}).status_code == 503 assert custom.selection().voice is None assert client.get("/api/voice/selection").json()["voice"] is None def test_selection_persists_across_restart(tmp_path, stack): from fastapi.testclient import TestClient app, custom, worker_state = build_app(tmp_path, stack) with TestClient(app) as client: client.post("/api/companion", json=CREATE) registered = client.post("/api/voice/voices", params={"label": "留声", "prompt_text": "x"}, content=make_wav(2)).json() client.post("/api/voice/preview", json={"voice": registered["id"]}) client.post("/api/voice/selection", json={"voice": registered["id"]}) # “重启”:同一批文件新建服务 app2, custom2, _ = build_app(tmp_path, stack) with TestClient(app2) as client2: client2.post("/api/companion", json=CREATE) selection = client2.get("/api/voice/selection").json() assert selection["voice"] == registered["id"] listing = client2.get("/api/voice/voices").json() entry = [v for v in listing["voices"] if v["id"] == registered["id"]][0] assert entry["label"] == "留声" and entry["selected"] is True assert entry["previewed"] is True # 试听凭据也在持久流水里 # 重启后无需重新试听即可再应用 other = client2.post("/api/voice/voices", params={"prompt_text": "y"}, content=make_wav(2)).json() client2.post("/api/voice/preview", json={"voice": other["id"]}) assert client2.post("/api/voice/selection", json={"voice": other["id"]}).status_code == 200 assert json.loads(Path(worker_state).read_text())["voice"] == other["id"] def test_session_captures_voice_and_pins_for_the_call(instance): client, stack, custom, worker_state = instance registered = client.post("/api/voice/voices", params={"prompt_text": "x"}, content=make_wav(2)).json() client.post("/api/voice/preview", json={"voice": registered["id"]}) client.post("/api/voice/selection", json={"voice": registered["id"]}) opened = client.post("/api/voice/session").json() assert opened["voice"] == registered["id"] assert json.loads(worker_state.read_text())["voice"] == registered["id"] service_session = client.app.state.voice_service.get(opened["session_id"]) assert service_session.voice == registered["id"] # 通话中改选:本通话定住,最迟下一通话生效 client.post("/api/voice/selection", json={"voice": "rain"}) assert client.app.state.voice_service.get(opened["session_id"]).voice == registered["id"] second = client.post("/api/voice/session").json() assert second["voice"] == "rain" def test_session_capture_falls_back_when_selection_invalid(instance): client, stack, custom, worker_state = instance registered = client.post("/api/voice/voices", params={"prompt_text": "x"}, content=make_wav(2)).json() client.post("/api/voice/preview", json={"voice": registered["id"]}) client.post("/api/voice/selection", json={"voice": registered["id"]}) stack.ids.remove(registered["id"]) # 音色从注册表消失 client.app.state.voice_service._tts_health.checked = -1e9 # 重新探测拿新名单 opened = client.post("/api/voice/session").json() assert opened["voice"] == "default" # 诚实回落,不冒充原音色 def test_session_without_selection_keeps_operator_voice(instance): client, stack, custom, worker_state = instance worker_state.write_text(json.dumps({"voice": "rain", "operator_note": "d1"}), encoding="utf-8") opened = client.post("/api/voice/session").json() assert opened["voice"] == "rain" # D1/部署域现值如实生效 assert json.loads(worker_state.read_text()) == {"voice": "rain", "operator_note": "d1"} def test_shared_generic_state_never_touched(instance): client, stack, custom, worker_state = instance registered = client.post("/api/voice/voices", params={"prompt_text": "x"}, content=make_wav(2)).json() client.post("/api/voice/preview", json={"voice": registered["id"]}) client.post("/api/voice/selection", json={"voice": registered["id"]}) client.post("/api/voice/session") assert stack.generic_voice_writes == 0 # 通用助手 voice_state 从不写 assert stack.deletes == [] # 既有音色从不删 assert all(upload["name"].startswith("cg-") for upload in stack.uploads) assert not any(v in stack._registry() for v in BUILTIN) # 11 支既有条目原样 def test_no_identity_rewrite(instance): client, stack, custom, worker_state = instance app = client.app registered = client.post("/api/voice/voices", params={"prompt_text": "x"}, content=make_wav(2)).json() client.post("/api/voice/preview", json={"voice": registered["id"]}) before = { "identity": app.state.store.query_one("SELECT identity_json FROM companion WHERE id=1"), "bazi": app.state.store.query_one("SELECT pillars,derived FROM bazi_core WHERE id=1"), "messages": app.state.store.query("SELECT id,role,text,turn_id,status FROM chat_message"), } client.post("/api/voice/selection", json={"voice": registered["id"]}) client.post("/api/voice/session") after = { "identity": app.state.store.query_one("SELECT identity_json FROM companion WHERE id=1"), "bazi": app.state.store.query_one("SELECT pillars,derived FROM bazi_core WHERE id=1"), "messages": app.state.store.query("SELECT id,role,text,turn_id,status FROM chat_message"), } assert before == after # 换声音只换发声:底色/名字/关系/聊天/记忆一概不动 def test_unconfigured_custom_endpoints_honest(tmp_path): from fastapi.testclient import TestClient provider = R2LLM() router = build_router(providers={t: provider for t in (Tier.HIGH, Tier.STANDARD, Tier.LOW)}) app = create_app(db_path=":memory:", router=router, life_config=LifeConfig(enabled=False), semantic_mode="stub") with TestClient(app) as client: listing = client.get("/api/voice/voices") assert listing.status_code == 200 and listing.json()["configured"] is False for method, path, kwargs in ( ("post", "/api/voice/voices", {"content": make_wav(2)}), ("post", "/api/voice/preview", {"json": {"voice": "rain"}}), ("post", "/api/voice/selection", {"json": {"voice": "rain"}})): response = getattr(client, method)(path, **kwargs) assert response.status_code == 503 assert client.get("/api/voice/selection").status_code == 503