v4BotTopicPreparation.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it } from "vitest";
  4. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  5. import type { UserInputRow } from "@zcode/shared/zcode-protocol-v4";
  6. import { TooltipProvider } from "@/components/ui/tooltip.js";
  7. import { BotTopicPreparationList as PreparationList } from "@/v4/BotTopicPreparation.js";
  8. const BotTopicPreparationList = (props: Parameters<typeof PreparationList>[0]) =>
  9. createElement(
  10. TooltipProvider,
  11. null,
  12. createElement(PreparationList, { provider: "feishu", ...props }),
  13. );
  14. describe("topic preparation presentation", () => {
  15. it("shows active preparation but omits failed rows entirely", () => {
  16. const html = renderToStaticMarkup(
  17. createElement(
  18. ZCodeIntlProvider,
  19. { initialLocale: "zh-CN", children: null },
  20. createElement(BotTopicPreparationList, {
  21. items: [
  22. { messageId: "a", senderName: "Alice", text: "first", status: "preparing" },
  23. { messageId: "b", senderName: "Bob", text: "second", status: "waitingStop" },
  24. {
  25. messageId: "c",
  26. senderName: "Carol",
  27. text: "third",
  28. status: "failed",
  29. error: "download failed",
  30. },
  31. ],
  32. }),
  33. ),
  34. );
  35. expect(html).toContain("Alice");
  36. expect(html).toContain("Bob");
  37. expect(html).toContain("正在准备材料");
  38. expect(html).toContain("等待当前执行停止");
  39. expect(html).not.toContain("download failed");
  40. expect(html).not.toContain("Carol");
  41. expect(html).not.toContain("材料准备失败");
  42. expect(html).not.toContain("重试");
  43. expect(html.match(/data-topic-preparation-loading="true"/g)).toHaveLength(2);
  44. });
  45. it("shows channel and loading in the source row, keeping an empty input bubble absent", () => {
  46. const html = renderToStaticMarkup(
  47. createElement(
  48. ZCodeIntlProvider,
  49. { initialLocale: "zh-CN", children: null },
  50. createElement(BotTopicPreparationList, {
  51. items: [
  52. { messageId: "a", senderName: "Alice", text: "first\nsecond", status: "preparing" },
  53. { messageId: "b", senderName: "Bob", text: " ", status: "waitingStop" },
  54. ],
  55. }),
  56. ),
  57. );
  58. expect(html.match(/data-v4-user-input-bubble="true"/g)).toHaveLength(1);
  59. expect(html.match(/data-v4-user-input-collapsible-content="true"/g)).toHaveLength(1);
  60. expect(html).toContain("飞书 · Alice");
  61. expect(html).toContain('data-topic-preparation-loading="true"');
  62. expect(html).not.toContain("Alice ·");
  63. expect(html).toContain('data-v4-user-input-collapsible-content="true"');
  64. expect(html.indexOf("Alice")).toBeLessThan(html.indexOf("data-v4-user-input-bubble"));
  65. expect(html).not.toContain(">正在准备材料<");
  66. });
  67. it("hides late preparation only for original message IDs already in formal rows", () => {
  68. const acceptedRows = [
  69. {
  70. kind: "userInput",
  71. botGroupSource: {
  72. messageId: "b",
  73. messages: [{ messageId: "a" }, { messageId: "b" }],
  74. },
  75. },
  76. ] as UserInputRow[];
  77. const html = renderToStaticMarkup(
  78. createElement(
  79. ZCodeIntlProvider,
  80. { initialLocale: "en-US", children: null },
  81. createElement(BotTopicPreparationList, {
  82. items: ["a", "b", "c"].map((messageId) => ({
  83. messageId,
  84. senderName: messageId,
  85. text: "same text",
  86. status: "preparing" as const,
  87. })),
  88. acceptedRows,
  89. }),
  90. ),
  91. );
  92. expect(html).not.toContain('data-topic-preparation="a"');
  93. expect(html).not.toContain('data-topic-preparation="b"');
  94. expect(html).toContain('data-topic-preparation="c"');
  95. });
  96. it("keeps the Lark channel name", () => {
  97. const html = renderToStaticMarkup(
  98. createElement(
  99. ZCodeIntlProvider,
  100. { initialLocale: "zh-CN", children: null },
  101. createElement(BotTopicPreparationList, {
  102. provider: "lark",
  103. items: [{ messageId: "a", senderName: "Alice", text: "hello", status: "preparing" }],
  104. }),
  105. ),
  106. );
  107. expect(html).toContain("Lark · Alice");
  108. expect(html).not.toContain("飞书");
  109. });
  110. });