文件
hyb-oa/client/tests/e2e/helpers/workflow.ts
T

50 行
1.5 KiB
TypeScript

import type { APIRequestContext } from "@playwright/test";
import { apiGetData, apiPostData } from "./auth";
type WorkflowTaskSummary = {
id: string;
workflowInstance: {
entityType: string;
entityId: string;
} | null;
};
type WorkflowTaskListResponse = {
items: WorkflowTaskSummary[];
};
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
export async function waitForWorkflowTask(api: APIRequestContext, input: {
entityId: string;
entityType: string;
attempts?: number;
intervalMs?: number;
}) {
const attempts = input.attempts ?? 20;
const intervalMs = input.intervalMs ?? 1000;
for (let index = 0; index < attempts; index += 1) {
const response = await apiGetData<WorkflowTaskListResponse>(api, "/api/workflow/tasks?status=pending&page=1&pageSize=50");
const matched = response.items.find(
(item) => item.workflowInstance?.entityType === input.entityType && item.workflowInstance.entityId === input.entityId,
);
if (matched) {
return matched;
}
await sleep(intervalMs);
}
throw new Error(`未在待办列表中找到 ${input.entityType}:${input.entityId}`);
}
export async function approveWorkflowTaskViaApi(api: APIRequestContext, taskId: string) {
return apiPostData<Record<string, unknown>>(api, `/api/workflow/tasks/${taskId}/approve`, {});
}
export async function rejectWorkflowTaskViaApi(api: APIRequestContext, taskId: string, comment: string) {
return apiPostData<Record<string, unknown>>(api, `/api/workflow/tasks/${taskId}/reject`, { comment });
}