workflowNotificationHoleRow.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // 留白通知行(docs/dynamic-workflow/transcript-and-notifications.md「The hole row's live state」):
  2. // 笔图标、三态种类词按 run 投影的 holes 翻转、主文本 `· run · 留白名` + 类型徽标、展开体的提示 /
  3. // 位置 / 类型 / 草稿路径 / 等待时长 / run 链接。永不 shimmer。
  4. import { createElement, type ReactNode } from "react";
  5. import { renderToStaticMarkup } from "react-dom/server";
  6. import { describe, expect, it, vi } from "vitest";
  7. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  8. vi.mock("@/components/ai-elements/code-block.js", () => ({
  9. CodeBlockHeader: () => null,
  10. CodeBlock: ({ code }: { code: string; children?: ReactNode }) =>
  11. createElement("pre", { "data-testid": "code-block" }, code),
  12. }));
  13. // eslint-disable-next-line import/first -- 必须在 code-block mock 之后再引入被测组件。
  14. import {
  15. WorkflowNotificationToolRow,
  16. type WorkflowNotificationToolRowProps,
  17. } from "@/v4/WorkflowNotificationToolRow.js";
  18. // eslint-disable-next-line import/first -- 同上。
  19. import { holeRowState } from "@/v4/WorkflowNotificationHoleRow.js";
  20. const HOLE = {
  21. kind: "hole" as const,
  22. siteId: "hole#1",
  23. ordinal: 1,
  24. name: "决定分组",
  25. type: "Plan",
  26. prompt: "侦察结果如下,请补全这一段。",
  27. draftPath: "/w/.zcode/workflow-drafts/flaky.dwf.ts",
  28. line: 12,
  29. before: "探索",
  30. after: "执行",
  31. reachedAt: Date.now() - 3 * 60_000,
  32. };
  33. function render(
  34. props: Partial<WorkflowNotificationToolRowProps>,
  35. locale: "en-US" | "zh-CN" = "zh-CN",
  36. ): string {
  37. return renderToStaticMarkup(
  38. createElement(
  39. ZCodeIntlProvider,
  40. { initialLocale: locale },
  41. createElement(WorkflowNotificationToolRow, {
  42. notification: HOLE,
  43. runName: "flaky-hunt",
  44. testIdKey: "turn-1:1",
  45. theme: "system",
  46. ...props,
  47. } as WorkflowNotificationToolRowProps),
  48. ),
  49. );
  50. }
  51. describe("holeRowState", () => {
  52. it("run 不在场 → left;在场且 waiting → waiting;在场且 filled / 不在表里 → filled / left", () => {
  53. expect(holeRowState(undefined, "hole#1")).toBe("left");
  54. expect(
  55. holeRowState([{ siteId: "hole#1", ordinal: 1, name: "n", state: "waiting" }], "hole#1"),
  56. ).toBe("waiting");
  57. expect(
  58. holeRowState([{ siteId: "hole#1", ordinal: 1, name: "n", state: "filled" }], "hole#1"),
  59. ).toBe("filled");
  60. expect(holeRowState([], "hole#1")).toBe("left");
  61. });
  62. });
  63. describe("WorkflowNotificationToolRow — hole", () => {
  64. it("三态种类词:等待补全 / 已补全 / 中性", () => {
  65. const waiting = render({
  66. holes: [{ siteId: "hole#1", ordinal: 1, name: "决定分组", state: "waiting" }],
  67. });
  68. expect(waiting).toContain("工作流留白 · 等待补全");
  69. expect(waiting).toContain('data-hole-state="waiting"');
  70. expect(
  71. render({ holes: [{ siteId: "hole#1", ordinal: 1, name: "决定分组", state: "filled" }] }),
  72. ).toContain("留白已补全");
  73. expect(render({})).toContain("工作流留白");
  74. expect(render({}, "en-US")).toContain("Workflow left a hole");
  75. expect(
  76. render(
  77. { holes: [{ siteId: "hole#1", ordinal: 1, name: "决定分组", state: "waiting" }] },
  78. "en-US",
  79. ),
  80. ).toContain("Workflow waiting for code");
  81. });
  82. it("主文本:run 名与留白名,不画类型;行永不 shimmer", () => {
  83. const html = render({});
  84. expect(html).toContain("flaky-hunt");
  85. expect(html).toContain('data-testid="workflow-hole-notification-name"');
  86. expect(html).toContain("决定分组");
  87. expect(html).not.toContain("workflow-hole-type");
  88. expect(html).not.toContain("Plan");
  89. expect(html).not.toContain("animate-pulse");
  90. });
  91. it("展开体:提示全文、位于两站之间、草稿路径:L 行、等待时长、run 链接;不写须返回的类型", () => {
  92. const html = render({
  93. forceOpen: true,
  94. holes: [{ siteId: "hole#1", ordinal: 1, name: "决定分组", state: "waiting" }],
  95. onOpenRun: () => undefined,
  96. });
  97. expect(html).toContain("侦察结果如下,请补全这一段。");
  98. expect(html).toContain("留白位于「探索」之后、「执行」之前;");
  99. expect(html).not.toContain("补全须返回");
  100. expect(html).not.toContain("Plan");
  101. expect(html).toContain("/w/.zcode/workflow-drafts/flaky.dwf.ts:L12");
  102. expect(html).toContain('data-testid="workflow-hole-notification-waited"');
  103. expect(html).toContain("等待中 · 3 分钟前");
  104. // run 链接沿用既有词条 `chat.toolCall.workflow.openRunDetails`。
  105. expect(html).toContain("查看实例详情");
  106. });
  107. it("已补全的行不写等待时长;只有一侧邻站时换措辞", () => {
  108. const filled = render({
  109. forceOpen: true,
  110. notification: { ...HOLE, before: undefined, after: "执行" },
  111. holes: [{ siteId: "hole#1", ordinal: 1, name: "决定分组", state: "filled" }],
  112. });
  113. expect(filled).not.toContain('data-testid="workflow-hole-notification-waited"');
  114. expect(filled).toContain("留白位于「执行」之前;");
  115. });
  116. });