// @vitest-environment jsdom
/**
 * Component tests for SkillsQuotaForm (settings — skill store quotas).
 *
 * i18n is NOT initialized in the test env, so t() returns the raw key; we
 * assert on roles/values, not labels.
 */
import '../../test/dom-setup';
import { describe, it, expect, vi } from 'vitest';
import { screen, fireEvent } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { renderWithProviders, renderStatefulForm } from '../../test/render-helpers';
import { SkillsQuotaForm } from './SkillsQuotaForm';

describe('SkillsQuotaForm', () => {
  it('renders all five quota fields with defaults', () => {
    const onChange = vi.fn();
    renderWithProviders(<SkillsQuotaForm config={{ skills: {} }} onChange={onChange} overriddenByEnv={{}} />);
    const numbers = screen.getAllByRole('spinbutton') as HTMLInputElement[];
    expect(numbers).toHaveLength(5);
    // Defaults: 50, 64, 5, 100, 2000
    expect(numbers[0]).toHaveValue(50);
    expect(numbers[1]).toHaveValue(64);
    expect(numbers[2]).toHaveValue(5);
    expect(numbers[3]).toHaveValue(100);
    expect(numbers[4]).toHaveValue(2000);
  });

  it('writes skills.maxPerUser as a Number', async () => {
    const onChange = vi.fn();
    const { getConfig } = renderStatefulForm(SkillsQuotaForm, { skills: { maxPerUser: 50 } }, { onChangeSpy: onChange });
    const numbers = screen.getAllByRole('spinbutton');
    fireEvent.change(numbers[0], { target: { value: '12' } });
    expect(getConfig().skills.maxPerUser).toBe(12);
    expect(onChange).toHaveBeenCalledWith('skills.maxPerUser', 12);
  });

  it('serializes a cleared quota as undefined', async () => {
    const onChange = vi.fn();
    renderWithProviders(<SkillsQuotaForm config={{ skills: { maxIndexChars: 2000 } }} onChange={onChange} overriddenByEnv={{}} />);
    const numbers = screen.getAllByRole('spinbutton');
    await userEvent.clear(numbers[4]);
    expect(onChange).toHaveBeenLastCalledWith('skills.maxIndexChars', undefined);
  });
});
