/**
 * WorkspaceList 组件单元测试
 */
import { render, screen, waitFor } from '@testing-library/react';
import { describe, it, expect, vi } from 'vitest';
import { BrowserRouter } from 'react-router-dom';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import WorkspaceList from '@/pages/workspace/WorkspaceList';
import * as workspaceApi from '@/api/workspace';

vi.mock('@/api/workspace');

const queryClient = new QueryClient({
  defaultOptions: {
    queries: { retry: false },
  },
});

const renderWorkspaceList = () => {
  return render(
    <QueryClientProvider client={queryClient}>
      <BrowserRouter>
        <WorkspaceList />
      </BrowserRouter>
    </QueryClientProvider>
  );
};

describe('WorkspaceList', () => {
  it('should render workspace list', async () => {
    const mockWorkspaces = [
      {
        id: '1',
        name: 'Test Workspace',
        subscription_plan: 'free',
        subscription_status: 'active',
        created_at: '2024-01-01',
      },
    ];

    vi.mocked(workspaceApi.getWorkspaces).mockResolvedValue({
      items: mockWorkspaces,
      total: 1,
      page: 1,
      page_size: 10,
    });

    renderWorkspaceList();

    await waitFor(() => {
      expect(screen.getByText('Test Workspace')).toBeInTheDocument();
    });
  });

  it('should show create workspace button', () => {
    renderWorkspaceList();
    
    expect(screen.getByText('创建工作空间')).toBeInTheDocument();
  });
});
