// Vitest 测试环境初始化
// 依据：vitest.config.ts setupFiles

import { beforeEach } from "vitest";
import "@testing-library/jest-dom/vitest";

// jsdom 环境补充：matchMedia（recharts 等库依赖）
if (!window.matchMedia) {
  Object.defineProperty(window, "matchMedia", {
    writable: true,
    value: (query: string): MediaQueryList => ({
      matches: false,
      media: query,
      onchange: null,
      addListener: () => {},
      removeListener: () => {},
      addEventListener: () => {},
      removeEventListener: () => {},
      dispatchEvent: () => false,
    }),
  });
}

// jsdom 环境补充：IntersectionObserver
if (!window.IntersectionObserver) {
  class MockIntersectionObserver {
    observe() {}
    unobserve() {}
    disconnect() {}
    takeRecords() {
      return [];
    }
  }
  Object.defineProperty(window, "IntersectionObserver", {
    writable: true,
    value: MockIntersectionObserver,
  });
}

// jsdom 环境补充：ResizeObserver（recharts 依赖）
if (!window.ResizeObserver) {
  class MockResizeObserver {
    observe() {}
    unobserve() {}
    disconnect() {}
  }
  Object.defineProperty(window, "ResizeObserver", {
    writable: true,
    value: MockResizeObserver,
  });
}

// 清理 localStorage
beforeEach(() => {
  localStorage.clear();
  sessionStorage.clear();
});
