配置 SSOT(TOML + .env) 统一错误契约 Auth 与事务边界 Redis / Celery 可靠性:业务 Redis(DB/0)与 Celery broker/backend(DB/1)显式拆分;连接池、sync client 可观测性(OpenTelemetry + LGTM)
79 lines
2.5 KiB
Python
79 lines
2.5 KiB
Python
"""FidelityCheckAgent:解析/LLM 失败时的 fail-closed(新建叙事)vs fail-open(续写)。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
from unittest.mock import MagicMock, patch
|
||
|
||
import pytest
|
||
|
||
from app.agents.memoir.fidelity_check_agent import FidelityCheckAgent
|
||
from app.core.config import settings
|
||
from app.core.llm_call import LLMCallError
|
||
from app.features.memoir.constants import memoir
|
||
|
||
|
||
def test_fidelity_fail_closed_on_parse_when_not_append(
|
||
monkeypatch: pytest.MonkeyPatch,
|
||
) -> None:
|
||
monkeypatch.setattr(memoir, "fidelity_check_enabled", True)
|
||
monkeypatch.setattr(memoir, "fidelity_fail_open_on_parse_error", False)
|
||
agent = FidelityCheckAgent()
|
||
llm = MagicMock()
|
||
with patch(
|
||
"app.agents.memoir.fidelity_check_agent.llm_json_call",
|
||
side_effect=LLMCallError("invoke", "simulated_bad_response"),
|
||
):
|
||
assert (
|
||
agent.passes(
|
||
oral_text=" oral ",
|
||
narrative_json='{"paragraphs":[{"content":"x"}]}',
|
||
llm=llm,
|
||
is_append=False,
|
||
)
|
||
is False
|
||
)
|
||
|
||
|
||
def test_fidelity_fail_open_on_parse_when_append(
|
||
monkeypatch: pytest.MonkeyPatch,
|
||
) -> None:
|
||
monkeypatch.setattr(memoir, "fidelity_check_enabled", True)
|
||
monkeypatch.setattr(memoir, "fidelity_fail_open_on_parse_error", False)
|
||
agent = FidelityCheckAgent()
|
||
llm = MagicMock()
|
||
with patch(
|
||
"app.agents.memoir.fidelity_check_agent.llm_json_call",
|
||
side_effect=LLMCallError("invoke", "simulated_bad_response"),
|
||
):
|
||
assert (
|
||
agent.passes(
|
||
oral_text=" oral ",
|
||
narrative_json='{"paragraphs":[{"content":"x"}]}',
|
||
llm=llm,
|
||
is_append=True,
|
||
)
|
||
is True
|
||
)
|
||
|
||
|
||
def test_fidelity_fail_open_global_flag_overrides_append(
|
||
monkeypatch: pytest.MonkeyPatch,
|
||
) -> None:
|
||
monkeypatch.setattr(memoir, "fidelity_check_enabled", True)
|
||
monkeypatch.setattr(memoir, "fidelity_fail_open_on_parse_error", True)
|
||
agent = FidelityCheckAgent()
|
||
llm = MagicMock()
|
||
with patch(
|
||
"app.agents.memoir.fidelity_check_agent.llm_json_call",
|
||
side_effect=LLMCallError("invoke", "simulated_bad_response"),
|
||
):
|
||
assert (
|
||
agent.passes(
|
||
oral_text=" oral ",
|
||
narrative_json='{"paragraphs":[{"content":"x"}]}',
|
||
llm=llm,
|
||
is_append=False,
|
||
)
|
||
is True
|
||
)
|