"""知识块粒度选择的单元测试。 背景是实测的三次翻车,每条判据都对应其中一次: 1. 客户问「起投多少」和「风险高吗」时命中同一块(整个产品小节),拿到**完全相同**的 整节内容,看起来像客服没听懂问题——所以把表格行拆成了行级子块。 2. 拆细之后「介绍一下」又被某一行抢答(返回"产品期限 90天封闭期")——所以要能换回整节。 3. 两次判据写错:用"含连字符"认子块时,整节块自己的编号 PROD-901 被误判成子块; 用"不含两位数字后缀"认整节块时,FAQ 块全被误判成整节块、把正确答案挤出了 top1。 """ from app.service.agent.implementations.customer_service import CustomerServiceAgent from app.service.knowledge_search_service import KnowledgeSearchService def test_parent_of_recognises_row_blocks() -> None: assert KnowledgeSearchService._parent_of("PROD-007-04") == "PROD-007" def test_parent_of_rejects_section_blocks() -> None: """整节块的编号本身就含连字符(PROD-901),不能被当成子块。""" assert KnowledgeSearchService._parent_of("PROD-901") is None assert KnowledgeSearchService._parent_of("FAQ-0016") is None assert KnowledgeSearchService._parent_of("HNW-003") is None def test_section_chosen_for_overview_question() -> None: """客户问整节时,用父块替掉抢答的那一行。""" child = {"doc_id": "PROD-007-05", "title": "手册 · 2.1 南方季季盈90天 · 产品期限"} parent = {"doc_id": "PROD-007", "content": "整节内容"} assert CustomerServiceAgent._prefer_section( "南方季季盈90天介绍一下", child, [child, parent] ) is parent def test_row_kept_when_question_names_the_field() -> None: """客户问的正是那一行时不能换成整节,否则"聚焦"就白做了。""" child = {"doc_id": "PROD-007-04", "title": "手册 · 2.1 南方季季盈90天 · 起投金额"} parent = {"doc_id": "PROD-007", "content": "整节内容"} assert CustomerServiceAgent._prefer_section( "季季盈90天起投多少", child, [child, parent] ) is child def test_missing_parent_falls_back_to_row() -> None: """父块没带回来时仍用子块:宁可答得窄,也不要拿不相干的块搪塞。""" child = {"doc_id": "PROD-007-05", "title": "手册 · 2.1 南方季季盈90天 · 产品期限"} assert CustomerServiceAgent._prefer_section("介绍一下", child, [child]) is child def test_plain_block_is_not_treated_as_section() -> None: """FAQ 这类独立块没有子块挂在下面,不该被当成整节块。""" plain = {"doc_id": "FAQ-0016", "title": "基金赎回到账需要多长时间?"} assert CustomerServiceAgent._prefer_section("基金赎回几天到账", plain, [plain]) is plain