from __future__ import annotations

import json
import unittest

from daily_report.robot_service import create_bot_message_body, create_reminder_payload, create_summary_payload, create_webhook_sign


class RobotServiceTest(unittest.TestCase):
    def test_creates_reminder_payload_with_submit_link(self) -> None:
        payload = create_reminder_payload("http://localhost:8787/submit", "每日工作汇报提醒")
        text = json.dumps(payload, ensure_ascii=False)

        self.assertEqual(payload["msg_type"], "interactive")
        self.assertIn("每日工作汇报提醒", text)
        self.assertIn("http://localhost:8787/submit", text)

    def test_creates_summary_payload_with_missing_employees(self) -> None:
        payload = create_summary_payload(
            "http://localhost:8787/manager",
            {
                "date": "2026-05-07",
                "expectedCount": 2,
                "submittedCount": 1,
                "missingCount": 1,
                "missing": [{"name": "Chen"}],
            },
        )
        text = json.dumps(payload, ensure_ascii=False)

        self.assertIn("2026-05-07", text)
        self.assertIn("1/2", text)
        self.assertIn("Chen", text)
        self.assertIn("http://localhost:8787/manager", text)

    def test_creates_webhook_signature(self) -> None:
        signature = create_webhook_sign("1599360473", "test-secret")

        self.assertIsInstance(signature, str)
        self.assertGreater(len(signature), 20)

    def test_creates_bot_interactive_message_body(self) -> None:
        payload = create_reminder_payload("http://localhost:8787/submit")
        body = create_bot_message_body(payload)

        self.assertEqual(body["msg_type"], "interactive")
        self.assertEqual(body["receive_id"], "")
        self.assertIn("请提交今日日报", body["content"])


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