2026-04-03 10:12:59 +08:00
|
|
|
|
"""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
|
2026-04-03 13:34:27 +08:00
|
|
|
|
from app.core.llm_call import LLMCallError
|
2026-05-22 13:44:50 +08:00
|
|
|
|
from app.features.memoir.constants import memoir
|
2026-04-03 10:12:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_fidelity_fail_closed_on_parse_when_not_append(
|
|
|
|
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
|
|
|
|
) -> None:
|
2026-05-22 13:44:50 +08:00
|
|
|
|
monkeypatch.setattr(memoir, "fidelity_check_enabled", True)
|
|
|
|
|
|
monkeypatch.setattr(memoir, "fidelity_fail_open_on_parse_error", False)
|
2026-04-03 10:12:59 +08:00
|
|
|
|
agent = FidelityCheckAgent()
|
|
|
|
|
|
llm = MagicMock()
|
|
|
|
|
|
with patch(
|
2026-04-03 13:34:27 +08:00
|
|
|
|
"app.agents.memoir.fidelity_check_agent.llm_json_call",
|
|
|
|
|
|
side_effect=LLMCallError("invoke", "simulated_bad_response"),
|
2026-04-03 10:12:59 +08:00
|
|
|
|
):
|
|
|
|
|
|
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:
|
2026-05-22 13:44:50 +08:00
|
|
|
|
monkeypatch.setattr(memoir, "fidelity_check_enabled", True)
|
|
|
|
|
|
monkeypatch.setattr(memoir, "fidelity_fail_open_on_parse_error", False)
|
2026-04-03 10:12:59 +08:00
|
|
|
|
agent = FidelityCheckAgent()
|
|
|
|
|
|
llm = MagicMock()
|
|
|
|
|
|
with patch(
|
2026-04-03 13:34:27 +08:00
|
|
|
|
"app.agents.memoir.fidelity_check_agent.llm_json_call",
|
|
|
|
|
|
side_effect=LLMCallError("invoke", "simulated_bad_response"),
|
2026-04-03 10:12:59 +08:00
|
|
|
|
):
|
|
|
|
|
|
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:
|
2026-05-22 13:44:50 +08:00
|
|
|
|
monkeypatch.setattr(memoir, "fidelity_check_enabled", True)
|
|
|
|
|
|
monkeypatch.setattr(memoir, "fidelity_fail_open_on_parse_error", True)
|
2026-04-03 10:12:59 +08:00
|
|
|
|
agent = FidelityCheckAgent()
|
|
|
|
|
|
llm = MagicMock()
|
|
|
|
|
|
with patch(
|
2026-04-03 13:34:27 +08:00
|
|
|
|
"app.agents.memoir.fidelity_check_agent.llm_json_call",
|
|
|
|
|
|
side_effect=LLMCallError("invoke", "simulated_bad_response"),
|
2026-04-03 10:12:59 +08:00
|
|
|
|
):
|
|
|
|
|
|
assert (
|
|
|
|
|
|
agent.passes(
|
|
|
|
|
|
oral_text=" oral ",
|
|
|
|
|
|
narrative_json='{"paragraphs":[{"content":"x"}]}',
|
|
|
|
|
|
llm=llm,
|
|
|
|
|
|
is_append=False,
|
|
|
|
|
|
)
|
|
|
|
|
|
is True
|
|
|
|
|
|
)
|