#!/usr/bin/env python3
"""E2E: 时间线视图"""
from helper import *
import time

header("Timeline")

pid = create_project(unique_name("E2E-时间线"))

# Create nodes sequentially (timestamps matter)
nid1 = create_node(pid, "第一步-信息收集", "note", "## 信息收集")["id"]
time.sleep(0.1)
nid2 = create_node(pid, "第二步-发现漏洞", "vulnerability", "## Shiro", after=nid1, edge_label="扫描发现")["id"]
time.sleep(0.1)
nid3 = create_node(pid, "第三步-获取权限", "attack_step", "## getshell", after=nid2, edge_label="利用")["id"]

# ── Timeline list ──
items = assert_ok(get(f"/api/v1/projects/{pid}/timeline"))
assert len(items) >= 3  # may include auto-created edge nodes

# Should be chronological
labels = [it["node_label"] for it in items if "步" in it["node_label"]]
assert labels == ["第一步-信息收集", "第二步-发现漏洞", "第三步-获取权限"], \
    f"wrong order: {labels}"
ok("chronological order correct")

# Check edge labels in timeline items
step2 = next(it for it in items if it["node_label"] == "第二步-发现漏洞")
incoming = [e["label"] for e in step2.get("incoming", [])]
assert "扫描发现" in incoming, f"missing edge in incoming: {incoming}"
ok("timeline includes incoming edge labels")

step3 = next(it for it in items if it["node_label"] == "第三步-获取权限")
outgoing = [e["label"] for e in step3.get("outgoing", [])]
# step3 may or may not have outgoing edges
ok("timeline structure valid")

delete_project(pid)

summary()
