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

vi.mock('../../../api', () => ({
  getListings: vi.fn(),
  collectListings: vi.fn(),
  getListingsCollectStatus: vi.fn(),
}));
import { getListings, getListingsCollectStatus } from '../../../api';
import ListingsTab from './ListingsTab.jsx';

beforeEach(() => {
  vi.clearAllMocks();
  getListingsCollectStatus.mockResolvedValue({ status: 'never_run' });
});

describe('ListingsTab', () => {
  it('빈 목록이면 안내 문구', async () => {
    getListings.mockResolvedValue({ listings: [] });
    render(<ListingsTab />);
    await waitFor(() => expect(screen.getByText(/매물이 없습니다/)).toBeInTheDocument());
  });

  it('매물+판정 tier·배열 필드를 크래시 없이 렌더 (매매=valuation, 배열 그대로)', async () => {
    getListings.mockResolvedValue({ listings: [{
      id: 1, complex_name: '상도더샵', dong: '상도동', deal_type: '매매',
      price: 95000, valuation_tier: '고가', ratio: 1.08,
      regulation_flags: ['토지거래허가'], reasons: ['호가가 최근 실거래 대비 8% 높음'],
    }] });
    render(<ListingsTab />);
    await waitFor(() => expect(screen.getByText('상도더샵')).toBeInTheDocument());
    expect(screen.getByText(/고가/)).toBeInTheDocument();
    expect(screen.getByText('토지거래허가')).toBeInTheDocument();
    expect(screen.getByText(/호가가 최근 실거래/)).toBeInTheDocument();
  });
});
