from __future__ import annotations import pytest from backend.services.workflow_feedback_interpreter import ( WorkflowFeedbackInterpretationError, WorkflowFeedbackInterpreter, interpret_workflow_feedback, ) CANDIDATES = { "TX-15-01": "candidate-1", "TX-15-02": "candidate-2", "TX-15-03": "candidate-3", } def _ids(items: object) -> list[str]: return [item.candidate_id for item in items] # type: ignore[union-attr] def test_interprets_chinese_likes_and_strict_rejection_by_short_number() -> None: result = interpret_workflow_feedback( "1、2喜欢,3严格拒绝", candidate_whitelist=CANDIDATES, ) assert _ids(result.liked) == ["candidate-1", "candidate-2"] assert result.rejected == () assert _ids(result.strict_rejected) == ["candidate-3"] assert result.final is None def test_interprets_full_display_code_as_final_candidate() -> None: result = interpret_workflow_feedback( "TX-15-03 采用", candidate_whitelist=CANDIDATES, ) assert result.final is not None assert result.final.candidate_id == "candidate-3" assert result.final.display_code == "TX-15-03" def test_interprets_prefix_keep_and_postfix_reject_without_cross_contamination() -> None: result = interpret_workflow_feedback( "保留01和03,02不要", candidate_whitelist=CANDIDATES, ) assert _ids(result.liked) == ["candidate-1", "candidate-3"] assert _ids(result.rejected) == ["candidate-2"] assert result.strict_rejected == () def test_interprets_english_feedback_and_round_directives() -> None: result = interpret_workflow_feedback( "like 1 and 2; hard reject 3; next round: cooler lighting; material: black coat", candidate_whitelist=CANDIDATES, ) assert _ids(result.liked) == ["candidate-1", "candidate-2"] assert _ids(result.strict_rejected) == ["candidate-3"] assert result.next_round_prompt_patch == "cooler lighting" assert result.material_intent == "black coat" def test_interprets_chinese_ordinal_references_and_directives() -> None: result = interpret_workflow_feedback( "第一张喜欢,第三张采用,下一轮加强轮廓光,素材用黑色外套", candidate_whitelist=CANDIDATES, ) assert _ids(result.liked) == ["candidate-1"] assert result.final is not None and result.final.candidate_id == "candidate-3" assert result.next_round_prompt_patch == "加强轮廓光" assert result.material_intent == "黑色外套" def test_rejects_hallucinated_or_out_of_range_candidate() -> None: with pytest.raises(WorkflowFeedbackInterpretationError) as exc_info: interpret_workflow_feedback( "04喜欢", candidate_whitelist=CANDIDATES, ) assert exc_info.value.reason == "unknown_candidate_reference" assert exc_info.value.details["reference"] == "04" assert exc_info.value.details["allowed_display_codes"] == list(CANDIDATES) def test_short_number_must_be_unique_but_full_display_code_remains_safe() -> None: ambiguous_candidates = { "TX-15-01": "candidate-round-15", "TX-16-01": "candidate-round-16", } exact = interpret_workflow_feedback( "tx-16-01 adopt", candidate_whitelist=ambiguous_candidates, ) assert exact.final is not None and exact.final.candidate_id == "candidate-round-16" with pytest.raises(WorkflowFeedbackInterpretationError) as exc_info: interpret_workflow_feedback( "01 adopt", candidate_whitelist=ambiguous_candidates, ) assert exc_info.value.reason == "ambiguous_candidate_reference" def test_llm_result_is_validated_through_display_code_whitelist_without_network() -> None: interpreter = WorkflowFeedbackInterpreter() result = interpreter.validate_llm_result( { "liked": ["01", {"display_code": "TX-15-02"}], "final": {"display_code": "TX-15-03"}, "material_intent": "keep the black coat reference", "next_round_prompt_patch": "use a calmer expression", }, candidate_whitelist=CANDIDATES, source_text="LLM result fixture", ) assert _ids(result.liked) == ["candidate-1", "candidate-2"] assert result.final is not None and result.final.candidate_id == "candidate-3" assert result.source == "llm_validated" assert result.to_dict()["final"] == { "candidate_id": "candidate-3", "display_code": "TX-15-03", } def test_llm_result_rejects_raw_candidate_id_and_unknown_display_code() -> None: interpreter = WorkflowFeedbackInterpreter() with pytest.raises(WorkflowFeedbackInterpretationError) as raw_id_error: interpreter.validate_llm_result( {"liked": [{"candidate_id": "candidate-1"}]}, candidate_whitelist=CANDIDATES, ) assert raw_id_error.value.reason == "invalid_llm_result" with pytest.raises(WorkflowFeedbackInterpretationError) as unknown_code_error: interpreter.validate_llm_result( {"final": "TX-99-01"}, candidate_whitelist=CANDIDATES, ) assert unknown_code_error.value.reason == "unknown_candidate_reference" def test_rejects_conflicting_feedback_and_multiple_final_candidates() -> None: with pytest.raises(WorkflowFeedbackInterpretationError) as conflict_error: interpret_workflow_feedback( "01喜欢,01不要", candidate_whitelist=CANDIDATES, ) assert conflict_error.value.reason == "conflicting_candidate_feedback" with pytest.raises(WorkflowFeedbackInterpretationError) as final_error: WorkflowFeedbackInterpreter().validate_llm_result( {"final": ["01", "02"]}, candidate_whitelist=CANDIDATES, ) assert final_error.value.reason == "multiple_final_candidates" def test_prompt_numbers_are_not_mistaken_for_candidate_feedback() -> None: result = interpret_workflow_feedback( "下一轮使用2K分辨率,不要红色背景", candidate_whitelist=CANDIDATES, ) assert result.has_candidate_feedback is False assert result.next_round_prompt_patch == "使用2K分辨率"