// @vitest-environment jsdom
import '../../test/dom-setup';
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { render, screen } from '@testing-library/react';
import type { WorkerStatusRow } from '../../api';

// react-i18next: pass keys through (we only assert on the dynamic data).
vi.mock('react-i18next', () => ({
  useTranslation: () => ({ t: (key: string) => key }),
}));

// Pet hooks are irrelevant here — stub to the "no pet" path so the dot renders.
vi.mock('../../hooks/useActivePet', () => ({ useActivePet: () => ({ data: null }) }));
vi.mock('../../hooks/usePetFrameAnalysis', () => ({ usePetFrameAnalysis: () => 0 }));

const workersMock = vi.fn();
vi.mock('../../hooks/useWorkerStatus', () => ({
  useWorkerStatus: () => workersMock(),
}));

import { WorkerStatusWidget } from './WorkerStatusWidget';

function row(over: Partial<WorkerStatusRow>): WorkerStatusRow {
  return { id: 'w1', name: 'w1', roles: ['task'], state: 'running', proxy: false, ...over };
}

describe('WorkerStatusWidget — occupants (admin)', () => {
  beforeEach(() => workersMock.mockReset());

  it('renders occupant names, with a kind badge only for non-agent jobs', () => {
    workersMock.mockReturnValue({
      workers: [row({ occupants: [{ user: 'Alice', kind: 'agent' }, { user: 'Bob', kind: 'reflection' }] })],
      isLoading: false,
      isError: false,
    });
    render(<WorkerStatusWidget />);
    const el = screen.getByTestId('worker-users-w1');
    expect(el.textContent).toContain('Alice');
    expect(el.textContent).toContain('Bob');
    // The reflection job surfaces a kind badge; the normal 'agent' job does not.
    const badge = screen.getByTestId('worker-kind-w1');
    expect(badge.textContent).toBe('reflection');
    expect(el.textContent).not.toContain('agent');
  });

  it('renders no occupant line when occupants is absent (non-admin payload)', () => {
    workersMock.mockReturnValue({
      workers: [row({ occupants: undefined })],
      isLoading: false,
      isError: false,
    });
    render(<WorkerStatusWidget />);
    expect(screen.queryByTestId('worker-users-w1')).toBeNull();
  });
});
