browserUseTabIcon.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /** @vitest-environment jsdom */
  2. import { readFileSync } from "node:fs";
  3. import { resolve } from "node:path";
  4. import { createElement } from "react";
  5. import { act, fireEvent, render } from "@testing-library/react";
  6. import { afterEach, describe, expect, it, vi } from "vitest";
  7. import { BrowserUseTabIcon } from "@/app-shell/BrowserUseTabIcon.js";
  8. import { BROWSER_USE_OPERATION_INDICATOR_DURATION_MS } from "@/lib/workspaceSidePane.js";
  9. afterEach(() => {
  10. vi.useRealTimers();
  11. });
  12. describe("BrowserUseTabIcon", () => {
  13. it("keeps the mouse indicator for five seconds and then restores the favicon", () => {
  14. vi.useFakeTimers();
  15. vi.setSystemTime(new Date("2026-07-13T00:00:00.000Z"));
  16. const operationUntil = Date.now() + BROWSER_USE_OPERATION_INDICATOR_DURATION_MS;
  17. const view = render(
  18. createElement(BrowserUseTabIcon, {
  19. tab: {
  20. id: "browser-use:tab-1",
  21. type: "browser-use",
  22. sessionId: "sess-1",
  23. tabId: "tab-1",
  24. faviconUrl: "https://example.com/favicon.ico",
  25. browserUseOperationUntil: operationUntil,
  26. },
  27. }),
  28. );
  29. const indicator = view.container.querySelector(
  30. '[data-browser-use-operation-indicator="active"]',
  31. );
  32. expect(indicator).not.toBeNull();
  33. expect(indicator?.classList.contains("browser-use-operation-breathe")).toBe(true);
  34. expect(
  35. indicator?.querySelector("svg")?.classList.contains("browser-use-operation-breathe"),
  36. ).toBe(false);
  37. expect(view.container.querySelector("img")).toBeNull();
  38. act(() => vi.advanceTimersByTime(BROWSER_USE_OPERATION_INDICATOR_DURATION_MS - 1));
  39. expect(
  40. view.container.querySelector('[data-browser-use-operation-indicator="active"]'),
  41. ).not.toBeNull();
  42. act(() => vi.advanceTimersByTime(1));
  43. expect(view.container.querySelector("[data-browser-use-operation-indicator]")).toBeNull();
  44. const favicon = view.container.querySelector("img");
  45. expect(favicon?.getAttribute("src")).toBe("https://example.com/favicon.ico");
  46. expect(favicon?.getAttribute("referrerpolicy")).toBe("no-referrer");
  47. fireEvent.error(favicon as HTMLImageElement);
  48. expect(view.container.querySelector("img")).toBeNull();
  49. expect(view.container.querySelector("svg")).not.toBeNull();
  50. });
  51. it("defines a visible wrapper animation with a reduced-motion fallback", () => {
  52. // Bugfix:下面断言里有跨行片段(`.browser-use-operation-breathe {\n animation: none;`),
  53. // 而仓库只对 *.mjs / *.sh 声明了 `eol=lf`,styles.css 在 `core.autocrlf=true` 的 Windows
  54. // 检出里是 CRLF,`toContain` 的 `\n` 匹配不到 `\r\n`,用例在 Windows 上必然失败。
  55. // 它想断言的是样式表内容契约,与换行字节无关,统一归一成 LF 再比对。
  56. const styles = readFileSync(
  57. resolve(process.cwd(), "packages/ui/src/styles.css"),
  58. "utf8",
  59. ).replaceAll("\r\n", "\n");
  60. expect(styles).toContain("@keyframes browser-use-operation-breathe");
  61. expect(styles).toContain("animation: browser-use-operation-breathe 900ms ease-in-out infinite");
  62. expect(styles).toContain("transform: scale(1.08)");
  63. expect(styles).toContain("prefers-reduced-motion: reduce");
  64. expect(styles).toContain(".browser-use-operation-breathe {\n animation: none;");
  65. expect(styles).toContain('.browser-use-viewport[data-browser-resize-dimmed="true"]');
  66. expect(styles).toContain("filter: brightness(0.88)");
  67. expect(styles).toContain("transition: filter 160ms ease-out");
  68. expect(styles).toContain(".browser-use-viewport {\n transition: none;");
  69. });
  70. });