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(); }); });