import '@testing-library/jest-dom';
import { vi, afterEach } from 'vitest';

// 模拟完整的 localStorage
const localStorageMock = {
  getItem: vi.fn(),
  setItem: vi.fn(),
  removeItem: vi.fn(),
  clear: vi.fn(),
};

Object.defineProperty(window, 'localStorage', {
  value: localStorageMock,
});

// 模拟 Tauri API
vi.mock('@tauri-apps/api/core', () => ({
  invoke: vi.fn(),
}));

// 模拟 window.__TAURI_INTERNALS__
Object.defineProperty(window, '__TAURI_INTERNALS__', {
  value: {},
  writable: true,
});

// 模拟 EventSource（SSE 连接在 authStore.login 中使用）
class MockEventSource {
  onmessage: ((event: MessageEvent) => void) | null = null;
  onerror: (() => void) | null = null;
  close() {}
}
Object.defineProperty(window, 'EventSource', {
  value: MockEventSource,
  writable: true,
});

// 清理 mocks 每个测试后
afterEach(() => {
  vi.clearAllMocks();
});
