permissionInteractionId.test.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { runInNewContext } from "node:vm";
  2. import { describe, expect, it } from "vitest";
  3. import {
  4. PERMISSION_INTERACTION_PREFIX,
  5. readPermissionInteractionIdsInBrowser,
  6. } from "./e2e/helpers/permission-interaction-id.js";
  7. function readIds(entries: Array<{ id: string; visible: boolean }>): string[] {
  8. const nodes = entries.map(({ id, visible }) => ({
  9. dataset: { testid: id },
  10. getClientRects: () => (visible ? [1] : []),
  11. getAttribute: (name: string) => (name === "data-testid" ? id : null),
  12. }));
  13. return runInNewContext(`(${readPermissionInteractionIdsInBrowser.toString()})(prefix)`, {
  14. prefix: PERMISSION_INTERACTION_PREFIX,
  15. __name: (value: unknown) => value,
  16. document: {
  17. querySelector: () => nodes.find((node) => node.dataset.testid.startsWith("v4-permission")),
  18. querySelectorAll: () => nodes,
  19. },
  20. });
  21. }
  22. describe("权限恢复的真实请求身份", () => {
  23. it("同一对话框中的不同 interaction 不能误判为同一个请求", () => {
  24. const generic = { id: "v4-permission-dialog", visible: true };
  25. expect(
  26. readIds([generic, { id: `${PERMISSION_INTERACTION_PREFIX}request-a`, visible: true }]),
  27. ).toEqual(["request-a"]);
  28. expect(
  29. readIds([generic, { id: `${PERMISSION_INTERACTION_PREFIX}request-b`, visible: true }]),
  30. ).toEqual(["request-b"]);
  31. });
  32. it("通用 ID、隐藏的旧请求和普通工具摘要都不能补充请求身份", () => {
  33. expect(
  34. readIds([
  35. { id: "v4-permission-dialog", visible: true },
  36. { id: `${PERMISSION_INTERACTION_PREFIX}old`, visible: false },
  37. { id: "tool-summary-trigger-normal-tool", visible: true },
  38. ]),
  39. ).toEqual([]);
  40. });
  41. it("重复摘要去重,多个不同可见请求保留以拒绝歧义", () => {
  42. expect(
  43. readIds([
  44. { id: `${PERMISSION_INTERACTION_PREFIX}a`, visible: true },
  45. { id: `${PERMISSION_INTERACTION_PREFIX}a`, visible: true },
  46. { id: `${PERMISSION_INTERACTION_PREFIX}b`, visible: true },
  47. ]),
  48. ).toEqual(["a", "b"]);
  49. });
  50. });