Files
37duAutomation/plugins/XQ0001-0100/XQ0013 社保费用申报导出并汇总/_selftest.py
T
puddy 4941e8b6c0 feat(plugin): 新增 XQ0006 周考勤异常处理、XQ0083 调动流程生效确认;其余插件累积改动
XQ0001 排班异常识别 / XQ0002 雇主险增减操作 / XQ0012 ERP 付款申请 / XQ0013 社保费用申报 / XQ0042 抖音平台核对 / XQ0084 银行卡更新:逻辑与界面 bundle 更新(含各自版本号变更)
2026-11-18 18:55:59 +08:00

163 lines
6.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""XQ0013 插件离线自测(不启动客户端、不点鼠标、不碰真实账户)。
覆盖:
1. 配置默认值与路径推导(输出目录/账户列表/子目录)
2. 图像模板清单(images/manifest.json)与文件存在性
3. Win32 元素库(selectors/selectors.xml)可解析、元素数正确
4. 账户列表读取规则(E 列 0 待执行 / 1 跳过 / 空跳过;缺列与空值报错)
5. 插件入口注册(需要 webhost 运行时;缺失时 skip 不误报)
由 SDK 兼容预检调用(会挂上 webhost 与各 SDK 路径);也可手工:
set PYTHONPATH=desktop\\electron-shell\\python;libs\\desktopauto
.venv\\Scripts\\python.exe "plugins\\XQ0001-0100\\XQ0013 社保费用申报导出并汇总\\_selftest.py"
"""
from __future__ import annotations
import importlib
import importlib.util
import os
import sys
import tempfile
from pathlib import Path
HERE = Path(__file__).resolve().parent
FAILS: list[str] = []
SKIPS: list[str] = []
def ok(cond: bool, msg: str) -> None:
print((" [OK] " if cond else " [x] ") + msg)
if not cond:
FAILS.append(msg)
def skip(msg: str) -> None:
print(" [SKIP] " + msg)
SKIPS.append(msg)
def _write_accounts_xlsx(path: str, rows: list[list]) -> None:
from openpyxl import Workbook # noqa: PLC0415
wb = Workbook()
ws = wb.active
ws.append(["门店", "税号", "密码", "备注", "执行标记"])
for r in rows:
ws.append(r)
wb.save(path)
def main() -> int:
try:
sys.stdout.reconfigure(encoding="utf-8")
except Exception: # noqa: BLE001
pass
sys.path.insert(0, str(HERE))
# 插件是包(内部有 ..config 相对导入),按壳子/预检同款方式用 spec 载入(目录名带空格不能直接 import)
package = "plugin_under_test"
pkg = None
try:
spec = importlib.util.spec_from_file_location(
package, str(HERE / "__init__.py"), submodule_search_locations=[str(HERE)])
pkg = importlib.util.module_from_spec(spec)
sys.modules[package] = pkg
spec.loader.exec_module(pkg)
except ModuleNotFoundError as e:
sys.modules.pop(package, None)
pkg = None
skip(f"未挂载插件运行时({e}),只跑纯配置检查")
def sub(name: str):
return importlib.import_module(f"{package}.{name}")
config = sub("config") if pkg else importlib.import_module("config")
# ---------- 1. 配置 ----------
print("== 配置默认值 ==")
ok(config.DEFAULT_CLIENT_EXE.endswith("EPEvenue_SB.exe"), "默认客户端路径")
ok(bool(config.DEFAULT_SAVE_DIR), "默认输出目录非空")
config.configure("", "", "")
ok(os.path.join(config.DEFAULT_SAVE_DIR, config.ACCOUNT_FILE_NAME) == config.account_file(),
"账户列表留空 → 输出目录/账户列表.xlsx")
config.configure(client_exe=r"C:\tmp\EPEvenue_SB.exe", save_dir=r"C:\tmp\out",
account_file=r"C:\tmp\accts.xlsx")
ok(config.account_file() == r"C:\tmp\accts.xlsx", "显式账户列表路径生效")
ok(set(config.dirs()) >= {"base", "screenshots", "declare", "detail", "summary"},
"输出子目录齐全")
config.configure("", "", "")
# ---------- 2. 图像模板 ----------
print("== 图像模板 ==")
man = config.manifest()
ok(len(man) >= 40, f"manifest 模板数 {len(man)}(≥40)")
missing = [k for k in config.TPL.values() if not os.path.isfile(config.template_path(k))]
ok(not missing, f"TPL 声明的模板文件都存在(缺失 {len(missing)})")
opt = config.image_opt("login_btn")
ok(bool(opt["path"]) and 0.5 <= float(opt["threshold"]) <= 1.0,
f"登录按钮匹配参数 threshold={opt['threshold']}")
if pkg is None:
print(f"\n失败 {len(FAILS)} 项,跳过 {len(SKIPS)} 项")
return 0 if not FAILS else 1
# ---------- 3. Win32 元素库 ----------
print("== Win32 元素库 ==")
selectors = sub("data.selectors")
xml = HERE / "selectors" / "selectors.xml"
ok(xml.is_file(), "selectors.xml 存在")
lib = selectors.Selectors(str(xml))
ok(len(lib.names()) >= 10, f"元素数量 {len(lib.names())}(≥10)")
# ---------- 4. 账户列表 ----------
print("== 账户列表读取 ==")
accounts = sub("data.accounts")
with tempfile.TemporaryDirectory() as tmp:
f = os.path.join(tmp, "账户列表.xlsx")
_write_accounts_xlsx(f, [
["A店", "91500A", "pwdA", "", "0"],
["B店", "91500B", "pwdB", "", "1"],
["C店", "91500C", "pwdC", "", ""],
])
rows = accounts.load_accounts(f)
ok(len(rows) == 1 and rows[0]["store"] == "A店" and rows[0]["row"] == 2,
"只返回 E=0 的账户(A店,Excel 第 2 行)")
f2 = os.path.join(tmp, "空.xlsx")
_write_accounts_xlsx(f2, [["D店", "91500D", "pwdD", "", "1"]])
try:
accounts.load_accounts(f2)
ok(False, "无待执行账户应报错")
except RuntimeError as e:
ok("没有待执行账户" in str(e), "无待执行账户 → 明确报错")
f3 = os.path.join(tmp, "缺列.xlsx")
from openpyxl import Workbook # noqa: PLC0415
wb = Workbook()
wb.active.append(["门店", "税号"])
wb.save(f3)
try:
accounts.load_accounts(f3)
ok(False, "列数不足应报错")
except RuntimeError as e:
ok("列数不完整" in str(e), "列数不足 → 明确报错")
# ---------- 5. 插件入口 ----------
print("== 插件入口 ==")
plugin = getattr(pkg, "SocialDeclarePlugin", None)
ok(plugin is not None, "插件类存在")
if plugin is not None:
ok(plugin.id == "xq0013", "插件 id = xq0013")
fields = plugin.fields or {}
ok(set(fields) >= {"mode", "client_exe", "save_dir", "account_file"},
"fields 声明完整(mode/client_exe/save_dir/account_file)")
ok(callable(getattr(plugin, "run", None)), "run 可调用")
print(f"\n失败 {len(FAILS)} 项" + (f",跳过 {len(SKIPS)} 项" if SKIPS else ""))
return 0 if not FAILS else 1
if __name__ == "__main__":
raise SystemExit(main())