kaikai_test/tests/test_scheduled.py
2026-05-07 16:31:56 +08:00

59 lines
2.1 KiB
Python

from __future__ import annotations
import json
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
from daily_report import scheduled
class ScheduledTest(unittest.TestCase):
def test_send_summary_builds_today_summary_payload(self) -> None:
with tempfile.TemporaryDirectory(prefix="daily-report-scheduled-") as temp:
root = Path(temp)
seed_path = root / "employees.json"
seed_path.write_text(
json.dumps(
[
{"feishu_user_id": "u_1", "name": "Lin", "active": True},
{"feishu_user_id": "u_2", "name": "Chen", "active": True},
],
ensure_ascii=False,
),
encoding="utf-8",
)
fake_file = root / "daily_report" / "config.py"
fake_file.parent.mkdir()
fake_file.write_text("", encoding="utf-8")
env = {
"DATABASE_PATH": "test.sqlite",
"EMPLOYEE_SEED_PATH": "employees.json",
"BASE_URL": "http://localhost:8787",
"FEISHU_WEBHOOK_URL": "https://example.test/webhook",
}
captured = {}
def fake_send(webhook_url: str, payload: dict) -> dict:
captured["webhook_url"] = webhook_url
captured["payload"] = payload
return {"ok": True}
with patch("daily_report.config.__file__", str(fake_file)), patch.dict("os.environ", env, clear=True), patch(
"daily_report.scheduled.send_webhook", fake_send
):
result = scheduled.send_summary("2026-05-07")
self.assertEqual(result, {"ok": True})
self.assertEqual(captured["webhook_url"], "https://example.test/webhook")
text = json.dumps(captured["payload"], ensure_ascii=False)
self.assertIn("2026-05-07 日报提交汇总", text)
self.assertIn("0/2", text)
self.assertIn("Chen、Lin", text)
if __name__ == "__main__":
unittest.main()