feat(workflow): 企业可用节点探针、灰度开通收敛与DataLink公式硬规则校验

This commit is contained in:
gitea committed 2026-10-20 13:08:36 +08:00
1 parent 4f0eea5725
commit 0768b94d65
14 files changed
+974 -22

No files matched your search

+231
View File
@@ -0,0 +1,231 @@
import { describe, expect, it, spyOn, afterEach } from "bun:test";
import { handleWorkflowCommand } from "./workflow/index.js";
import * as services from "../../domains/platform/services.js";
import * as api from "../../domains/platform/api.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: "ID01rJEL708iTI",
name: "浙江麦瑞克科技有限公司",
baseUrl: "https://app.ekuaibao.com",
source: "openapi",
credentials: {
accessToken: "mock-access-token",
corpId: "ID01rJEL708iTI",
staffId: "ID01rJEL708iTI:RkQ8G8L_eMhI00",
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 nodes 字典探针与灰度开通测试", () => {
let spies: ReturnType<typeof spyOn>[] = [];
afterEach(() => {
for (const spy of spies) {
spy.mockRestore();
}
spies = [];
delete process.env.HOSE_ADMIN_PASSWORD;
});
it("workflow nodes: 成功探查当前企业已开通节点字典", async () => {
spies.push(
spyOn(helpers, "createPiperClientForCorp").mockResolvedValue({} as any),
spyOn(services, "getWorkflowAvailableNodes").mockResolvedValue({
allTypes: new Set(["http", "code", "condition", "clock"]),
actionNodes: [
{ name: "http节点", type: "http" },
{ name: "代码节点", type: "code" },
],
controlNodes: [{ name: "条件节点", type: "condition" }],
triggerNodes: [{ name: "定时触发", type: "clock" }],
})
);
const res = await handleWorkflowCommand(
{
area: "workflow",
action: "nodes",
args: [],
options: {},
raw: ["workflow", "nodes"],
},
mockRuntime
);
expect(res.exitCode).toBe(0);
expect(res.stdout).toContain("当前企业已开通工作流节点字典 [浙江麦瑞克科技有限公司]");
expect(res.stdout).toContain("代码节点 (code)");
expect(res.stdout).toContain("已开通节点数");
});
it("workflow nodes --all: 成功输出与 108 个灰度节点对照表", async () => {
spies.push(
spyOn(helpers, "createPiperClientForCorp").mockResolvedValue({} as any),
spyOn(services, "getWorkflowAvailableNodes").mockResolvedValue({
allTypes: new Set(["code", "http"]),
actionNodes: [{ name: "代码节点", type: "code" }],
controlNodes: [],
triggerNodes: [],
})
);
const res = await handleWorkflowCommand(
{
area: "workflow",
action: "nodes",
args: [],
options: { all: true },
raw: ["workflow", "nodes", "--all"],
},
mockRuntime
);
expect(res.exitCode).toBe(0);
expect(res.stdout).toContain("工作流节点字典与灰度开通对照 [浙江麦瑞克科技有限公司]");
expect(res.stdout).toContain("code-js2");
expect(res.stdout).toContain("🔒 未开通");
expect(res.stdout).toContain("✅ 已开通");
});
it("workflow nodes --json: 输出机器可读的结构化 JSON", async () => {
spies.push(
spyOn(helpers, "createPiperClientForCorp").mockResolvedValue({} as any),
spyOn(services, "getWorkflowAvailableNodes").mockResolvedValue({
allTypes: new Set(["code"]),
actionNodes: [{ name: "代码节点", type: "code" }],
controlNodes: [],
triggerNodes: [],
})
);
const jsonRuntime: CliRuntime = { ...mockRuntime, jsonMode: true };
const res = await handleWorkflowCommand(
{
area: "workflow",
action: "nodes",
args: [],
options: {},
raw: ["workflow", "nodes", "--json"],
},
jsonRuntime
);
expect(res.exitCode).toBe(0);
const parsedJson = JSON.parse(res.stdout);
expect(parsedJson.corpId).toBe("ID01rJEL708iTI");
expect(parsedJson.totalGranted).toBe(1);
expect(parsedJson.grantedTypes).toContain("code");
});
it("workflow nodes grant: 免密模式下成功发起灰度节点开通", async () => {
const grantSpy = spyOn(api, "grantWorkflowNodesApi").mockResolvedValue({
success: true,
saved: ["code-js2"],
skipped: [],
rawMessage: "新开通的节点:code-js2",
});
spies.push(grantSpy);
const res = await handleWorkflowCommand(
{
area: "workflow",
action: "nodes",
args: ["grant", "code-js2"],
options: {},
raw: ["workflow", "nodes", "grant", "code-js2"],
},
mockRuntime
);
expect(res.exitCode).toBe(0);
expect(res.stdout).toContain("灰度节点开通结果 [浙江麦瑞克科技有限公司]");
expect(res.stdout).toContain("新开通成功");
expect(res.stdout).toContain("code-js2");
expect(grantSpy).toHaveBeenCalledWith(
expect.objectContaining({
nodes: ["code-js2"],
token: "mock-access-token",
corpId: "ID01rJEL708iTI",
})
);
});
it("workflow nodes grant: 节点名称自动中文归一化 (如 'JS节点(新)' 映射为 'code-js2')", async () => {
const grantSpy = spyOn(api, "grantWorkflowNodesApi").mockResolvedValue({
success: true,
saved: ["code-js2"],
skipped: [],
rawMessage: "新开通的节点:code-js2",
});
spies.push(grantSpy);
const res = await handleWorkflowCommand(
{
area: "workflow",
action: "nodes",
args: ["grant", "JS节点(新)"],
options: {},
raw: ["workflow", "nodes", "grant", "JS节点(新)"],
},
mockRuntime
);
expect(res.exitCode).toBe(0);
expect(grantSpy).toHaveBeenCalledWith(
expect.objectContaining({
nodes: ["code-js2"],
})
);
});
it("workflow nodes grant: 管理员密码保护预留生效 (未提供密码或密码错误被拒绝)", async () => {
process.env.HOSE_ADMIN_PASSWORD = "secret_admin_pass";
// 1. 无密码调用被拒绝
expect(
services.grantWorkflowNodes(mockCorp, {
nodes: ["code-js2"],
})
).rejects.toThrow("管理员保护");
// 2. 错误密码调用被拒绝
expect(
services.grantWorkflowNodes(mockCorp, {
nodes: ["code-js2"],
password: "wrong_password",
})
).rejects.toThrow("管理员保护");
// 3. 正确密码调用放行
const grantSpy = spyOn(api, "grantWorkflowNodesApi").mockResolvedValue({
success: true,
saved: ["code-js2"],
skipped: [],
rawMessage: "开通成功",
});
spies.push(grantSpy);
const result = await services.grantWorkflowNodes(mockCorp, {
nodes: ["code-js2"],
password: "secret_admin_pass",
});
expect(result.success).toBe(true);
expect(grantSpy).toHaveBeenCalled();
});
});