Files
37duAutomation/plugins/XQ0001-0100/XQ0013 社保费用申报导出并汇总/_selftest.py
T
puddy a2774aa3a1 feat(xq0013): 社保申报全流程迁移完成(v0.3.0)
日常申报/特殊申报导出、申报记录 PDF 导出,合并职工未申报明细并汇总上下半月金额回写账户表副本;改为 Win32 消息驱动 + 图像/OCR 登录,移除旧 selectors/client 模块。
2026-11-26 09:28:32 +08:00

158 lines
6.5 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. 配置默认值与路径推导(client_dir/run_dir/各输出子目录)
2. 图像模板清单(images/manifest.json)与文件存在性
3. 账户列表按表头名读写(执行状态≠1 待跑;表头缺失回退 A/B/C;回写落盘)
4. 插件入口注册(需要 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], header=None) -> None:
from openpyxl import Workbook # noqa: PLC0415
wb = Workbook()
ws = wb.active
ws.append(header or ["门店", "税号", "密码", "执行状态",
"日常申报状态", "特殊申报状态", "申报记录"])
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(config.client_dir() == r"C:\tmp", "client_dir = 客户端 exe 所在目录")
ok(config.run_dir().endswith(os.path.join("out", config.RUN_DIR_NAME)),
"run_dir = 输出目录/_run")
ok(config.declare_dir().endswith(config.DIR_DECLARE), "申报结果目录")
ok(config.detail_dir().endswith(config.DIR_DETAIL), "职工未申报明细目录")
ok(config.summary_dir().endswith(config.DIR_SUMMARY), "汇总结果目录")
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, v in man.items()
if not os.path.isfile(os.path.join(config.IMAGES_DIR, v.get("file") or ""))]
ok(not missing, f"manifest 声明的模板文件都存在(缺失 {len(missing)})")
if pkg is None:
print(f"\n失败 {len(FAILS)} 项,跳过 {len(SKIPS)} 项")
return 0 if not FAILS else 1
# ---------- 3. 账户列表 ----------
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", "", "", "", ""],
])
sheet = accounts.AccountSheet(f)
rows = sheet.accounts()
ok(len(rows) == 2 and {r["store"] for r in rows} == {"A店", "C店"},
"执行状态≠1 的户待跑(A店 0 / C店 空)")
ok(all(r["row"] in (2, 4) for r in rows), "Excel 行号正确(2/4)")
sheet.set(2, "日常申报状态", "已导出")
sheet.set(2, "执行状态", config.STATE_DONE)
sheet.save()
sheet2 = accounts.AccountSheet(f)
rows2 = sheet2.accounts()
ok(len(rows2) == 1 and rows2[0]["store"] == "C店", "回写后 A店 不再待跑")
ok(sheet2.ws.cell(row=2, column=5).value == "已导出", "回写落盘成功")
# 表头缺失 → 回退 A/B/C
f2 = os.path.join(tmp, "无表头.xlsx")
_write_accounts_xlsx(f2, [["D店", "91500D", "pwdD"]], header=["x", "y", "z"])
rows3 = accounts.AccountSheet(f2).accounts()
ok(len(rows3) == 1 and rows3[0]["store"] == "D店" and rows3[0]["tax_no"] == "91500D",
"表头缺失时回退 A门店/B税号/C密码")
# ---------- 4. 插件入口 ----------
print("== 插件入口 ==")
plugin = getattr(pkg, "SocialDeclarePlugin", None)
ok(plugin is not None, "插件类存在")
if plugin is not None:
ok(plugin.id == "xq0013", "插件 id = xq0013")
ok(plugin.version == "0.3.0", "插件 version = 0.3.0")
fields = plugin.fields or {}
ok(set(fields) >= {"mode", "submit", "client_exe", "save_dir", "account_file"},
"fields 声明完整(mode/submit/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())