Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

8.8 KB
Raw Blame
import { ModalHost } from '@lobehub/ui';
import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
import type * as I18next from 'i18next';
import { createElement } from 'react';
import { afterEach, describe, expect, it, vi } from 'vitest';

import verify from '../../../../../packages/locales/src/default/verify';
import DecisionBar from './DecisionBar';
import { openRejectModal } from './modals';

vi.mock('react-i18next', () => ({ useTranslation: () => ({ t: (key: string) => key }) }));
vi.mock('i18next', async (importOriginal) => ({
  ...(await importOriginal<typeof I18next>()),
  t: (key: string) => key,
}));
// The live feedback preview reads SWR and the user store; its own test covers it.
vi.mock('./RejectFeedbackPreview', () => ({ default: () => null }));
afterEach(cleanup);

describe('DecisionBar copy', () => {
  it('keeps the copy prompt action and uses Fix for rerunning the repair', () => {
    // Regression (#18843): the rerun handoff was collapsed into copy-only.
    // Embedded drafts into the composer; standalone copies the repair prompt — both hang off these keys.
    expect(verify['acceptance.bar.copyReview']).toBe('Copy repair prompt');
    expect(verify['acceptance.bar.rerun']).toBe('Fix');
    expect(verify['acceptance.bar.rerunDrafted']).toBe(
      'Added to your composer — review and send it.',
    );
    expect(verify['acceptance.bar.rerunSent']).toBe(
      'Sent to the source conversation — the repair round is starting.',
    );
  });
});

const props = {
  acceptedCount: 0,
  feedbackCount: 1,
  ignoredCount: 0,
  needsFixCount: 1,
  onAccept: vi.fn(),
  onAddComment: vi.fn(),
  onCopyReview: vi.fn(),
  onOpenFeedback: vi.fn(),
  onRejectComment: vi.fn(),
  onRerun: vi.fn(),
  pending: false,
  rerunAvailable: true,
  rerunPending: false,
  state: 'settled' as const,
  statusText: 'Needs a fix',
  totalCount: 1,
};

it('standalone only copies repair instructions even when an origin can rerun', () => {
  render(createElement(DecisionBar, { ...props, embedded: false }));
  expect(screen.queryByRole('button', { name: 'acceptance.bar.rerun' })).toBeNull();
  fireEvent.click(screen.getByRole('button', { name: 'acceptance.bar.copyReview' }));
  expect(props.onCopyReview).toHaveBeenCalledOnce();
  expect(props.onRerun).not.toHaveBeenCalled();
});

it('the portal hands the repair to its conversation', () => {
  render(createElement(DecisionBar, { ...props, embedded: true }));
  expect(screen.queryByRole('button', { name: 'acceptance.bar.copyReview' })).toBeNull();
  fireEvent.click(screen.getByRole('button', { name: 'acceptance.bar.rerun' }));
  expect(props.onRerun).toHaveBeenCalledOnce();
});

it('keeps the repair prompt available after an aggregate rejection', () => {
  render(
    createElement(DecisionBar, { ...props, embedded: false, feedbackCount: 0, state: 'rejected' }),
  );
  expect(screen.getByRole('button', { name: 'acceptance.bar.copyReview' })).toBeTruthy();
  expect(screen.queryByRole('button', { name: 'acceptance.bar.rerun' })).toBeNull();
});

it.each(['', '   ', '  Please add dark mode evidence  '])(
  'allows returning a delivery with an optional reason (%j)',
  async (reason) => {
    const onConfirm = vi.fn().mockResolvedValue(true);
    render(createElement(ModalHost));
    render(
      createElement(DecisionBar, {
        ...props,
        feedbackCount: 0,
        needsFixCount: 0,
        onRejectComment: () =>
          openRejectModal({ acceptanceId: 'acc-1', dispatchAvailable: true, onConfirm }),
      }),
    );

    fireEvent.click(screen.getByRole('button', { name: 'acceptance.bar.rejectComment' }));
    const input = await screen.findByRole('textbox');
    fireEvent.change(input, { target: { value: reason } });
    const submit = screen.getByRole('button', { name: 'acceptance.actions.confirmReject' });
    expect(submit).not.toBeDisabled();
    fireEvent.click(submit);

    await waitFor(() => expect(onConfirm).toHaveBeenCalledWith(reason.trim()));
    await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull());
  },
);

it('without an authoring agent, the reject dialog copies the prompt instead of promising a round', async () => {
  // Regression: the dialog said 打回并发起下一轮 though nothing could start one.
  const onConfirm = vi.fn().mockResolvedValue(true);
  render(createElement(ModalHost));
  render(
    createElement(DecisionBar, {
      ...props,
      feedbackCount: 0,
      needsFixCount: 0,
      onRejectComment: () =>
        openRejectModal({ acceptanceId: 'acc-1', dispatchAvailable: false, onConfirm }),
    }),
  );

  fireEvent.click(screen.getByRole('button', { name: 'acceptance.bar.rejectComment' }));
  fireEvent.change(await screen.findByRole('textbox'), { target: { value: '补一段录屏' } });

  expect(screen.queryByRole('button', { name: 'acceptance.actions.confirmReject' })).toBeNull();
  expect(screen.getByText('acceptance.reject.descriptionCopy')).toBeTruthy();
  fireEvent.click(screen.getByRole('button', { name: 'acceptance.actions.confirmRejectCopy' }));

  await waitFor(() => expect(onConfirm).toHaveBeenCalledWith('补一段录屏'));
});

describe('a teammate who cannot decide', () => {
  // Regression (T-945): a member's decision bar returned null, so the delivery
  // read as one nobody could decide. The pair stays in view, disabled, with why.
  it('sees Reject and Accept disabled, with the reason beside them', () => {
    const onAccept = vi.fn();
    const onRejectComment = vi.fn();
    render(
      createElement(DecisionBar, {
        ...props,
        feedbackCount: 0,
        needsFixCount: 0,
        onAccept,
        onRejectComment,
        readOnlyReason: 'acceptance.bar.readOnlyReason',
      }),
    );

    const reject = screen.getByRole('button', { name: 'acceptance.bar.rejectComment' });
    const accept = screen.getByRole('button', { name: 'acceptance.actions.accept' });
    expect(reject).toBeDisabled();
    expect(accept).toBeDisabled();
    expect(screen.getByText('acceptance.bar.readOnlyReason')).toBeVisible();

    fireEvent.click(reject);
    fireEvent.click(accept);
    expect(onRejectComment).not.toHaveBeenCalled();
    expect(onAccept).not.toHaveBeenCalled();
  });

  it('is not handed the repair handoff a decider gets once feedback is queued', () => {
    render(createElement(DecisionBar, { ...props, readOnlyReason: 'reason' }));

    expect(screen.queryByRole('button', { name: 'acceptance.bar.copyReview' })).toBeNull();
    expect(screen.queryByRole('button', { name: 'acceptance.bar.addComment' })).toBeNull();
    expect(screen.getByRole('button', { name: 'acceptance.actions.accept' })).toBeDisabled();
  });

  it('keeps the reason without actions while a round is still running', () => {
    render(createElement(DecisionBar, { ...props, readOnlyReason: 'reason', state: 'live' }));

    expect(screen.getByText('reason')).toBeVisible();
    expect(screen.queryByRole('button', { name: 'acceptance.actions.accept' })).toBeNull();
  });
});

it('a decider keeps live Reject and Accept without a reason line', () => {
  render(createElement(DecisionBar, { ...props, feedbackCount: 0, needsFixCount: 0 }));

  expect(screen.getByRole('button', { name: 'acceptance.bar.rejectComment' })).toBeEnabled();
  expect(screen.getByRole('button', { name: 'acceptance.actions.accept' })).toBeEnabled();
  expect(screen.queryByTestId('acceptance-decision-reason')).toBeNull();
});

describe('the docked overview', () => {
  it('draws one strip segment per check, coloured by the reviewer decision', () => {
    render(
      createElement(DecisionBar, {
        ...props,
        acceptedCount: 2,
        feedbackCount: 0,
        needsFixCount: 1,
        segments: ['accepted', 'accepted', 'needsFix', 'pending'],
        totalCount: 4,
      }),
    );

    const strip = screen.getByRole('img', { name: 'acceptance.bar.stripLabel' });
    expect([...strip.children].map((node) => node.getAttribute('data-decision'))).toEqual([
      'accepted',
      'accepted',
      'needsFix',
      'pending',
    ]);
  });

  it('spells the tally out beside the state line while a decision is open', () => {
    render(
      createElement(DecisionBar, {
        ...props,
        acceptedCount: 2,
        feedbackCount: 0,
        needsFixCount: 1,
        segments: ['accepted', 'accepted', 'needsFix', 'pending'],
        totalCount: 4,
      }),
    );

    expect(screen.getByText('acceptance.bar.tally.accepted')).toBeVisible();
    expect(screen.getByText('acceptance.bar.tally.needsFix')).toBeVisible();
    expect(screen.getByText('acceptance.bar.tally.pending')).toBeVisible();
    expect(screen.queryByText('acceptance.bar.tally.ignored')).toBeNull();
  });

  it('drops the tally once the delivery is decided', () => {
    render(
      createElement(DecisionBar, {
        ...props,
        feedbackCount: 0,
        segments: ['accepted'],
        state: 'accepted',
      }),
    );

    expect(screen.queryByText('acceptance.bar.tally.accepted')).toBeNull();
  });
});