import unittest


class CreativeReviewScanTests(unittest.TestCase):
    def test_parse_rejected_result_collects_reasons_and_locations(self):
        from tools.creative_review import parse_review_result

        parsed = parse_review_result({
            "dynamic_creative_id": 123,
            "reject_message_list": ["整体拒绝"],
            "site_set_result_list": [
                {
                    "site_set": "SITE_SET_MOMENTS",
                    "system_status": "DYNAMIC_CREATIVE_STATUS_DENIED",
                    "reject_message": "朋友圈拒绝",
                }
            ],
            "element_result_list": [
                {
                    "element_name": "image",
                    "element_type": "ELEMENT_TYPE_IMAGE",
                    "review_status": "REVIEW_STATUS_REJECTED",
                    "element_reject_detail_info": [
                        {
                            "reason": "图片含违规信息",
                            "reject_info_location": [
                                {"x": 1, "y": 2, "width": 3, "height": 4}
                            ],
                        }
                    ],
                }
            ],
        })

        self.assertEqual(parsed.review_status, "rejected")
        self.assertIn("整体拒绝", parsed.reject_messages)
        self.assertIn("朋友圈拒绝", parsed.reject_messages)
        self.assertIn("图片含违规信息", parsed.reject_messages)
        self.assertEqual(len(parsed.rejection_facts), 2)

    def test_parse_pending_result(self):
        from tools.creative_review import parse_review_result

        parsed = parse_review_result({
            "dynamic_creative_id": 456,
            "is_all_component_compose_pending": True,
            "delay_message_list": ["审核延迟"],
        })

        self.assertEqual(parsed.review_status, "pending")
        self.assertIn("审核延迟", parsed.delay_messages)

    def test_chunk_ids_by_account_limits_to_100(self):
        from tools.creative_review import group_review_tasks

        tasks = [
            {"account_id": 1, "dynamic_creative_id": i}
            for i in range(250)
        ] + [{"account_id": 2, "dynamic_creative_id": 999}]

        groups = list(group_review_tasks(tasks, batch_size=100))

        self.assertEqual(len(groups), 4)
        self.assertEqual(groups[0][0], 1)
        self.assertEqual(len(groups[0][1]), 100)
        self.assertEqual(len(groups[1][1]), 100)
        self.assertEqual(len(groups[2][1]), 50)
        self.assertEqual(groups[3], (2, [999]))


if __name__ == "__main__":
    unittest.main()
