feishu-runtime-error-ui.test.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { mkdir, readFile, writeFile } from "node:fs/promises";
  2. import { join } from "node:path";
  3. import {
  4. getE2EAppDataPaths,
  5. seedSettings,
  6. waitForDefaultWorkspaceReady,
  7. } from "#e2e/helpers/desktop-app";
  8. import {
  9. createFeishuConfig,
  10. readBotSyntheticFixture,
  11. } from "#e2e/bots/helpers/bots-service-harness";
  12. const BOT_NAME = "Feishu Runtime Error E2E";
  13. const BOTS_DIALOG_TEXT = "机器人";
  14. describe("Feishu runtime error Desktop UI", () => {
  15. before(async () => {
  16. const fixture = await readBotSyntheticFixture("feishu-runtime-error-ui.json");
  17. expect(fixture).toMatchObject({
  18. caseId: "BOT-E2E-CF-01",
  19. classification: "synthetic",
  20. timing: "controlled-process",
  21. });
  22. expect(fixture.syntheticReason.length).toBeGreaterThan(20);
  23. const { appDataDir } = getE2EAppDataPaths();
  24. const config = createFeishuConfig("feishu", "feishu-runtime-error-e2e");
  25. config.bots[0] = {
  26. ...config.bots[0]!,
  27. name: BOT_NAME,
  28. enabled: false,
  29. credentialRef: "bot:feishu-runtime-error-e2e:credential",
  30. providerUserId: "ou_bound_runtime_error_e2e",
  31. };
  32. await mkdir(appDataDir, { recursive: true });
  33. await writeFile(join(appDataDir, "bot-config.v3.json"), `${JSON.stringify(config, null, 2)}\n`);
  34. await seedSettings({ locale: "zh-CN", localePreference: "zh-CN" });
  35. });
  36. it("BOT-E2E-CF-01 keeps the binding visible when the Feishu connection fails", async () => {
  37. // E2E runner 首帧会跟随系统语言;刷新后读取本用例写入的中文设置,
  38. // 同时让 renderer 从真实 BotsService 加载 case-local Bot 配置。
  39. await browser.refresh();
  40. await waitForDefaultWorkspaceReady(60_000);
  41. await openBotsDialog();
  42. await browser.waitUntil(() => botsDialogIncludes(BOT_NAME), {
  43. timeout: 20_000,
  44. timeoutMsg: "机器人弹窗没有加载隔离的 Feishu E2E 配置",
  45. });
  46. const enabled = await browser.execute(
  47. (dialogText, botName) => {
  48. const dialog = Array.from(document.querySelectorAll<HTMLElement>("[role=dialog]")).find(
  49. (candidate) =>
  50. candidate.textContent?.includes(dialogText) && candidate.textContent.includes(botName),
  51. );
  52. const toggle = dialog?.querySelector<HTMLElement>("[role=switch]");
  53. if (!toggle) return false;
  54. toggle.click();
  55. return true;
  56. },
  57. BOTS_DIALOG_TEXT,
  58. BOT_NAME,
  59. );
  60. expect(enabled).toBe(true);
  61. await waitForBoundConnectionError();
  62. // v3 是当前唯一写入文件;真实 UI 保存后仍保留绑定,不能写回旧版 strict Config。
  63. const persisted = JSON.parse(
  64. await readFile(join(getE2EAppDataPaths().appDataDir, "bot-config.v3.json"), "utf8"),
  65. );
  66. expect(persisted.version).toBe(3);
  67. expect(persisted.bots[0]).toMatchObject({
  68. enabled: true,
  69. providerUserId: "ou_bound_runtime_error_e2e",
  70. });
  71. const dialogText = await readBotsDialogText();
  72. expect(dialogText).toContain("飞书连接失败");
  73. expect(dialogText).toContain("连接中断");
  74. expect(dialogText).toContain("长连接数可能已达上限或飞书服务繁忙");
  75. expect(dialogText).toContain("若持续失败,可绑定新的机器人");
  76. expect(dialogText).toContain("解绑");
  77. expect(dialogText).not.toContain("已连通");
  78. const statusControlGap = await browser.execute(() => {
  79. const status = Array.from(document.querySelectorAll("span")).find(
  80. (element) => element.textContent?.trim() === "连接中断",
  81. );
  82. const unbind = Array.from(document.querySelectorAll("button")).find((element) =>
  83. element.textContent?.includes("解绑"),
  84. );
  85. if (!status || !unbind) return null;
  86. return unbind.getBoundingClientRect().left - status.getBoundingClientRect().right;
  87. });
  88. expect(statusControlGap).not.toBeNull();
  89. expect(statusControlGap ?? -1).toBeGreaterThanOrEqual(0);
  90. const artifactDir = process.env.ZCODE_E2E_ARTIFACT_DIR?.trim();
  91. if (artifactDir) {
  92. await browser.saveScreenshot(join(artifactDir, "BOT-E2E-CF-01-feishu-runtime-error.png"));
  93. }
  94. });
  95. });
  96. async function openBotsDialog() {
  97. const remoteControl = await $('button[aria-label="移动端远程控制"]');
  98. await remoteControl.waitForClickable({ timeout: 20_000 });
  99. await remoteControl.click();
  100. const openBots = await $('[data-testid="web-remote-control-open-bots"]');
  101. await openBots.waitForClickable({ timeout: 20_000 });
  102. await openBots.click();
  103. }
  104. async function waitForBoundConnectionError() {
  105. await browser.waitUntil(
  106. // 新增运行状态轮询应更新已打开的弹窗;关闭重开会掩盖后台刷新漏接。
  107. () => botsDialogIncludes("连接中断"),
  108. {
  109. interval: 500,
  110. timeout: 20_000,
  111. timeoutMsg: "Feishu runtime error 没有投影为已绑定连接中断状态",
  112. },
  113. );
  114. }
  115. async function botsDialogIncludes(expected: string) {
  116. return (await readBotsDialogText()).includes(expected);
  117. }
  118. async function readBotsDialogText() {
  119. return browser.execute(
  120. (dialogText, botName) => {
  121. const dialog = Array.from(document.querySelectorAll<HTMLElement>("[role=dialog]")).find(
  122. (candidate) =>
  123. candidate.textContent?.includes(dialogText) && candidate.textContent.includes(botName),
  124. );
  125. return dialog?.textContent ?? "";
  126. },
  127. BOTS_DIALOG_TEXT,
  128. BOT_NAME,
  129. ) as Promise<string>;
  130. }