Files
37duAutomation/tests/test_log_upload_client.js
puddy 843ff244a6 feat: 新增多插件 UI 支持及运行日志集中回传功能
本次提交包含多项功能更新:
1. 为多个插件(xq0002/0003/0004/0008/0010/0011/0020/0031_xq0032/0033/0035/0037/0045_xq0046/0047/0048)新增 UI 界面及配置支持
2. 新增运行日志集中回传功能,客户端自动上传运行日志到中心机集中存储
3. 新增插件分类字段支持,用于场景库分组管理
4. 优化 xq0034/0037/0048 等插件的功能及描述
5. 修复控制面响应兼容问题,支持新旧版本数据格式
6. 新增 xq0033 插件的状态存储模块,用于历史记录管理
7. 优化 CRM 登录窗口模式,支持全屏置顶
2026-10-26 15:42:50 +08:00

96 lines
5.0 KiB
JavaScript

// 客户端 log-upload 集成测试:对本地测试控制面(127.0.0.1:8890) 回传运行日志 + 壳日志增量。
// 用法:node tests/test_log_upload_client.js (需先启动测试服务端)
"use strict";
const fs = require("fs");
const path = require("path");
const TMP = path.join(__dirname, "_tmp_logupload");
const APP_DATA = path.join(TMP, "37duAutomation");
// 必须在 require 任何 electron 模块前设置(config.js 只在变量未定义时读 .env)
process.env.LOCALAPPDATA = TMP;
process.env.CONTROLPLANE_URL = process.env.TEST_CP_URL || "http://127.0.0.1:8890";
process.env.CONTROLPLANE_TOKEN = "test-token-123";
process.env.CONTROLPLANE_ACCOUNT = "tester";
process.env.SHELL_PLUGIN_ROOT = path.join(TMP, "plugins"); // 防 pluginRoot() 扫仓库根
const SHELL_DIR = path.join(__dirname, "..", "desktop", "electron-shell", "electron");
// ---- 造本地数据 ----
fs.rmSync(TMP, { recursive: true, force: true });
fs.mkdirSync(path.join(APP_DATA, "logs"), { recursive: true });
fs.mkdirSync(path.join(APP_DATA, "run_history"), { recursive: true });
fs.writeFileSync(path.join(APP_DATA, "device_id"), "dev-client-test-01", "utf-8");
fs.writeFileSync(path.join(APP_DATA, "logs", "shell.log"),
"2026-10-22 09:00:00 INFO app 启动\n2026-10-22 09:00:01 INFO plugin-sync 同步完成\n", "utf-8");
const RUN_A = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
const RUN_B = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
const now = Math.floor(Date.now() / 1000);
fs.writeFileSync(path.join(APP_DATA, "run_history.jsonl"),
JSON.stringify({ run_id: RUN_A, account: "tester", plugin_id: "XQ0011", name: "插件甲", version: "1.0.0",
trigger: "manual", status: "error", ok: false, error: "登录失败", summary: "",
started: now - 2000, finished: now - 1900, duration_sec: 100 }) + "\n" +
JSON.stringify({ run_id: RUN_B, account: "tester", plugin_id: "XQ0021", name: "插件乙", version: "2.0.0",
trigger: "schedule", schedule_id: "s-1", status: "done", ok: true, error: "", summary: "完成",
started: now - 1000, finished: now - 900, duration_sec: 100 }) + "\n", "utf-8");
fs.writeFileSync(path.join(APP_DATA, "run_history", `${RUN_A}.jsonl`),
JSON.stringify({ t: "09:00:01", level: "INFO", msg: "开始执行" }) + "\n" +
JSON.stringify({ t: "09:00:02", level: "ERROR", msg: "登录失败:密码不正确" }) + "\n", "utf-8");
fs.writeFileSync(path.join(APP_DATA, "run_history", `${RUN_B}.jsonl`),
JSON.stringify({ t: "10:00:01", level: "INFO", msg: "开始" }) + "\n" +
JSON.stringify({ t: "10:00:02", level: "INFO", msg: "处理完成" }) + "\n", "utf-8");
const logUpload = require(path.join(SHELL_DIR, "log-upload.js"));
function assert(cond, name) {
if (!cond) { console.error("FAIL:", name); process.exitCode = 1; }
else console.log("PASS:", name);
}
(async () => {
// 1) 整次运行日志回传(新 run_id)
const ok = await logUpload.uploadRun({
run_id: "cccccccccccccccccccccccccccccccc", account: "tester", plugin_id: "XQ0031",
name: "插件丙", version: "3.0.0", trigger: "manual", status: "done", ok: true,
error: "", summary: "OK", started: 1700000400, finished: 1700000500, duration_sec: 100,
logs: [{ t: "11:00:01", level: "INFO", msg: "start" }],
});
assert(ok === true, "uploadRun 成功回传");
// 2) 壳日志增量:第一次全量 + 追加新行后只传增量
await logUpload.uploadShellLog();
fs.appendFileSync(path.join(APP_DATA, "logs", "shell.log"),
"2026-10-22 09:10:00 ERROR runner 执行进程异常退出\n", "utf-8");
await logUpload.uploadShellLog();
// 3) 启动补报:run_history 中两条(RUN_A/RUN_B)→ 服务端幂等 upsert
await logUpload.backfillRecentRuns();
// 4) 离线暂存 + 补报:patch cp.request 模拟服务端不可达 → 应入 pending;恢复后 flush 应成功
const cp = require(path.join(SHELL_DIR, "cpclient.js"));
const realRequest = cp.request;
const offlineRunId = "dddddddddddddddddddddddddddddddd";
cp.request = async (method, urlPath, opts) => {
if (urlPath === "/api/runtime/logs" && JSON.stringify(opts && opts.json || "").includes(offlineRunId)) {
throw new Error("simulated offline");
}
return realRequest(method, urlPath, opts);
};
const offline = await logUpload.uploadRun({
run_id: offlineRunId, account: "tester", plugin_id: "XQ0041",
name: "插件丁", version: "4.0.0", trigger: "manual", status: "error", ok: false,
error: "offline 测试", summary: "", started: 1700000600, finished: 1700000700, duration_sec: 100,
logs: [{ t: "12:00:01", level: "ERROR", msg: "boom" }],
});
assert(offline === false, "不可达时 uploadRun 返回 false");
const pending = fs.readFileSync(path.join(APP_DATA, "pending_log_uploads.jsonl"), "utf-8").trim();
assert(pending.includes(offlineRunId), "离线日志已暂存 pending");
cp.request = realRequest; // 恢复在线
const flushed = await logUpload.flushPending();
assert(flushed === 1, "恢复后 flushPending 补报 1 条");
console.log("客户端 log-upload 测试完成");
})().catch((e) => { console.error("TEST ERROR:", e); process.exitCode = 1; });