botsDialogRuntimeError.test.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import type { BotConfig, BotRuntimeInfo } from "@zcode/shared";
  5. import { ALL_BOT_WORKSPACES } from "@zcode/shared";
  6. import { BotSummaryCard } from "@/BotsDialog/BotSummaryCard.js";
  7. import { ProviderSettingsCard } from "@/BotsDialog/ProviderSettingsCard.js";
  8. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  9. const bot: BotConfig = {
  10. id: "feishu-bot",
  11. name: "咪处长",
  12. provider: "feishu",
  13. enabled: true,
  14. credentialRef: "credential-ref",
  15. feishuAppId: "cli_test",
  16. allowedWorkspaces: [ALL_BOT_WORKSPACES],
  17. allowedCommands: {},
  18. currentOptions: {},
  19. replyMode: "streaming_card",
  20. };
  21. const runtime: BotRuntimeInfo = {
  22. botId: bot.id,
  23. provider: bot.provider,
  24. status: "error",
  25. message: "Feishu WebSocket failed: code: 1000040351, system busy",
  26. };
  27. function renderZh(element: ReturnType<typeof createElement>): string {
  28. return renderToStaticMarkup(
  29. createElement(ZCodeIntlProvider, { initialLocale: "zh-CN" }, element),
  30. );
  31. }
  32. describe("BotsDialog runtime error", () => {
  33. it("shows a runtime failure instead of the unbound summary", () => {
  34. const markup = renderZh(
  35. createElement(BotSummaryCard, {
  36. bot,
  37. runtime,
  38. selectedBotDisplayName: bot.name,
  39. selectedBotName: bot.name,
  40. fallbackBotName: "飞书机器人",
  41. renaming: false,
  42. onStartRename: vi.fn(),
  43. onCommitNameDraft: vi.fn(),
  44. onNameDraftChange: vi.fn(),
  45. onNameInputKeyDown: vi.fn(),
  46. onPatchBot: vi.fn(),
  47. }),
  48. );
  49. expect(markup).toContain("飞书连接失败");
  50. expect(markup).not.toContain("未绑定");
  51. });
  52. it("shows delivery errors independently of an active connection", () => {
  53. const markup = renderZh(
  54. createElement(ProviderSettingsCard, {
  55. bot,
  56. runtime: {
  57. ...runtime,
  58. status: "connected",
  59. deliveryError: "HTTP 200, code=230101, log_id=delivery-log",
  60. },
  61. credentialValue: "",
  62. bindCode: {
  63. botId: bot.id,
  64. code: "BB2549",
  65. createdAt: 1,
  66. expiresAt: 60_001,
  67. ttlMs: 60_000,
  68. },
  69. bindExpired: false,
  70. bindRemainingMs: 26_000,
  71. bindCountdownProgress: 43,
  72. feishuRegistration: null,
  73. feishuRegistrationLoading: false,
  74. weixinRegistration: null,
  75. weixinRegistrationLoading: false,
  76. weixinActivated: false,
  77. secretSaving: false,
  78. onCredentialValueChange: vi.fn(),
  79. onSaveSecret: vi.fn(),
  80. onRemoveSecret: vi.fn(),
  81. onOpenTelegramBotFather: vi.fn(),
  82. onStartWeixinRegistration: vi.fn(),
  83. onStartFeishuRegistration: vi.fn(),
  84. onCreateBindCode: vi.fn(),
  85. onUnbind: vi.fn(),
  86. onCopyBindCommand: vi.fn(),
  87. }),
  88. );
  89. expect(markup).toContain("机器人回复发送失败");
  90. expect(markup).toContain("230101");
  91. expect(markup).toContain("delivery-log");
  92. expect(markup).not.toContain("无法连接飞书");
  93. });
  94. it("replaces an active bind command with the persistent connection error", () => {
  95. const markup = renderZh(
  96. createElement(ProviderSettingsCard, {
  97. bot,
  98. runtime,
  99. credentialValue: "",
  100. bindCode: {
  101. botId: bot.id,
  102. code: "BB2549",
  103. createdAt: 1,
  104. expiresAt: 60_001,
  105. ttlMs: 60_000,
  106. },
  107. bindExpired: false,
  108. bindRemainingMs: 26_000,
  109. bindCountdownProgress: 43,
  110. feishuRegistration: null,
  111. feishuRegistrationLoading: false,
  112. weixinRegistration: null,
  113. weixinRegistrationLoading: false,
  114. weixinActivated: false,
  115. secretSaving: false,
  116. onCredentialValueChange: vi.fn(),
  117. onSaveSecret: vi.fn(),
  118. onRemoveSecret: vi.fn(),
  119. onOpenTelegramBotFather: vi.fn(),
  120. onStartWeixinRegistration: vi.fn(),
  121. onStartFeishuRegistration: vi.fn(),
  122. onCreateBindCode: vi.fn(),
  123. onUnbind: vi.fn(),
  124. onCopyBindCommand: vi.fn(),
  125. }),
  126. );
  127. expect(markup).toContain("无法连接飞书");
  128. expect(markup).toContain("1000040351");
  129. expect(markup).toContain("system busy");
  130. expect(markup).not.toContain("/bind BB2549");
  131. expect(markup).not.toContain("刷新绑定码");
  132. });
  133. it("keeps an existing binding while showing that its connection is interrupted", () => {
  134. const markup = renderZh(
  135. createElement(ProviderSettingsCard, {
  136. bot: { ...bot, providerUserId: "ou_bound_user" },
  137. runtime,
  138. credentialValue: "",
  139. bindCode: null,
  140. bindExpired: false,
  141. bindRemainingMs: 0,
  142. bindCountdownProgress: 0,
  143. feishuRegistration: null,
  144. feishuRegistrationLoading: false,
  145. weixinRegistration: null,
  146. weixinRegistrationLoading: false,
  147. weixinActivated: false,
  148. secretSaving: false,
  149. onCredentialValueChange: vi.fn(),
  150. onSaveSecret: vi.fn(),
  151. onRemoveSecret: vi.fn(),
  152. onOpenTelegramBotFather: vi.fn(),
  153. onStartWeixinRegistration: vi.fn(),
  154. onStartFeishuRegistration: vi.fn(),
  155. onCreateBindCode: vi.fn(),
  156. onUnbind: vi.fn(),
  157. onCopyBindCommand: vi.fn(),
  158. }),
  159. );
  160. expect(markup).toContain("连接中断");
  161. expect(markup).toContain("长连接数可能已达上限或飞书服务繁忙");
  162. expect(markup).toContain("若持续失败,可绑定新的机器人");
  163. expect(markup).toContain("解绑");
  164. expect(markup).not.toContain(">已连通<");
  165. expect(markup).not.toContain("暂时不能完成绑定");
  166. });
  167. });