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

vi.mock('../../api', () => ({ getNodeStatus: vi.fn() }));

const sample = {
    redis_ok: true,
    paused: false,
    paused_reason: null,
    workers: [
        { name: 'image-render', kind: 'render', alive: true, state: 'idle', queue_depth: 0, dead_letter: 0, processing: 0, jobs_done: 5, jobs_failed: 0, last_beat_age_s: 3 },
        { name: 'insta-render', kind: 'render', alive: false, state: null, queue_depth: 3, dead_letter: 0, processing: 0, jobs_done: 0, jobs_failed: 0, last_beat_age_s: null },
    ],
    links: [],
};

describe('InfraMonitor', () => {
    beforeEach(() => vi.clearAllMocks());

    it('renders worker cards from /nodes (grid mode in jsdom — no WebGL)', async () => {
        getNodeStatus.mockResolvedValue(sample);
        render(<InfraMonitor />);
        await waitFor(() => expect(screen.getByText('Image Render')).toBeInTheDocument());
        expect(screen.getByText('Insta Render')).toBeInTheDocument();
        // alive 워커(image-render, idle)는 '대기' 상태 라벨
        expect(screen.getByText('대기')).toBeInTheDocument();
        // 오프라인 워커(insta-render)는 '오프라인' 라벨
        expect(screen.getByText('오프라인')).toBeInTheDocument();
    });

    it('shows error state when /nodes fails', async () => {
        getNodeStatus.mockRejectedValue(new Error('down'));
        render(<InfraMonitor />);
        await waitFor(() => expect(screen.getByText('집계 서버 연결 끊김')).toBeInTheDocument());
    });
});
