botGroupDeliveryAction.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { createElement, type ContextType } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import {
  5. BotGroupDeliveryAction,
  6. ConnectedBotGroupDeliveryAction,
  7. } from "@/v4/BotGroupDeliveryAction.js";
  8. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  9. import type { BotGroupDelivery } from "@zcode/shared";
  10. import { DeliveryContext } from "@/v4/botGroupDeliveryContext.js";
  11. const render = (results: BotGroupDelivery[]) =>
  12. renderToStaticMarkup(
  13. createElement(
  14. ZCodeIntlProvider,
  15. { initialLocale: "zh-CN" },
  16. createElement(BotGroupDeliveryAction, {
  17. turnId: "product-turn-a",
  18. sourceCommandIds: ["command-a"],
  19. syncing: true,
  20. results,
  21. onRetry: vi.fn(),
  22. onReconcile: vi.fn(),
  23. }),
  24. ),
  25. );
  26. const result = (
  27. status: BotGroupDelivery["status"],
  28. sourceCommandId?: string,
  29. ): BotGroupDelivery => ({
  30. id: "delivery",
  31. taskId: "task",
  32. sourceCommandId,
  33. text: "SECRET_FULL_RESULT ou_sender",
  34. status,
  35. updatedAt: 1,
  36. });
  37. describe("group reply delivery action", () => {
  38. it.each(["sent", "failed", "unknown"] as const)(
  39. "topic-bound reply hides details and preserves %s delivery handling",
  40. (status) => {
  41. const value = {
  42. taskId: "task",
  43. context: {
  44. botId: "bot",
  45. workspacePath: "/workspace",
  46. mode: "task",
  47. activeTaskId: "task",
  48. updatedAt: 1,
  49. group: {
  50. chatId: "chat",
  51. threadId: "thread",
  52. name: "群聊",
  53. ownerId: "owner",
  54. enabled: true,
  55. taskIds: ["task"],
  56. currentOptions: {},
  57. deliveries: { delivery: result(status, "command-a") },
  58. },
  59. },
  60. provider: "feishu",
  61. busyId: null,
  62. retry: vi.fn(),
  63. reconcile: vi.fn(),
  64. retryPreparation: vi.fn(),
  65. } satisfies NonNullable<ContextType<typeof DeliveryContext>>;
  66. const html = renderToStaticMarkup(
  67. createElement(
  68. ZCodeIntlProvider,
  69. { initialLocale: "zh-CN" },
  70. createElement(
  71. DeliveryContext.Provider,
  72. { value },
  73. createElement(ConnectedBotGroupDeliveryAction, {
  74. turnId: "turn",
  75. sourceCommandIds: ["command-a"],
  76. }),
  77. ),
  78. ),
  79. );
  80. expect(html).not.toContain("data-bot-topic-details");
  81. expect(html).not.toContain("话题详情");
  82. if (status === "sent") expect(html).toBe("");
  83. else expect(html).toContain('data-bot-group-delivery="turn"');
  84. },
  85. );
  86. it.each(["pending", "sent", "invalidated"] as const)("hides %s deliveries", (status) => {
  87. expect(render([result(status, "command-a")])).toBe("");
  88. });
  89. it("never guesses another turn or a legacy delivery onto this reply", () => {
  90. expect(render([result("failed", "command-b"), result("unknown")])).toBe("");
  91. });
  92. it.each(["failed", "unknown"] as const)(
  93. "shows a compact %s action without duplicated content",
  94. (status) => {
  95. const html = render([result(status, "command-a")]);
  96. expect(html).toContain('data-bot-group-delivery="product-turn-a"');
  97. expect(html).toContain(status === "failed" ? "飞书发送失败" : "飞书待确认");
  98. expect(html).not.toContain("SECRET_FULL_RESULT");
  99. expect(html).not.toContain("ou_sender");
  100. expect(html).not.toContain("<aside");
  101. expect(html).not.toContain("已核对:已收到");
  102. },
  103. );
  104. });