import React from 'react';
import { describe, expect, it, vi, beforeEach } from 'vitest';
import { render, screen, waitFor } from '@testing-library/react';

vi.mock('@/lib/api/auth', () => ({
  listSessions: vi.fn(),
  revokeSession: vi.fn(),
}));

vi.mock('@/store/authStore', () => ({
  useAuthStore: vi.fn(),
}));

import { listSessions } from '@/lib/api/auth';
import { useAuthStore } from '@/store/authStore';
import { SessionsTab } from './SessionsTab';

describe('SessionsTab', () => {
  beforeEach(() => {
    vi.clearAllMocks();
    (useAuthStore as any).mockImplementation((selector: any) =>
      selector({
        logout: vi.fn(),
        session: { id: 'session-1' },
      })
    );
  });

  it('renders current and other sessions from API', async () => {
    (listSessions as any).mockResolvedValue({
      items: [
        {
          id: 'session-1',
          status: 'active',
          current: true,
          createdAt: Date.now(),
          updatedAt: Date.now(),
          expiresAt: Date.now() + 1000,
          deviceName: 'MacBook Pro',
          userAgent: 'Mozilla/5.0',
          ipAddress: '127.0.0.1',
        },
        {
          id: 'session-2',
          status: 'active',
          current: false,
          createdAt: Date.now(),
          updatedAt: Date.now(),
          expiresAt: Date.now() + 1000,
          deviceName: 'iPhone',
          userAgent: 'Mobile Safari',
          ipAddress: '10.0.0.2',
        },
      ],
      total: 2,
    });

    render(<SessionsTab />);

    await waitFor(() => {
      expect(screen.getByText('MacBook Pro')).toBeInTheDocument();
    });

    expect(screen.getByText('当前设备')).toBeInTheDocument();
    expect(screen.getByText('iPhone')).toBeInTheDocument();
    expect(screen.getByText('撤销会话')).toBeInTheDocument();
  });
});
