173 lines
10 KiB
TypeScript
173 lines
10 KiB
TypeScript
import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react';
|
||
import { createMemoryRouter, RouterProvider } from 'react-router-dom';
|
||
import { afterEach, beforeEach, expect, it, vi } from 'vitest';
|
||
import CloudAgents from '@/pages/CloudAgents';
|
||
import { EMPTY_CLOUD_CONFIGURATION } from '../../shared/cloud-agents';
|
||
|
||
const api = vi.hoisted(() => ({ list: vi.fn(), create: vi.fn(), get: vi.fn(), save: vi.fn(), call: vi.fn(), events: vi.fn(), recovery: vi.fn(), remember: vi.fn(), resolvePending: vi.fn() }));
|
||
const auth = vi.hoisted(() => ({ user: { userId: 'creator' } }));
|
||
vi.mock('@/lib/cloud-agents-api', () => ({ cloudAgentsApi: api }));
|
||
vi.mock('@/stores/auth', () => ({ useAuthStore: (selector: (state: typeof auth) => unknown) => selector(auth) }));
|
||
const draft = {
|
||
slug: 'ml-' + 'a'.repeat(32), name: '写作搭档', purpose: '帮助写作', system_prompt: '原始指令',
|
||
draft_revision: 1, updated_at: '2026-09-10T00:00:00Z',
|
||
configuration: EMPTY_CLOUD_CONFIGURATION, published_version: null, enabled: true,
|
||
};
|
||
function open() {
|
||
const router = createMemoryRouter([
|
||
{ path: '/cloud-agents', element: <CloudAgents /> },
|
||
{ path: '/module-select', element: <div>模块首页</div> },
|
||
], { initialEntries: ['/cloud-agents'] });
|
||
const view = render(<RouterProvider router={router} />);
|
||
return { router, ...view };
|
||
}
|
||
beforeEach(() => {
|
||
vi.resetAllMocks(); auth.user.userId = 'creator';
|
||
api.recovery.mockResolvedValue({ recent: null, pending: [] });
|
||
api.remember.mockResolvedValue({ saved: true });
|
||
api.list.mockResolvedValue({ agents: [draft], next_cursor: null });
|
||
api.call.mockImplementation(async (operation: string) => operation === 'knowledge'
|
||
? { databases: [], models: [] } : { models: [], resources: {}, pricing: null });
|
||
});
|
||
afterEach(cleanup);
|
||
|
||
it('restores the exact previous thread and presents an uncertain operation without automatically replaying it', async () => {
|
||
const published = { ...draft, published_version: 1 };
|
||
const pending = { id: 'submit:pending', operation: 'submit', input: { slug: draft.slug, request_id: 'same-request', thread_id: 'older-thread', query: '上次未确认输入' }, created_at: '2026-09-10T01:00:00Z' };
|
||
api.recovery.mockResolvedValue({ recent: { slug: draft.slug, thread_id: 'older-thread', mode: 'published' }, pending: [pending] });
|
||
api.get.mockResolvedValue(published);
|
||
api.call.mockImplementation(async (operation: string, input) => {
|
||
if (operation === 'threads') return { threads: ['newer-thread', 'older-thread'].map(thread_id => ({ thread_id, client_thread_id: thread_id, mode: 'published', title: thread_id })), next_offset: null };
|
||
if (operation === 'history') return { thread_id: input.thread_id, messages: [{ id: 1, role: 'assistant', content: '恢复了旧对话' }], run: null, next_offset: null };
|
||
if (operation === 'attachments') return { attachments: [] };
|
||
if (operation === 'files') return { files: [] };
|
||
throw new Error(operation);
|
||
});
|
||
open();
|
||
await screen.findByText('恢复了旧对话');
|
||
expect(screen.getByLabelText('历史对话')).toHaveValue('older-thread');
|
||
expect(api.resolvePending).not.toHaveBeenCalled();
|
||
expect(api.call.mock.calls.some(call => call[0] === 'submit')).toBe(false);
|
||
api.resolvePending.mockResolvedValue({ result: { request_id: 'same-request', status: 'queued' } });
|
||
api.recovery.mockResolvedValue({ recent: null, pending: [] });
|
||
fireEvent.click(screen.getByRole('button', { name: '重试并确认结果' }));
|
||
await screen.findByText('发送消息的结果已确认。');
|
||
expect(api.resolvePending).toHaveBeenCalledWith('submit:pending', false);
|
||
});
|
||
|
||
it('keeps creation input and its uncertain operation when an existing card is clicked', async () => {
|
||
api.create.mockRejectedValueOnce(new Error('连接中断')).mockResolvedValueOnce(draft);
|
||
open();
|
||
const existing = await screen.findByRole('button', { name: /写作搭档/ });
|
||
fireEvent.click(screen.getByRole('button', { name: '创建智能体' }));
|
||
fireEvent.change(screen.getByLabelText('名称'), { target: { value: '新搭档' } });
|
||
fireEvent.change(screen.getByLabelText('用途'), { target: { value: '新用途' } });
|
||
fireEvent.click(existing);
|
||
expect(screen.queryByTestId('cloud-agent-editor')).not.toBeInTheDocument();
|
||
expect(screen.getByLabelText('名称')).toHaveValue('新搭档');
|
||
fireEvent.click(screen.getByRole('button', { name: '创建草稿' }));
|
||
await screen.findByRole('alert');
|
||
fireEvent.click(existing);
|
||
expect(screen.queryByTestId('cloud-agent-editor')).not.toBeInTheDocument();
|
||
fireEvent.click(screen.getByRole('button', { name: '重试创建' }));
|
||
await screen.findByTestId('cloud-agent-editor');
|
||
expect(api.create.mock.calls[1][0]).toEqual(api.create.mock.calls[0][0]);
|
||
});
|
||
|
||
it('keeps the same creation operation after a lost response', async () => {
|
||
api.list.mockResolvedValue({ agents: [], next_cursor: null });
|
||
api.create.mockRejectedValueOnce(new Error('连接中断')).mockResolvedValueOnce(draft);
|
||
open();
|
||
fireEvent.click(await screen.findByText('创建第一个智能体'));
|
||
fireEvent.change(screen.getByLabelText('名称'), { target: { value: '写作搭档' } });
|
||
fireEvent.change(screen.getByLabelText('用途'), { target: { value: '帮助写作' } });
|
||
fireEvent.click(screen.getByRole('button', { name: '创建草稿' }));
|
||
await screen.findByRole('alert');
|
||
fireEvent.click(screen.getByRole('button', { name: '重试创建' }));
|
||
await screen.findByTestId('cloud-agent-editor');
|
||
expect(api.create).toHaveBeenCalledTimes(2);
|
||
expect(api.create.mock.calls[1][0]).toEqual(api.create.mock.calls[0][0]);
|
||
});
|
||
|
||
it('preserves local input on conflicts and requires an explicit choice before retry', async () => {
|
||
api.save.mockRejectedValueOnce(new Error('草稿已更新'));
|
||
api.get.mockResolvedValue({ ...draft, draft_revision: 2, system_prompt: '其他设备内容' });
|
||
open();
|
||
fireEvent.click(await screen.findByRole('button', { name: /写作搭档/ }));
|
||
fireEvent.change(screen.getByLabelText('角色与指令'), { target: { value: '我的修改' } });
|
||
fireEvent.click(screen.getByRole('button', { name: '保存草稿' }));
|
||
await screen.findByText('云端已有修订 2,本地输入仍保留');
|
||
expect(screen.getByLabelText('角色与指令')).toHaveValue('我的修改');
|
||
expect(screen.getByRole('button', { name: '保存草稿' })).toBeDisabled();
|
||
fireEvent.click(screen.getByText('保留我的内容,继续编辑'));
|
||
api.save.mockResolvedValue({ ...draft, draft_revision: 3, system_prompt: '我的修改' });
|
||
fireEvent.click(screen.getByRole('button', { name: '保存草稿' }));
|
||
await screen.findByRole('button', { name: '已保存' });
|
||
expect(api.save.mock.calls[1][1]).toMatchObject({ expected_revision: 2, system_prompt: '我的修改' });
|
||
});
|
||
|
||
it('blocks module navigation with unsaved edits, cancels leaving, then saves before proceeding', async () => {
|
||
const { router } = open();
|
||
fireEvent.click(await screen.findByRole('button', { name: /写作搭档/ }));
|
||
fireEvent.change(screen.getByLabelText('名称'), { target: { value: '新的名称' } });
|
||
await act(async () => { await router.navigate('/module-select'); });
|
||
expect(screen.getByRole('dialog', { name: '未保存的修改' })).toBeVisible();
|
||
expect(router.state.location.pathname).toBe('/cloud-agents');
|
||
fireEvent.click(screen.getByText('继续编辑'));
|
||
expect(screen.getByLabelText('名称')).toHaveValue('新的名称');
|
||
await act(async () => { await router.navigate('/module-select'); });
|
||
api.save.mockResolvedValue({ ...draft, name: '新的名称', draft_revision: 2 });
|
||
fireEvent.click(screen.getByText('保存并离开'));
|
||
await screen.findByText('模块首页');
|
||
expect(api.save).toHaveBeenCalledTimes(1);
|
||
});
|
||
|
||
it('clears the previous account workspace and ignores its delayed list', async () => {
|
||
let finish!: (value: unknown) => void;
|
||
api.list.mockImplementationOnce(() => new Promise((resolve) => { finish = resolve; }))
|
||
.mockResolvedValue({ agents: [], next_cursor: null });
|
||
const view = open();
|
||
auth.user.userId = 'other';
|
||
view.rerender(<RouterProvider key="other-account" router={view.router} />);
|
||
await screen.findByText('你的第一个智能体,从这里开始');
|
||
await act(async () => finish({ agents: [draft], next_cursor: null }));
|
||
await waitFor(() => expect(screen.queryByText('写作搭档')).not.toBeInTheDocument());
|
||
});
|
||
|
||
it('warns before leaving an uncertain creation and keeps its retry intent', async () => {
|
||
api.list.mockResolvedValue({ agents: [], next_cursor: null });
|
||
api.create.mockRejectedValue(new Error('连接中断'));
|
||
const { router } = open();
|
||
fireEvent.click(await screen.findByText('创建第一个智能体'));
|
||
fireEvent.change(screen.getByLabelText('名称'), { target: { value: '写作搭档' } });
|
||
fireEvent.change(screen.getByLabelText('用途'), { target: { value: '帮助写作' } });
|
||
fireEvent.click(screen.getByRole('button', { name: '创建草稿' }));
|
||
await screen.findByRole('alert');
|
||
await act(async () => { await router.navigate('/module-select'); });
|
||
expect(screen.getByRole('dialog', { name: '离开创建' })).toBeVisible();
|
||
fireEvent.click(screen.getByRole('button', { name: '继续创建' }));
|
||
fireEvent.click(screen.getByRole('button', { name: '重试创建' }));
|
||
await waitFor(() => expect(api.create).toHaveBeenCalledTimes(2));
|
||
expect(api.create.mock.calls[1][0]).toEqual(api.create.mock.calls[0][0]);
|
||
});
|
||
|
||
|
||
it('keeps unsent preview text when configuration changes and waits before switching saved revision', async () => {
|
||
const configured = { ...draft, configuration: { ...EMPTY_CLOUD_CONFIGURATION, model: 'test-model' } };
|
||
api.list.mockResolvedValue({ agents: [configured], next_cursor: null });
|
||
api.save.mockResolvedValue({ ...configured, name: '新的名称', draft_revision: 2 });
|
||
open();
|
||
fireEvent.click(await screen.findByRole('button', { name: /写作搭档/ }));
|
||
const prompt = await screen.findByPlaceholderText('输入消息或任务目标…');
|
||
fireEvent.change(prompt, { target: { value: '还未发送的问题' } });
|
||
fireEvent.change(screen.getByLabelText('名称'), { target: { value: '新的名称' } });
|
||
expect(prompt).toHaveValue('还未发送的问题');
|
||
expect(screen.getByText(/预览仍使用已保存的修订 1/)).toBeVisible();
|
||
expect(screen.getByRole('button', { name: '保存草稿' })).toBeDisabled();
|
||
fireEvent.change(prompt, { target: { value: '' } });
|
||
await waitFor(() => expect(screen.getByRole('button', { name: '保存草稿' })).toBeEnabled());
|
||
fireEvent.click(screen.getByRole('button', { name: '保存草稿' }));
|
||
await screen.findByRole('button', { name: '已保存' });
|
||
expect(api.save).toHaveBeenCalledTimes(1);
|
||
});
|