Files
hose-use/src/domains/platform/workflow/models/choose-serialization.test.ts
T
gitea 7a19fa8337 feat(workflow): 保存期接口权限预检与 choose 表达式序列化开关
## 一、保存期「接口 → 应用」权限预检

工作流 HTTP 节点调用某些开放接口前,企业必须先开通对应应用,否则运行时
返回 403「未开通功能,无权访问」,而该报文字段里完全看不出缺哪个应用。
真实案例:某客户「单据审批」应用未开通,流程跑到第 9 步才被网关拦下,
排查成本极高。

本次在保存/静态分析阶段按接口路径反推所需应用:

- 新增 `analysis/openapi-app-requirements.ts`:接口路径 → 所需应用的映射表
  (首条 `/api/openapi/v1/backlog/data/` + POST/PUT/PATCH → 「单据审批」
  `TICKET_AUDIT_switch`、productId `o0hMBw000000g0`),配套
  `extractStaticUrlPath`(去域名、截断到第一个 `@{`、去 query/hash)与
  `matchAppRequirement`。
- `analysis/analyzer.ts` 6.3 节重构:以路径反查为主,节点显式声明的
  `powerCode`(`params` / URL query)作为交叉印证;两者指向同一权限时走
  路径规则,以便报文带上接口名、应用名、productId 与判定依据路径。显式
  声明了其它 `powerCode` 时以声明为准,避免两套规则打架。
- 新增保存期钩子 `preflightNodeAppRequirements` / `renderAppRequirementAlerts`
  (`cli/commands/workflow/shared.ts`),接入 `node add` / `node edit`:
  只检查本次写入的 HTTP 节点,未开通则输出 ⛔ 段并在 JSON 模式落到
  `appRequirementAlerts`;读不到企业应用清单时降级为 ⚠️,不阻断写入。
- `domains/store/tools.ts` 抽出 `isClientGoodOpened` 与
  `fetchOpenedPowerCodes`,`workflow analyze` 改为复用,移除内联汇总逻辑
  (`isClientGoodOpened` 仍从 `analyze.ts` 再导出以保持既有契约)。

判定边界(刻意收紧,宁可漏报不误报):只对 HTTP 节点生效;URL 若整体由
上游节点变量拼接则完全不判断;无法静态读出接口路径时不做任何猜测性告警。

## 二、choose 表达式序列化开关

合思前端序列化模板字符串时多写了一个右花括号,产出 `...}')}` 形态;
后端 Jackson 默认未开启 `FAIL_ON_TRAILING_TOKENS`,尾随 `}` 被静默忽略,
因此该形态在引擎侧可正常执行。

- 新增 `models/choose-serialization.ts`:`frontend` / `pure` 双模式序列化,
  以及 `parseChoosePayloadLoose`(逐个剥离尾部多余 `}` 再解析)。
- 全局配置新增 `chooseSerialization`,默认 `frontend`(与设计器字节一致),
  可用 `--choose-serialization` 单次覆盖;读取侧永远双形态兼容。
- 修复 `branch/explain.ts` 的 `parseChooseTag` 在 `}}` 形态下静默返回 null
  的缺陷,该缺陷会导致真实数据上的分支解释与字段引用分析失效。

## 测试

新增 24 个用例,覆盖:
- `choose-serialization.test.ts`:双模式序列化与宽松解析
- `openapi-app-requirements.test.ts`:静态路径提取、路径命中、方法不符跳过、
  纯变量 URL 跳过、非 HTTP 节点不判断、未开通/已开通/无清单三种诊断
- `shared.app-preflight.test.ts`:预检只处理 HTTP 节点、动态 URL 跳过、
  读不到清单降级不阻断、告警分级渲染

`bun test` 1068 pass / 0 fail;`bun run check` 零错误;`check:readme` 通过。

## 实机核对(全部只读,未触碰任何生产流程配置)

- 真实流程的两个 HTTP 节点均命中「单据审批」;同时确认设计器确实会把
  `powerCode` 写入节点 `params`,因此显式声明与路径反推两条规则都需要保留。
- 已开通该应用的企业 → 无诊断;未开通的企业 → 精确打出硬门禁并指向具体
  节点 key 与判定依据路径。
2026-11-04 16:43:34 +08:00

132 lines
4.4 KiB
TypeScript

import { describe, expect, test } from "bun:test";
import {
inspectChooseTags,
normalizeChooseTags,
parseChoosePayloadLoose,
serializeChooseTag,
} from "./choose-serialization.js";
import { parseChooseTag } from "../branch/explain.js";
const payload = {
key: "action_detail",
properties: [{ key: "id", label: "单据ID", type: "TEXT" }],
label: "查询单据详情.单据ID",
};
const json = JSON.stringify(payload);
describe("parseChoosePayloadLoose", () => {
test("解析配平形态", () => {
expect(parseChoosePayloadLoose(json)).toBe(json);
});
test("解析设计器产出的多余右花括号形态", () => {
expect(parseChoosePayloadLoose(`${json}}`)).toBe(json);
});
test("解析多个多余右花括号", () => {
expect(parseChoosePayloadLoose(`${json}}}`)).toBe(json);
});
test("嵌套对象仍能正确剥离", () => {
const nested = JSON.stringify({ key: "a", properties: [{ key: "b", meta: { x: 1 } }] });
expect(parseChoosePayloadLoose(`${nested}}`)).toBe(nested);
});
test("非 JSON 载荷(旧式路径写法)返回 null,不猜测", () => {
expect(parseChoosePayloadLoose("body.details")).toBeNull();
expect(parseChoosePayloadLoose("action_missing.data")).toBeNull();
});
test("空载荷返回 null", () => {
expect(parseChoosePayloadLoose("")).toBeNull();
});
});
describe("serializeChooseTag", () => {
test("frontend 模式多一个右花括号", () => {
expect(serializeChooseTag(json, "frontend")).toBe(`@{choose('${json}}')}`);
});
test("pure 模式花括号配平", () => {
expect(serializeChooseTag(json, "pure")).toBe(`@{choose('${json}')}`);
});
test("frontend 产出可被再次解析(不累加)", () => {
const once = serializeChooseTag(json, "frontend");
const twice = normalizeChooseTags(once, "frontend");
expect(twice).toBe(once);
});
});
describe("normalizeChooseTags", () => {
const prefix = "https://wx2.ekuaibao.com/api/openapi/v1/backlog/data/[";
const suffix = "]";
test("frontend 模式补齐前端形态", () => {
const pureUrl = `${prefix}@{choose('${json}')}${suffix}`;
expect(normalizeChooseTags(pureUrl, "frontend")).toBe(`${prefix}@{choose('${json}}')}${suffix}`);
});
test("pure 模式去掉多余花括号", () => {
const frontendUrl = `${prefix}@{choose('${json}}')}${suffix}`;
expect(normalizeChooseTags(frontendUrl, "pure")).toBe(`${prefix}@{choose('${json}')}${suffix}`);
});
test("静态前缀与后缀完整保留", () => {
const frontendUrl = `${prefix}@{choose('${json}}')}${suffix}`;
const out = normalizeChooseTags(frontendUrl, "frontend");
expect(out.startsWith(prefix)).toBe(true);
expect(out.endsWith(suffix)).toBe(true);
});
test("同一字符串内多个标签全部归一化", () => {
const text = `a@{choose('${json}')}b@{choose('${json}')}c`;
const out = normalizeChooseTags(text, "frontend");
expect(out).toBe(`a@{choose('${json}}')}b@{choose('${json}}')}c`);
});
test("不含 choose 标签的字符串原样返回", () => {
expect(normalizeChooseTags("https://example.com/plain", "frontend")).toBe("https://example.com/plain");
});
test("旧式非 JSON 载荷原样保留,不被破坏", () => {
const legacy = "@{choose('body.details')}";
expect(normalizeChooseTags(legacy, "frontend")).toBe(legacy);
expect(normalizeChooseTags(legacy, "pure")).toBe(legacy);
});
});
describe("inspectChooseTags", () => {
test("区分配平与多花括号形态", () => {
const text = `@{choose('${json}}')} @{choose('${json}')} @{choose('body.x')}`;
expect(inspectChooseTags(text)).toEqual({
total: 3,
balanced: 1,
extraBrace: 1,
unparsable: 1,
});
});
});
describe("parseChooseTag 兼容设计器产出形态", () => {
test("配平形态可解析", () => {
expect(parseChooseTag(`@{choose('${json}')}`)).toEqual({
upstreamNode: "action_detail",
upstreamProp: "id",
label: "查询单据详情.单据ID",
});
});
test("多花括号形态同样可解析(回归:早期实现直接 JSON.parse 会失败)", () => {
expect(parseChooseTag(`@{choose('${json}}')}`)).toEqual({
upstreamNode: "action_detail",
upstreamProp: "id",
label: "查询单据详情.单据ID",
});
});
test("非 JSON 载荷返回 null", () => {
expect(parseChooseTag("@{choose('body.details')}")).toBeNull();
});
});