dashboard/test_home_smoke.py
# -*- coding: utf-8 -*-
"""首页冒烟测试:造本地 sqlite 数据 → 直接调 /api/home/summary。
只在本地 data/dashboard.db 上跑(STORAGE_BACKEND=sqlite),
不碰云库,不碰任何生产数据。
"""
import json
import os
import sqlite3
import sys
from datetime import date, timedelta
DB = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data", "dashboard.db")
TODAY = date.today()
D = lambda n: (TODAY + timedelta(days=n)).strftime("%Y-%m-%d")
def seed():
c = sqlite3.connect(DB)
c.execute("DELETE FROM 任务表")
c.execute("DELETE FROM 权限申请表")
c.execute("DELETE FROM 门店表")
c.execute("DELETE FROM 用户表")
stores = [("s1", "长沙麓谷店", "QQ"), ("s2", "长沙中南店", "iCAR"),
("s3", "长沙远大路店", "纵横"), ("s4", "岳阳脸谱店", "捷途")]
for sid, name, brand in stores:
c.execute('INSERT INTO 门店表("编号","名称","品牌","负责人","创建时间") '
'VALUES(?,?,?,?,?)', (sid, name, brand, "张三", D(-90)))
users = [("u1", "admin", "阿杰", "董事长", "__all__"),
("u2", "mgr1", "李四", "普通员工", "长沙麓谷店")]
for uid, acc, nm, role, sc in users:
c.execute('INSERT INTO 用户表("编号","账号","姓名","密码哈希","角色","门店范围") '
'VALUES(?,?,?,?,?,?)', (uid, acc, nm, "pbkdf2_sha256$1$a$b", role, sc))
tasks = [
("t1", "补齐 6 家门店负责人与品牌线", "门店档案占位「—」", "董事长", "李四",
"董事长", "高", "进行中", "刚下发", D(-5), "全部补录完成", "长沙麓谷店"),
("t2", "旅胜店库存超 90 天车辆清库方案", "压库 23 台", "董事长", "张三",
"董事长", "高", "待验收", "已提交", D(-1), "方案含时间表", "岳阳脸谱店"),
("t3", "中南纵横 SCRM token 重新授权", "token 被挤掉", "董事长", "李四",
"董事长", "中", "进行中", "已扫码", D(3), "探测返回 200", "长沙中南店"),
("t4", "远大路店线索有效性复盘", "有效性 18%", "董事长", "王五",
"董事长", "中", "已驳回", "需重做", D(5), "给出渠道归因", "长沙远大路店"),
("t5", "10 月毛利规则复核", "四维毛利对不上", "董事长", "李四",
"董事长", "低", "已完成", "已通过", D(-10), "复核表签字", ""),
("t6", "iCAR V27 上市物料到店", "上市前布置", "董事长", "赵六",
"李四", "中", "进行中", "在途", D(7), "物料签收单", "长沙中南店"),
]
for t in tasks:
c.execute('INSERT INTO 任务表("编号","标题","背景","下发人","执行人","验收人",'
'"优先级","状态","进度","截止","验收标准","门店","创建时间") '
'VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)', t + (D(-8),))
apps = [
("pa1", "mgr1", "李四", "品牌总", "指定门店", "长沙麓谷店", "需要看两家店对比", "待审批"),
("pa2", "newbie", "新人小王", "普通员工", "本店", "长沙中南店", "刚入职需看本店数据", "待审批"),
]
for a in apps:
c.execute('INSERT INTO 权限申请表("编号","账号","姓名","期望角色","期望范围",'
'"期望门店","申请理由","状态","申请时间") VALUES(?,?,?,?,?,?,?,?,?)',
a + (D(-2),))
cfg = {
"brands": [
{"brand": "QQ", "stores": [
{"store_id": "s1", "interfaces": [
{"id": "leads_s1", "name": "SCRM 唯一线索", "kind": "线索",
"token": "x", "health": "ok", "last_ok_at": D(0)},
{"id": "stock_s1", "name": "奥丁在库", "kind": "库存",
"token": "y", "health": "bad", "health_at": D(0),
"health_msg": "401 token 已失效,需重新扫码"},
]}]},
{"brand": "iCAR", "stores": [
{"store_id": "s2", "interfaces": [
{"id": "leads_s2", "name": "SCRM 唯一线索", "kind": "线索",
"token": "x", "health": "ok", "last_ok_at": D(0)},
]}]},
{"brand": "纵横", "stores": [
{"store_id": "s3", "interfaces": [
{"id": "leads_s3", "name": "SCRM 唯一线索", "kind": "线索",
"token": "", "status": "未接入"},
]}]},
]
}
raw = json.dumps(cfg, ensure_ascii=False)
c.execute('INSERT OR REPLACE INTO 看板配置表("编号","配置") VALUES(?,?)',
("api_connectors", raw))
c.commit()
c.close()
print("[seed] 完成:4 门店 / 2 用户 / 6 任务 / 2 申请 / 3 品牌接口")
if __name__ == "__main__":
if os.environ.get("STORAGE_BACKEND", "").lower() != "sqlite":
sys.exit("必须 STORAGE_BACKEND=sqlite 才跑(避免碰云库)")
seed()
import app as A
from flask import g as FG
cli = A.app.test_client()
with A.app.test_request_context():
FG.user = {"account": "admin", "name": "阿杰", "role": "董事长",
"store_scope": "__all__"}
A.home_hub.invalidate()
d = A.home_hub.build(force=True)
print("\n===== 待办 =====")
print("total", d["todo"]["total"], "buckets", d["todo"]["buckets"])
for it in d["todo"]["items"]:
print(" [%s][%s] %s | %s" % (it["priority"], it["kind_label"],
it["title"], it["due_left"]))
print("\n===== 问题 =====")
print("total", d["issues"]["total"], d["issues"]["by_cat"])
for it in d["issues"]["items"]:
print(" [%s] %s → %s" % (it["level"], it["title"], it["detail"][:50]))
print("\n===== 健康 =====")
print(json.dumps(d["health"], ensure_ascii=False, indent=1))
print("\n===== 权限 =====")
print(json.dumps(d["perm"], ensure_ascii=False))
print("\n===== 异常 =====")
print("ready", d["anomaly"].get("ready"), "note", d["anomaly"].get("note", ""))
print("\n===== AI 上下文 =====")
with A.app.test_request_context():
FG.user = {"account": "admin", "name": "阿杰", "role": "董事长",
"store_scope": "__all__"}
A.home_hub.invalidate()
print(A.home_hub.context_text())
print("\n===== 受限账号:店长 =====")
with A.app.test_request_context():
FG.user = {"account": "mgr1", "name": "李四", "role": "普通员工",
"store_scope": "长沙麓谷店"}
A.home_hub.invalidate()
d2 = A.home_hub.build(force=True)
print("perm", json.dumps(d2["perm"], ensure_ascii=False))
print("待办 total", d2["todo"]["total"])
for it in d2["todo"]["items"]:
print(" [%s] %s" % (it["priority"], it["title"]))
print("问题 total", d2["issues"]["total"])
for it in d2["issues"]["items"]:
print(" [%s] %s" % (it["level"], it["title"]))
print("健康模块可见?", d2["perm"]["modules"]["health"])