#!/usr/bin/env python3
"""E2E: 日报 (Daily Report)"""
from helper import *

def run():
    header("日报")
    pid = create_project(unique_name("日报测试"))

    # 先创建几个节点，都是今天的
    today = __import__("datetime").datetime.now().strftime("%Y-%m-%d")

    previous = None
    for i, label in enumerate(["发现 Nacos", "CVE-2022-22965", "获取 webshell"]):
        node_type = "vulnerability" if i else "service"
        node = create_node(
            pid,
            label,
            node_type,
            content=f"## {label}\n测试内容行 {i+1}",
            after=previous,
            edge_label="日报链路" if previous else None,
        )
        previous = node["id"]

    # 1. 查询今天日报
    resp = get(f"/api/v1/projects/{pid}/daily-report?date={today}")
    j = assert_ok(resp)
    assert "markdown" in j, f"日报应有 markdown 字段, got keys: {list(j.keys())}"
    assert j["node_count"] == 3, f"今天应有 3 个节点, got {j['node_count']}"
    assert "发现 Nacos" in j["markdown"], f"markdown 应包含节点标签, got: {j['markdown'][:100]}"
    ok("今天日报含3个节点")

    # 2. 查询没有节点的日期
    resp = get(f"/api/v1/projects/{pid}/daily-report?date=2020-01-01")
    j = assert_ok(resp)
    assert j["node_count"] == 0, f"2020-01-01 应无节点, got {j['node_count']}"
    assert "暂无" in j.get("markdown", "") or j["node_count"] == 0
    ok("无节点日期返回空日报")

    # 3. 不传日期参数 —— 默认今天
    resp = get(f"/api/v1/projects/{pid}/daily-report")
    j = assert_ok(resp)
    assert j["node_count"] == 3, f"默认今天应有 3 个节点, got {j['node_count']}"
    ok("默认日期 = 今天")

    # 4. Markdown 格式检查
    md = j["markdown"]
    assert "# " in md, "日报应有标题"
    assert "发现 Nacos" in md, "日报应包含第一个节点"
    ok("Markdown 格式包含标题和节点")

    delete_project(pid)
    summary()

if __name__ == "__main__":
    run()
