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

26.6 KB
Raw Blame
import { describe, it, expect, vi, afterEach } from 'vitest';
import { render, screen, fireEvent, cleanup, act } from '@testing-library/react';

// The two hooks are the component's only API callers — stub them so the test
// exercises the config controls, not the network.
vi.mock('../../../../hooks/useCodeReviewDefaults', () => ({
  useCodeReviewDefaults: () => ({
    reviewers: ['copilot'],
    usernames: [],
    optionalReviewers: [],
    reviewerMaxRounds: {},
    stopMode: 'clean',
    reviewerApplies: false,
  }),
}));
vi.mock('../../../../hooks/useReviewerModelOptions', () => ({
  default: () => ({ optionsByReviewer: {}, freeText: {}, unavailable: {}, loaded: true }),
}));
vi.mock('../../ReviewerPicker', () => ({
  default: () => <div data-testid="reviewer-picker" />,
}));

import GlobalConfigControls from './GlobalConfigControls';

const BASE_CONFIG = {
  type: 'cron',
  cronExpression: '0 7 * * *',
  enabled: true,
  providerId: null,
  model: null,
  effort: null,
  prompt: 'do the thing',
  status: {},
};

// The real `onUpdate` (ScheduleTab's handleUpdateTask) is async, and several
// handlers here attach a rejection handler to what it returns — so the default
// mock must resolve a promise, not `undefined`.
function renderControls({ taskMetadata, onUpdate = vi.fn(async () => {}), taskType = 'feature-ideas', config: extraConfig = {}, setUpdating = () => {}, providers = [] } = {}) {
  render(
    <GlobalConfigControls
      taskType={taskType}
      config={{ ...BASE_CONFIG, taskMetadata, ...extraConfig }}
      onUpdate={onUpdate}
      onTrigger={() => {}}
      providers={providers}
      apps={[]}
      updating={false}
      setUpdating={setUpdating}
      allTaskTypes={['feature-ideas']}
    />
  );
  return onUpdate;
}

const prSelect = () => screen.queryByLabelText('After opening PR');

afterEach(() => {
  cleanup();
  vi.clearAllMocks();
});

describe('GlobalConfigControls — After opening PR', () => {
  it('hides the selector when the task does not open a PR', () => {
    renderControls({ taskMetadata: { useWorktree: true, openPR: false } });
    expect(prSelect()).not.toBeInTheDocument();
  });

  it('defaults an unpinned task to the app-default option', () => {
    renderControls({ taskMetadata: { useWorktree: true, openPR: true } });
    expect(prSelect()).toHaveValue('');
    expect(screen.getByTestId('reviewer-picker')).toBeInTheDocument();
  });

  it('shows a legacy reviewLoop task the policy it actually runs under', () => {
    renderControls({ taskMetadata: { useWorktree: true, openPR: true, reviewLoop: true } });
    expect(prSelect()).toHaveValue('review-then-merge');
  });

  // Keeps unrelated keys, and drops the legacy reviewLoop bit so it can't
  // outvote the pin the user just made.
  it('persists the picked policy into taskMetadata', () => {
    const onUpdate = renderControls({ taskMetadata: { useWorktree: true, openPR: true, simplify: true, reviewLoop: true } });
    fireEvent.change(prSelect(), { target: { value: 'merge-on-green' } });
    expect(onUpdate).toHaveBeenCalledWith('feature-ideas', {
      taskMetadata: { useWorktree: true, openPR: true, simplify: true, prCompletion: 'merge-on-green' },
    });
  });

  it('clears the pin (back to the app default) when App default is picked', () => {
    const onUpdate = renderControls({ taskMetadata: { useWorktree: true, openPR: true, prCompletion: 'leave-open' } });
    expect(prSelect()).toHaveValue('leave-open');
    fireEvent.change(prSelect(), { target: { value: '' } });
    expect(onUpdate).toHaveBeenCalledWith('feature-ideas', {
      taskMetadata: { useWorktree: true, openPR: true },
    });
  });

  it.each(['merge-on-green', 'leave-open'])('hides the reviewer picker for %s', (prCompletion) => {
    renderControls({ taskMetadata: { useWorktree: true, openPR: true, prCompletion } });
    expect(screen.queryByTestId('reviewer-picker')).not.toBeInTheDocument();
  });

  it('keeps the reviewer picker for review-then-merge', () => {
    renderControls({ taskMetadata: { useWorktree: true, openPR: true, prCompletion: 'review-then-merge' } });
    expect(screen.getByTestId('reviewer-picker')).toBeInTheDocument();
  });

  it('keeps the reviewer picker for a legacy reviewLoop task that opens no PR', () => {
    renderControls({ taskMetadata: { useWorktree: true, openPR: false, reviewLoop: true } });
    expect(screen.getByTestId('reviewer-picker')).toBeInTheDocument();
  });

  it('keeps the reviewer picker for a claim flow, whose shipped metadata sets neither flag', () => {
    // A claim PROMPT opens and merges its own PR and runs the reviewers itself,
    // so the resolved list is operative even though `openPR` and `reviewLoop` are
    // both false — which is exactly the shipped `claim-work` metadata. Hiding the
    // picker here leaves a reviewer override that every claim obeys with no
    // control anywhere that can clear it, while the claim surfaces tell the user
    // to come here and do precisely that.
    renderControls({ taskType: 'claim-work', taskMetadata: { useWorktree: false, openPR: false, claimFlow: true } });
    expect(screen.getByTestId('reviewer-picker')).toBeInTheDocument();
  });

  it('explains that a scheduled task reviewer override can fall back to system tiers', () => {
    renderControls({
      taskMetadata: {
        useWorktree: true,
        openPR: true,
        prCompletion: 'review-then-merge',
        reviewers: ['claude'],
      },
    });
    expect(screen.getByText(/This task’s reviewer list is its preferred tier.*system Code Review Defaults tier/)).toBeInTheDocument();
  });

  it('resets the task review override while preserving unrelated task metadata', () => {
    const onUpdate = renderControls({
      taskMetadata: {
        useWorktree: true,
        openPR: true,
        prCompletion: 'review-then-merge',
        reviewers: ['codex'],
        usernames: ['example-reviewer'],
        optionalReviewers: ['codex'],
        reviewerMaxRounds: { codex: 2 },
        reviewerModels: { codex: 'example-model' },
        reviewerEfforts: { codex: 'high' },
        reviewStopMode: 'first-blocking',
        reviewerApplies: true,
      },
    });

    fireEvent.click(screen.getByRole('button', { name: 'Use system Code Review Defaults' }));

    expect(onUpdate).toHaveBeenCalledWith('feature-ideas', {
      taskMetadata: {
        useWorktree: true,
        openPR: true,
        prCompletion: 'review-then-merge',
      },
    });
  });
});

describe('GlobalConfigControls — branch-reconcile batch size', () => {
  it('shows the safe default and persists a selected branch batch', () => {
    const onUpdate = renderControls({ taskType: 'branch-reconcile', taskMetadata: { cleanupMerged: true } });
    const select = screen.getByLabelText('Branches per agent');
    expect(select).toHaveValue('3');
    fireEvent.change(select, { target: { value: '5' } });
    expect(onUpdate).toHaveBeenCalledWith('branch-reconcile', {
      taskMetadata: { cleanupMerged: true, branchesPerAgent: 5 }
    });
  });
});

describe('GlobalConfigControls — error-log-triage run caps', () => {
  it('shows the shipped caps and persists a changed cap beside the other one', () => {
    const onUpdate = renderControls({ taskType: 'error-log-triage', taskMetadata: { triageMaxInvestigations: 2 } });
    expect(screen.getByLabelText('Occurrences before investigating')).toHaveValue('3');
    fireEvent.change(screen.getByLabelText('Investigations per run'), { target: { value: '0' } });
    expect(onUpdate).toHaveBeenCalledWith('error-log-triage', {
      taskMetadata: { triageMaxInvestigations: 0 }
    });
  });
});

describe('GlobalConfigControls — dependency-freedom removal bar', () => {
  it('is hidden for other audits', () => {
    renderControls({ taskType: 'better-complexity' });
    expect(screen.queryByLabelText('Removal bar')).not.toBeInTheDocument();
  });

  it('shows the default bar and persists the extreme bar explicitly', () => {
    const onUpdate = renderControls({ taskType: 'better-dependency-freedom', taskMetadata: { fileIssues: true } });
    const select = screen.getByLabelText('Removal bar');
    expect(select).toHaveValue('default');
    fireEvent.change(select, { target: { value: 'extreme' } });
    expect(onUpdate).toHaveBeenCalledWith('better-dependency-freedom', {
      taskMetadata: { fileIssues: true, depfreeMode: 'extreme' }
    });
  });
});

describe('GlobalConfigControls — issue exclude labels', () => {
  it('is hidden for a task type without the issue author filter', () => {
    renderControls({ taskType: 'feature-ideas' });
    expect(screen.queryByLabelText('Leave issues with these labels for humans')).toBeNull();
  });

  it('seeds the input from the configured labels and commits a parsed list on blur', () => {
    const onUpdate = renderControls({
      taskType: 'claim-issue',
      taskMetadata: { issueAuthorFilter: 'self', issueExcludeLabels: ['good first issue'] },
      onUpdate: vi.fn().mockResolvedValue(undefined),
    });
    const input = screen.getByLabelText('Leave issues with these labels for humans');
    expect(input).toHaveValue('good first issue');

    fireEvent.change(input, { target: { value: 'good first issue, help wanted' } });
    fireEvent.blur(input);

    expect(onUpdate).toHaveBeenCalledWith('claim-issue', {
      taskMetadata: { issueAuthorFilter: 'self', issueExcludeLabels: ['good first issue', 'help wanted'] }
    });
  });

  it('commits an empty array when the input is cleared', () => {
    const onUpdate = renderControls({
      taskType: 'claim-work',
      taskMetadata: { issueExcludeLabels: ['good first issue'] },
      onUpdate: vi.fn().mockResolvedValue(undefined),
    });
    const input = screen.getByLabelText('Leave issues with these labels for humans');

    fireEvent.change(input, { target: { value: '' } });
    fireEvent.blur(input);

    expect(onUpdate).toHaveBeenCalledWith('claim-work', {
      taskMetadata: { issueExcludeLabels: [] }
    });
  });

  it('gates RunTaskButton on the in-flight save (setUpdating around the commit)', async () => {
    let resolveUpdate;
    const onUpdate = vi.fn(() => new Promise((resolve) => { resolveUpdate = resolve; }));
    const setUpdating = vi.fn();
    renderControls({
      taskType: 'claim-issue',
      taskMetadata: { issueExcludeLabels: [] },
      onUpdate,
      setUpdating,
    });
    const input = screen.getByLabelText('Leave issues with these labels for humans');

    fireEvent.change(input, { target: { value: 'good first issue' } });
    fireEvent.blur(input);

    // setUpdating(true) must fire BEFORE the PATCH resolves, not after — a
    // caller that gates RunTaskButton on it needs the disable to take effect
    // while onUpdate is still in flight.
    expect(setUpdating).toHaveBeenCalledWith(true);
    expect(setUpdating).not.toHaveBeenCalledWith(false);

    await act(async () => { resolveUpdate(); await Promise.resolve(); });
    expect(setUpdating).toHaveBeenLastCalledWith(false);
  });
});

describe('GlobalConfigControls — require approval', () => {
  it('toggles requireApproval on the task metadata', () => {
    const onUpdate = renderControls({ taskType: 'do-release', taskMetadata: { useWorktree: false, openPR: false } });
    fireEvent.click(screen.getByRole('button', { name: /Require approval/i }));
    expect(onUpdate).toHaveBeenCalledWith('do-release', {
      taskMetadata: { useWorktree: false, openPR: false, requireApproval: true },
    });
  });

  it('turns requireApproval off when it is already on', () => {
    const onUpdate = renderControls({
      taskType: 'do-release',
      taskMetadata: { requireApproval: true },
    });
    fireEvent.click(screen.getByRole('button', { name: /Require approval/i }));
    expect(onUpdate).toHaveBeenCalledWith('do-release', {
      taskMetadata: { requireApproval: false },
    });
  });
});

describe('GlobalConfigControls — release options', () => {
  it('is hidden for other tasks', () => {
    renderControls();
    expect(screen.queryByText('Release Options')).not.toBeInTheDocument();
  });

  it('shows options ON by default and writes an explicit false when turned off', () => {
    const onUpdate = renderControls({ taskType: 'do-release', taskMetadata: { slashdoCommand: 'release', resolveBlockers: false } });
    expect(screen.getByRole('button', { name: 'Disable merge dependency updates' })).toHaveAttribute('aria-pressed', 'true');
    fireEvent.click(screen.getByRole('button', { name: 'Disable merge dependency updates' }));
    expect(onUpdate).toHaveBeenCalledWith('do-release', {
      taskMetadata: { slashdoCommand: 'release', resolveBlockers: false, mergeDependencyUpdates: false },
    });
  });

  it('turns an explicitly disabled option back on', () => {
    const onUpdate = renderControls({ taskType: 'do-release', taskMetadata: { resolveBlockers: false } });
    fireEvent.click(screen.getByRole('button', { name: 'Enable resolve blockers' }));
    expect(onUpdate).toHaveBeenCalledWith('do-release', { taskMetadata: { resolveBlockers: true } });
  });
});

describe('GlobalConfigControls — stall nudge', () => {
  it('shows the nudge ON for a task that never set it and writes an explicit false when turned off', () => {
    const onUpdate = renderControls({ taskMetadata: { useWorktree: true } });
    expect(screen.getByRole('button', { name: 'Disable stall nudge' })).toHaveAttribute('aria-pressed', 'true');
    fireEvent.click(screen.getByRole('button', { name: 'Disable stall nudge' }));
    expect(onUpdate).toHaveBeenCalledWith('feature-ideas', { taskMetadata: { useWorktree: true, stallNudge: false } });
  });

  it('shows do-release with the nudge off and turns it back on', () => {
    const onUpdate = renderControls({ taskType: 'do-release', taskMetadata: { slashdoCommand: 'release', stallNudge: false } });
    expect(screen.getByRole('button', { name: 'Enable stall nudge' })).toHaveAttribute('aria-pressed', 'false');
    fireEvent.click(screen.getByRole('button', { name: 'Enable stall nudge' }));
    expect(onUpdate).toHaveBeenCalledWith('do-release', { taskMetadata: { slashdoCommand: 'release', stallNudge: true } });
  });
});

describe('GlobalConfigControls — file issues only', () => {
  it('is hidden for non-audit tasks', () => {
    renderControls();
    expect(screen.queryByRole('button', { name: /File issues only/i })).not.toBeInTheDocument();
  });

  it('toggles fileIssues and forces the no-code posture on', () => {
    const onUpdate = renderControls({
      taskType: 'security',
      taskMetadata: { useWorktree: true, openPR: true, simplify: true, fileIssues: false },
      config: { fileIssuesCapable: true, defaultFileIssues: false },
    });
    fireEvent.click(screen.getByRole('button', { name: /File issues only/i }));
    expect(onUpdate).toHaveBeenCalledWith('security', {
      taskMetadata: { useWorktree: false, openPR: false, simplify: false, fileIssues: true },
    });
  });

  it('restores required worktree isolation when module-hygiene switches to do-work mode', () => {
    const onUpdate = renderControls({
      taskType: 'module-hygiene',
      taskMetadata: { useWorktree: false, openPR: false, simplify: false, fileIssues: true },
      config: { fileIssuesCapable: true, defaultFileIssues: true, doWorkRequiresWorktree: true },
    });
    fireEvent.click(screen.getByRole('button', { name: /File issues only/i }));
    expect(onUpdate).toHaveBeenCalledWith('module-hygiene', {
      taskMetadata: { useWorktree: true, openPR: false, simplify: false, fileIssues: false },
    });
  });
});

describe('GlobalConfigControls — cadence + perpetual', () => {
  const cadenceSelect = () => screen.getByLabelText('Interval Type');

  it('offers exactly the two cadence variants', () => {
    renderControls();
    expect([...cadenceSelect().options].map((o) => o.value)).toEqual(['on-demand', 'cron']);
  });

  it('toggling Perpetual writes only the flag, leaving the cron cadence intact', async () => {
    const onUpdate = renderControls({ config: { perpetual: false } });
    await act(async () => {
      fireEvent.click(screen.getByLabelText('Enable perpetual drain'));
    });
    expect(onUpdate).toHaveBeenCalledWith('feature-ideas', { perpetual: true });
  });

  it('lets on-demand tasks drain manually without exposing a recheck timer', async () => {
    const onUpdate = renderControls({ config: { type: 'on-demand', perpetual: false } });
    await act(async () => fireEvent.click(screen.getByLabelText('Enable perpetual drain')));
    expect(onUpdate).toHaveBeenCalledWith('feature-ideas', { perpetual: true, autoStart: false });
    cleanup();
    renderControls({ config: { type: 'on-demand', perpetual: true, autoStart: false } });
    expect(screen.getByRole('option', { name: 'On Demand (manual trigger only)' }).selected).toBe(true);
    expect(screen.getByLabelText('Automatic starts and rechecks')).not.toBeChecked();
    expect(screen.queryByText('Recheck Cadence')).not.toBeInTheDocument();
    expect(screen.getByText(/No timer starts or resumes it/)).toBeInTheDocument();
  });

  it('explains automatic rechecks without claiming manual-only execution', () => {
    renderControls({ config: { type: 'on-demand', cronExpression: null, perpetual: true } });
    expect(screen.getByText('Recheck Cadence')).toBeInTheDocument();
    expect(screen.getByRole('option', { name: 'Automatic drain' }).selected).toBe(true);
    expect(screen.getByText(/Turn automatic starts off/)).toBeInTheDocument();
    expect(screen.queryByText('Only runs when manually triggered')).not.toBeInTheDocument();

    cleanup();
    renderControls({ config: { type: 'on-demand', cronExpression: null, perpetual: false, recheckCron: '0 9 * * *' } });
    expect(screen.getByRole('option', { name: 'On Demand (manual trigger only)' }).selected).toBe(true);
    expect(screen.queryByText('Recheck Cadence')).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: 'Save schedule' })).not.toBeInTheDocument();

    cleanup();
    // A cron+perpetual task rechecks on its OWN expression, so it needs none.
    renderControls({ config: { type: 'cron', cronExpression: '0 7 * * *', perpetual: true } });
    expect(screen.queryByText('Recheck Cadence')).not.toBeInTheDocument();
  });

  it('shows parked skip causes while leaving unattributed parks unchanged', () => {
    renderControls({
      config: {
        type: 'on-demand',
        perpetual: true,
        autoStart: true,
        status: { reason: 'perpetual-drain' },
        perpetualStatus: {
          globalParked: false,
          parkedAppCount: 1,
          trackedAppCount: 1,
          nextRecheckAt: null,
          parkReason: 'no-actionable-issues',
          parkCounts: { open: 88, inFlight: 2, filtered: 86 },
          parkSkipCauses: { 'needs-input': 49, blocked: 17 }
        }
      }
    });
    expect(screen.getByText('1 app(s) parked (no-actionable-issues) — 49 needs-input, 17 blocked')).toBeInTheDocument();

    cleanup();
    renderControls({
      config: {
        type: 'on-demand',
        perpetual: true,
        autoStart: true,
        status: { reason: 'perpetual-drain' },
        perpetualStatus: {
          globalParked: false,
          parkedAppCount: 1,
          trackedAppCount: 1,
          nextRecheckAt: null,
          parkReason: 'branch-reconcile'
        }
      }
    });
    expect(screen.getByText('1 app(s) parked (branch-reconcile)')).toBeInTheDocument();
  });
});

describe('GlobalConfigControls — failed saves', () => {
  it('rolls back a cadence change when onUpdate resolves false and releases the busy state', async () => {
    const onUpdate = vi.fn().mockResolvedValue(false);
    const setUpdating = vi.fn();
    renderControls({ config: { type: 'cron' }, onUpdate, setUpdating });

    await act(async () => fireEvent.change(screen.getByLabelText('Interval Type'), { target: { value: 'on-demand' } }));

    expect(screen.getByLabelText('Interval Type')).toHaveValue('cron');
    expect(setUpdating).toHaveBeenLastCalledWith(false);
  });

  it('keeps the cron draft open when onUpdate resolves false and releases the busy state', async () => {
    const onUpdate = vi.fn().mockResolvedValue(false);
    const setUpdating = vi.fn();
    renderControls({ onUpdate, setUpdating });
    const expression = screen.getByLabelText('Cron expression');
    fireEvent.change(expression, { target: { value: '15 8 * * *' } });

    await act(async () => fireEvent.click(screen.getByRole('button', { name: 'Save schedule' })));

    expect(screen.getByLabelText('Cron expression')).toHaveValue('15 8 * * *');
    expect(screen.getByText('Unsaved changes — save before closing')).toBeInTheDocument();
    expect(setUpdating).toHaveBeenLastCalledWith(false);
  });

  it('keeps a first cron draft visible when an on-demand task fails to save it', async () => {
    const onUpdate = vi.fn().mockResolvedValue(false);
    renderControls({ config: { type: 'on-demand' }, onUpdate });
    await act(async () => fireEvent.change(screen.getByLabelText('Interval Type'), { target: { value: 'cron' } }));
    const expression = screen.getByLabelText('Cron expression');
    fireEvent.change(expression, { target: { value: '15 8 * * *' } });

    await act(async () => fireEvent.click(screen.getByRole('button', { name: 'Save schedule' })));

    expect(screen.getByLabelText('Interval Type')).toHaveValue('cron');
    expect(screen.getByLabelText('Cron expression')).toHaveValue('15 8 * * *');
    expect(screen.getByText('Unsaved changes — save before closing')).toBeInTheDocument();
  });

  it('keeps the prompt draft open when onUpdate resolves false and releases the busy state', async () => {
    const onUpdate = vi.fn().mockResolvedValue(false);
    const setUpdating = vi.fn();
    renderControls({ onUpdate, setUpdating });
    fireEvent.click(screen.getByRole('button', { name: 'Edit prompt' }));
    const prompt = screen.getByLabelText('Task prompt');
    fireEvent.change(prompt, { target: { value: 'new prompt draft' } });

    await act(async () => fireEvent.click(screen.getByRole('button', { name: 'Save Prompt' })));

    expect(screen.getByLabelText('Task prompt')).toHaveValue('new prompt draft');
    expect(setUpdating).toHaveBeenLastCalledWith(false);
  });
});

// A stall (the forge CLI failing several evaluations in a row) can coexist
// with an apparently-healthy "Draining" state — the gate skips without
// parking on purpose (#7551) — so it must render regardless of park state.
describe('GlobalConfigControls — perpetual probe-failure stall', () => {
  it('renders the redacted probe-failure diagnostic alongside a still-draining status', () => {
    renderControls({
      config: {
        type: 'on-demand',
        cronExpression: null,
        perpetual: true,
        autoStart: true,
        perpetualStatus: {
          globalParked: false,
          parkedAppCount: 0,
          trackedAppCount: 1,
          nextRecheckAt: null,
          parkReason: null,
          stall: { cli: 'gh', reason: 'gh-list-failed', detail: 'gh: authentication failed', consecutive: 3 }
        }
      }
    });
    expect(screen.getByText(/Probe failing/)).toBeInTheDocument();
    expect(screen.getByText(/gh-list-failed/)).toBeInTheDocument();
    expect(screen.getByText(/authentication failed/)).toBeInTheDocument();
    expect(screen.getByText(/3 consecutive/)).toBeInTheDocument();
    // Still reports the drain as healthy — a stall does not imply a park.
    expect(screen.getByText(/Draining — actionable work available/)).toBeInTheDocument();
  });

  it('renders nothing extra when the probe is healthy (no stall)', () => {
    renderControls({
      config: {
        type: 'on-demand',
        cronExpression: null,
        perpetual: true,
        autoStart: true,
        perpetualStatus: { globalParked: false, parkedAppCount: 0, trackedAppCount: 1, nextRecheckAt: null, parkReason: null, stall: null }
      }
    });
    expect(screen.queryByText(/Probe failing/)).not.toBeInTheDocument();
  });
});


describe('GlobalConfigControls — external issue isolation', () => {
  it('offers text API providers and explains the source policy while retaining an invalid saved pin', async () => {
    const onUpdate = renderControls({
      taskType: 'issue-watcher',
      config: { providerId: 'coding-cli', promptMode: 'runtime-generated' },
      providers: [
        { id: 'coding-cli', name: 'Coding CLI', type: 'cli', enabled: true },
        { id: 'local-api', name: 'Local API', type: 'api', enabled: true },
        { id: 'another-cli', name: 'Another CLI', type: 'cli', enabled: true },
      ],
    });
    expect(screen.getByRole('option', { name: 'Coding CLI (API provider required)' })).toBeDisabled();
    expect(screen.getByRole('option', { name: 'Local API' })).toBeInTheDocument();
    expect(screen.queryByRole('option', { name: 'Another CLI' })).not.toBeInTheDocument();
    expect(screen.getByRole('link', { name: 'Abuse Guard source settings' })).toHaveAttribute('href', '/models/llms/abuse');
    await act(async () => { fireEvent.change(screen.getByLabelText('Provider (optional)'), { target: { value: '' } }); });
    expect(onUpdate).toHaveBeenCalledWith('issue-watcher', { providerId: null, model: null, effort: null });
  });
});

it('persists leave-open for claim-issue while keeping configured reviews available', () => {
   const taskMetadata = { useWorktree: false, openPR: false, claimFlow: true };
   const onUpdate = renderControls({ taskType: 'claim-issue', taskMetadata });
   expect(prSelect()).toHaveValue('');
   fireEvent.change(prSelect(), { target: { value: 'leave-open' } });
   expect(onUpdate).toHaveBeenCalledWith('claim-issue', { taskMetadata: { ...taskMetadata, prCompletion: 'leave-open' } });
   expect(screen.getByTestId('reviewer-picker')).toBeInTheDocument();
});

it('persists merge-on-green for claim-issue and hides the reviewer picker', () => {
   const taskMetadata = { useWorktree: false, openPR: false, claimFlow: true };
   const onUpdate = renderControls({ taskType: 'claim-issue', taskMetadata });
   expect(prSelect()).toHaveValue('');
   fireEvent.change(prSelect(), { target: { value: 'merge-on-green' } });
   expect(onUpdate).toHaveBeenCalledWith('claim-issue', { taskMetadata: { ...taskMetadata, prCompletion: 'merge-on-green' } });
});

it('hides the reviewer picker when claim-issue is configured with merge-on-green', () => {
   renderControls({ taskType: 'claim-issue', taskMetadata: { useWorktree: false, openPR: false, claimFlow: true, prCompletion: 'merge-on-green' } });
   expect(screen.queryByTestId('reviewer-picker')).not.toBeInTheDocument();
});

it('offers and saves twelve parallel claim workers without dropping other metadata', async () => {
  const onUpdate = renderControls({ taskType: 'claim-issue', taskMetadata: { simplify: true } });
  const select = screen.getByLabelText('Swarm Mode');
  expect([...select.options].map(option => Number(option.value))).toEqual([0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]);
  await act(async () => fireEvent.change(select, { target: { value: '12' } }));
  expect(onUpdate).toHaveBeenCalledWith('claim-issue', { taskMetadata: { simplify: true, swarmCount: 12 } });
});