Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

6.4 KB
Raw Blame
/** @vitest-environment jsdom */
import { act, cleanup, fireEvent, render, screen } from '@testing-library/react';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { createInstance } from 'i18next';
import { I18nextProvider, initReactI18next } from 'react-i18next';

import type { WorkbenchMessage } from '../../context/ApiContext';
import zh from '../../i18n/zh.json';
import { FailureRetry } from './FailureRetry';

const i18n = createInstance();
void i18n.use(initReactI18next).init({
  lng: 'zh', resources: { zh: { translation: zh } }, interpolation: { escapeValue: false },
});
const notice = {
  id: 'notice', session_id: 'session', type: 'notify', author: 'agent', source: 'agent',
  text: '模型连接中断', content: {},
  metadata: { event: 'backend_failure', failure_id: 'failed', turn_id: 'turn' },
} as WorkbenchMessage;

afterEach(cleanup);

function mount(props: Partial<Parameters<typeof FailureRetry>[0]> = {}) {
  const onRetry = vi.fn().mockResolvedValue(true);
  render(
    <I18nextProvider i18n={i18n}>
      <FailureRetry message={notice} onRetry={onRetry} {...props} />
    </I18nextProvider>,
  );
  return onRetry;
}

describe('failed-turn retry action', () => {
  it.each([
    '⚠️ 本轮执行被中断——Avibe 服务在它运行期间重启。如需继续,请点击“重试”(如有显示),或发送“继续”。',
    '[Avibe Harness] 一项后台任务已随 Avibe 重启停止。如需继续,请点击“重试”(如有显示),或发送“继续”给 Agent。',
  ])('MESSAGE-DELIVERY-320: offers retry for a linked restart notice: %s', async (text) => {
    const onRetry = mount({
      message: {
        ...notice,
        text,
        metadata: {
          event: 'backend_failure', backend: 'codex',
          failure_id: 'turn:interrupted-turn', turn_id: 'interrupted-turn', detached: false,
          replayed: true,
        },
      },
    });
    await act(async () => { fireEvent.click(screen.getByRole('button', { name: '重试' })); });
    expect(onRetry).toHaveBeenCalledExactlyOnceWith('notice');
    expect(screen.getByRole('button', { name: '已请求重试' })).toBeTruthy();
  });

  it('MESSAGE-DELIVERY-322: offers no retry when the interrupted command continues as a Watch', () => {
    mount({
      message: {
        ...notice,
        text: '⚠️ 本轮执行被中断——Avibe 服务在它运行期间重启。正在运行的命令已作为 Watch w_1 继续执行,结果会发到这里。',
        metadata: {
          event: 'backend_failure', backend: 'vibey',
          failure_id: 'turn:interrupted-turn', turn_id: 'interrupted-turn', detached: false,
          replayed: true, watch_ids: ['w_1'],
        },
      },
    });
    expect(screen.queryByRole('button', { name: '重试' })).toBeNull();
  });

  it('locks a click burst and reports only an admitted retry', async () => {
    let finish!: (value: boolean) => void;
    const pending = new Promise<boolean>((resolve) => { finish = resolve; });
    const onRetry = vi.fn().mockReturnValue(pending);
    mount({ onRetry });
    const button = screen.getByRole('button', { name: '重试' });
    fireEvent.click(button);
    fireEvent.click(button);
    expect(onRetry).toHaveBeenCalledExactlyOnceWith('notice');
    expect((button as HTMLButtonElement).disabled).toBe(true);
    await act(async () => { finish(true); });
    expect(screen.getByRole('button', { name: '已请求重试' })).toBeTruthy();
  });

  it('unlocks after an unsuccessful submission', async () => {
    const onRetry = vi.fn().mockResolvedValueOnce(false).mockResolvedValueOnce(true);
    mount({ onRetry });
    await act(async () => { fireEvent.click(screen.getByRole('button')); });
    expect((screen.getByRole('button') as HTMLButtonElement).disabled).toBe(false);
    await act(async () => { fireEvent.click(screen.getByRole('button')); });
    expect(onRetry).toHaveBeenCalledTimes(2);
  });

  it.each(['queued', 'claimed', 'accepted'])('restores %s feedback on reload', (state) => {
    mount({ message: { ...notice, content: { failure_retry: { delivery_id: 'delivery', state } } } });
    expect((screen.getByRole('button', { name: '已请求重试' }) as HTMLButtonElement).disabled).toBe(true);
  });

  it.each(['queued', 'claimed'])('unlocks a %s retry after durable retirement without remounting', async (state) => {
    const onRetry = vi.fn().mockResolvedValue(true);
    const row = (message: WorkbenchMessage) => (
      <I18nextProvider i18n={i18n}>
        <FailureRetry message={message} onRetry={onRetry} />
      </I18nextProvider>
    );
    const { rerender } = render(row(notice));
    await act(async () => { fireEvent.click(screen.getByRole('button')); });
    expect((screen.getByRole('button', { name: '已请求重试' }) as HTMLButtonElement).disabled).toBe(true);
    rerender(row({ ...notice, content: { failure_retry: { delivery_id: 'delivery', state } } }));
    expect((screen.getByRole('button') as HTMLButtonElement).disabled).toBe(true);
    rerender(row({
      ...notice, content: { failure_retry: { delivery_id: 'delivery', state: 'retired' } },
    }));
    expect((screen.getByRole('button', { name: '重试' }) as HTMLButtonElement).disabled).toBe(false);
    await act(async () => { fireEvent.click(screen.getByRole('button')); });
    expect(onRetry).toHaveBeenCalledTimes(2);
    rerender(row({
      ...notice, content: { failure_retry: { delivery_id: 'next-delivery', state: 'accepted' } },
    }));
    expect((screen.getByRole('button', { name: '已请求重试' }) as HTMLButtonElement).disabled).toBe(true);
  });

  it.each([
    { readOnly: true },
    { message: { ...notice, metadata: { ...notice.metadata, event: 'progress' } } },
    { message: { ...notice, metadata: { ...notice.metadata, detached: true } } },
    { message: { ...notice, metadata: { event: 'backend_failure' } } },
    { message: { ...notice, text: 'Avibe 服务重启,本轮执行被中断', metadata: {} } },
    {
      message: {
        ...notice,
        text: '[Avibe Harness] 一项后台任务已随 Avibe 重启停止。如需继续,请点击“重试”(如有显示),或发送“继续”给 Agent。',
        metadata: { event: 'backend_failure', failure_id: 'turn:unbound', replayed: true },
      },
    },
    { message: { ...notice, author: 'user' } as WorkbenchMessage },
  ])('does not offer an ineligible action', (props) => {
    mount(props);
    expect(screen.queryByRole('button')).toBeNull();
  });
});