Files
hose-use/src/domains/platform/workflow/execution/workflow-debug.test.ts
T
gitea 71aa9b54a7 refactor(workflow): 收敛 Job 状态语义与监听事件过滤
同一份逻辑在两个地方各写一遍,必然在某次修改后漂移:

- Job 状态判定:watch/helpers.ts 与 execution/debug.ts 各有一份终态集合,
  且对数字 state 的解释相反——watch 把 state:0 视为 COMPLETED,
  debug 把它当成未知继续轮询到超时,同一个 Job 在两条路径上结论相反。
  统一到 execution/job-status.ts(watch 已依赖 execution,方向单向),
  helpers 保留转发导出,debug 删除本地实现;新增两条用例固定该语义。
- --failed-only 事件过滤:pipeline-watcher.ts 与 single-job.ts 逐字重复,
  抽为 watch/helpers.ts 的 shouldEmitWatchEvent(),两处调用点同步瘦身。

同步更新 execution 模块 README,说明状态语义的两个入口必须一起考虑。
2026-11-01 22:02:06 +08:00

108 lines
4.8 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import { runWorkflowDebugTest } from "../services.js";
function setup(status = "COMPLETED", errors: string[] = []) {
const posts: string[] = [];
const response = (data: unknown) => new Response(JSON.stringify({ code: 200, data }));
const client = {
post: async (path: string) => { posts.push(path); return response("job"); },
get: async (path: string) => {
if (path.endsWith("/level/failed")) {
if (errors.includes("failures")) throw new Error("403 failures unavailable");
return response([]);
}
if (path.endsWith("/execution")) {
if (errors.includes("tree")) throw new Error("403 execution unavailable");
return response([]);
}
if (errors.includes("job")) throw new Error("403 job unavailable");
return response({ id: "job", status });
},
} as any;
return { client, posts };
}
describe("workflow debug completion evidence", () => {
it("reports completed only when all diagnostics were fetched", async () => {
const { client } = setup();
const result = await runWorkflowDebugTest(client, "pipe", { flowCodeOrId: "ID01-record" });
expect(result.outcome).toBe("completed");
expect(result.timedOut).toBe(false);
expect(result.diagnosticsComplete).toBe(true);
});
it("reports a failed terminal job", async () => {
const { client } = setup("FAILED");
const result = await runWorkflowDebugTest(client, "pipe", { flowCodeOrId: "ID01-record" });
expect(result.outcome).toBe("failed");
});
it("keeps timed-out jobs queryable and exposes missing diagnostic evidence", async () => {
const { client } = setup("RUNNING", ["tree", "failures"]);
const result = await runWorkflowDebugTest(client, "pipe", { flowCodeOrId: "ID01-record", timeoutMs: 0 });
expect(result.jobId).toBe("job");
expect(result.status).toBe("RUNNING");
expect(result.outcome).toBe("timed_out");
expect(result.timedOut).toBe(true);
expect(result.diagnosticsComplete).toBe(false);
expect(result.diagnosticsErrors.map(error => error.source)).toEqual(["executionTree", "failedExecutions"]);
});
it("marks partial diagnostic reads as incomplete rather than clean success", async () => {
const { client } = setup("COMPLETED", ["tree"]);
const result = await runWorkflowDebugTest(client, "pipe", { flowCodeOrId: "ID01-record" });
expect(result.outcome).toBe("incomplete");
expect(result.diagnosticsErrors[0]?.message).toContain("403");
});
it("preserves job identity when polling fails and does not hide the error", async () => {
const { client } = setup("RUNNING", ["job"]);
const result = await runWorkflowDebugTest(client, "pipe", { flowCodeOrId: "ID01-record" });
expect(result.jobId).toBe("job");
expect(result.outcome).toBe("incomplete");
expect(result.diagnosticsErrors[0]?.source).toBe("job");
});
it("rejects invalid timing options before creating a mutating job", async () => {
const { client, posts } = setup();
await expect(runWorkflowDebugTest(client, "pipe", { flowCodeOrId: "ID01-record", timeoutMs: -1 })).rejects.toThrow("timeoutMs");
expect(posts).toEqual([]);
});
describe("numeric job state must agree with the watch pipeline", () => {
const withState = (state: number) => {
const { client } = setup();
const get = client.get;
client.get = async (path: string) => {
if (path.endsWith("/job")) {
return new Response(JSON.stringify({ code: 200, data: { id: "job", state } }), {
headers: { "content-type": "application/json" },
});
}
return get(path);
};
return client;
};
it("reads state 0 as completed instead of polling until timeout", async () => {
const result = await runWorkflowDebugTest(withState(0), "pipe", { flowCodeOrId: "ID01-record", timeoutMs: 100, pollIntervalMs: 1 });
expect(result.status).toBe("COMPLETED");
expect(result.outcome).toBe("completed");
expect(result.timedOut).toBe(false);
});
it("reads state 1 as still running", async () => {
const result = await runWorkflowDebugTest(withState(1), "pipe", { flowCodeOrId: "ID01-record", timeoutMs: 0 });
expect(result.status).toBe("RUNNING");
expect(result.outcome).toBe("timed_out");
});
});
it("retries a newly created job's HTTP 404 instead of reporting an authorization failure", async () => {
const { client } = setup();
const get = client.get;
let jobReads = 0;
client.get = async (path: string) => {
if (path.endsWith("/job") && jobReads++ === 0) throw { response: { status: 404 } };
return get(path);
};
const result = await runWorkflowDebugTest(client, "pipe", { flowCodeOrId: "ID01-record", timeoutMs: 100, pollIntervalMs: 1 });
expect(result.outcome).toBe("completed");
expect(jobReads).toBe(2);
});
});