"""周期、元信息、常规项目 / 分类 / 标签 / 执行记录 / 设置。""" from __future__ import annotations from app import constants # ------------------------------------------------------------------ 周期 def test_周期列表会自动补出今天所在的周(h): data = h.periods() assert data["weeks"] and data["today_id"] today = [w for w in data["weeks"] if w["id"] == data["today_id"]][0] y, m, w = constants.week_of_date(__import__("datetime").date.today()) assert (today["year"], today["month"], today["week"]) == (y, m, w) sorts = [x["sort_key"] for x in data["weeks"]] assert sorts == sorted(sorts) # 顶栏按时间正序 assert all(x["first_day"] <= x["last_day"] for x in data["weeks"]) month_keys = {(x["year"], x["month"]) for x in data["months"]} assert (y, m) in month_keys assert sum(x["week_count"] for x in data["months"]) == len(data["weeks"]) def test_新建周期会按标签推年份并跨年(h): base = h.periods()["today_id"] latest = [w for w in h.periods()["weeks"] if w["id"] == base][0] r = h.admin.post("/api/periods", json={"label": "12月第1周"}) assert r.status_code == 200 p = r.json()["period"] # 12 月不早于当前月份 → 仍然是今年 assert p["year"] == latest["year"] and (p["month"], p["week"]) == (12, 1) assert p["sort_key"] == p["year"] * 10000 + 1201 # 同一个周期不会重复建 again = h.admin.post("/api/periods", json={"label": "12月第1周"}).json() assert again["created"] is False and again["period"]["id"] == p["id"] # 再补 1 月:月份比最新周期小,说明跨年,年份 +1 p2 = h.admin.post("/api/periods", json={"label": "1月第1周"}).json()["period"] assert p2["year"] == latest["year"] + 1 and p2["sort_key"] > p["sort_key"] # 标签里写了年份就按写的算 p3 = h.admin.post("/api/periods", json={"label": f"{latest['year'] - 1}年3月第2周"}).json()["period"] assert p3["year"] == latest["year"] - 1 and p3["week"] == 2 def test_新建周期的入参校验(h): assert h.admin.post("/api/periods", json={}).status_code == 400 assert h.admin.post("/api/periods", json={"label": "随便写点什么"}).status_code == 400 p = h.period(2027, 5, 3) assert (p["year"], p["month"], p["week"], p["sort_key"]) == (2027, 5, 3, 20270503) def test_有记录的周期不能删(h): pid = h.today_period_id p = h.project("要被记录的周期项目") h.update(p["id"], pid, content="本周推进") r = h.admin.delete(f"/api/periods/{pid}") assert r.status_code == 400 and "执行记录" in r.json()["detail"] empty = h.period(2030, 1, 1) assert h.admin.delete(f"/api/periods/{empty['id']}").json() == {"ok": True} assert h.admin.delete("/api/periods/424242").status_code == 404 def test_元信息给状态优先级和AI状态(h): meta = h.admin.get("/api/meta").json() assert [x["code"] for x in meta["statuses"]] == [c for c, _, _ in constants.STATUSES] assert [x["code"] for x in meta["priorities"]] == [c for c, _, _ in constants.PRIORITIES] assert meta["ai"]["enabled"] is False # 没配 Key:走本地规则 assert meta["ai"]["has_key"] is False # ------------------------------------------------------------------ 分类 / 标签 def test_分类增删改(h): c = h.category("研发平台", color="#123456") assert c["color"] == "#123456" assert h.admin.post("/api/categories", json={"name": "研发平台"}).status_code == 400 up = h.admin.patch(f"/api/categories/{c['id']}", json={"name": "研发中台", "color": "#000"}) assert up.json()["name"] == "研发中台" assert h.admin.delete(f"/api/categories/{c['id']}").status_code == 200 assert h.admin.patch(f"/api/categories/{c['id']}", json={"name": "x"}).status_code == 404 assert h.admin.delete("/api/categories/424242").status_code == 404 def test_标签重复提交返回同一个(h): t = h.admin.post("/api/tags", json={"name": "重点"}).json() again = h.admin.post("/api/tags", json={"name": "重点"}).json() assert t["id"] == again["id"] assert h.admin.delete(f"/api/tags/{t['id']}").json() == {"ok": True} assert h.admin.delete("/api/tags/424242").status_code == 404 # ------------------------------------------------------------------ 项目 def test_项目字段与人员标签的写入(h): cat = h.category("交付项目") p = h.project("智慧园区", category_id=cat["id"], owners=["张三", "李四"], members=["王五", "赵六"], tags=["新客户", "重点"], is_key=True, progress=35, priority="p1", description="备注说明") assert p["owner"] == "张三" and p["owners"] == ["张三", "李四"] # 第一位是主负责人 assert p["members"] == ["王五", "赵六"] assert {t["name"] for t in p["tags"]} == {"新客户", "重点"} assert p["category"]["name"] == "交付项目" and p["is_key"] and p["progress"] == 35 # 标签是自动创建的,不用先建 assert {t["name"] for t in h.admin.get("/api/tags").json()} >= {"新客户", "重点"} # 换负责人名单是整体替换 up = h.admin.patch(f"/api/projects/{p['id']}", json={"owners": ["李四"], "tags": []}) assert up.json()["owner"] == "李四" and up.json()["owners"] == ["李四"] and up.json()["tags"] == [] def test_项目的重名与空名保护(h): h.project("同名项目") assert h.admin.post("/api/projects", json={"name": "同名项目"}).status_code == 400 p = h.project("另一个项目") assert h.admin.patch(f"/api/projects/{p['id']}", json={"name": "同名项目"}).status_code == 400 assert h.admin.patch(f"/api/projects/{p['id']}", json={"name": " "}).status_code == 400 assert h.admin.patch("/api/projects/424242", json={"name": "不存在"}).status_code == 404 assert h.admin.get("/api/projects/424242").status_code == 404 assert h.admin.delete("/api/projects/424242").status_code == 404 def test_项目列表的筛选与本周期状态(h): pid = h.today_period_id cat = h.category("筛选专用") a = h.project("甲项目", category_id=cat["id"], owners=["陈明"], is_key=True) b = h.project("乙项目", owners=["刘芳"]) h.update(b["id"], pid, content="乙本周有记录") rows = {x["name"]: x for x in h.admin.get("/api/projects").json()} assert rows["甲项目"]["updated_this_period"] is False and rows["甲项目"]["idle_weeks"] is None assert rows["乙项目"]["updated_this_period"] is True and rows["乙项目"]["update_count"] == 1 assert rows["甲项目"]["task_count"] == 0 assert {x["id"] for x in h.admin.get("/api/projects", params={"is_key": True}).json()} == {a["id"]} assert {x["id"] for x in h.admin.get("/api/projects", params={"keyword": "甲"}).json()} == {a["id"]} assert {x["id"] for x in h.admin.get("/api/projects", params={"category_id": cat["id"]}).json()} == {a["id"]} assert {x["id"] for x in h.admin.get("/api/projects", params={"owner": "陈明"}).json()} == {a["id"]} # 归档后默认看不到,archived=true 才出现 h.admin.patch(f"/api/projects/{a['id']}", json={"archived": True}) assert a["id"] not in {x["id"] for x in h.admin.get("/api/projects").json()} assert a["id"] in {x["id"] for x in h.admin.get("/api/projects", params={"archived": "true"}).json()} assert a["id"] in {x["id"] for x in h.admin.get("/api/projects", params={"archived": "all"}).json()} assert b["id"] in {x["id"] for x in h.admin.get("/api/projects", params={"owner": "刘芳"}).json()} def test_按周与按月都能取到本周期数据(h): week = h.period(2028, 3, 2) p = h.project("跨月记录") h.update(p["id"], week["id"], content="2028年3月第2周") # 项目清单始终给全量,周期范围只决定「本周期是否更新 / 隔了几周没更新」 rows = h.admin.get("/api/projects", params={"period_id": week["id"]}).json() assert [x["name"] for x in rows] == ["跨月记录"] and rows[0]["updated_this_period"] is True rows = h.admin.get("/api/projects", params={"year": 2028, "month": 3}).json() assert rows and rows[0]["updated_this_period"] is True assert rows[0]["idle_weeks"] == 0 # 该年月还没有任何周期 → 清单照旧返回,只是都算「本周期没更新」 rows = h.admin.get("/api/projects", params={"year": 2027, "month": 1}).json() assert [x["name"] for x in rows] == ["跨月记录"] and rows[0]["updated_this_period"] is False assert h.admin.get("/api/updates", params={"period_id": week["id"]}).json()[0]["content"] == "2028年3月第2周" assert h.admin.get("/api/updates", params={"year": 2028, "month": 3}).json() != [] assert h.admin.get("/api/updates", params={"year": 2026, "month": 1}).json() == [] def test_执行记录的增改删(h): pid = h.today_period_id p = h.project("记录演练") task = h.admin.post("/api/tasks", json={"project_id": p["id"], "name": "接口联调"}).json() assert h.admin.post("/api/tasks", json={"project_id": p["id"], "name": "接口联调"}).json()["id"] == task["id"] assert h.admin.post("/api/tasks", json={"name": "接口联调"}).status_code == 404 assert h.admin.post("/api/tasks", json={"project_id": p["id"], "name": " "}).status_code == 400 u = h.update(p["id"], pid, task_id=task["id"], content="联调过半", progress=60, status="at_risk", risk="等待接口文档", next_step="约对方对齐", owner="张三") assert u["content"] == "联调过半" and u["status"] == "at_risk" and u["task_id"] == task["id"] assert u["period_label"] or u["period_id"] == pid up = h.admin.patch(f"/api/updates/{u['id']}", json={"progress": 80, "status": "in_progress"}) assert up.json()["progress"] == 80 and up.json()["status"] == "in_progress" assert up.json()["content"] == "联调过半" # 没提交的字段保持原值 assert h.admin.patch(f"/api/updates/{u['id']}", json={"period_id": None}).status_code == 400 assert h.admin.patch("/api/updates/424242", json={"progress": 1}).status_code == 404 detail = h.admin.get(f"/api/projects/{p['id']}").json() assert detail["tasks"] and detail["updates"] assert h.admin.delete(f"/api/updates/{u['id']}").status_code == 200 assert h.admin.get("/api/updates", params={"project_id": p["id"]}).json() == [] assert h.admin.delete(f"/api/updates/{u['id']}").status_code == 404 def test_删除项目会带走它的记录(h): pid = h.today_period_id p = h.project("用完就删") h.update(p["id"], pid, content="临时记录") assert h.admin.delete(f"/api/projects/{p['id']}").status_code == 200 assert h.admin.get("/api/updates", params={"project_id": p["id"]}).json() == [] def test_设置只认AI相关的键(h): r = h.admin.put("/api/settings", json={"values": {"ai_model": "glm-4", "ai_api_key": "sk-x", "ai_temperature": "0.5", "偷改的键": "1"}}) assert r.json() == {"ok": True} s = h.admin.get("/api/settings").json() assert s["ai_model"] == "glm-4" and s["ai_api_key"] == "sk-x" and s["ai_temperature"] == "0.5" assert "偷改的键" not in s assert h.admin.get("/api/meta").json()["ai"]["has_key"] is True # 配了 Key 就显示已配置