132 lines
3.7 KiB
TypeScript
132 lines
3.7 KiB
TypeScript
import { describe, expect, it, spyOn, afterEach } from "bun:test";
|
|
import { handleWebhookCommand } from "./webhook.js";
|
|
import * as shared from "./shared.js";
|
|
import * as services from "../../../domains/platform/services.js";
|
|
import type { CliParsedArgs, CliRuntime } from "../../types.js";
|
|
|
|
function createMockRuntime(overrides: Partial<CliRuntime> = {}): CliRuntime {
|
|
const corp: any = {
|
|
id: "corp-1",
|
|
name: "测试企业",
|
|
corpId: "corp-1",
|
|
env: "test",
|
|
baseUrl: "https://app.ekuaibao.com",
|
|
source: "manual",
|
|
credentials: {
|
|
corpId: "corp-1",
|
|
accessToken: "token-1",
|
|
staffId: "user-1",
|
|
},
|
|
};
|
|
return {
|
|
corpContext: {
|
|
listCorps: () => [corp],
|
|
getCurrent: () => corp,
|
|
switchCorp: () => true,
|
|
resolve: () => corp,
|
|
} as any,
|
|
selectedCorp: corp,
|
|
jsonMode: false,
|
|
outputOptions: {},
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
describe("workflow webhook command", () => {
|
|
let spies: ReturnType<typeof spyOn>[] = [];
|
|
|
|
afterEach(() => {
|
|
for (const spy of spies) {
|
|
spy.mockRestore();
|
|
}
|
|
spies = [];
|
|
});
|
|
|
|
it("prints usage when pipelineId is not provided", async () => {
|
|
const parsed: CliParsedArgs = {
|
|
area: "workflow",
|
|
action: "webhook",
|
|
args: [],
|
|
options: {},
|
|
raw: ["webhook"],
|
|
};
|
|
const runtime = createMockRuntime();
|
|
const result = await handleWebhookCommand(parsed, runtime);
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.stderr).toContain("用法: hose-use workflow webhook");
|
|
});
|
|
|
|
it("previews webhook payload in dry-run mode", async () => {
|
|
const parsed: CliParsedArgs = {
|
|
area: "workflow",
|
|
action: "webhook",
|
|
args: ["send", "881196955148578838"],
|
|
options: { "dry-run": true, body: '{"foo":"bar"}' },
|
|
raw: ["send", "881196955148578838", "--dry-run"],
|
|
};
|
|
const runtime = createMockRuntime();
|
|
|
|
const mockDetail = {
|
|
id: "881196955148578838",
|
|
label: "测试 Webhook 工作流",
|
|
tasks: [
|
|
{
|
|
name: "trigger_webhook",
|
|
type: "webhook",
|
|
label: "Webhook 触发器",
|
|
urlPath: "test-token-12345",
|
|
},
|
|
],
|
|
};
|
|
|
|
spies.push(
|
|
spyOn(shared, "createWorkflowDetailClient").mockResolvedValue({
|
|
client: {} as any,
|
|
clients: { ekbClient: {} as any } as any,
|
|
}),
|
|
spyOn(services, "getWorkflowDetail").mockResolvedValue(mockDetail as any)
|
|
);
|
|
|
|
const result = await handleWebhookCommand(parsed, runtime);
|
|
expect(result.exitCode).toBe(0);
|
|
expect(result.stdout).toContain("[DRY-RUN]");
|
|
expect(result.stdout).toContain("881196955148578838");
|
|
expect(result.stdout).toContain("https://piper.ekuaibao.com/api/webhook/test-token-12345");
|
|
});
|
|
|
|
it("rejects non-webhook workflows with clear error", async () => {
|
|
const parsed: CliParsedArgs = {
|
|
area: "workflow",
|
|
action: "webhook",
|
|
args: ["881196955148578838"],
|
|
options: { yes: true },
|
|
raw: ["881196955148578838", "--yes"],
|
|
};
|
|
const runtime = createMockRuntime();
|
|
|
|
const mockDetail = {
|
|
id: "881196955148578838",
|
|
label: "定时触发流程",
|
|
tasks: [
|
|
{
|
|
name: "trigger_clock",
|
|
type: "clock",
|
|
label: "定时触发器",
|
|
},
|
|
],
|
|
};
|
|
|
|
spies.push(
|
|
spyOn(shared, "createWorkflowDetailClient").mockResolvedValue({
|
|
client: {} as any,
|
|
clients: { ekbClient: {} as any } as any,
|
|
}),
|
|
spyOn(services, "getWorkflowDetail").mockResolvedValue(mockDetail as any)
|
|
);
|
|
|
|
const result = await handleWebhookCommand(parsed, runtime);
|
|
expect(result.exitCode).toBe(1);
|
|
expect(result.stderr).toContain("首节点不是 Webhook 触发器");
|
|
});
|
|
});
|