workflowLaneName.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { describe, expect, it } from "vitest";
  2. import { laneDisplayName } from "../src/components/workflow-graph/lane-name.js";
  3. /**
  4. * 车道显示名的唯一策略点。它只要一个 formatMessage,不碰 React,所以能在这里直接单测:
  5. * 文案在渲染时才本地化,投影里存的永远是数据(脚本给的名字 + 匿名序号)。
  6. */
  7. // 假 formatter:把 message id 和参数原样回显。断言于是读的是「走了哪条文案」,
  8. // 而不是某个语言的具体译文——译文改字不该弄红这些测试。
  9. function formatMessage({ id }: { id: string }, values?: Record<string, string | number>): string {
  10. if (values === undefined) return id;
  11. const args = Object.entries(values)
  12. .map(([key, value]) => `${key}=${value}`)
  13. .join(",");
  14. return `${id}(${args})`;
  15. }
  16. describe("laneDisplayName", () => {
  17. it("shows a script-authored actor name verbatim, never localized", () => {
  18. // 名字是脚本作者写下的文案,本地化它就是改作者的话。
  19. expect(laneDisplayName({ laneClass: "agent", name: "planner" }, formatMessage)).toBe("planner");
  20. // 有名字时序号无关:编号只是没名字时的区分手段。
  21. expect(
  22. laneDisplayName({ anonymousIndex: 2, laneClass: "agent", name: "planner" }, formatMessage),
  23. ).toBe("planner");
  24. });
  25. it("names a lone unnamed actor lane, instead of leaking its site id", () => {
  26. // 过去这里回退到 head.id,界面上就是一行「actor#3」——站点 id 是身份,不是名字。
  27. expect(laneDisplayName({ laneClass: "agent" }, formatMessage)).toBe(
  28. "chat.toolCall.workflow.graph.lane.anonymous",
  29. );
  30. });
  31. it("numbers unnamed actor lanes when the projection says several coexist", () => {
  32. expect(laneDisplayName({ anonymousIndex: 1, laneClass: "agent" }, formatMessage)).toBe(
  33. "chat.toolCall.workflow.graph.lane.anonymousIndexed(index=1)",
  34. );
  35. expect(laneDisplayName({ anonymousIndex: 2, laneClass: "agent" }, formatMessage)).toBe(
  36. "chat.toolCall.workflow.graph.lane.anonymousIndexed(index=2)",
  37. );
  38. });
  39. it("keeps the two synthetic lanes on their own localized labels", () => {
  40. // workspace 不是 actor(没有信箱),unresolved 是没能定位到站点的 ask:
  41. // 两者都不靠名字识别,文案固定。
  42. expect(laneDisplayName({ laneClass: "workspace" }, formatMessage)).toBe(
  43. "chat.toolCall.workflow.graph.lane.script",
  44. );
  45. expect(laneDisplayName({ laneClass: "unresolved" }, formatMessage)).toBe(
  46. "chat.toolCall.workflow.graph.lane.unresolved",
  47. );
  48. });
  49. it("renders an interpolated name's static shape instead of the anonymous fallback", () => {
  50. // `` agent(`研究员${i + 1}`) `` 的形状仍然是作者的话,所以同样不本地化。
  51. expect(
  52. laneDisplayName({ laneClass: "agent", namePattern: { head: "研究员" } }, formatMessage),
  53. ).toBe("研究员…");
  54. expect(
  55. laneDisplayName({ laneClass: "agent", namePattern: { tail: "-worker" } }, formatMessage),
  56. ).toBe("…-worker");
  57. expect(
  58. laneDisplayName({ laneClass: "agent", namePattern: { head: "a", tail: "b" } }, formatMessage),
  59. ).toBe("a…b");
  60. });
  61. it("prefers a literal name over a pattern, and a pattern over the anonymous fallback", () => {
  62. // 优先级只有一条:精炼名 > 字面量名 > 形状 > 本地化兜底。带形状的车道**算有名字**,
  63. // 所以 anonymousIndex 就算在场也不该冒出来。
  64. expect(
  65. laneDisplayName(
  66. { laneClass: "agent", name: "planner", namePattern: { head: "研究员" } },
  67. formatMessage,
  68. ),
  69. ).toBe("planner");
  70. expect(
  71. laneDisplayName(
  72. { anonymousIndex: 2, laneClass: "agent", namePattern: { head: "研究员" } },
  73. formatMessage,
  74. ),
  75. ).toBe("研究员…");
  76. });
  77. it("shows the author's word over the interpolated shape", () => {
  78. // 三档顺位:作者原词 > 插值形状 > 匿名兜底。没有第四个来源——模型精炼那一档已撤回
  79. // (docs/dynamic-workflow/presentation.md)。
  80. expect(
  81. laneDisplayName(
  82. { laneClass: "agent", name: "planner", namePattern: { head: "研究员" } },
  83. formatMessage,
  84. ),
  85. ).toBe("planner");
  86. });
  87. it("falls back to anonymous rather than render a lone ellipsis", () => {
  88. // 分析器不发空 pattern,但协议上 `{}` 能过 `.strict()`;不兜住就会在车道头留一个
  89. // 什么也没说的省略号。
  90. expect(laneDisplayName({ laneClass: "agent", namePattern: {} }, formatMessage)).toBe(
  91. "chat.toolCall.workflow.graph.lane.anonymous",
  92. );
  93. });
  94. });