Download mail/tests/test_decide.py from luokelabs/luoke-bench: direct link, hf CLI and curl.
- Browser
- Download file 5.74 kB
-
https://huggingface.co/datasets/luokelabs/luoke-bench/resolve/main/mail/tests/test_decide.py
- Command line
-
hf download hf://datasets/luokelabs/luoke-bench/mail/tests/test_decide.py
-
curl -L -o test_decide.py https://huggingface.co/datasets/luokelabs/luoke-bench/resolve/main/mail/tests/test_decide.py
5.74 kB
| """run_decide.py without a model: the options read from the prompt, the prompt as analyze builds it, the scoring.""" | |
| import os | |
| import sys | |
| import unittest | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # mail/ | |
| import mailrepo # noqa: E402 | |
| ROOT = mailrepo.setup() | |
| sys.path.insert(0, os.path.join(ROOT, "tests")) # isolate.py: the app's guard against touching real data | |
| sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # mail/ before luoke-mail's bench/ | |
| import isolate # noqa: F401,E402 -- first: never the user's real data (tests/isolate.py) | |
| import run_decide as rd # noqa: E402 | |
| from luoke_mail import samples, triage # noqa: E402 | |
| def item(tag, action, suspicious=False, priority=None, cc=False, body="你好", watch=False, auth=None): | |
| mail = {"from_name": "某人", "from_addr": "a@b.example", "to": ["x@y.example"] if cc else [samples.ME], | |
| "cc": [samples.ME] if cc else [], "subject": "主题", "body": body, "date": "2026-10-12", "auth": auth} | |
| return {"tag": tag, "mail": mail, "watch": watch, | |
| "expect": {"action": set(action), "suspicious": suspicious, "priority": set(priority) if priority else None}} | |
| class Options(unittest.TestCase): | |
| def test_every_value_gets_the_meaning_the_prompt_gives_it(self): | |
| self.assertEqual([o["name"] for o in rd.ACTION_OPTIONS], list(triage.ACTIONS)) | |
| self.assertEqual([o["name"] for o in rd.PRIORITY_OPTIONS], list(triage.PRIORITIES)) | |
| said = {o["name"]: o["description"] for o in rd.ACTION_OPTIONS + rd.PRIORITY_OPTIONS} | |
| self.assertTrue(said["reply"].startswith("对方在等用户回信")) | |
| self.assertIn("是 reply 不是 schedule", said["schedule"]) # the long one keeps its own "reply" | |
| self.assertTrue(said["follow_up"].startswith("有用户必须完成的事")) | |
| self.assertEqual(said["ignore"], "广告、群发或诈骗,可直接忽略") | |
| self.assertEqual(said["low"], "看看就行或不用管") | |
| self.assertFalse(any(d.endswith("/") for d in said.values())) | |
| class Prompt(unittest.TestCase): | |
| link = rd.RecipeLink("", "m", "") | |
| def test_the_same_messages_as_analyze_with_the_verdict_kept_back(self): | |
| it = item("a", ["archive"], auth="pass") | |
| messages, cut = rd.prompt(it, self.link) | |
| self.assertFalse(cut) | |
| told = triage.build_messages(it["mail"], "", "中文", samples.TODAY, samples.ME) | |
| kept = triage.build_messages({**it["mail"], "auth": None}, "", "中文", samples.TODAY, samples.ME) | |
| self.assertEqual(messages, kept) | |
| self.assertNotEqual(messages, told) | |
| def test_a_long_body_is_cut_to_the_recipe(self): | |
| messages, cut = rd.prompt(item("a", ["archive"], body="字" * 20000), self.link) | |
| self.assertTrue(cut) | |
| self.assertLessEqual(sum(len(m["content"]) for m in messages), rd.RECIPE["max_chars"]) | |
| self.assertTrue(rd.one_text(messages).startswith(messages[0]["content"] + "\n\n")) | |
| class Scoring(unittest.TestCase): | |
| def test_suspicious_turns_to_ignore_and_a_cc_is_not_high(self): | |
| it = item("a", ["reply"], cc=True) | |
| r = {"probs": {"action": {"reply": 0.7, "ignore": 0.3}, "priority": {"high": 0.9, "low": 0.1}, "suspicious": 0.6}} | |
| self.assertEqual(rd.decision(r, it, 0.5), {"action": "ignore", "action_raw": "reply", "priority": "medium", "suspicious": True}) | |
| self.assertEqual(rd.decision(r, it, 0.7)["action"], "reply") | |
| def test_a_refusal_counts_wrong_either_way(self): | |
| items = [item("s", ["ignore"], suspicious=True), item("n", ["archive"], priority=["low"])] | |
| refused = {"probs": {"action": None, "priority": None, "suspicious": None}, "seconds": 1.0} | |
| s = rd.tally(items, [refused, refused]) | |
| self.assertEqual((s["action"], s["caught"], s["false_alarm"], s["priority"]), (0, 0, 1, 0)) | |
| self.assertEqual(s["refusals"], {"action": 2, "priority": 2, "suspicious": 2}) | |
| def test_watch_items_are_left_out_and_every_arm_reads_the_same(self): | |
| items = [item("s", ["ignore"], suspicious=True), item("n", ["archive"], priority=["low"]), | |
| item("w", ["reply"], watch=True)] | |
| g = [{"triage": {"action": "ignore", "priority": "low", "suspicious": True}, "seconds": 3.0}, | |
| {"triage": {"action": "archive", "priority": "low", "suspicious": False}, "seconds": 2.0}, | |
| {"error": "x"}] | |
| g3 = [{"answer": {"action": "reply", "priority": "low", "suspicious": True}, "seconds": 1.0}, | |
| {"answer": {"action": "archive", "priority": "medium", "suspicious": False}, "seconds": 1.0}, | |
| {"error": "x"}] | |
| sg, sg3 = rd.tally(items, g), rd.tally(items, g3) | |
| self.assertEqual((sg["n"], sg["action"], sg["caught"], sg["false_alarm"], sg["priority"], sg["failed"]), (2, 2, 1, 0, 1, 0)) | |
| self.assertEqual((sg3["action"], sg3["action_raw"], sg3["priority"]), (2, 1, 0)) | |
| self.assertEqual(sg["seconds_median"], 2.5) | |
| def test_a_saved_result_is_not_replaced_unless_asked(self): | |
| import tempfile | |
| with tempfile.TemporaryDirectory() as d: | |
| path = rd.result_path(d, "rewrite", "a9", False) | |
| self.assertEqual(path, os.path.join(d, "rewrite", "a9.json")) | |
| os.makedirs(os.path.dirname(path)) | |
| with open(path, "w") as f: | |
| f.write("{}") | |
| with self.assertRaises(SystemExit): | |
| rd.result_path(d, "rewrite", "a9", False) | |
| self.assertEqual(rd.result_path(d, "rewrite", "a9", True), path) | |
| self.assertEqual(rd.result_path(d, "samples", "a9", False), os.path.join(d, "samples", "a9.json")) | |
| if __name__ == "__main__": | |
| unittest.main() | |