Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

4.6 KB
Raw Blame
import { describe, it, expect, vi } from 'vitest';
import { render, screen, fireEvent } from '@testing-library/react';
import ImageGenSettingsForm from './ImageGenSettingsForm';

// Stub the heavy child pickers so the test pins ONLY this form's own
// responsibility: preserving every control + arranging them flat vs. grouped.
vi.mock('../media/BackendChipStrip', () => ({
  default: () => <div data-testid="backend-chip-strip" />,
}));
vi.mock('./ImageGenControls', () => ({
  default: () => <div data-testid="image-gen-controls" />,
}));
vi.mock('./LoraPicker', () => ({
  default: () => <div data-testid="lora-picker" />,
}));
vi.mock('../media/StylePresetPicker', () => ({
  default: () => <div data-testid="style-preset-picker" />,
}));

const BACKENDS = [{ id: 'local', label: 'Local' }];
const SECTION_TITLES = ['Backend', 'Model & LoRA', 'Prompts & Style'];

const renderForm = (props) =>
  render(<ImageGenSettingsForm value={{ mode: 'local' }} onChange={vi.fn()} {...props} />);

describe('ImageGenSettingsForm layout', () => {
  it('offers removal of retained LoRAs when a text-only model replaces the prior selection', () => {
    const onChange = vi.fn();
    renderForm({ value: { mode: 'local', modelId: 'moss', loras: [{ filename: 'example.safetensors' }] },
      models: [{ id: 'moss', supportsLoras: false }], showLoras: true, onChange });
    expect(screen.queryByTestId('lora-picker')).toBeNull();
    fireEvent.click(screen.getByRole('button', { name: 'Remove selected LoRAs' }));
    expect(onChange).toHaveBeenCalledWith({ mode: 'local', modelId: 'moss', loras: [] });
  });
  it('flat mode (default) renders no section headings but keeps all controls', () => {
    renderForm({ availableBackends: BACKENDS });
    SECTION_TITLES.forEach((title) => expect(screen.queryByRole('heading', { name: title })).toBeNull());
    expect(screen.getByTestId('backend-chip-strip')).toBeTruthy();
    expect(screen.getByTestId('image-gen-controls')).toBeTruthy();
    expect(screen.getByLabelText('Extra style (optional)')).toBeTruthy();
    expect(screen.getByLabelText('Negative prompt (optional)')).toBeTruthy();
  });

  it('grouped mode renders the three organized sections', () => {
    renderForm({ availableBackends: BACKENDS, grouped: true });
    SECTION_TITLES.forEach((title) =>
      expect(screen.getByRole('heading', { name: title })).toBeTruthy());
    // Every control from the flat layout is still present.
    expect(screen.getByTestId('backend-chip-strip')).toBeTruthy();
    expect(screen.getByTestId('image-gen-controls')).toBeTruthy();
    expect(screen.getByLabelText('Extra style (optional)')).toBeTruthy();
    expect(screen.getByLabelText('Negative prompt (optional)')).toBeTruthy();
  });

  it('grouped mode drops the Backend section (but not the warning) when no backend is configured', () => {
    renderForm({ availableBackends: [], grouped: true });
    expect(screen.queryByRole('heading', { name: 'Backend' })).toBeNull();
    expect(screen.getByText(/No image gen backend configured/)).toBeTruthy();
    // Model + Prompts sections still render.
    expect(screen.getByRole('heading', { name: 'Model & LoRA' })).toBeTruthy();
    expect(screen.getByRole('heading', { name: 'Prompts & Style' })).toBeTruthy();
  });

  it('grouped mode drops the Prompts & Style section when both style fields and preset are hidden', () => {
    renderForm({ availableBackends: BACKENDS, grouped: true, showStyleFields: false, showStylePreset: false });
    expect(screen.getByRole('heading', { name: 'Model & LoRA' })).toBeTruthy();
    expect(screen.queryByRole('heading', { name: 'Prompts & Style' })).toBeNull();
    expect(screen.queryByLabelText('Extra style (optional)')).toBeNull();
  });

  it('grouped mode files the LoRA picker under Model & LoRA and the style preset under Prompts & Style', () => {
    // Pickers only render in local mode; assert each lands in its named section
    // (not just "somewhere on the page") so the grouping is actually pinned.
    renderForm({ availableBackends: BACKENDS, grouped: true, showLoras: true, showStylePreset: true });
    const modelSection = screen.getByRole('heading', { name: 'Model & LoRA' }).closest('section');
    const promptSection = screen.getByRole('heading', { name: 'Prompts & Style' }).closest('section');
    expect(modelSection).toContainElement(screen.getByTestId('lora-picker'));
    expect(modelSection).toContainElement(screen.getByTestId('image-gen-controls'));
    expect(promptSection).toContainElement(screen.getByTestId('style-preset-picker'));
    expect(promptSection).toContainElement(screen.getByLabelText('Negative prompt (optional)'));
  });
});