"""人力库(花名册 / 负载)、每周重点工作、周视图汇总。""" from __future__ import annotations from app import constants def _row(h, name): rows = [x for x in h.admin.get("/api/staff").json()["items"] if x["name"] == name] assert rows, f"花名册里找不到 {name}" return rows[0] # ------------------------------------------------------------------ 人力库 def test_花名册的增删改查(h): a = h.staff("陈明", group="Nex产品组", affiliation="汇智", note="接口负责人") assert (a["group"], a["affiliation"], a["note"]) == ("Nex产品组", "汇智", "接口负责人") assert a["sort_order"] == 1 # 不给序号就排在最后 assert h.staff("李雷", group="AI项目组", affiliation="云数科技")["group"] == "AI产品组" assert _row(h, "李雷")["affiliation"] == "云数" # 历史写法归一到标准口径 assert h.admin.post("/api/staff", json={"name": "陈明"}).status_code == 400 assert h.admin.post("/api/staff", json={"name": " "}).status_code == 400 assert h.admin.patch("/api/staff/424242", json={"note": "x"}).status_code == 404 assert h.admin.delete("/api/staff/424242").status_code == 404 upd = h.admin.patch(f"/api/staff/{a['id']}", json={"note": "换成项目负责人", "偷改": 1}).json() assert upd["synced"] == {"projects": 0, "custom_projects": 0} assert upd["staff"]["note"] == "换成项目负责人" and "偷改" not in upd["staff"] assert h.admin.patch(f"/api/staff/{a['id']}", json={"name": "李雷"}).status_code == 400 assert h.admin.patch(f"/api/staff/{a['id']}", json={"name": " "}).status_code == 400 # 归档后默认列表看不到,但名字仍占着,不能新建同名 h.admin.patch(f"/api/staff/{a['id']}", json={"archived": True}) assert "陈明" not in [x["name"] for x in h.admin.get("/api/staff").json()["items"]] assert "陈明" in [x["name"] for x in h.admin.get("/api/staff", params={"archived": "true"}).json()["items"]] assert h.admin.post("/api/staff", json={"name": "陈明"}).status_code == 400 # 关键字命中姓名或备注;小组筛选按标准口径 assert [x["name"] for x in h.admin.get("/api/staff", params={"archived": "all", "keyword": "负责人"}).json()["items"]] == ["陈明"] assert [x["name"] for x in h.admin.get("/api/staff", params={"keyword": "李雷"}).json()["items"]] == ["李雷"] assert [x["name"] for x in h.admin.get("/api/staff", params={"group": "AI产品组"}).json()["items"]] == ["李雷"] def test_负载统计与删除保护(h): s = h.staff("王芳", group="定制产品组") p = h.project("园区项目", owners=["王芳"], is_key=True) cp = h.custom("定制甲", owner="王芳", amount=100000, revenue_amount=40000) h.project("别人的项目", owners=["赵四"], members=["王芳"]) row = _row(h, "王芳") assert (row["project_count"], row["key_project_count"]) == (1, 1) assert row["custom_count"] == 1 assert (row["custom_amount"], row["custom_pending"]) == (100000.0, 60000.0) assert row["last_update"] is None # 名下项目还没有任何执行记录 data = h.admin.get("/api/staff").json() assert data["summary"]["total"] == 1 and data["summary"]["with_project"] == 1 assert {"name": "定制产品组", "count": 1} in data["summary"]["groups"] assert {"name": "未填写", "count": 1} in data["summary"]["affiliations"] assert data["summary"]["outside"] == [{"name": "赵四", "project_count": 1, "custom_count": 0}] assert data["dict"]["groups"] and data["dict"]["affiliations"] # 有执行记录之后,负载上能看到最近一次动静在哪一周 pid = h.today_period_id h.update(p["id"], pid, content="本周推进") assert _row(h, "王芳")["last_update"] == h.periods()["weeks"][-1]["full_label"] # 名下有在管项目:默认不给删,提示归档;确认要删才删 r = h.admin.delete(f"/api/staff/{s['id']}") assert r.status_code == 400 and "归档" in r.json()["detail"] assert h.admin.delete(f"/api/staff/{s['id']}?force=true").json()["ok"] is True assert h.admin.get("/api/projects").json() and h.admin.get("/api/custom/projects").json() # 改名要把名下项目的负责人一并带过去,否则负载和记录会认不出人 q = h.staff("旧名字") mine = h.project("要跟着改名的项目", owners=["旧名字"], other_owners=["旧名字"]) patched = h.admin.patch(f"/api/staff/{q['id']}", json={"name": "新名字"}).json() assert patched["synced"] == {"projects": 1, "custom_projects": 0} fresh = h.admin.get(f"/api/projects/{mine['id']}").json()["project"] assert fresh["owner"] == "新名字" and "旧名字" not in fresh["owners"] assert cp["id"] # ------------------------------------------------------------------ 每周重点工作 def test_重点工作按录入周归属(h): today = h.periods()["today_id"] item = h.focus("整理三季度复盘") assert item["period_id"] == today # 不传周期就按今天所在的周 assert item["status"] == "not_started" and item["delayed"] is False assert h.admin.post("/api/focus", json={"content": " "}).status_code == 400 assert h.admin.post("/api/focus", json={"content": "x", "status": "起飞了"}).status_code == 400 assert h.admin.post("/api/focus", json={"content": "x", "period_id": 424242}).status_code == 400 bad = h.admin.post("/api/focus", json={"content": "x", "due_date": "下周五"}) assert bad.status_code == 400 and "YYYY-MM-DD" in bad.json()["detail"] week = h.period(2028, 3, 2) other = h.focus("明年这时候干", period_id=week["id"], due_date="2028-03-20", detail="说明") assert other["period_id"] == week["id"] and other["due_date"] == "2028-03-20" # 改预计完成时间不会把条目搬去别的周 moved = h.admin.patch(f"/api/focus/{other['id']}", json={"due_date": "2026-01-05"}).json() assert moved["due_date"] == "2026-01-05" and moved["period_id"] == week["id"] assert h.admin.patch(f"/api/focus/{other['id']}", json={"due_date": ""}).json()["due_date"] is None done = h.admin.patch(f"/api/focus/{other['id']}", json={"status": "done", "result": "已完成", "delayed": True}).json() assert (done["status"], done["result"], done["delayed"]) == ("done", "已完成", True) assert h.admin.patch(f"/api/focus/{other['id']}", json={"status": "不认识"}).status_code == 400 assert h.admin.patch(f"/api/focus/{other['id']}", json={"content": ""}).status_code == 400 assert h.admin.patch("/api/focus/424242", json={"status": "done"}).status_code == 404 rows = h.admin.get("/api/focus").json() assert [x["id"] for x in rows] == [other["id"], item["id"]] # 新录入在前 assert [x["id"] for x in h.admin.get("/api/focus", params={"status": "done"}).json()] == [other["id"]] assert [x["id"] for x in h.admin.get("/api/focus", params={"keyword": "复盘"}).json()] == [item["id"]] assert [x["id"] for x in h.admin.get("/api/focus", params={"keyword": "说明"}).json()] == [other["id"]] h.admin.patch(f"/api/focus/{item['id']}", json={"archived": True}) assert item["id"] not in [x["id"] for x in h.admin.get("/api/focus").json()] assert item["id"] in [x["id"] for x in h.admin.get("/api/focus", params={"archived": "true"}).json()] assert h.admin.delete(f"/api/focus/{item['id']}").json()["ok"] is True assert h.admin.delete("/api/focus/424242").status_code == 404 # ------------------------------------------------------------------ 周视图 def test_周视图把两条线的动态合在一起(h): week = h.period(2028, 3, 2) key = h.project("重点项目", owners=["王芳"], is_key=True) quiet = h.project("沉默的重点项目", is_key=True) h.update(key["id"], week["id"], content="接口联调过半", progress=60, status="at_risk", risk="等对方给文档", next_step="周三对齐") h.update(key["id"], week["id"], content="出现阻塞", status="blocked") # 入库/下单时间取本周第一天,确保「本周新增」判定落在选中的这一周里 week_first = constants.period_first_day(2028, 3, 2) busy = h.custom("本周有动静的定制", owner="王芳", amount=100000, entry_date=week_first, sign_date=week_first) idle = h.custom("躺着不动的定制", amount=200000) h.custom_update(busy["id"], week["id"], content="完成部署", progress=80, amount_delta=50000, revenue_delta=20000, risk="验收时间待定") d = h.admin.get("/api/weekly", params={"period_id": week["id"]}).json() assert [x["id"] for x in d["periods"]] == [week["id"]] projects = {x["name"]: x for x in d["projects"]} assert set(projects) == {"重点项目", "沉默的重点项目"} # 非重点又没动静的不占版面 assert projects["重点项目"]["has_update"] and projects["重点项目"]["update_count"] == 2 assert projects["重点项目"]["progress"] == 60.0 assert projects["重点项目"]["progress_source"] == "本周期" assert projects["重点项目"]["risks"] == ["等对方给文档"] assert projects["重点项目"]["status"] == "blocked" # 多条记录取最坏状态 assert projects["沉默的重点项目"]["has_update"] is False assert projects["沉默的重点项目"]["progress_source"] == "项目档案" # 定制这一线:本周有动静 / 本周新增 / 重点才占版面,躺着不动的默认不出现 custom = {x["name"]: x for x in d["custom"]} assert set(custom) == {"本周有动静的定制"} assert custom["本周有动静的定制"]["is_new"] is True # 入库时间落在本周 assert custom["本周有动静的定制"]["period_amount_delta"] == 50000.0 assert custom["本周有动静的定制"]["period_revenue_delta"] == 20000.0 k = d["kpi"] assert (k["key_total"], k["key_updated"], k["key_idle"]) == (2, 1, 1) assert (k["update_count"], k["custom_new"], k["custom_updated"]) == (2, 1, 1) assert k["amount_delta"] == 50000.0 and k["revenue_delta"] == 20000.0 assert k["risk_count"] == 2 and k["at_risk_count"] == 1 and k["blocked_count"] == 1 # 按人筛选只看他名下的活儿 byowner = h.admin.get("/api/weekly", params={"period_id": week["id"], "owner": "王芳"}).json() assert [x["name"] for x in byowner["projects"]] == ["重点项目"] assert [x["name"] for x in byowner["custom"]] == ["本周有动静的定制"] # 月视图:该月各周合并统计,跨周的记录也算进来 monthly = h.admin.get("/api/weekly", params={"year": 2028, "month": 3}).json() assert monthly["kpi"]["update_count"] == 2 and monthly["periods"][0]["id"] == week["id"] # all_custom:在途的定制项目全部列出来,没动静的也盯一眼 everything = h.admin.get("/api/weekly", params={"period_id": week["id"], "all_custom": True}).json() assert {x["name"] for x in everything["custom"]} == {"本周有动静的定制", "躺着不动的定制"} assert {x["name"]: x["is_new"] for x in everything["custom"]}["躺着不动的定制"] is False assert everything["kpi"]["custom_total"] == 2 def test_周视图的没更新周数(h): w1 = h.period(2028, 3, 1) w2 = h.period(2028, 3, 2) w3 = h.period(2028, 3, 3) p = h.project("看更新节奏", is_key=True) h.update(p["id"], w1["id"], content="第一周更新过一次") rows = {x["name"]: x for x in h.admin.get("/api/weekly", params={"period_id": w3["id"]}).json()["projects"]} assert rows["看更新节奏"]["idle_weeks"] == 2 and rows["看更新节奏"]["has_update"] is False rows = {x["name"]: x for x in h.admin.get("/api/weekly", params={"period_id": w2["id"]}).json()["projects"]} assert rows["看更新节奏"]["idle_weeks"] == 1 assert rows["看更新节奏"]["last_update"] == "2028年3月第1周" def test_选中的年月还没有周期时给空骨架(h): h.project("空视图") d = h.admin.get("/api/weekly", params={"year": 2031, "month": 6}).json() assert d["periods"] == [] and d["projects"] == [] and d["custom"] == [] assert set(d["kpi"]) == {"key_total", "key_updated", "key_idle", "project_listed", "update_count", "custom_total", "custom_new", "custom_updated", "custom_amount", "amount_delta", "revenue_delta", "risk_count", "at_risk_count", "blocked_count"} assert all(not v for v in d["kpi"].values())