三处 P0: - 生产企业零拦截:handleGrantNodes 用 isCorpProd 直接拒绝,无 --force 逃生口,且拦截发生在 --yes 确认之前;补 2 条单测。 - dry-run 预览失真:payload 改由编排层显式构造,固定附带开关 (recalculate-the-approval-flow / ui-notification / start) 提为 GRANT_ATTACHED_OPTIONS,dry-run 与成功输出都会展示。刻意不改 真实 payload 语义,避免破坏 Piper 历史接口契约。 - api.ts 越权:只留换票、POST、响应校验;业务码判定与 saved/skipped 映射移到 catalog.ts 的 mapGrantResult。新增 assertNoAuthFailure 复用 EASO 响应体系,401/403 统一转成 AuthenticationError(CLI 退出码 11)。 P1:修 gray-nodes-catalog 同名「数据映射」(mapping/dmn) 静默覆盖, normalizeGrayNodes 遇歧义抛 ValidationError;节点去重;删 node.ts 死常量;Piper 测试域名常量收敛到 core/http;修 filterKw 的 args[1] 越界;归一化逻辑收敛为一份;同步文档与测试名。 校验:bun test 1099 pass / 0 fail,bun run check、check:readme 通过。 本次不打 tag、不触发 Release;Skill 内嵌变更等 0.9.3 发版生效。
167 lines
6.2 KiB
TypeScript
167 lines
6.2 KiB
TypeScript
import { afterEach, describe, expect, it, spyOn } from "bun:test";
|
|
import ky from "ky";
|
|
import { grantWorkflowNodesApi, SCRIPT_ENGINE_TEST_BASE_URL } from "./catalog/api.js";
|
|
import { AuthenticationError } from "../../../../core/errors.js";
|
|
import { GRANT_ATTACHED_OPTIONS, grantWorkflowNodes, mapGrantResult } from "../catalog/catalog.js";
|
|
import { normalizeGrayNodes } from "../nodes/gray-nodes-catalog.js";
|
|
|
|
describe("grantWorkflowNodesApi transport", () => {
|
|
let spies: ReturnType<typeof spyOn>[] = [];
|
|
|
|
afterEach(() => {
|
|
for (const spy of spies) spy.mockRestore();
|
|
spies = [];
|
|
});
|
|
|
|
function mockJwt(): void {
|
|
spies.push(spyOn(ky, "get").mockImplementation((() => ({
|
|
json: async () => ({ id: "business-jwt" }),
|
|
})) as never));
|
|
}
|
|
|
|
it("exchanges EASO JWT and posts the caller-supplied payload unchanged", async () => {
|
|
mockJwt();
|
|
let requestedUrl = "";
|
|
let requestOptions: any;
|
|
spies.push(spyOn(ky, "post").mockImplementation((async (url: string, options: any) => {
|
|
requestedUrl = url;
|
|
requestOptions = options;
|
|
return { json: async () => ({ code: 200, msg: "新开通的节点:code" }) };
|
|
}) as never));
|
|
|
|
const payload = {
|
|
corporationId: "corp-1",
|
|
dslTypes: ["code"],
|
|
...GRANT_ATTACHED_OPTIONS,
|
|
};
|
|
const result = await grantWorkflowNodesApi({
|
|
token: "access-token",
|
|
corpId: "corp-1",
|
|
staffId: "corp-1:staff-1",
|
|
baseUrl: "https://app.ekuaibao.com///",
|
|
payload,
|
|
});
|
|
|
|
expect(result).toMatchObject({ code: 200, msg: "新开通的节点:code" });
|
|
expect(ky.get).toHaveBeenCalledWith(
|
|
"https://app.ekuaibao.com/api/engine/realm/$EASO/actions/jwt",
|
|
expect.objectContaining({
|
|
searchParams: { corpId: "corp-1" },
|
|
headers: expect.objectContaining({
|
|
accesstoken: "access-token",
|
|
corpid: "corp-1",
|
|
staffid: "corp-1:staff-1",
|
|
}),
|
|
}),
|
|
);
|
|
expect(requestedUrl).toBe("https://piper.ekuaibao.com/api/easo/dsl/corporation");
|
|
expect(requestOptions.headers).toEqual({ Authorization: "Bearer business-jwt" });
|
|
expect(requestOptions.json).toEqual(payload);
|
|
});
|
|
|
|
it("uses the test Piper endpoint only when isTest is explicitly enabled", async () => {
|
|
mockJwt();
|
|
let requestedUrl = "";
|
|
spies.push(spyOn(ky, "post").mockImplementation((async (url: string) => {
|
|
requestedUrl = url;
|
|
return { json: async () => ({ code: 200, msg: "新开通的节点:code" }) };
|
|
}) as never));
|
|
|
|
await grantWorkflowNodesApi({
|
|
token: "access-token",
|
|
corpId: "corp-1",
|
|
staffId: "corp-1:staff-1",
|
|
payload: { dslTypes: ["code"] },
|
|
isTest: true,
|
|
});
|
|
|
|
expect(requestedUrl).toBe(`${SCRIPT_ENGINE_TEST_BASE_URL}/api/easo/dsl/corporation`);
|
|
});
|
|
|
|
it("maps 401/403 business codes to AuthenticationError for CLI exit code 11", async () => {
|
|
mockJwt();
|
|
spies.push(spyOn(ky, "post").mockImplementation((async () => ({
|
|
json: async () => ({ code: 403, msg: "无该自建应用权限" }),
|
|
})) as never));
|
|
|
|
await expect(grantWorkflowNodesApi({
|
|
token: "access-token",
|
|
corpId: "corp-1",
|
|
staffId: "corp-1:staff-1",
|
|
payload: { dslTypes: ["code"] },
|
|
})).rejects.toBeInstanceOf(AuthenticationError);
|
|
});
|
|
|
|
it("returns the raw failing business code so the orchestrator can report success:false", async () => {
|
|
mockJwt();
|
|
spies.push(spyOn(ky, "post").mockImplementation((async () => ({
|
|
json: async () => ({ code: 500, msg: "JWT 签名无效" }),
|
|
})) as never));
|
|
|
|
const record = await grantWorkflowNodesApi({
|
|
token: "access-token",
|
|
corpId: "corp-1",
|
|
staffId: "corp-1:staff-1",
|
|
payload: { dslTypes: ["code"] },
|
|
});
|
|
|
|
expect(mapGrantResult(record, false)).toEqual({
|
|
success: false, saved: [], skipped: [], rawMessage: "JWT 签名无效", isTest: false,
|
|
});
|
|
});
|
|
|
|
it("maps EASO exchange transport failures to a domain API error", async () => {
|
|
spies.push(spyOn(ky, "get").mockImplementation((() => {
|
|
throw new Error("Request failed with status code 404");
|
|
}) as never));
|
|
|
|
await expect(grantWorkflowNodesApi({
|
|
token: "access-token",
|
|
corpId: "corp-1",
|
|
staffId: "corp-1:staff-1",
|
|
payload: { dslTypes: ["code"] },
|
|
})).rejects.toThrow("灰度节点开通 EASO 换票失败: Request failed with status code 404");
|
|
});
|
|
|
|
it("surfaces Piper transport failures without exposing credentials", async () => {
|
|
mockJwt();
|
|
spies.push(spyOn(ky, "post").mockImplementation((async () => {
|
|
throw new Error("connect ETIMEDOUT");
|
|
}) as never));
|
|
|
|
await expect(grantWorkflowNodesApi({
|
|
token: "secret-access-token",
|
|
corpId: "corp-1",
|
|
staffId: "corp-1:staff-1",
|
|
payload: { dslTypes: ["code"] },
|
|
})).rejects.toThrow("灰度节点开通 Piper 请求失败: connect ETIMEDOUT");
|
|
});
|
|
});
|
|
|
|
describe("grantWorkflowNodes orchestration", () => {
|
|
it("maps saved/skipped from structured data and from Chinese fallback messages", () => {
|
|
expect(mapGrantResult({ code: 200, data: { saved: ["code"], skipped: ["risk"] } }, true)).toEqual({
|
|
success: true, saved: ["code"], skipped: ["risk"], rawMessage: "", isTest: true,
|
|
});
|
|
expect(mapGrantResult({ code: 200, msg: "新开通的节点:code, risk" }, false).saved).toEqual(["code", "risk"]);
|
|
expect(mapGrantResult({ code: 200, msg: "以下节点已开通,不再重复开通:code" }, false).skipped).toEqual(["code"]);
|
|
expect(mapGrantResult({ code: 500, msg: "拒绝" }, false).success).toBe(false);
|
|
});
|
|
|
|
it("normalizes Chinese names and rejects ambiguous ones instead of silently picking one", () => {
|
|
expect(normalizeGrayNodes(["JS节点(新)"])).toEqual(["code-js2"]);
|
|
expect(normalizeGrayNodes(["mapping", "dmn"])).toEqual(["mapping", "dmn"]);
|
|
expect(() => normalizeGrayNodes(["数据映射"])).toThrow("多个灰度类型");
|
|
});
|
|
|
|
it("rejects an ambiguous Chinese node before issuing any request", async () => {
|
|
const corp = {
|
|
id: "corp-1",
|
|
name: "测试企业",
|
|
env: "test",
|
|
credentials: { accessToken: "token", corpId: "corp-1", staffId: "staff" },
|
|
} as any;
|
|
await expect(grantWorkflowNodes(corp, { nodes: ["数据映射"] })).rejects.toThrow("多个灰度类型");
|
|
});
|
|
});
|