import unittest from agentd.core import build_prompt, handoff_summary ISSUE = {"name": "T-1 do it", "description_html": "
goal & scope
"} COMMENTS = [ {"id": "c1", "created_at": "t1", "comment_html": "please also check X
"}, {"id": "c2", "created_at": "t2", "comment_html": "[worker] 第 1 轮开始
"}, {"id": "c3", "created_at": "t3", "comment_html": "[codex] 第 1 轮结束
[question] A or B?"}, {"id": "c4", "created_at": "t4", "comment_html": "
B, please
"}, ] class PromptTest(unittest.TestCase): def test_fresh_prompt_has_everything_but_worker_comments(self): p = build_prompt("GW-1", ISSUE, COMMENTS, set(), False, protocol="PROTO", intro="INTRO") self.assertTrue(p.startswith("INTRO")) self.assertIn("## 协议\nPROTO", p) self.assertIn("## 工作项 GW-1: T-1 do it\n\ngoal & scope", p) self.assertIn("please also check X", p) self.assertIn("[question] A or B?", p) self.assertNotIn("[worker]", p) self.assertTrue(p.rstrip().endswith("不要等待人工回复。")) def test_resumed_prompt_only_new_comments(self): p = build_prompt("GW-1", ISSUE, COMMENTS, {"c1", "c2", "c3"}, True, protocol="PROTO", intro="INTRO") self.assertNotIn("PROTO", p) self.assertNotIn("INTRO", p) self.assertNotIn("please also check X", p) self.assertIn("B, please", p) self.assertIn("同一线程", p) def test_resumed_without_new_comments_asks_to_continue(self): p = build_prompt("GW-1", ISSUE, COMMENTS, {"c1", "c2", "c3", "c4"}, True) self.assertIn("没有新评论", p) p = build_prompt("GW-1", ISSUE, COMMENTS, {"c1", "c2", "c3", "c4"}, True, retry_note="RETRY") self.assertIn("RETRY", p) self.assertNotIn("没有新评论", p) def test_new_comment_filter_is_by_id_not_timestamp(self): late_but_old = [{"id": "c9", "created_at": "t0", "comment_html": "clock-skewed reply
"}, {"id": "c1", "created_at": "t9", "comment_html": "edited old one
"}] p = build_prompt("GW-1", ISSUE, late_but_old, {"c1"}, True) self.assertIn("clock-skewed reply", p) self.assertNotIn("edited old one", p) def test_handoff(self): rec = {"runs": 3, "tags": ["progress", "progress", "question"], "last_message": "[question] A or B?"} h = handoff_summary(rec, "线程已续 3 轮") self.assertIn("3 轮", h) self.assertIn("progress, progress, question", h) self.assertIn("[question] A or B?", h) p = build_prompt("GW-1", ISSUE, COMMENTS, set(), False, handoff=h) self.assertIn("## 交接摘要\n" + h, p) if __name__ == "__main__": unittest.main()