"""The sets written after real inboxes (sets/task_sets_real.py) run through the app's code with a canned model.""" import json 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_tasks # noqa: E402 import task_sets_real as R # noqa: E402 from luoke_mail import digest, engines # noqa: E402 class Canned(engines.Direct): def __init__(self, answer): super().__init__("http://127.0.0.1:1/v1", "canned") self.answer = answer def limits(self, task): return {"max_chars": 12000, "batch": 1} def chat(self, task, messages, schema=None, **kw): return json.dumps(self.answer, ensure_ascii=False), 1, 1 class Sets(unittest.TestCase): def test_sizes_and_defaults(self): self.assertEqual((len(R.DRAFT), len(R.READ), len(R.PICK), sum(map(len, R.CLEANUP.values())), len(R.SENDERS)), (15, 10, 6, 15, 10)) self.assertFalse(set(run_tasks.OBSERVED) & set(run_tasks.TASKS)) def test_a_draft_with_the_users_phone_is_a_hard_failure(self): items, score = run_tasks.run_draft(Canned({"body": f"好的,我的手机号是 {R.PHONE}"})) self.assertGreaterEqual(score["hard_failures"], 1) self.assertEqual(score["n"], 15) def test_every_letter_reads_with_its_links(self): for c in R.READ: mail = digest.read(c["raw"]) self.assertTrue(mail["links"], mail["subject"]) def test_an_advert_shown_is_a_hard_failure(self): answer = {"items": [{"title": "赞助:某云 GPU", "score": 3, "lasting": False, "link": None, "topics": [], "summary": "x", "why": "y"}]} _, score = run_tasks.run_digest_read(Canned(answer)) self.assertGreaterEqual(score["hard_failures"], 1) def test_a_pick_of_what_the_user_dislikes_is_a_hard_failure(self): _, score = run_tasks.run_digest_pick(Canned({"picks": [{"n": 2, "why": "x"}]})) self.assertGreaterEqual(score["hard_failures"], 1) if __name__ == "__main__": unittest.main()