civic-backup / tests /test_scope_rules.py
khiemdztv's picture
Backup source code and docs
cf29045 verified
Raw History Blame Contribute Delete
4.34 kB
"""Cổng phạm vi, chuẩn hoá thủ tục và trạng thái slot — theo feedback vòng test thực tế."""
import pytest
from src.common.schema import CivicResponse
from src.generation.template_verbalizer import NO_BASIS_TEXT, NO_PROCEDURE_TEXT, render
from src.inference.pipeline import (_NO_APPLICATION_ID, _NOT_SUBMITTED, _PLACEHOLDER_PLACE,
final_state_of)
from src.policies.action_policy import match_context_rule, match_procedure_alias
@pytest.mark.parametrize("text", ["hello", "xin chào", "Chào bạn!", "cảm ơn bạn nhé",
"bạn là ai", "Bạn là ai và có thể giúp tôi việc gì?"])
def test_small_talk_is_terminal(text):
rule = match_context_rule(text)
assert rule and rule["name"] == "small_talk" and rule["terminal"]
def test_small_talk_does_not_swallow_real_questions():
assert match_context_rule("chào bạn, tôi bị mất căn cước thì phải làm sao") is None
def test_land_acquisition_is_out_of_scope_not_overdue():
rule = match_context_rule("UBND mở rộng đường và lấy vào phần đất có sổ đỏ của gia đình "
"tôi nhưng chưa thấy thông báo bồi thường")
assert rule["name"] == "out_of_scope_land_acquisition"
assert rule["final_state"] == "OUT_OF_SCOPE"
assert rule["set_labels"]["intent"] != "COMPLAIN_OVERDUE"
def test_land_registration_is_not_out_of_scope():
assert match_context_rule("tôi muốn sang tên sổ đỏ do mua lại nhà của người khác") is None
def test_company_type_asked_only_when_missing():
assert match_context_rule("tôi muốn thành lập 1 công ty")["name"] == "company_type_missing"
assert match_context_rule("tôi muốn thành lập công ty TNHH một thành viên") is None
@pytest.mark.parametrize("text,alias", [
("tôi muốn sang tên sổ đỏ do mua lại nhà của người khác", "land_transfer_registration"),
("tôi bị mất căn cước thì phải làm sao", "citizen_id_reissue"),
("Em có thể thông báo trực tuyến tìm kiếm việc làm hàng tháng không?",
"monthly_job_search_notice"),
])
def test_procedure_aliases(text, alias):
a = match_procedure_alias(text)
assert a and a["name"] == alias and a["procedure"]
def test_land_transfer_forbids_public_housing_sale():
a = match_procedure_alias("sang tên sổ đỏ")
assert "tài sản công" in a["forbid_titles"]
def test_not_applicable_and_not_submitted_patterns():
assert _NO_APPLICATION_ID.search("thủ tục này trên VNeID không cấp mã hồ sơ")
assert _NO_APPLICATION_ID.search("tôi không có mã hồ sơ")
assert not _NO_APPLICATION_ID.search("mã hồ sơ của tôi là G01.801.909-260909-890232")
assert _NOT_SUBMITTED.search("tôi chưa nộp hồ sơ, chỉ muốn biết")
assert _PLACEHOLDER_PLACE.search("Nghị quyết 50/2025/NQ-HĐND của tỉnh X")
assert not _PLACEHOLDER_PLACE.search("Nghị quyết của tỉnh Lạng Sơn")
@pytest.mark.parametrize("kw,state", [
(dict(next_action="OUT_OF_SCOPE", answer="x"), "OUT_OF_SCOPE"),
(dict(reason_code="ASK_USER", answer="x"), "ASK_CLARIFICATION"),
(dict(next_action="REQUEST_MISSING_INFO", answer="x"), "ASK_CLARIFICATION"),
(dict(reason_code="ABSTAIN_LOW_RETRIEVAL_SCORE", answer="x"), "ABSTAIN"),
(dict(next_action="PROVIDE_STATUS", can_answer_directly=True, answer="x"), "ANSWER"),
])
def test_final_state(kw, state):
assert final_state_of(CivicResponse(**kw)) == state
def test_abstain_text_is_context_aware():
base = {"missing_slots": [], "next_action": "PROVIDE_LEGAL_BASIS"}
tax = render({**base, "state": {"intent": "REQUEST_POLICY_CLARIFICATION"}}, [],
"ABSTAIN_LOW_RETRIEVAL_SCORE")
assert NO_BASIS_TEXT in tax and "cơ quan đã tiếp nhận hồ sơ" not in tax
proc = render({**base, "state": {"intent": "REQUEST_PROCEDURE_GUIDE"}}, [],
"ABSTAIN_LOW_RETRIEVAL_SCORE")
assert NO_PROCEDURE_TEXT in proc
overdue = render({**base, "state": {"intent": "COMPLAIN_OVERDUE"}}, [],
"ABSTAIN_LOW_RETRIEVAL_SCORE")
assert "cơ quan đã tiếp nhận hồ sơ" in overdue # hồ sơ đã nộp: hỏi cơ quan là hợp lý