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

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