feat(workflow,corps): add dynamic staff link switching and deep failed job inspection
This commit is contained in:
1 parent
0499ccb2cc
commit
0c6a3ee660
8 files changed
+737
-32
No files matched your search
@@ -0,0 +1,223 @@
|
||||
import { describe, expect, it, spyOn, afterEach } from "bun:test";
|
||||
import { handleWorkflowCommand } from "./workflow/index.js";
|
||||
import * as helpers from "../helpers.js";
|
||||
import type { CliRuntime } from "../types.js";
|
||||
import type { CorpConfig } from "../../core/config/types.js";
|
||||
|
||||
const mockCorp: CorpConfig = {
|
||||
id: "test-corp",
|
||||
name: "测试企业",
|
||||
baseUrl: "https://app.ekuaibao.com",
|
||||
source: "openapi",
|
||||
credentials: {
|
||||
accessToken: "mock-token",
|
||||
corpId: "test-corp",
|
||||
staffId: "test-corp:STAFF_TEST",
|
||||
appKey: "test-key",
|
||||
appSecurity: "test-security",
|
||||
},
|
||||
};
|
||||
|
||||
const mockRuntime: CliRuntime = {
|
||||
corpContext: {
|
||||
listCorps: () => [mockCorp],
|
||||
getCurrent: () => mockCorp,
|
||||
switchCorp: () => true,
|
||||
resolve: () => mockCorp,
|
||||
} as any,
|
||||
selectedCorp: mockCorp,
|
||||
jsonMode: false,
|
||||
outputOptions: {},
|
||||
};
|
||||
|
||||
describe("CLI - workflow job 执行详情与异常精准透视", () => {
|
||||
let spies: ReturnType<typeof spyOn>[] = [];
|
||||
|
||||
afterEach(() => {
|
||||
for (const spy of spies) {
|
||||
spy.mockRestore();
|
||||
}
|
||||
spies = [];
|
||||
});
|
||||
|
||||
it("fails when no corp is selected", async () => {
|
||||
const res = await handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "job",
|
||||
args: ["job_123"],
|
||||
options: {},
|
||||
raw: ["workflow", "job", "job_123"],
|
||||
},
|
||||
{ ...mockRuntime, selectedCorp: null }
|
||||
);
|
||||
expect(res.exitCode).toBe(1);
|
||||
expect(res.stderr).toContain("未选中企业");
|
||||
});
|
||||
|
||||
it("fails when no jobId is provided", async () => {
|
||||
const res = await handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "job",
|
||||
args: [],
|
||||
options: {},
|
||||
raw: ["workflow", "job"],
|
||||
},
|
||||
mockRuntime
|
||||
);
|
||||
expect(res.exitCode).toBe(1);
|
||||
expect(res.stderr).toContain("用法: hose-use workflow job <jobId>");
|
||||
});
|
||||
|
||||
it("handles workflow job --failed with deep error inspection and inputs/outputs", async () => {
|
||||
const mockPiperClient = {
|
||||
get: (url: string) => {
|
||||
let payload: any = {};
|
||||
if (url.includes("/execution/level/failed")) {
|
||||
payload = { number: 1, size: 100, totalPages: 0, totalItems: 0, items: [] };
|
||||
} else if (url.includes("/execution")) {
|
||||
payload = [
|
||||
{
|
||||
id: "exec_1",
|
||||
name: "auto_approve",
|
||||
displayName: "资金调拨自动审批",
|
||||
type: "flow-approval",
|
||||
status: "FAILED",
|
||||
executionTime: 350,
|
||||
error: { message: "400: 无待办审批事项" },
|
||||
inputs: { formId: "ZJ-20261014-001368", action: "agree" },
|
||||
outputs: { code: 400, message: "无待办审批事项" },
|
||||
},
|
||||
];
|
||||
} else if (url.includes("/jobs/job_err")) {
|
||||
payload = {
|
||||
id: "job_err",
|
||||
pipelineId: "pipe_1",
|
||||
state: "FAILED",
|
||||
startTime: "2026-10-14 10:00:00",
|
||||
durationTime: 1200,
|
||||
};
|
||||
}
|
||||
return new Response(JSON.stringify({ code: 200, data: payload }), {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
spies.push(
|
||||
spyOn(helpers, "createClientsForCorp").mockResolvedValue({
|
||||
ekbClient: {
|
||||
get: () => ({ json: async () => [{ id: "plat_1", active: true }] }),
|
||||
},
|
||||
easoClient: mockPiperClient,
|
||||
} as any)
|
||||
);
|
||||
spies.push(spyOn(helpers, "createPiperClientForCorp").mockResolvedValue(mockPiperClient as any));
|
||||
|
||||
// 文本输出测试
|
||||
const res = await handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "job",
|
||||
args: ["job_err"],
|
||||
options: { failed: true },
|
||||
raw: ["workflow", "job", "job_err", "--failed"],
|
||||
},
|
||||
mockRuntime
|
||||
);
|
||||
|
||||
expect(res.exitCode).toBe(0);
|
||||
expect(res.stdout).toContain("Job 异常/失败执行节点透视");
|
||||
expect(res.stdout).toContain("资金调拨自动审批");
|
||||
expect(res.stdout).toContain("400: 无待办审批事项");
|
||||
expect(res.stdout).toContain("ZJ-20261014-001368");
|
||||
|
||||
// --json 模式测试
|
||||
const jsonRes = await handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "job",
|
||||
args: ["job_err"],
|
||||
options: { failed: true, json: true },
|
||||
raw: ["workflow", "job", "job_err", "--failed", "--json"],
|
||||
},
|
||||
{ ...mockRuntime, jsonMode: true }
|
||||
);
|
||||
|
||||
expect(jsonRes.exitCode).toBe(0);
|
||||
const parsed = JSON.parse(jsonRes.stdout);
|
||||
expect(parsed.totalFailed).toBe(1);
|
||||
expect(parsed.failedNodes[0].id).toBe("exec_1");
|
||||
expect(parsed.failedNodes[0].error.message).toBe("400: 无待办审批事项");
|
||||
expect(parsed.failedNodes[0].inputs.formId).toBe("ZJ-20261014-001368");
|
||||
});
|
||||
|
||||
it("handles workflow job --node=<name|id> targeting specific execution node", async () => {
|
||||
const mockPiperClient = {
|
||||
get: (url: string) => {
|
||||
let payload: any = {};
|
||||
if (url.includes("/execution")) {
|
||||
payload = [
|
||||
{
|
||||
id: "node_code",
|
||||
name: "codeRunner_1",
|
||||
displayName: "MVEL计算节点",
|
||||
type: "code",
|
||||
status: "COMPLETED",
|
||||
executionTime: 85,
|
||||
inputs: { expression: "pvTotal * dailyRate" },
|
||||
outputs: { result: 78787.55 },
|
||||
},
|
||||
];
|
||||
}
|
||||
return new Response(JSON.stringify({ code: 200, data: payload }), {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
spies.push(
|
||||
spyOn(helpers, "createClientsForCorp").mockResolvedValue({
|
||||
ekbClient: {
|
||||
get: () => ({ json: async () => [{ id: "plat_1", active: true }] }),
|
||||
},
|
||||
easoClient: mockPiperClient,
|
||||
} as any)
|
||||
);
|
||||
spies.push(spyOn(helpers, "createPiperClientForCorp").mockResolvedValue(mockPiperClient as any));
|
||||
|
||||
const res = await handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "job",
|
||||
args: ["job_single"],
|
||||
options: { node: "MVEL计算" },
|
||||
raw: ["workflow", "job", "job_single", "--node=MVEL计算"],
|
||||
},
|
||||
mockRuntime
|
||||
);
|
||||
|
||||
expect(res.exitCode).toBe(0);
|
||||
expect(res.stdout).toContain("工作流节点执行详情 [node_code]");
|
||||
expect(res.stdout).toContain("MVEL计算节点");
|
||||
expect(res.stdout).toContain("pvTotal * dailyRate");
|
||||
expect(res.stdout).toContain("78787.55");
|
||||
|
||||
// 节点不存在时的错误提示拦截
|
||||
const notFoundRes = await handleWorkflowCommand(
|
||||
{
|
||||
area: "workflow",
|
||||
action: "job",
|
||||
args: ["job_single"],
|
||||
options: { node: "不存在的节点" },
|
||||
raw: ["workflow", "job", "job_single", "--node=不存在的节点"],
|
||||
},
|
||||
mockRuntime
|
||||
);
|
||||
expect(notFoundRes.exitCode).toBe(1);
|
||||
expect(notFoundRes.stderr).toContain("未在 Job [job_single] 的执行树中找到匹配 \"不存在的节点\" 的节点");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user