featureBoundaryGraph.test.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { readFile } from "node:fs/promises";
  2. import { resolve } from "node:path";
  3. import { describe, expect, it } from "vitest";
  4. import { parse as parseYaml } from "yaml";
  5. import {
  6. getFeatureBoundaryGraphStats,
  7. getFeatureBoundaryNeighborhood,
  8. type FeatureBoundaryGraph,
  9. } from "@/data/featureBoundaryGraph.js";
  10. import { normalizeFeatureBoundaryGraph } from "@/data/featureBoundaryGraphSource.js";
  11. const fixture: FeatureBoundaryGraph = {
  12. schemaVersion: 1,
  13. graphId: "fixture",
  14. purpose: "test",
  15. nodes: [
  16. {
  17. id: "capability.alpha",
  18. kind: "capability",
  19. label: "Alpha",
  20. aliases: [],
  21. docs: [],
  22. codeSeeds: [],
  23. invariants: [],
  24. },
  25. {
  26. id: "service.beta",
  27. kind: "service",
  28. label: "Beta",
  29. aliases: [],
  30. docs: [],
  31. codeSeeds: [],
  32. invariants: [],
  33. },
  34. {
  35. id: "state.gamma",
  36. kind: "state-owner",
  37. label: "Gamma",
  38. aliases: [],
  39. docs: [],
  40. codeSeeds: [],
  41. invariants: [],
  42. },
  43. ],
  44. edges: [
  45. {
  46. from: "capability.alpha",
  47. to: "service.beta",
  48. type: "served-by",
  49. rank: "must-inspect",
  50. },
  51. {
  52. from: "state.gamma",
  53. to: "capability.alpha",
  54. type: "projects-to",
  55. rank: "should-inspect",
  56. },
  57. ],
  58. };
  59. describe("feature boundary graph helpers", () => {
  60. it("保留节点、边和按类型统计", () => {
  61. expect(getFeatureBoundaryGraphStats(fixture)).toEqual({
  62. nodeCount: 3,
  63. edgeCount: 2,
  64. unresolvedEndpointCount: 0,
  65. kindCounts: {
  66. capability: 1,
  67. service: 1,
  68. "state-owner": 1,
  69. },
  70. });
  71. });
  72. it("返回选中节点的一阶邻居,并补出未声明端点", () => {
  73. const graph: FeatureBoundaryGraph = {
  74. ...fixture,
  75. edges: [
  76. ...fixture.edges,
  77. {
  78. from: "capability.alpha",
  79. to: "missing.delta",
  80. type: "depends-on",
  81. rank: "conditional",
  82. },
  83. ],
  84. };
  85. const neighborhood = getFeatureBoundaryNeighborhood(graph, "capability.alpha");
  86. expect(neighborhood.nodes.map((node) => node.id)).toEqual([
  87. "capability.alpha",
  88. "service.beta",
  89. "state.gamma",
  90. "missing.delta",
  91. ]);
  92. expect(neighborhood.edges).toHaveLength(3);
  93. expect(neighborhood.edges[2]).toMatchObject({
  94. source: "capability.alpha",
  95. target: "missing.delta",
  96. });
  97. });
  98. it("解析当前 feature boundary YAML 并保留未声明端点", async () => {
  99. const source = await readFile(
  100. resolve(
  101. process.cwd(),
  102. "../../.agents/skills/feature-boundary-planner/references/zcode-feature-graph.yaml",
  103. ),
  104. "utf8",
  105. );
  106. const graph = normalizeFeatureBoundaryGraph(parseYaml(source));
  107. const stats = getFeatureBoundaryGraphStats(graph);
  108. expect(stats).toMatchObject({
  109. nodeCount: 324,
  110. edgeCount: 629,
  111. unresolvedEndpointCount: 2,
  112. });
  113. expect(graph.nodes.filter((node) => node.kind === "unresolved").map((node) => node.id)).toEqual(
  114. ["capability.mcp-oauth-authorization", "persistence.desktop-user-data-root"],
  115. );
  116. });
  117. it("危险命令 V1 关联真实代码起点和隔离边界", async () => {
  118. const source = await readFile(
  119. resolve(
  120. process.cwd(),
  121. "../../.agents/skills/feature-boundary-planner/references/zcode-feature-graph.yaml",
  122. ),
  123. "utf8",
  124. );
  125. const graph = normalizeFeatureBoundaryGraph(parseYaml(source));
  126. const capabilityId = "capability.dangerous-command-approval";
  127. const capability = graph.nodes.find((node) => node.id === capabilityId);
  128. expect(capability?.docs).toContain("docs/dangerous-command-approval-v1-plan.md");
  129. expect(capability?.codeSeeds).toEqual([
  130. "matchDangerousCommand · apps/zcode-cli/packages/core/src/tool/handlers/guarded/command.ts",
  131. "resolveToolPermission · apps/zcode-cli/packages/core/src/tool/executor/permission-flow.ts",
  132. "buildProtocolPermissionOptions · apps/zcode-cli/packages/bootstrap/src/permission-options.ts",
  133. "getZCodeAgentAvailableModes · packages/shared/src/zcode-agent-model-state.ts",
  134. "buildAutomationModeOption · packages/ui/src/settings/automationAgentConfigOptions.ts",
  135. "filterModeOptions · apps/zcode-cli/packages/tui/src/app-mode-command.ts",
  136. ]);
  137. expect(graph.edges.filter((edge) => edge.from === capabilityId).map((edge) => edge.to)).toEqual(
  138. [
  139. "surface.parent-task-blocking-interaction",
  140. "boundary.desktop-continuous",
  141. "boundary.web-remote-replayable",
  142. "boundary.workspace-key",
  143. ],
  144. );
  145. });
  146. });