create-workflow-graph-bounds-holes.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { describe, expect, it } from "vitest";
  2. import { CreateWorkflowCausalityGraphSchema } from "@zcode/contracts";
  3. import {
  4. MAIN_LANE,
  5. type CausalityGraph,
  6. type ControlFlowGraph,
  7. type FlowHole,
  8. } from "@zcode/dynamic-workflow/projections";
  9. import { boundCausalityGraph } from "../src/tool/handlers/create-workflow-graph-bounds.js";
  10. // docs/dynamic-workflow/presentation.md「The display contract」:`holes` 是还开着的留白(控制流图的
  11. // `flow.holes`,源序、≤32),`fill` 标在补全写进来的阶段与站点上;一处还开着的留白也是一个站点
  12. // (kind `hole`、车道 `main`)。裁剪层只做上限与引用完整性,不改语义。
  13. function step(id: string, extra: Record<string, unknown> = {}) {
  14. return {
  15. id,
  16. kind: "ask" as const,
  17. label: id,
  18. loc: { line: 1, column: 1 },
  19. lane: "actor#1",
  20. region: "root",
  21. certainty: "always" as const,
  22. ...extra,
  23. };
  24. }
  25. function graphWith(holes: FlowHole[] | undefined, stepsExtra: Record<string, unknown> = {}) {
  26. const causality = {
  27. steps: [
  28. step("ask#1", { phase: "phase#1" }),
  29. step("hole#1/ask#1", { phase: "hole#1", ...stepsExtra }),
  30. // 还开着的留白:kind `hole`、主代理的车道。
  31. step("hole#2", { kind: "hole", label: "收尾", lane: MAIN_LANE, phase: "phase#1" }),
  32. ],
  33. regions: [],
  34. lanes: [{ id: "actor#1", name: "a" }, { id: MAIN_LANE }],
  35. edges: [],
  36. phases: [
  37. { id: "phase#1", name: "A" },
  38. { id: "hole#1", name: "决定分组" },
  39. ],
  40. } as unknown as CausalityGraph;
  41. const flow = {
  42. nodes: [],
  43. edges: [],
  44. phases: [
  45. { id: "phase#1", name: "A" },
  46. { id: "hole#1", name: "决定分组", fill: "hole#1" },
  47. ],
  48. ...(holes === undefined ? {} : { holes }),
  49. } as unknown as ControlFlowGraph;
  50. return { causality, flow };
  51. }
  52. describe("boundCausalityGraph — holes and fills", () => {
  53. it("copies the open holes from the flow graph in order and the fill marks on phases and steps", () => {
  54. const { causality, flow } = graphWith(
  55. [{ siteId: "hole#2", name: "收尾", type: "string[]", phase: "phase#1", tail: true }],
  56. { fill: "hole#1" },
  57. );
  58. const bound = boundCausalityGraph(causality, flow);
  59. expect(bound.holes).toEqual([
  60. { siteId: "hole#2", name: "收尾", type: "string[]", phase: "phase#1", tail: true },
  61. ]);
  62. expect(bound.steps.find((s) => s.id === "hole#1/ask#1")?.fill).toBe("hole#1");
  63. expect(bound.steps.find((s) => s.id === "ask#1")?.fill).toBeUndefined();
  64. expect(bound.phases?.find((p) => p.id === "hole#1")?.fill).toBe("hole#1");
  65. expect(bound.phases?.find((p) => p.id === "phase#1")?.fill).toBeUndefined();
  66. // 留白站点与它的 `main` 车道都活着。
  67. expect(bound.steps.find((s) => s.id === "hole#2")).toMatchObject({
  68. kind: "hole",
  69. lane: MAIN_LANE,
  70. });
  71. expect(bound.lanes.map((lane) => lane.id)).toContain(MAIN_LANE);
  72. expect(bound.truncated).toBeUndefined();
  73. // 契约 schema(.strict())认这份载荷。
  74. expect(CreateWorkflowCausalityGraphSchema.parse(bound)).toEqual(bound);
  75. });
  76. it("leaves holes absent when the script has none, and drops a phase reference the vocabulary lost", () => {
  77. const { causality, flow } = graphWith(undefined);
  78. expect("holes" in boundCausalityGraph(causality, flow)).toBe(false);
  79. const unlisted = graphWith([{ siteId: "hole#2", name: "收尾", type: "T", phase: "phase#9" }]);
  80. const bound = boundCausalityGraph(unlisted.causality, unlisted.flow);
  81. expect(bound.holes).toEqual([{ siteId: "hole#2", name: "收尾", type: "T" }]);
  82. });
  83. it("caps the holes at 32 and flags truncation", () => {
  84. const many: FlowHole[] = Array.from({ length: 40 }, (_, i) => ({
  85. siteId: `hole#${i + 2}`,
  86. name: `h${i}`,
  87. type: "T",
  88. }));
  89. const { causality, flow } = graphWith(many);
  90. const bound = boundCausalityGraph(causality, flow);
  91. expect(bound.holes).toHaveLength(32);
  92. expect(bound.holes?.[0]?.siteId).toBe("hole#2");
  93. expect(bound.truncated).toBe(true);
  94. expect(CreateWorkflowCausalityGraphSchema.safeParse(bound).success).toBe(true);
  95. });
  96. });
  97. // 递归留白:嵌套的补全标记(`fill: "hole#1/hole#1"`)与嵌套的开放留白原样透传。
  98. describe("boundCausalityGraph — nested holes", () => {
  99. it("keeps nested fill marks and nested open holes", () => {
  100. const { causality, flow } = graphWith(
  101. [{ siteId: "hole#1/hole#1/hole#2", name: "更深", type: "Deep", phase: "phase#1" }],
  102. { fill: "hole#1/hole#1" },
  103. );
  104. (flow.phases as { fill?: string }[])[1]!.fill = "hole#1/hole#1";
  105. const bound = boundCausalityGraph(causality, flow);
  106. expect(bound.steps.find((s) => s.id === "hole#1/ask#1")?.fill).toBe("hole#1/hole#1");
  107. expect(bound.phases?.find((p) => p.id === "hole#1")?.fill).toBe("hole#1/hole#1");
  108. expect(bound.holes).toEqual([
  109. { siteId: "hole#1/hole#1/hole#2", name: "更深", type: "Deep", phase: "phase#1" },
  110. ]);
  111. expect(CreateWorkflowCausalityGraphSchema.parse(bound)).toEqual(bound);
  112. });
  113. });