Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

67.9 KB
Raw Blame
// @vitest-environment jsdom
import { act, cleanup, fireEvent, render, screen, waitFor, within } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { MemoryRouter, useLocation } from 'react-router-dom';
import { isValidElement } from 'react';
import { CindyMakeSection } from '../CindyMakeSection';
import type { CindyMakeMergeState, CindyMakeMergeRequest } from '../../../../shared/cindyMakeMerge';
import type { CindyMakePersonalBuildState } from '../../../../shared/cindyMakeSession';
import type { CindyMakeSyncRequest, CindyMakeSyncState } from '../../../../shared/cindyMakeSync';
import { startMakeDoctor } from '@/lib/cindyMakeDoctor';
import { setCindyMakeForceManagedTools } from '@/lib/cindyMakeSettings';
import { setDataOwnerGeneration } from '@/contexts/dataOwnerGeneration';
import { toast } from '@/lib/toast';
import type {
  CindyMakeGlobalState,
  MakeDoctorReport,
  MakeSourceStatus,
} from '../../../../shared/cindyMakeDoctor';

vi.mock('react-i18next', () => ({
  useTranslation: () => ({
    i18n: { language: 'en' },
    t: (key: string, values?: { resolved: number; reused: number; downloaded: number }) =>
      key === 'cindyMake.source.cacheProgress' && values
        ? `${key} ${values.resolved} / ${values.reused} / ${values.downloaded}`
        : key,
  }),
}));
vi.mock('@/lib/toast', () => ({ toast: { error: vi.fn(), success: vi.fn() } }));
vi.mock('@/state/newMakerDraft', () => ({
  getDraft: () => ({
    vendor: 'codex',
    lastByVendor: { codex: { model: 'test-model', permissionMode: 'ask' } },
  }),
  getFastModeForModel: () => false,
}));
const confirmMerge = vi.hoisted(() =>
  vi.fn(async (_options?: unknown, _signal?: AbortSignal) => true),
);
vi.mock('@/components/ui/confirm-dialog-provider', () => ({
  useConfirmDialog: () => ({
    confirm: confirmMerge,
  }),
}));

type Api = Parameters<typeof startMakeDoctor>[1];
type Listener = Parameters<NonNullable<Api>['onDesktopCommandTriggered']>[0];
type Result = Awaited<ReturnType<NonNullable<Api>['executeDesktopCommand']>>;

function harness(
  history: {
    busy: boolean;
    activeWork?: boolean;
    build?: CindyMakePersonalBuildState;
    canBuild?: boolean;
  } = {
    busy: false,
  },
) {
  const listeners = new Set<Listener>();
  const sourceListeners = new Set<(status: MakeSourceStatus) => void>();
  const stateListeners = new Set<(state: CindyMakeGlobalState) => void>();
  let globalState: CindyMakeGlobalState = {};
  const runs = new Map<string, (result: Result) => void>();
  const api = {
    getCindyMakeSettings: vi.fn(async () => ({ syncLatestBeforeBuild: false })),
    setCindyMakeSyncLatestBeforeBuild: vi.fn(async (enabled: boolean) => ({
      syncLatestBeforeBuild: enabled,
    })),
    cindyMakeMerge: vi.fn(
      async (_request: CindyMakeMergeRequest): Promise<CindyMakeMergeState | undefined> =>
        undefined,
    ),
    cindyMakeSync: vi.fn(
      async (_request: CindyMakeSyncRequest): Promise<CindyMakeSyncState> => ({
        running: true,
        step: 'resuming',
      }),
    ),
    getCindyMakeState: vi.fn(async (): Promise<CindyMakeGlobalState> => ({
      ...globalState,
      source: globalState.source ?? (await api.getCindyMakeSourceStatus()),
    })),
    onCindyMakeState: (listener: (state: CindyMakeGlobalState) => void) => {
      stateListeners.add(listener);
      return () => {
        stateListeners.delete(listener);
      };
    },
    openCindyMakeToolsDir: vi.fn(async () => ({ success: true })),
    getCindyMakeSourceStatus: vi.fn(async () => undefined as MakeSourceStatus | undefined),
    openCindyMakeSourceDir: vi.fn(async () => ({ success: true })),
    onCindyMakeSourceStatus: (listener: (status: MakeSourceStatus) => void) => {
      sourceListeners.add(listener);
      return () => {
        sourceListeners.delete(listener);
      };
    },
    cancelCindyMakeSource: vi.fn(async () => ({ success: true })),
    onDesktopCommandTriggered: (listener: Listener) => {
      listeners.add(listener);
      return () => {
        listeners.delete(listener);
      };
    },
    executeDesktopCommand: vi.fn<NonNullable<Api>['executeDesktopCommand']>((_name, ctx) => {
      if (ctx.doctorAction === 'cancel') return Promise.resolve({ success: true });
      return new Promise((resolve) => {
        runs.set(ctx.doctorRunId!, resolve);
      });
    }),
  };
  vi.stubGlobal('electronAPI', {
    getCindyMakeHistory: async () => ({
      canBuild: false,
      ...history,
      items: Object.values(globalState.tasks ?? {})
        .filter((report) => report.task)
        .map((report) => ({
          schema: 1,
          runId: report.runId,
          sessionId: report.task!.sessionId,
          title: report.task!.title ?? '',
          request: report.task!.request ?? '',
          createdAt: 1,
          updatedAt: 1,
          completions: [],
          receipts: [],
          versions: [],
          lifecycle: 'editing',
          integration: 'unintegrated',
          actions: ['open'],
        })),
    }),
    getCindyMakeSettings: api.getCindyMakeSettings,
    setCindyMakeSyncLatestBeforeBuild: api.setCindyMakeSyncLatestBeforeBuild,
    cindyMakeMerge: api.cindyMakeMerge,
    cindyMakeSync: api.cindyMakeSync,
    maker: api,
    getCindyMakeState: api.getCindyMakeState,
    onCindyMakeState: api.onCindyMakeState,
    openCindyMakeToolsDir: api.openCindyMakeToolsDir,
    getCindyMakeSourceStatus: async () => {
      const source = await api.getCindyMakeSourceStatus();
      if (source) {
        globalState = { ...globalState, source };
        for (const listener of stateListeners) listener(globalState);
      }
      return source;
    },
    openCindyMakeSourceDir: api.openCindyMakeSourceDir,
    onCindyMakeSourceStatus: api.onCindyMakeSourceStatus,
    cancelCindyMakeSource: api.cancelCindyMakeSource,
  });
  const starts = () => api.executeDesktopCommand.mock.calls.filter(([, ctx]) => !ctx.doctorAction);
  /** Main's global source broadcast, whichever window or workflow drives the job. */
  const pushSource = async (status: MakeSourceStatus) => {
    await act(async () => {
      api.getCindyMakeSourceStatus.mockResolvedValue(status);
      for (const listener of sourceListeners) listener(status);
      globalState = { ...globalState, source: status };
      for (const listener of stateListeners) listener(globalState);
    });
  };
  const complete = async (
    runId: string,
    version: string,
    checks: MakeDoctorReport['checks'] = [{ id: 'node', status: 'passed', version }],
  ) => {
    const report: MakeDoctorReport = {
      runId,
      platform: 'win32',
      arch: 'x64',
      status: 'completed',
      checks,
    };
    await act(async () => {
      runs.get(runId)!({ success: true, doctorReport: report });
    });
  };
  const pushState = async (state: CindyMakeGlobalState) => {
    await act(async () => {
      globalState = state;
      for (const listener of stateListeners) listener(state);
    });
  };
  return { api, starts, complete, listeners, pushSource, pushState, stateListeners };
}

function renderVersions(ui: Parameters<typeof render>[0]) {
  return render(
    isValidElement(ui) && ui.type === MemoryRouter ? ui : <MemoryRouter>{ui}</MemoryRouter>,
  );
}

/** Environment-specific cases explicitly enter the second tab after opening Settings. */
function renderEnvironment(ui: Parameters<typeof render>[0]) {
  const view = renderVersions(ui);
  fireEvent.click(screen.getByRole('tab', { name: 'settings.cindyMake.tabs.environment' }));
  return view;
}

beforeEach(() => {
  confirmMerge.mockReset().mockResolvedValue(true);
  vi.mocked(toast.success).mockClear();
  vi.mocked(toast.error).mockClear();
  setDataOwnerGeneration('settings-make-test-owner');
  vi.stubEnv('DEV', true);
  setCindyMakeForceManagedTools(false);
});
afterEach(() => {
  cleanup();
  setCindyMakeForceManagedTools(false);
  vi.unstubAllEnvs();
  vi.unstubAllGlobals();
});

describe('Settings > Cindy Make', () => {
  it('opens version control first and preserves the keyboard tab order and scoped creation action', async () => {
    const h = harness();
    const getVersions = vi.fn(async () => ({
      currentId: 'original',
      selectedId: 'original',
      switching: false,
      versions: [
        { id: 'original', kind: 'original', available: true, compatible: true, version: '0.1.99' },
      ],
    }));
    vi.stubGlobal('electronAPI', { ...window.electronAPI, getCindyVersions: getVersions });
    render(
      <MemoryRouter>
        <CindyMakeSection />
      </MemoryRouter>,
    );
    await h.complete(h.starts()[0][1].doctorRunId!, '22.23.2');
    const environment = screen.getByRole('tab', { name: 'settings.cindyMake.tabs.environment' });
    const versions = screen.getByRole('tab', { name: 'settings.cindyMake.tabs.versions' });
    expect(screen.getAllByRole('tab')).toEqual([versions, environment]);
    expect(versions.getAttribute('aria-selected')).toBe('true');
    expect(versions.getAttribute('aria-controls')).toBe(
      screen.getByRole('tabpanel', { name: 'settings.cindyMake.tabs.versions' }).id,
    );
    expect(screen.getAllByRole('tabpanel')).toHaveLength(1);
    expect(
      within(screen.getByRole('tabpanel', { name: 'settings.cindyMake.tabs.versions' })).getByRole(
        'region',
        { name: 'settings.cindyMake.source.title' },
      ),
    ).toBeTruthy();
    expect(await screen.findByText('0.1.99')).toBeTruthy();
    // Sync is the one place the personal version moves; generating has no sync switch.
    expect(
      screen.queryByRole('switch', { name: 'settings.cindyMake.syncBeforeBuild.ariaLabel' }),
    ).toBeNull();
    const create =
      within(screen.getByRole('tabpanel', { name: 'settings.cindyMake.tabs.versions' })).getByRole(
        'button',
        { name: 'settings.cindyMake.create.title' },
      );
    expect(create).toBeTruthy();
    expect(create.querySelector('svg')).toBeNull();
    const source = screen.getByRole('region', { name: 'settings.cindyMake.source.title' });
    expect(
      source.parentElement?.contains(
        screen.getByRole('heading', { name: /cindyMake.versions.current/ }),
      ),
    ).toBe(true);

    expect(getVersions).toHaveBeenCalledTimes(1);
    versions.focus();
    fireEvent.keyDown(versions, { key: 'ArrowRight' });
    expect(document.activeElement).toBe(environment);
    const environmentPanel = screen.getByRole('tabpanel', {
      name: 'settings.cindyMake.tabs.environment',
    });
    expect(environment.getAttribute('aria-selected')).toBe('true');
    expect(environment.getAttribute('aria-controls')).toBe(environmentPanel.id);
    expect(screen.queryByRole('region', { name: 'settings.cindyMake.source.title' })).toBeNull();
    expect(screen.queryByRole('heading', { name: 'cindyMake.versions.title' })).toBeNull();
    expect(screen.queryByRole('button', { name: 'settings.cindyMake.create.title' })).toBeNull();
    fireEvent.keyDown(environment, { key: 'ArrowRight' });
    expect(document.activeElement).toBe(versions);
    await waitFor(() => expect(getVersions).toHaveBeenCalledTimes(2));
    fireEvent.keyDown(versions, { key: 'End' });
    expect(document.activeElement).toBe(environment);
    expect(screen.getByRole('tabpanel', { name: 'settings.cindyMake.tabs.environment' })).toBe(
      environmentPanel,
    );
    fireEvent.keyDown(environment, { key: 'Home' });
    expect(document.activeElement).toBe(versions);
    await waitFor(() => expect(getVersions).toHaveBeenCalledTimes(3));
    expect(h.starts()).toHaveLength(1);
  });
  it('keeps background preparation, expansion and task search intact while switching tabs', async () => {
    const h = harness();
    renderVersions(
      <MemoryRouter>
        <CindyMakeSection />
      </MemoryRouter>,
    );
    const checkRun = h.starts()[0][1].doctorRunId!;
    await h.complete(checkRun, '22.23.2');
    const task: MakeDoctorReport = {
      runId: 'personal-run',
      platform: 'win32',
      arch: 'x64',
      status: 'completed',
      checks: [],
      task: {
        sessionId: 'personal-task',
        title: 'Blue background',
        request: 'Make it blue',
        phase: 'completed',
      },
    };
    await h.pushState({
      source: {
        status: 'preparing',
        path: '/managed/source',
        ref: 'main',
        phase: 'cloning',
        progress: { stage: 'receiving', percent: 25 },
      },
      tasks: { 'personal-run': task },
    });
    const sourceCard = screen.getByRole('region', { name: 'settings.cindyMake.source.title' });
    expect(within(sourceCard).getByText('/managed/source')).toBeTruthy();
    expect(
      within(sourceCard).queryByRole('button', { name: 'cindyMake.overview.details' }),
    ).toBeNull();
    const tasks = screen.getByRole('region', { name: 'cindyMake.history.title' });
    fireEvent.change(within(tasks).getByRole('textbox'), { target: { value: 'Blue' } });
    fireEvent.click(screen.getByRole('tab', { name: 'settings.cindyMake.tabs.environment' }));
    expect(screen.queryByRole('region', { name: 'settings.cindyMake.source.title' })).toBeNull();
    expect(screen.queryByRole('region', { name: 'cindyMake.history.title' })).toBeNull();
    await h.pushSource({
      status: 'preparing',
      path: '/managed/source',
      ref: 'main',
      phase: 'cloning',
      progress: { stage: 'receiving', percent: 75 },
    });
    fireEvent.click(screen.getByRole('tab', { name: 'settings.cindyMake.tabs.versions' }));
    expect(screen.getByRole('region', { name: 'settings.cindyMake.source.title' })).toBe(
      sourceCard,
    );
    expect(within(sourceCard).getByText('/managed/source')).toBeTruthy();
    expect(within(sourceCard).getByRole('status').textContent).toContain('(75%)');
    expect((within(tasks).getByRole('textbox') as HTMLInputElement).value).toBe('Blue');
    expect(h.starts()).toHaveLength(1);
    expect(
      h.api.executeDesktopCommand.mock.calls.some(
        ([, context]) => context.doctorAction === 'cancel',
      ),
    ).toBe(false);
    expect(h.api.cancelCindyMakeSource).not.toHaveBeenCalled();
  });
  it('leads with a plain-language summary and keeps hashes and the source folder in collapsed technical details', async () => {
    const h = harness();
    h.api.getCindyMakeSourceStatus.mockResolvedValue({
      status: 'ready',
      path: '/managed/source',
      ref: 'main',
      commit: 'a'.repeat(40),
      mainCommit: 'c'.repeat(40),
    } satisfies MakeSourceStatus);
    renderVersions(<CindyMakeSection />);
    const card = await screen.findByRole('region', { name: 'settings.cindyMake.source.title' });
    await waitFor(() => expect(within(card).getByText('cindyMake.summary.personal')).toBeTruthy());
    const details = within(card)
      .getByText('cindyMake.summary.technical')
      .closest('details') as HTMLDetailsElement;
    expect(details.open).toBe(false);
    // The summary sits outside the technical details; hashes, local main and the folder sit inside.
    expect(details.contains(within(card).getByText('cindyMake.summary.personal'))).toBe(false);
    expect(details.contains(within(card).getByText('cindyMake.overview.localMain'))).toBe(true);
    expect(details.contains(within(card).getByText('/managed/source'))).toBe(true);
    expect(
      details.contains(
        within(card).getByRole('button', { name: 'settings.cindyMake.source.reset' }),
      ),
    ).toBe(true);
  });
  it('starts one Sync without another confirmation and passes the Agent preferences', async () => {
    const h = harness();
    const source: MakeSourceStatus = {
      status: 'ready',
      path: '/managed/source',
      ref: 'main',
      commit: 'a'.repeat(40),
    };
    h.api.getCindyMakeSourceStatus.mockResolvedValue(source);
    renderVersions(<CindyMakeSection />);
    const button = await screen.findByRole('button', { name: 'cindyMake.sync.action' });
    expect(screen.getAllByRole('button', { name: 'cindyMake.sync.action' })).toHaveLength(1);
    expect(screen.queryByRole('button', { name: 'settings.cindyMake.source.update' })).toBeNull();
    fireEvent.click(button);
    await waitFor(() =>
      expect(h.api.cindyMakeSync).toHaveBeenCalledExactlyOnceWith({
        action: 'sync',
        createOptions: expect.objectContaining({ agentKind: 'codex', model: 'test-model' }),
      }),
    );
    expect(confirmMerge).not.toHaveBeenCalled();
    expect(h.api.cindyMakeMerge).not.toHaveBeenCalled();
    expect(h.starts().filter(([, ctx]) => ctx.makeAction === 'prepare-source')).toHaveLength(0);
  });
  it.each(['merged', 'failed'] as const)(
    'shows live progress, then a quiet success or actionable %s result on reentry',
    async (result) => {
      const h = harness();
      const source: MakeSourceStatus = {
        status: 'ready',
        path: '/managed/source',
        ref: 'main',
        commit: 'b'.repeat(40),
        mainCommit: 'c'.repeat(40),
        latestVersion: {
          status: 'ready',
          channel: 'dev',
          ref: 'main',
          commit: 'a'.repeat(40),
          ahead: 0,
          behind: 6,
        },
      };
      const previous: CindyMakeMergeState = {
        id: 'previous-update',
        status: 'merged',
        ref: 'main',
        upstreamCommit: 'a'.repeat(40),
      };
      h.api.getCindyMakeSourceStatus.mockResolvedValue(source);
      const view = renderVersions(<CindyMakeSection />);
      await h.pushState({ source, upstreamMerge: previous });
      const card = screen.getByRole('region', { name: 'settings.cindyMake.source.title' });
      const expectVersionsVisible = (region: HTMLElement) => {
        for (const commit of ['a', 'c']) {
          expect(within(region).getByText(commit.repeat(12)).title).toBe(commit.repeat(40));
        }
        expect(within(region).queryByText('b'.repeat(12))).toBeNull();
        expect(within(region).getByText('cindyMake.source.details.latest.dev')).toBeTruthy();
        expect(within(region).getByText(/cindyMake.source.details.latest.behind/)).toBeTruthy();
      };
      expectVersionsVisible(card);
      fireEvent.click(within(card).getByRole('button', { name: 'cindyMake.sync.action' }));
      await waitFor(() => expect(h.api.cindyMakeSync).toHaveBeenCalledOnce());
      expectVersionsVisible(card);
      expect(within(card).queryByText('cindyMake.merge.status.merged')).toBeNull();
      const current = { ...previous, id: 'current-update' };
      const running = { running: true, step: 'official' as const };
      for (const status of ['fetching', 'merging', 'checking'] as const) {
        await h.pushState({ source, upstreamMerge: { ...current, status }, personalSync: running });
        expect(within(card).getByText('cindyMake.sync.step.official')).toBeTruthy();
        expectVersionsVisible(card);
        // One line says what Sync does; no second, technical status for the same work.
        expect(within(card).queryByText('cindyMake.merge.status.' + status)).toBeNull();
        expect(within(card).queryByText('settings.cindyMake.source.status.ready')).toBeNull();
        expect(
          within(card)
            .getByRole('button', { name: 'cindyMake.sync.action' })
            .getAttribute('aria-busy'),
        ).toBe('true');
      }
      const finalState: CindyMakeMergeState = {
        ...current,
        status: result,
        ...(result === 'failed' ? { error: 'gitFailed' as const } : {}),
      };
      const ended: CindyMakeSyncState =
        result === 'merged'
          ? { done: { at: 1, ref: 'main' } }
          : { error: 'failed' };
      await h.pushState({ source, upstreamMerge: finalState, personalSync: ended });
      if (result === 'failed') {
        expect(within(card).queryByText('cindyMake.merge.status.failed')).toBeNull();
        expect(within(card).getByText('cindyMake.sync.errors.failed')).toBeTruthy();
      } else expect(within(card).getByText('cindyMake.sync.doneDevelopment')).toBeTruthy();
      // The result stays in the card; Sync does not add a toast on top.
      expect(toast.success).not.toHaveBeenCalled();
      expect(toast.error).not.toHaveBeenCalled();
      view.unmount();
      renderVersions(<CindyMakeSection />);
      const reopened = screen.getByRole('region', { name: 'settings.cindyMake.source.title' });
      await waitFor(() =>
        expect(
          within(reopened).getByText(
            result === 'merged' ? 'cindyMake.sync.doneDevelopment' : 'cindyMake.sync.errors.failed',
          ),
        ).toBeTruthy(),
      );
      expectVersionsVisible(reopened);
      expect(within(reopened).getAllByRole('status')).toHaveLength(1);
      expect(within(reopened).queryByText('settings.cindyMake.source.status.ready')).toBeNull();
    },
  );
  it('shows the source card immediately and keeps local details visible while the latest lookup waits', async () => {
    const h = harness();
    let finish!: (source: MakeSourceStatus) => void;
    const pending = new Promise<MakeSourceStatus>((resolve) => {
      finish = resolve;
    });
    h.api.getCindyMakeSourceStatus.mockReturnValue(pending);
    renderVersions(<CindyMakeSection />);
    const card = screen.getByRole('region', { name: 'settings.cindyMake.source.title' });
    expect(within(card).getByText('settings.cindyMake.source.description')).toBeTruthy();
    expect(within(card).queryByRole('status')).toBeNull();
    expect(
      within(card).queryByRole('button', { name: 'settings.cindyMake.source.prepare' }),
    ).toBeNull();
    expect(within(card).queryByRole('button', { name: 'cindyMake.sync.action' })).toBeNull();

    const source: MakeSourceStatus = {
      status: 'ready',
      path: '/managed/source',
      ref: 'main',
      mainCommit: 'a'.repeat(40),
    };
    await h.pushState({ source });
    expect(within(card).getByText(source.path)).toBeTruthy();
    expect(within(card).getByText('a'.repeat(12))).toBeTruthy();
    expect(within(card).getByRole('button', { name: 'cindyMake.sync.action' })).toBeTruthy();
    await act(async () =>
      finish({
        ...source,
        latestVersion: { status: 'unavailable', channel: 'dev' },
      }),
    );
    expect(screen.getByRole('region', { name: 'settings.cindyMake.source.title' })).toBe(card);
    expect(within(card).getByText('a'.repeat(12))).toBeTruthy();
    expect(within(card).getByText(/cindyMake.overview.lookupUnavailable/)).toBeTruthy();
  });
  it('keeps version details when opening Settings during an update and refreshes them on completion', async () => {
    const h = harness();
    const source: MakeSourceStatus = {
      status: 'ready',
      path: '/managed/source',
      ref: 'main',
      commit: 'b'.repeat(40),
      mainCommit: 'c'.repeat(40),
      personalAhead: 0,
      personalBehind: 4,
    };
    const merge: CindyMakeMergeState = {
      id: 'merge',
      status: 'fetching',
      ref: 'main',
      upstreamCommit: 'a'.repeat(40),
    };
    await h.pushState({ source, upstreamMerge: merge });
    renderVersions(<CindyMakeSection />);
    const card = screen.getByRole('region', { name: 'settings.cindyMake.source.title' });
    await waitFor(() =>
      expect(within(card).getByRole('status').textContent).toBe('cindyMake.merge.status.fetching'),
    );
      expect(within(card).queryByText('cindyMake.merge.status.merged')).toBeNull();
      expect(within(card).queryByText('settings.cindyMake.source.status.ready')).toBeNull();

    for (const status of ['fetching', 'merging', 'checking'] as const) {
      await h.pushState({
        source,
        upstreamMerge: { ...merge, status, hasWorkspace: true },
      });
      expect(within(card).getByText('c'.repeat(12))).toBeTruthy();
      expect(within(card).queryByText('b'.repeat(12))).toBeNull();
      expect(within(card).getByRole('status').textContent).toBe(`cindyMake.merge.status.${status}`);
      expect(within(card).queryByText('cindyMake.overview.comparison.mainAhead')).toBeNull();
    }
    // A conflict waits on Sync's line, whoever started the update.
    await h.pushState({
      source,
      upstreamMerge: { ...merge, status: 'resolving', hasWorkspace: true, sessionId: 'task' },
      personalSync: { waiting: { kind: 'official', reason: 'working', sessionId: 'task' } },
    });
    expect(within(card).getByText('cindyMake.sync.waiting.working.official')).toBeTruthy();
    expect(within(card).queryByText('cindyMake.overview.comparison.mainAhead')).toBeNull();
    await h.pushState({
      source: { ...source, commit: 'd'.repeat(40), mainCommit: 'd'.repeat(40), personalBehind: 0 },
      upstreamMerge: { ...merge, status: 'merged' },
    });
    expect(within(card).getAllByText('d'.repeat(12))).toHaveLength(1);
    expect(within(card).queryByText('b'.repeat(12))).toBeNull();
    expect(within(card).queryByText('c'.repeat(12))).toBeNull();
    // The plain-language summary announces the status; the technical comparison stays silent.
    expect(within(card).getByRole('status').textContent).toBe(
      'cindyMake.summary.included.unverified',
    );
    expect(within(card).getByText('cindyMake.overview.comparison.same')).toBeTruthy();
    expect(within(card).queryByText('cindyMake.merge.status.merged')).toBeNull();
  });
  it.each([
    ['missing', undefined],
    ['failed', 'installFailed'],
    ['cancelled', 'cancelled'],
  ] as const)(
    'keeps recovery for %s source available after an update failed without a workspace',
    async (status, error) => {
      const h = harness();
      const source: MakeSourceStatus = { status, error, path: '/managed/source', ref: 'main' };
      renderVersions(<CindyMakeSection />);
      await h.pushState({
        source: { ...source, status: 'ready', error: undefined },
        upstreamMerge: {
          id: 'previous-update',
          status: 'failed',
          ref: 'main',
          upstreamCommit: 'a'.repeat(40),
          error: 'gitFailed',
          hasWorkspace: false,
        },
      });
      const card = screen.getByRole('region', { name: 'settings.cindyMake.source.title' });
      expect(within(card).queryByText('cindyMake.merge.errors.gitFailed')).toBeNull();

      await h.pushSource(source);
      expect(within(card).getByRole('status').textContent).toContain(
        `settings.cindyMake.source.status.${status}`,
      );
      expect(within(card).queryByText('cindyMake.merge.errors.gitFailed')).toBeNull();
      if (error) expect(within(card).getByText(`cindyMake.source.errors.${error}`)).toBeTruthy();
      fireEvent.click(
        within(card).getByRole('button', {
          name:
            status === 'missing'
              ? 'settings.cindyMake.source.prepare'
              : 'settings.cindyMake.source.retry',
        }),
      );
      expect(within(card).getByRole('status').textContent).toBe(
        'settings.cindyMake.source.status.preparing',
      );
      expect(h.starts().at(-1)?.[1].makeAction).toBe('prepare-source');
      await h.pushSource({
        status: 'preparing',
        path: source.path,
        phase: 'cloning',
        progress: { stage: 'receiving', percent: 43 },
      });
      expect(within(card).getByRole('status').textContent).toContain('(43%)');
      fireEvent.click(within(card).getByRole('button', { name: 'settings.cindyMake.source.stop' }));
      expect(h.api.cancelCindyMakeSource).toHaveBeenCalledTimes(1);
    },
  );
  it('lets a Sync conflict continue in its task without asking again', async () => {
    const h = harness();
    const source: MakeSourceStatus = { status: 'ready', path: '/managed/source', ref: 'main' };
    renderVersions(<CindyMakeSection />);
    await h.pushState({
      source,
      upstreamMerge: {
        id: 'update-conflict',
        status: 'resolving',
        ref: 'main',
        upstreamCommit: 'a'.repeat(40),
        hasWorkspace: true,
        sessionId: 'merge-task',
      },
      personalSync: {
        waiting: { kind: 'official', reason: 'working', sessionId: 'merge-task' },
      },
    });
    const card = screen.getByRole('region', { name: 'settings.cindyMake.source.title' });
    expect(within(card).getByText('cindyMake.sync.waiting.working.official')).toBeTruthy();
    expect(within(card).getByRole('button', { name: 'cindyMake.merge.openTask' })).toBeTruthy();
    // Sync itself waits: pressing it again would only show the same conflict.
    expect(
      within(card).getByRole('button', { name: 'cindyMake.sync.action' }).hasAttribute('disabled'),
    ).toBe(true);
    expect(confirmMerge).not.toHaveBeenCalled();

    // Abandoning asks once, then gives the decision to Main.
    confirmMerge.mockResolvedValueOnce(false);
    fireEvent.click(within(card).getByRole('button', { name: 'cindyMake.sync.abandon.action' }));
    await waitFor(() => expect(confirmMerge).toHaveBeenCalledOnce());
    expect(h.api.cindyMakeSync).not.toHaveBeenCalled();
    fireEvent.click(within(card).getByRole('button', { name: 'cindyMake.sync.abandon.action' }));
    await waitFor(() =>
      expect(h.api.cindyMakeSync).toHaveBeenCalledExactlyOnceWith({ action: 'abandon' }),
    );
    expect(confirmMerge.mock.calls[1][0]).toMatchObject({
      title: 'cindyMake.sync.abandon.title',
      confirmVariant: 'destructive',
    });
  });
  it('allows version switching with retained conflicts and during active work', async () => {
    const history = { busy: true, activeWork: false };
    const h = harness(history);
    const versions = {
      currentId: 'personal',
      selectedId: 'personal',
      switching: false,
      versions: [
        { id: 'original', kind: 'original', available: true, compatible: true },
        { id: 'personal', kind: 'personal', available: true, compatible: true },
      ],
    };
    const switchVersion = vi.fn(async () => versions);
    vi.stubGlobal('electronAPI', {
      ...window.electronAPI,
      getCindyVersions: async () => versions,
      actCindyVersion: switchVersion,
    });
    renderVersions(<CindyMakeSection />);
    const source: MakeSourceStatus = { status: 'ready', path: '/managed/source', ref: 'main' };
    const conflict: CindyMakeMergeState = {
      id: 'update',
      status: 'conflict',
      ref: 'main',
      upstreamCommit: 'a'.repeat(40),
      hasWorkspace: true,
    };
    await h.pushState({ source, upstreamMerge: conflict });
    fireEvent.click(
      await screen.findByRole('button', { name: 'cindyMake.overview.switchVersion' }),
    );
    const button = screen.getByRole('button', { name: 'cindyMake.versions.switch' });
    expect(button.hasAttribute('disabled')).toBe(false);
    fireEvent.click(button);
    await waitFor(() => expect(switchVersion).toHaveBeenCalledWith('switch', 'original'));
    history.activeWork = true;
    await h.pushState({ source, upstreamMerge: { ...conflict } });
    await waitFor(() => expect(button.hasAttribute('disabled')).toBe(false));
    history.activeWork = false;
    await h.pushState({ source, upstreamMerge: { ...conflict } });
    await waitFor(() => expect(button.hasAttribute('disabled')).toBe(false));
    expect(screen.queryByRole('button', { name: 'cindyMake.merge.resolve' })).toBeNull();
  });
  it('offers generating once the personal version changed since it was last generated', async () => {
    const h = harness({ busy: false, canBuild: true });
    const versions = {
      currentId: 'original',
      selectedId: 'original',
      switching: false,
      versions: [
        { id: 'original', kind: 'original', available: true, compatible: true },
        { id: 'personal', kind: 'personal', available: true, compatible: true, commit: 'b'.repeat(40) },
      ],
    };
    const generate = vi.fn(async () => ({ items: [], busy: false, canBuild: false }));
    vi.stubGlobal('electronAPI', {
      ...window.electronAPI,
      getCindyVersions: async () => versions,
      generateCindyMakePersonal: generate,
    });
    renderVersions(
      <MemoryRouter>
        <CindyMakeSection />
      </MemoryRouter>,
    );
    await h.pushState({
      source: {
        status: 'ready',
        path: '/managed/source',
        ref: 'main',
        commit: 'c'.repeat(40),
        baseCommit: 'a'.repeat(40),
      },
      personalSync: { done: { at: 1, ref: 'main' } },
    });
    const card = screen.getByRole('region', { name: 'settings.cindyMake.source.title' });
    expect(await within(card).findByText('cindyMake.sync.needsGenerate')).toBeTruthy();
    fireEvent.click(
      within(card).getByRole('button', { name: 'cindyMake.history.generatePersonal' }),
    );
    await waitFor(() => expect(generate).toHaveBeenCalledOnce());
  });
  it('lets the user use a result that lost changes, after confirming, from Sync’s line', async () => {
    const h = harness();
    const source: MakeSourceStatus = { status: 'ready', path: '/managed/source', ref: 'main' };
    renderVersions(
      <MemoryRouter>
        <CindyMakeSection />
      </MemoryRouter>,
    );
    await h.pushState({
      source,
      upstreamMerge: {
        id: 'merge',
        status: 'failed',
        error: 'checksFailed',
        missing: { count: 2, commits: [] },
        ref: 'github',
        upstreamCommit: 'a'.repeat(40),
        remote: { base: 'b'.repeat(40) },
        hasWorkspace: true,
        sessionId: 'merge-task',
      },
      personalSync: {
        waiting: { kind: 'combine', reason: 'missing', missing: 2, sessionId: 'merge-task' },
      },
    });
    const card = screen.getByRole('region', { name: 'settings.cindyMake.source.title' });
    expect(within(card).getByText('cindyMake.sync.waiting.missing')).toBeTruthy();
    // No resolve button of the old lifecycle next to Sync.
    expect(screen.queryByRole('button', { name: 'cindyMake.merge.resolve' })).toBeNull();
    confirmMerge.mockResolvedValueOnce(false);
    fireEvent.click(within(card).getByRole('button', { name: 'cindyMake.sync.accept.action' }));
    await waitFor(() => expect(confirmMerge).toHaveBeenCalledOnce());
    expect(h.api.cindyMakeSync).not.toHaveBeenCalled();
    fireEvent.click(within(card).getByRole('button', { name: 'cindyMake.sync.accept.action' }));
    await waitFor(() =>
      expect(h.api.cindyMakeSync).toHaveBeenCalledExactlyOnceWith({ action: 'accept' }),
    );
    expect(confirmMerge.mock.calls[1][0]).toMatchObject({
      title: 'cindyMake.sync.accept.title',
      confirmVariant: 'destructive',
    });
    expect(h.api.cindyMakeMerge).not.toHaveBeenCalled();
  });
  it('leaves an unfinished cancellation to Sync, which completes it first', async () => {
    const h = harness();
    const source: MakeSourceStatus = { status: 'ready', path: '/managed/source', ref: 'main' };
    renderVersions(<CindyMakeSection />);
    await h.pushState({
      source,
      upstreamMerge: {
        id: 'merge',
        status: 'failed',
        error: 'cancelFailed',
        cancellationRequested: true,
        ref: 'main',
        upstreamCommit: 'a'.repeat(40),
        hasWorkspace: false,
      },
    });
    expect(screen.queryByRole('button', { name: 'cindyMake.merge.retryCancel' })).toBeNull();
    fireEvent.click(screen.getByRole('button', { name: 'cindyMake.sync.action' }));
    await waitFor(() =>
      expect(h.api.cindyMakeSync).toHaveBeenCalledWith(expect.objectContaining({ action: 'sync' })),
    );
    expect(h.api.cindyMakeMerge).not.toHaveBeenCalled();
  });
  it.each(['resolving', 'failed'] as const)(
    'keeps a %s upstream task outside the unfinished production list',
    async (status) => {
      const h = harness();
      const merge: CindyMakeMergeState = {
        id: 'abcd-merge',
        status,
        ref: 'main',
        upstreamCommit: 'a'.repeat(40),
        hasWorkspace: true,
        sessionId: 'merge-task',
      };
      h.api.getCindyMakeState.mockResolvedValue({
        source: { status: 'ready', path: '/managed/source', ref: 'main' },
        upstreamMerge: merge,
        personalSync: {
          waiting: {
            kind: 'official',
            reason: status === 'failed' ? 'failed' : 'working',
            sessionId: 'merge-task',
          },
        },
      });
      renderVersions(
        <MemoryRouter>
          <CindyMakeSection />
        </MemoryRouter>,
      );
      expect(await screen.findByRole('button', { name: 'cindyMake.merge.openTask' })).toBeTruthy();
      fireEvent.click(screen.getByRole('tab', { name: 'settings.cindyMake.tabs.versions' }));
      expect(
        screen.getByRole('heading', {
          name: 'cindyMake.history.title · cindyMake.history.taskCount',
        }),
      ).toBeTruthy();
      const report: MakeDoctorReport = {
        runId: 'make-run',
        platform: 'win32',
        arch: 'x64',
        status: 'completed',
        checks: [],
        task: {
          sessionId: 'make-task',
          title: 'Personal feature',
          request: 'A change',
          phase: 'completed',
        },
      };
      await h.pushState({ upstreamMerge: merge, tasks: { 'make-run': report } });
      const panel = screen.getByRole('region', { name: 'cindyMake.history.title' });
      expect(
        await within(panel).findByRole('heading', {
          name: 'cindyMake.history.title · cindyMake.history.taskCount',
        }),
      ).toBeTruthy();
      expect(within(panel).getByRole('button', { name: /Personal feature/ })).toBeTruthy();
      expect(within(panel).queryByText('cindyMake.merge.taskTitle')).toBeNull();
      expect(within(panel).queryByRole('button', { name: 'cindyMake.merge.openTask' })).toBeNull();
      expect(h.api.cindyMakeMerge).not.toHaveBeenCalled();
    },
  );

  it.each(['missing', 'passed'] as const)(
    'shows the new %s check instead of a completed preparation from before entry',
    async (status) => {
      const environment = harness();
      const preparation: MakeDoctorReport = {
        runId: 'historical-preparation',
        mode: 'prepare',
        platform: 'win32',
        arch: 'x64',
        status: status === 'missing' ? 'completed' : 'failed',
        checks: [{ id: 'node', status: status === 'missing' ? 'passed' : 'missing' }],
      };
      await environment.pushState({ environmentPrepare: { active: false, report: preparation } });
      renderEnvironment(<CindyMakeSection />);
      const runId = environment.starts()[0][1].doctorRunId!;
      await environment.complete(runId, '', [{ id: 'node', status }]);
      await environment.pushState({
        environmentPrepare: { active: false, report: preparation },
        environmentCheck: {
          active: false,
          report: {
            ...preparation,
            runId,
            mode: 'check',
            status: 'completed',
            checks: [{ id: 'node', status }],
          },
        },
      });
      expect(screen.getByRole('status').textContent).toContain(
        status === 'missing' ? 'cindyMakeDoctor.needsAttention' : 'cindyMakeDoctor.passed',
      );
      expect(screen.queryByText('cindyMake.prepare.passed')).toBeNull();
      expect(screen.queryByText('cindyMake.prepare.failed')).toBeNull();
    },
  );

  it('restores a background installation after reentry, keeps completion, and stops the real run', async () => {
    const environment = harness();
    const first = renderEnvironment(<CindyMakeSection />);
    await environment.complete(environment.starts()[0][1].doctorRunId!, '', [
      { id: 'node', status: 'missing' },
    ]);
    fireEvent.click(screen.getByText('cindyMake.prepare.install'));
    const preparationId = environment.starts()[1][1].doctorRunId!;
    const preparation: MakeDoctorReport = {
      runId: preparationId,
      mode: 'prepare',
      platform: 'win32',
      arch: 'x64',
      status: 'running',
      checks: [
        { id: 'node', status: 'downloading', progress: { loaded: 42, total: 100, percent: 42 } },
      ],
    };
    await environment.pushState({ environmentPrepare: { active: true, report: preparation } });
    first.unmount();
    expect(environment.stateListeners.size).toBe(0);
    const second = renderEnvironment(<CindyMakeSection />);
    await screen.findByText('cindyMake.prepare.stop');
    const reentryCheckId = environment.starts()[2][1].doctorRunId!;
    await environment.complete(reentryCheckId, '', [{ id: 'node', status: 'missing' }]);
    expect(screen.queryByText('cindyMake.prepare.install')).toBeNull();
    fireEvent.click(screen.getByText('cindyMakeDoctor.details'));
    expect(screen.getByRole('progressbar').getAttribute('aria-valuenow')).toBe('42');
    expect(environment.stateListeners.size).toBe(1);
    expect(
      environment.api.executeDesktopCommand.mock.calls.some(
        ([, ctx]) => ctx.doctorAction === 'cancel',
      ),
    ).toBe(false);
    fireEvent.click(screen.getByText('cindyMake.prepare.stop'));
    expect(environment.api.executeDesktopCommand).toHaveBeenLastCalledWith('cindy-make', {
      doctorRunId: preparationId,
      doctorAction: 'cancel',
    });
    await environment.pushState({
      environmentPrepare: {
        active: false,
        report: {
          ...preparation,
          status: 'completed',
          checks: [{ id: 'node', status: 'passed', version: '24.16.0' }],
        },
      },
    });
    expect(screen.getByRole('status').textContent).toContain('cindyMake.prepare.passed');
    expect(screen.queryByRole('progressbar')).toBeNull();
    fireEvent.click(screen.getByText('cindyMake.prepare.retry'));
    await environment.complete(environment.starts()[3][1].doctorRunId!, '', [
      { id: 'node', status: 'missing' },
    ]);
    expect(screen.getByText('cindyMake.prepare.install')).toBeTruthy();
    expect(environment.starts().filter(([command]) => command === 'cindy-make')).toHaveLength(1);
    second.unmount();
    expect(environment.stateListeners.size).toBe(0);
    await environment.complete(preparationId, '24.16.0');
  });

  it('ignores a stale initial snapshot after newer preparation progress arrives', async () => {
    const environment = harness();
    let resolveSnapshot!: (state: CindyMakeGlobalState) => void;
    environment.api.getCindyMakeState.mockImplementationOnce(
      () =>
        new Promise((resolve) => {
          resolveSnapshot = resolve;
        }),
    );
    renderEnvironment(<CindyMakeSection />);
    const preparation: MakeDoctorReport = {
      runId: 'new-preparation',
      mode: 'prepare',
      platform: 'win32',
      arch: 'x64',
      status: 'running',
      checks: [{ id: 'node', status: 'installing' }],
    };
    await environment.pushState({ environmentPrepare: { active: true, report: preparation } });
    await act(async () =>
      resolveSnapshot({
        environmentPrepare: {
          active: true,
          report: { ...preparation, runId: 'old-preparation' },
        },
      }),
    );
    fireEvent.click(screen.getByText('cindyMake.prepare.stop'));
    expect(environment.api.executeDesktopCommand).toHaveBeenLastCalledWith('cindy-make', {
      doctorRunId: 'new-preparation',
      doctorAction: 'cancel',
    });
  });

  it('keeps the fresh check when an unobserved preparation completes before a stale active snapshot', async () => {
    const environment = harness();
    let resolveSnapshot!: (state: CindyMakeGlobalState) => void;
    environment.api.getCindyMakeState.mockImplementationOnce(
      () =>
        new Promise((resolve) => {
          resolveSnapshot = resolve;
        }),
    );
    renderEnvironment(<CindyMakeSection />);
    const preparation: MakeDoctorReport = {
      runId: 'finishing-preparation',
      mode: 'prepare',
      platform: 'win32',
      arch: 'x64',
      status: 'completed',
      checks: [{ id: 'node', status: 'passed', version: '24.16.0' }],
    };
    await environment.pushState({ environmentPrepare: { active: false, report: preparation } });
    await environment.complete(environment.starts()[0][1].doctorRunId!, '', [
      { id: 'node', status: 'missing' },
    ]);
    await act(async () =>
      resolveSnapshot({
        environmentPrepare: {
          active: true,
          report: {
            ...preparation,
            status: 'running',
            checks: [{ id: 'node', status: 'installing' }],
          },
        },
      }),
    );
    expect(screen.getByRole('status').textContent).toContain('cindyMakeDoctor.needsAttention');
    expect(screen.getByText('cindyMake.prepare.install')).toBeTruthy();
    expect(screen.queryByText('cindyMake.prepare.stop')).toBeNull();
  });

  it('does not let a delayed initial snapshot undo an explicit recheck', async () => {
    const environment = harness();
    let resolveSnapshot!: (state: CindyMakeGlobalState) => void;
    environment.api.getCindyMakeState.mockImplementationOnce(
      () =>
        new Promise((resolve) => {
          resolveSnapshot = resolve;
        }),
    );
    renderEnvironment(<CindyMakeSection />);
    const preparation: MakeDoctorReport = {
      runId: 'completed-preparation',
      mode: 'prepare',
      platform: 'win32',
      arch: 'x64',
      status: 'running',
      checks: [{ id: 'node', status: 'installing' }],
    };
    await environment.pushState({ environmentPrepare: { active: true, report: preparation } });
    await environment.pushState({
      environmentPrepare: {
        active: false,
        report: {
          ...preparation,
          status: 'completed',
          checks: [{ id: 'node', status: 'passed' }],
        },
      },
    });
    fireEvent.click(screen.getByText('cindyMake.prepare.retry'));
    await act(async () =>
      resolveSnapshot({ environmentPrepare: { active: true, report: preparation } }),
    );
    await environment.complete(environment.starts()[1][1].doctorRunId!, '', [
      { id: 'node', status: 'missing' },
    ]);
    expect(screen.getByText('cindyMake.prepare.install')).toBeTruthy();
    expect(screen.queryByText('cindyMake.prepare.retry')).toBeNull();
  });

  it('offers creation from the version overview without starting preparation on open or cancel', async () => {
    const environment = harness();
    renderEnvironment(
      <MemoryRouter>
        <CindyMakeSection />
      </MemoryRouter>,
    );
    await act(async () => {
      fireEvent.click(screen.getByRole('tab', { name: 'settings.cindyMake.tabs.versions' }));
    });
    const entry = screen.getByRole('button', { name: 'settings.cindyMake.create.title' });
    entry.focus();
    fireEvent.click(entry);
    expect(screen.getByRole('dialog', { name: 'settings.cindyMake.create.title' })).toBeTruthy();
    expect(within(screen.getByRole('dialog')).getByRole('textbox')).toBeTruthy();
    fireEvent.click(screen.getByRole('button', { name: 'settings.cindyMake.create.cancel' }));
    expect(screen.queryByRole('dialog')).toBeNull();
    await waitFor(() => expect(document.activeElement).toBe(entry));
    expect(environment.starts()).toHaveLength(1);
    expect(environment.starts()[0][0]).toBe('cindy-make-doctor');
  });

  it('checks on each entry without creating a task or starting preparation, and allows recheck after all passed', async () => {
    const h = harness();
    const first = renderEnvironment(<CindyMakeSection />);
    expect(h.starts()).toHaveLength(1);
    expect(h.starts()[0][0]).toBe('cindy-make-doctor');
    await h.complete(h.starts()[0][1].doctorRunId!, '22.23.2');
    const environmentCard = screen.getByRole('region', { name: 'cindyMakeDoctor.title' });
    const status = within(environmentCard).getByRole('status');
    const footer = status.parentElement!;
    expect(status.textContent).toContain('cindyMakeDoctor.passed');
    expect(
      footer.contains(within(environmentCard).getByText('settings.cindyMake.openToolsDir')),
    ).toBe(true);
    expect(footer.contains(within(environmentCard).getByText('cindyMakeDoctor.recheck'))).toBe(
      true,
    );
    fireEvent.click(screen.getByText('cindyMakeDoctor.recheck'));
    expect(h.starts()).toHaveLength(2);
    first.unmount();
    renderEnvironment(<CindyMakeSection />);
    expect(h.starts()).toHaveLength(3);
    expect(h.starts().every(([name]) => name === 'cindy-make-doctor')).toBe(true);
  });

  it('opens Cindy’s managed tools folder from the section header', async () => {
    const h = harness();
    renderEnvironment(<CindyMakeSection />);
    await h.complete(h.starts()[0][1].doctorRunId!, '22.23.2');
    fireEvent.click(screen.getByText('settings.cindyMake.openToolsDir'));
    expect(h.api.openCindyMakeToolsDir).toHaveBeenCalledTimes(1);
  });

  it('shows the persisted source summary and opens its managed folder', async () => {
    const h = harness();
    const source: MakeSourceStatus = {
      status: 'ready',
      path: 'C:\\Users\\test\\cindy-make\\source',
      channel: 'dev',
      version: '0.0.0-dev',
      ref: 'main',
      commit: '0123456789abcdef',
      branch: 'cindy-personal',
      currentBranch: 'main',
      baseCommit: 'b'.repeat(40),
      mainCommit: 'c'.repeat(40),
      mainRemoteCommit: 'd'.repeat(40),
      mainBehind: 3,
      mainAhead: 0,
      latestVersion: {
        status: 'ready',
        channel: 'dev',
        ref: 'main',
        commit: 'f'.repeat(40),
        ahead: 0,
        behind: 6,
      },
    };
    h.api.getCindyMakeSourceStatus = vi.fn(async () => source);
    h.api.openCindyMakeSourceDir = vi.fn(async () => ({ success: true }));
    vi.stubGlobal('electronAPI', {
      ...window.electronAPI,
      cindyMakeMerge: h.api.cindyMakeMerge,
      cindyMakeSync: h.api.cindyMakeSync,
      maker: h.api,
      getCindyMakeState: h.api.getCindyMakeState,
      onCindyMakeState: h.api.onCindyMakeState,
      openCindyMakeToolsDir: h.api.openCindyMakeToolsDir,
      getCindyMakeSourceStatus: h.api.getCindyMakeSourceStatus,
      openCindyMakeSourceDir: h.api.openCindyMakeSourceDir,
      onCindyMakeSourceStatus: h.api.onCindyMakeSourceStatus,
      cancelCindyMakeSource: h.api.cancelCindyMakeSource,
    });
    renderVersions(<CindyMakeSection />);
    await waitFor(() => expect(screen.getByText('c'.repeat(12))).toBeTruthy());
    const sourceCard = screen.getByRole('region', { name: 'settings.cindyMake.source.title' });
    expect(within(sourceCard).queryByText('0123456789ab')).toBeNull();
    expect(within(sourceCard).getByText(source.path)).toBeTruthy();
    expect(within(sourceCard).queryByText(source.baseCommit!.slice(0, 12))).toBeNull();
    const openButton = within(sourceCard).getByRole('button', {
      name: 'settings.cindyMake.source.openDir',
    });
    fireEvent.click(openButton);
    expect(h.api.openCindyMakeSourceDir).toHaveBeenCalledTimes(1);
    expect(
      within(sourceCard).queryByRole('button', { name: 'cindyMake.overview.details' }),
    ).toBeNull();
    expect(within(sourceCard).queryByText(source.baseCommit!.slice(0, 12))).toBeNull();
    await h.pushSource({ ...source, currentBranch: 'feature/next', mainCommit: 'e'.repeat(40) });
    expect(within(sourceCard).getByText(source.path)).toBeTruthy();
    expect(within(sourceCard).queryByText('feature/next')).toBeNull();
    expect(within(sourceCard).getByText('e'.repeat(12))).toBeTruthy();
    expect(
      within(sourceCard).getByRole('button', { name: 'cindyMake.sync.action' }),
    ).toBeTruthy();
    expect(
      within(sourceCard).getByRole('button', { name: 'settings.cindyMake.source.reset' }),
    ).toBeTruthy();
  });

  it.each(['waiting', 'checkingRemote', 'checkingLocal', 'cloning', 'installing'] as const)(
    'shows a source job %s elsewhere and stops it from Settings',
    async (phase) => {
      const h = harness();
      h.api.getCindyMakeSourceStatus.mockResolvedValue({
        status: 'preparing',
        path: 'C:\\managed\\source',
        ref: 'main',
        phase,
      });
      renderVersions(<CindyMakeSection />);
      const sourceCard = await screen.findByRole('region', {
        name: 'settings.cindyMake.source.title',
      });
      expect(
        within(sourceCard).getByText(new RegExp('cindyMake.source.phase.' + phase)),
      ).toBeTruthy();
      expect(within(sourceCard).queryByText('settings.cindyMake.source.prepare')).toBeNull();
      expect(
        within(sourceCard).queryByRole('button', { name: 'settings.cindyMake.source.reset' }),
      ).toBeNull();
      fireEvent.click(within(sourceCard).getByText('settings.cindyMake.source.stop'));
      expect(h.api.cancelCindyMakeSource).toHaveBeenCalledTimes(1);
      // Settings itself never started a doctor run for this job.
      expect(h.starts().filter(([name]) => name === 'cindy-make')).toHaveLength(0);
      await h.pushSource({ status: 'cancelled', path: 'C:\\managed\\source', error: 'cancelled' });
      expect(within(sourceCard).getByText('settings.cindyMake.source.retry')).toBeTruthy();
      expect(
        within(sourceCard).getByRole('button', { name: 'settings.cindyMake.source.reset' }),
      ).toBeTruthy();
    },
  );

  it.each([
    ['failed', 'installFailed'],
    ['cancelled', 'cancelled'],
  ] as const)(
    'keeps a restored %s result visible when collapsed, until retry starts',
    async (status, error) => {
      const h = harness();
      const source: MakeSourceStatus = {
        status,
        error,
        path: '/managed/source',
        phase: 'caching',
        dependencies: { resolved: 21, reused: 12, downloaded: 9, added: 0 },
      };
      h.api.getCindyMakeSourceStatus.mockResolvedValue(source);
      renderVersions(<CindyMakeSection />);
      const sourceCard = await screen.findByRole('region', {
        name: 'settings.cindyMake.source.title',
      });
      expect(within(sourceCard).getByRole('status').textContent).toContain(
        'settings.cindyMake.source.status.' + status,
      );
      expect(within(sourceCard).getByRole('status').textContent).toContain(
        'cindyMake.source.errors.' + error,
      );
      fireEvent.click(within(sourceCard).getByText('settings.cindyMake.source.retry'));
      expect(within(sourceCard).getByRole('status').textContent).toBe(
        'settings.cindyMake.source.status.preparing',
      );
      await h.pushSource({ status: 'preparing', path: source.path, phase: 'checkingRemote' });
      expect(within(sourceCard).getByRole('status').textContent).toContain(
        'cindyMake.source.phase.checkingRemote',
      );
      expect(within(sourceCard).queryByText('cindyMake.source.errors.' + error)).toBeNull();
      const sourceReads = h.api.getCindyMakeSourceStatus.mock.calls.length;
      await h.pushSource({ status: 'ready', path: source.path });
      expect(h.api.getCindyMakeSourceStatus).toHaveBeenCalledTimes(sourceReads + 1);
      expect(within(sourceCard).getByRole('status').textContent).toBe(
        'cindyMake.summary.included.unverified',
      );
      expect(within(sourceCard).getByText('cindyMake.overview.comparison.unknown')).toBeTruthy();
      expect(
        within(sourceCard).queryByRole('button', { name: 'cindyMake.sync.action' }),
      ).toBeTruthy();
      expect(within(sourceCard).getByText(source.path)).toBeTruthy();
      expect(
        within(sourceCard).getByRole('button', { name: 'cindyMake.sync.action' }),
      ).toBeTruthy();
    },
  );

  it('keeps the clear action for a failed checkout so a dirty tree has a way out', async () => {
    const h = harness();
    const source: MakeSourceStatus = {
      status: 'failed',
      path: 'C:\\Users\\test\\cindy-make\\source',
      channel: 'dev',
      version: '0.0.0-dev',
      ref: 'main',
      error: 'dirty',
    };
    h.api.getCindyMakeSourceStatus = vi.fn(async () => source);
    vi.stubGlobal('electronAPI', {
      ...window.electronAPI,
      cindyMakeMerge: h.api.cindyMakeMerge,
      cindyMakeSync: h.api.cindyMakeSync,
      maker: h.api,
      getCindyMakeState: h.api.getCindyMakeState,
      onCindyMakeState: h.api.onCindyMakeState,
      openCindyMakeToolsDir: h.api.openCindyMakeToolsDir,
      getCindyMakeSourceStatus: h.api.getCindyMakeSourceStatus,
      openCindyMakeSourceDir: vi.fn(async () => ({ success: true })),
      onCindyMakeSourceStatus: h.api.onCindyMakeSourceStatus,
      cancelCindyMakeSource: h.api.cancelCindyMakeSource,
    });
    renderVersions(<CindyMakeSection />);
    await waitFor(() => expect(screen.getByText('cindyMake.source.errors.dirty')).toBeTruthy());
    const sourceCard = screen.getByRole('region', { name: 'settings.cindyMake.source.title' });
    expect(
      within(sourceCard).getByRole('button', { name: 'settings.cindyMake.source.reset' }),
    ).toBeTruthy();
  });

  it('prepares missing source and updates an existing source through the shared Make action', async () => {
    const h = harness();
    const source: MakeSourceStatus = {
      status: 'missing',
      path: 'C:\\Users\\test\\cindy-make\\source',
    };
    h.api.getCindyMakeSourceStatus = vi.fn(async () => source);
    vi.stubGlobal('electronAPI', {
      ...window.electronAPI,
      cindyMakeMerge: h.api.cindyMakeMerge,
      cindyMakeSync: h.api.cindyMakeSync,
      maker: h.api,
      getCindyMakeState: h.api.getCindyMakeState,
      onCindyMakeState: h.api.onCindyMakeState,
      openCindyMakeToolsDir: h.api.openCindyMakeToolsDir,
      getCindyMakeSourceStatus: h.api.getCindyMakeSourceStatus,
      openCindyMakeSourceDir: h.api.openCindyMakeSourceDir,
      onCindyMakeSourceStatus: h.api.onCindyMakeSourceStatus,
      cancelCindyMakeSource: h.api.cancelCindyMakeSource,
    });
    renderVersions(<CindyMakeSection />);
    await waitFor(() => expect(screen.getByText('settings.cindyMake.source.prepare')).toBeTruthy());
    fireEvent.click(screen.getByText('settings.cindyMake.source.prepare'));
    expect(screen.getByText('settings.cindyMake.source.stop')).toBeTruthy();
    expect(h.starts()).toHaveLength(2);
    expect(h.starts()[1][0]).toBe('cindy-make');
    expect(h.starts()[1][1].makeAction).toBe('prepare-source');

    cleanup();
    const ready: MakeSourceStatus = { ...source, status: 'ready', commit: '0123456789abcdef' };
    h.api.getCindyMakeSourceStatus = vi.fn(async () => ready);
    vi.stubGlobal('electronAPI', {
      ...window.electronAPI,
      cindyMakeMerge: h.api.cindyMakeMerge,
      cindyMakeSync: h.api.cindyMakeSync,
      maker: h.api,
      getCindyMakeState: h.api.getCindyMakeState,
      onCindyMakeState: h.api.onCindyMakeState,
      openCindyMakeToolsDir: h.api.openCindyMakeToolsDir,
      getCindyMakeSourceStatus: h.api.getCindyMakeSourceStatus,
      openCindyMakeSourceDir: h.api.openCindyMakeSourceDir,
      onCindyMakeSourceStatus: h.api.onCindyMakeSourceStatus,
      cancelCindyMakeSource: h.api.cancelCindyMakeSource,
    });
    renderVersions(<CindyMakeSection />);
    await waitFor(() =>
      expect(screen.getByRole('button', { name: 'cindyMake.sync.action' })).toBeTruthy(),
    );
    fireEvent.click(screen.getByRole('button', { name: 'cindyMake.sync.action' }));
    await waitFor(() =>
      expect(h.api.cindyMakeSync).toHaveBeenCalledWith(expect.objectContaining({ action: 'sync' })),
    );
    expect(h.api.cindyMakeMerge).not.toHaveBeenCalled();
  });

  it('disables manual source sync while a personal version is being built', async () => {
    const h = harness({ busy: true, activeWork: true, build: { status: 'checking' } });
    h.api.getCindyMakeSourceStatus.mockResolvedValue({
      status: 'ready',
      path: 'managed-source',
      ref: 'main',
    });
    renderVersions(<CindyMakeSection />);
    const button = await screen.findByRole('button', { name: 'cindyMake.sync.action' });
    await waitFor(() => expect((button as HTMLButtonElement).disabled).toBe(true));
    fireEvent.click(button);
    expect(h.api.cindyMakeMerge).not.toHaveBeenCalled();
    expect(h.api.cindyMakeSync).not.toHaveBeenCalled();
  });

  it('confirms and starts clearing the source without re-pulling it', async () => {
    const h = harness();
    const source: MakeSourceStatus = {
      status: 'ready',
      path: 'C:\\Users\\test\\cindy-make\\source',
      channel: 'dev',
      ref: 'main',
      commit: '0123456789abcdef',
    };
    h.api.getCindyMakeSourceStatus = vi.fn(async () => source);
    vi.stubGlobal('electronAPI', {
      ...window.electronAPI,
      cindyMakeMerge: h.api.cindyMakeMerge,
      cindyMakeSync: h.api.cindyMakeSync,
      maker: h.api,
      getCindyMakeState: h.api.getCindyMakeState,
      onCindyMakeState: h.api.onCindyMakeState,
      openCindyMakeToolsDir: h.api.openCindyMakeToolsDir,
      getCindyMakeSourceStatus: h.api.getCindyMakeSourceStatus,
      openCindyMakeSourceDir: h.api.openCindyMakeSourceDir,
      onCindyMakeSourceStatus: h.api.onCindyMakeSourceStatus,
      cancelCindyMakeSource: h.api.cancelCindyMakeSource,
    });
    renderVersions(<CindyMakeSection />);
    fireEvent.click(await screen.findByRole('button', { name: 'settings.cindyMake.source.reset' }));
    await waitFor(() => expect(h.starts()).toHaveLength(2));
    expect(h.starts()[1][0]).toBe('cindy-make');
    expect(h.starts()[1][1].makeAction).toBe('clear-source');
  });

  it('keeps environment results while the source card progresses, fails and retries', async () => {
    const h = harness();
    const source: MakeSourceStatus = { status: 'ready', path: 'C:\\managed\\source', ref: 'main' };
    h.api.getCindyMakeSourceStatus.mockResolvedValue(source);
    renderEnvironment(<CindyMakeSection />);
    await h.complete(h.starts()[0][1].doctorRunId!, '22.23.2', [
      { id: 'node', status: 'passed', version: '22.23.2' },
      { id: 'native', status: 'missing' },
    ]);
    const environmentCard = screen.getByRole('region', { name: 'cindyMakeDoctor.title' });
    const previousEnvironment = environmentCard.textContent;
    fireEvent.click(screen.getByRole('tab', { name: 'settings.cindyMake.tabs.versions' }));
    const sourceCard = screen.getByRole('region', { name: 'settings.cindyMake.source.title' });
    fireEvent.click(within(sourceCard).getByRole('button', { name: 'cindyMake.sync.action' }));
    expect(within(sourceCard).getByText('settings.cindyMake.source.path')).toBeTruthy();
    expect(environmentCard.textContent).toBe(previousEnvironment);
    await h.pushSource({
      ...source,
      status: 'preparing',
      phase: 'fetching',
      progress: { stage: 'receiving', percent: 43 },
    });
    expect(within(sourceCard).queryByRole('progressbar')).toBeNull();
    expect(within(sourceCard).getByText('cindyMake.source.gitProgress.receiving')).toBeTruthy();
    expect(within(sourceCard).getByText(/cindyMake.source.phase.fetching/)).toBeTruthy();
    expect(within(sourceCard).getByRole('status').textContent).toContain('(43%)');
    await h.pushSource({
      ...source,
      status: 'preparing',
      phase: 'caching',
      dependencies: { resolved: 21, reused: 12, downloaded: 9, added: 0 },
    });
    expect(within(sourceCard).getByRole('status').textContent).toContain(
      'cindyMake.source.phase.caching',
    );
    expect(within(sourceCard).getByRole('status').textContent).toContain(
      'cindyMake.source.cacheProgress 21 / 12 / 9',
    );
    expect(within(sourceCard).queryByText('(43%)')).toBeNull();
    expect(within(sourceCard).queryByText('cindyMake.code.dependencyActivity.packages')).toBeNull();
    expect(environmentCard.textContent).toBe(previousEnvironment);
    await h.pushSource({ ...source, status: 'failed', error: 'gitUnavailable' });
    expect(within(sourceCard).getByText('cindyMake.source.errors.gitUnavailable')).toBeTruthy();
    expect(within(sourceCard).queryByRole('progressbar')).toBeNull();
    expect(environmentCard.textContent).toBe(previousEnvironment);
    fireEvent.click(within(sourceCard).getByText('settings.cindyMake.source.retry'));
    expect(within(sourceCard).queryByText('cindyMake.source.errors.gitUnavailable')).toBeNull();
    expect(h.starts()).toHaveLength(2);
    expect(
      h
        .starts()
        .slice(1)
        .every(([name, ctx]) => name === 'cindy-make' && ctx.makeAction === 'prepare-source'),
    ).toBe(true);
    expect(within(sourceCard).getByText('settings.cindyMake.source.stop')).toBeTruthy();
    expect(environmentCard.textContent).toBe(previousEnvironment);
  });

  it('does not cancel an initial environment check or restart source work when the environment is rechecked', async () => {
    const h = harness();
    h.api.getCindyMakeSourceStatus.mockResolvedValue({
      status: 'missing',
      path: 'C:\\managed\\source',
    });
    renderVersions(<CindyMakeSection />);
    await waitFor(() => expect(screen.getByText('settings.cindyMake.source.prepare')).toBeTruthy());
    fireEvent.click(screen.getByText('settings.cindyMake.source.prepare'));
    expect(h.starts()).toHaveLength(2);
    expect(
      h.api.executeDesktopCommand.mock.calls.some(([, ctx]) => ctx.doctorAction === 'cancel'),
    ).toBe(false);
    await h.complete(h.starts()[0][1].doctorRunId!, '22.23.2');
    expect(screen.getByText('settings.cindyMake.source.stop')).toBeTruthy();
    fireEvent.click(screen.getByRole('tab', { name: 'settings.cindyMake.tabs.environment' }));
    expect(
      within(screen.getByRole('region', { name: 'cindyMakeDoctor.title' })).getByRole('status')
        .textContent,
    ).toContain('cindyMakeDoctor.passed');
    fireEvent.click(screen.getByText('cindyMakeDoctor.recheck'));
    expect(h.starts()).toHaveLength(3);
    expect(h.starts()[2][0]).toBe('cindy-make-doctor');
    fireEvent.click(screen.getByRole('tab', { name: 'settings.cindyMake.tabs.versions' }));
    expect(screen.getByText('settings.cindyMake.source.stop')).toBeTruthy();
    expect(
      h.api.executeDesktopCommand.mock.calls.some(([, ctx]) => ctx.doctorAction === 'cancel'),
    ).toBe(false);
  });

  it('does not restore a previously tracked completion after switching check modes back', async () => {
    const environment = harness();
    renderEnvironment(<CindyMakeSection />);
    await environment.complete(environment.starts()[0][1].doctorRunId!, '');
    const preparation: MakeDoctorReport = {
      runId: 'tracked-preparation',
      mode: 'prepare',
      platform: 'win32',
      arch: 'x64',
      status: 'running',
      checks: [{ id: 'node', status: 'installing' }],
    };
    await environment.pushState({ environmentPrepare: { active: true, report: preparation } });
    await environment.pushState({
      environmentPrepare: {
        active: false,
        report: { ...preparation, status: 'completed', checks: [{ id: 'node', status: 'passed' }] },
      },
    });
    expect(screen.getByRole('status').textContent).toContain('cindyMake.prepare.passed');
    fireEvent.click(screen.getByRole('switch'));
    await environment.complete(environment.starts()[1][1].doctorRunId!, '');
    fireEvent.click(screen.getByRole('switch'));
    await environment.complete(environment.starts()[2][1].doctorRunId!, '', [
      { id: 'node', status: 'missing' },
    ]);
    expect(screen.getByRole('status').textContent).toContain('cindyMakeDoctor.needsAttention');
    expect(screen.getByText('cindyMake.prepare.install')).toBeTruthy();
  });

  it('changing the switch leaves the old check running, ignores late results, and carries the option into the chat command', async () => {
    const h = harness();
    const view = renderEnvironment(<CindyMakeSection />);
    const oldRun = h.starts()[0][1].doctorRunId!;
    fireEvent.click(screen.getByRole('switch'));
    expect(
      h.api.executeDesktopCommand.mock.calls.some(([, ctx]) => ctx.doctorAction === 'cancel'),
    ).toBe(false);
    const latest = h.starts()[1][1];
    expect(latest.forceManagedTools).toBe(true);
    await h.complete(latest.doctorRunId!, '22.23.2', [
      { id: 'node', status: 'passed', version: '22.23.2' },
      { id: 'git', status: 'missing' },
    ]);
    await h.complete(oldRun, '18.0.0');
    fireEvent.click(screen.getByText('cindyMakeDoctor.details'));
    expect(screen.getByText('22.23.2')).toBeTruthy();
    expect(screen.queryByText('18.0.0')).toBeNull();
    fireEvent.click(screen.getByText('cindyMake.prepare.install'));
    expect(h.starts()).toHaveLength(3);
    expect(h.starts()[2][0]).toBe('cindy-make');
    expect(h.starts()[2][1].forceManagedTools).toBe(true);
    view.unmount();
    expect(h.listeners.size).toBe(0);
    const runId = startMakeDoctor(vi.fn(), h.api, 'cindy-make');
    expect(h.api.executeDesktopCommand).toHaveBeenLastCalledWith('cindy-make', {
      doctorRunId: runId,
      forceManagedTools: true,
    });
    await h.complete(runId, '22.23.2');
    setCindyMakeForceManagedTools(false);
    const normalRun = startMakeDoctor(vi.fn(), h.api, 'cindy-make');
    expect(h.api.executeDesktopCommand).toHaveBeenLastCalledWith('cindy-make', {
      doctorRunId: normalRun,
    });
    await h.complete(normalRun, '24.1.0');
  });

  it('leaving the page only removes its progress subscription and does not cancel the check', () => {
    const h = harness();
    const view = renderEnvironment(<CindyMakeSection />);
    const runId = h.starts()[0][1].doctorRunId;
    expect(h.listeners.size).toBe(1);
    view.unmount();
    expect(h.listeners.size).toBe(0);
    expect(h.api.executeDesktopCommand).not.toHaveBeenCalledWith('cindy-make-doctor', {
      doctorRunId: runId,
      doctorAction: 'cancel',
    });
  });

  it('keeps the environment page but hides and disables the test switch in production', () => {
    vi.stubEnv('DEV', false);
    setCindyMakeForceManagedTools(true);
    const h = harness();
    renderEnvironment(<CindyMakeSection />);
    expect(screen.queryByRole('switch')).toBeNull();
    expect(h.starts()).toHaveLength(1);
    expect(h.starts()[0][1]).not.toHaveProperty('forceManagedTools');
  });
});