desktopAppLexicalInputAdmission.test.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // @vitest-environment jsdom
  2. import { afterEach, describe, expect, it, vi } from "vitest";
  3. import { setInputValueByTestIdDom } from "#e2e/helpers/desktop-app";
  4. afterEach(() => {
  5. document.body.replaceChildren();
  6. vi.unstubAllGlobals();
  7. });
  8. function mountInput(unlockAfterProbe: boolean) {
  9. const input = document.createElement("div");
  10. input.dataset.testid = "v4-composer-input";
  11. input.setAttribute("contenteditable", "false");
  12. input.setAttribute("data-e2e-lexical-bridge", "ready");
  13. // jsdom 不实现该浏览器属性;根据真实 DOM 属性模拟可编辑状态。
  14. Object.defineProperty(input, "isContentEditable", {
  15. get: () => input.getAttribute("contenteditable") === "true",
  16. });
  17. const writes: Array<{ editable: boolean; text: string }> = [];
  18. const focus = vi.fn();
  19. Object.assign(input, {
  20. __zcodeLexicalInputE2E: {
  21. focus,
  22. getText: () => input.textContent ?? "",
  23. setText: (text: string) => {
  24. writes.push({ editable: input.isContentEditable, text });
  25. input.textContent = text;
  26. },
  27. },
  28. });
  29. document.body.append(input);
  30. let lastResult: unknown;
  31. vi.stubGlobal("browser", {
  32. pause: vi.fn(async () => undefined),
  33. execute: async (script: (...args: unknown[]) => unknown, ...args: unknown[]) => {
  34. lastResult = Reflect.apply(script, undefined, args);
  35. return lastResult;
  36. },
  37. waitUntil: async (predicate: () => Promise<boolean>) => {
  38. for (let turn = 0; turn < 4; turn++) {
  39. if (await predicate()) return;
  40. // 用显式 DOM 状态变化模拟连接完成,不依赖延时,也不替测试重写输入。
  41. if (
  42. unlockAfterProbe &&
  43. typeof lastResult === "object" &&
  44. lastResult !== null &&
  45. "reason" in lastResult &&
  46. lastResult.reason === "not-editable"
  47. ) {
  48. input.setAttribute("contenteditable", "true");
  49. }
  50. }
  51. throw new Error("input remains unavailable");
  52. },
  53. });
  54. return { input, writes, focus };
  55. }
  56. describe("desktop E2E Lexical 输入准入", () => {
  57. it("冷恢复期间不通过 bridge 写入禁用编辑器,恢复可编辑后只写一次", async () => {
  58. const { input, writes, focus } = mountInput(true);
  59. await setInputValueByTestIdDom("v4-composer-input", "E2E_COLD_RESUME_INPUT");
  60. expect(writes).toEqual([{ editable: true, text: "E2E_COLD_RESUME_INPUT" }]);
  61. expect(focus).toHaveBeenCalledOnce();
  62. expect(input.textContent).toBe("E2E_COLD_RESUME_INPUT");
  63. });
  64. it("持续禁用时明确失败,不改变用户草稿或调用 bridge", async () => {
  65. const { input, writes, focus } = mountInput(false);
  66. input.textContent = "saved draft";
  67. await expect(setInputValueByTestIdDom("v4-composer-input", "replacement")).rejects.toThrow(
  68. "not-editable",
  69. );
  70. expect(writes).toEqual([]);
  71. expect(focus).not.toHaveBeenCalled();
  72. expect(input.textContent).toBe("saved draft");
  73. });
  74. });