"""企微机器人显示名:后台配置 + 新旧都认。""" from __future__ import annotations import os import sys import unittest ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..")) if ROOT not in sys.path: sys.path.insert(0, ROOT) os.environ.setdefault("LEDGER_BACKEND", "memory") from agent.policy.bot_name import ( DEFAULT_BOT_DISPLAY_NAME, REPLY_CODE, at_bot, bot_display_name, bot_mention_names, strip_bot_mention, text_mentions_bot, ) from agent.policy.bot_reply import override_catalog, render from agent.policy import inquiry_copy as copy class BotDisplayNameTests(unittest.TestCase): def tearDown(self) -> None: override_catalog(None) def test_default_when_empty(self) -> None: override_catalog({}) self.assertEqual(bot_display_name(), DEFAULT_BOT_DISPLAY_NAME) self.assertEqual(at_bot(), f"@{DEFAULT_BOT_DISPLAY_NAME}") self.assertIn("询价小助手", bot_mention_names()) self.assertIn(DEFAULT_BOT_DISPLAY_NAME, bot_mention_names()) def test_override_from_admin(self) -> None: override_catalog({REPLY_CODE: "测试助手名"}) self.assertEqual(bot_display_name(), "测试助手名") self.assertTrue(text_mentions_bot("@测试助手名 WO1")) self.assertTrue(text_mentions_bot("@询价小助手 WO1")) self.assertEqual(strip_bot_mention("@测试助手名 2"), "2") self.assertEqual(strip_bot_mention("@询价小助手 2"), "2") def test_collab_hint_uses_display_name(self) -> None: override_catalog({REPLY_CODE: "智报通AIQ"}) hint = copy.collab_hint("WO202610060030") self.assertIn("WO202610060030@智报通AIQ", hint) self.assertNotIn("询价小助手", hint) def test_template_bot_name_placeholder(self) -> None: override_catalog( { REPLY_CODE: "智报通AIQ", "inquiring.air_collab_hint": "请发送 {{ticket_no}}@{{bot_name}}", } ) self.assertEqual( render("inquiring.air_collab_hint", "缺省", ticket_no="WO1"), "请发送 WO1@智报通AIQ", ) def test_legacy_and_new_strip(self) -> None: override_catalog({REPLY_CODE: "智报通AIQ"}) rest_old = strip_bot_mention("工单编号WO202610070026@询价小助手") rest_new = strip_bot_mention("@智报通AIQ WO202610070026") self.assertNotIn("询价小助手", rest_old) self.assertNotIn("智报通AIQ", rest_new) self.assertIn("WO202610070026", rest_old) self.assertIn("WO202610070026", rest_new) def test_strip_wecom_lock_instruction(self) -> None: """锁舱 instructionText 不得把 @机器人 原样带给 TMS。""" override_catalog({REPLY_CODE: "智价通AIQ"}) cleaned = strip_bot_mention("@智价通AIQ AIR-OPT01, TH, TH641") self.assertEqual(cleaned, "AIR-OPT01, TH, TH641") self.assertNotIn("@", cleaned) self.assertNotIn("智价通AIQ", cleaned) if __name__ == "__main__": unittest.main()