🤯 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
/**
* @vitest-environment happy-dom
*/
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { EMPTY_EDITOR_STATE } from '@/libs/editor/constants';
import { type SaveStatus } from '@/types/saveState';
import EditorCanvas from './index';
const editorProps = vi.hoisted(() => ({
last: undefined as any,
}));
const autoSaveHintProps = vi.hoisted(() => ({
last: undefined as any,
}));
const codeEditorPaneProps = vi.hoisted(() => ({
last: undefined as any,
}));
const editorDocuments = {
json: undefined as unknown,
markdown: '',
};
const editor = {
getDocument: vi.fn((format: 'json' | 'markdown') => editorDocuments[format]),
setDocument: vi.fn((format: 'json' | 'markdown', value: unknown) => {
if (format === 'markdown') {
editorDocuments.markdown = String(value ?? '');
} else {
editorDocuments.json = value;
}
}),
};
const handleContentChange = vi.fn();
const flushSave = vi.fn().mockResolvedValue(undefined);
const retryPromptSave = vi.fn().mockResolvedValue(undefined);
const setHasEdited = vi.fn();
const messageError = vi.fn();
const permissionState = {
allowed: false,
};
const agentStoreMock = vi.hoisted(() => {
const updateAgentConfigById = vi.fn();
return {
listeners: new Set<() => void>(),
state: {
activeAgentId: 'agent-a',
agentMap: {
'agent-a': {
editorData: undefined,
systemRole: 'readonly prompt',
},
} as Record<string, { editorData?: unknown; systemRole?: string }>,
streamingSystemRole: undefined as string | undefined,
streamingSystemRoleAgentId: undefined as string | undefined,
streamingSystemRoleInProgress: false,
updateAgentConfigById,
},
};
});
const { state: agentStoreState } = agentStoreMock;
const { updateAgentConfigById } = agentStoreState;
const profileStoreState = {
editor,
flushSave,
handleContentChange,
hasEdited: false,
lockState: { holderId: null, lockedByOther: false, pending: false },
promptLastUpdatedTime: null as Date | null,
promptSaveStatus: 'idle' as SaveStatus,
retryPromptSave,
setHasEdited,
};
type UseSyncExternalStore = <Snapshot>(
subscribe: (onStoreChange: () => void) => () => void,
getSnapshot: () => Snapshot,
getServerSnapshot?: () => Snapshot,
) => Snapshot;
vi.mock('@lobehub/editor/react', () => ({
Editor: Object.assign(
vi.fn((props: any) => {
editorProps.last = props;
return <div data-testid="profile-editor" />;
}),
{ withProps: (_plugin: unknown, props: unknown) => ({ props }) },
),
}));
vi.mock('@lobehub/editor', () => ({
ReactMentionPlugin: vi.fn(),
ReactTablePlugin: vi.fn(),
ReactToolbarPlugin: vi.fn(),
}));
vi.mock('@/components/Editor/AutoSaveHint', () => ({
default: vi.fn((props: any) => {
autoSaveHintProps.last = props;
return (
<button data-testid="prompt-save-status" type="button" onClick={props.onRetry}>
{props.saveStatus}
</button>
);
}),
}));
vi.mock('@/components/CodeEditorPane', () => ({
default: vi.fn((props: any) => {
codeEditorPaneProps.last = props;
return (
<textarea
data-testid="prompt-source-editor"
readOnly={props.readOnly}
value={props.value}
onChange={(event) => props.onChange?.(event.target.value)}
/>
);
}),
}));
vi.mock('@/components/InfoTooltip', () => ({
default: ({ title }: { title: string }) => (
<button aria-label={title} data-testid="prompt-description-tooltip" type="button" />
),
}));
vi.mock('@lobehub/ui', async (importOriginal) => ({
...(await importOriginal<object>()),
toast: {
error: (...args: unknown[]) => messageError(...args),
},
}));
vi.mock('@/features/ChatInput/InputEditor/plugins', () => ({
createChatInputRichPlugins: () => [],
}));
vi.mock('@/hooks/usePermission', () => ({
usePermission: () => ({ allowed: permissionState.allowed, reason: 'requires member' }),
}));
vi.mock('@/store/agent', async () => {
const { useSyncExternalStore } = await vi.importActual<{
useSyncExternalStore: UseSyncExternalStore;
}>('react');
return {
useAgentStore: (selector: any) =>
useSyncExternalStore(
(listener) => {
agentStoreMock.listeners.add(listener);
return () => agentStoreMock.listeners.delete(listener);
},
() => selector(agentStoreState),
() => selector(agentStoreState),
),
};
});
vi.mock('../ProfileEditor/MentionList', () => ({
useMentionOptions: () => undefined,
}));
vi.mock('../store', () => ({
useProfileStore: (selector: any) => selector(profileStoreState),
useStoreApi: () => ({
getState: () => profileStoreState,
}),
}));
vi.mock('./TypoBar', () => ({
default: () => <div />,
}));
vi.mock('./useSlashItems', () => ({
useSlashItems: () => [],
}));
describe('Agent profile EditorCanvas', () => {
beforeEach(() => {
vi.clearAllMocks();
editorProps.last = undefined;
autoSaveHintProps.last = undefined;
codeEditorPaneProps.last = undefined;
localStorage.clear();
permissionState.allowed = false;
agentStoreMock.listeners.clear();
agentStoreState.activeAgentId = 'agent-a';
agentStoreState.agentMap = {
'agent-a': {
editorData: undefined,
systemRole: 'readonly prompt',
},
};
agentStoreState.streamingSystemRole = undefined;
agentStoreState.streamingSystemRoleAgentId = undefined;
agentStoreState.streamingSystemRoleInProgress = false;
profileStoreState.promptLastUpdatedTime = null;
profileStoreState.promptSaveStatus = 'idle';
editorDocuments.json = undefined;
editorDocuments.markdown = '';
updateAgentConfigById.mockResolvedValue(undefined);
});
afterEach(() => {
vi.useRealTimers();
});
it('passes editable=false to the editor when workspace permission blocks edits', () => {
render(<EditorCanvas />);
expect(editorProps.last?.editable).toBe(false);
});
it('uses the rich-editor placeholder key', () => {
render(<EditorCanvas />);
expect(editorProps.last?.lineEmptyPlaceholder).toBe('settingAgent.prompt.editorPlaceholder');
expect(editorProps.last?.placeholder).toBe('settingAgent.prompt.editorPlaceholder');
});
it('switches between the visual editor and Markdown source mode', async () => {
agentStoreState.agentMap = {
'agent-a': {
editorData: undefined,
systemRole: '# Core instructions',
},
};
render(<EditorCanvas />);
act(() => editorProps.last?.onInit());
fireEvent.click(screen.getByRole('button', { name: 'settingAgent.prompt.mode.source' }));
await waitFor(() =>
expect(screen.getByTestId('prompt-source-editor')).toHaveValue('# Core instructions'),
);
expect(codeEditorPaneProps.last?.language).toBe('markdown');
expect(screen.getByRole('button', { name: 'settingAgent.prompt.mode.source' })).toHaveAttribute(
'aria-pressed',
'true',
);
fireEvent.click(screen.getByRole('button', { name: 'settingAgent.prompt.mode.visual' }));
expect(screen.queryByTestId('prompt-source-editor')).not.toBeInTheDocument();
expect(screen.getByRole('button', { name: 'settingAgent.prompt.mode.visual' })).toHaveAttribute(
'aria-pressed',
'true',
);
});
it('seeds source mode from the persisted Markdown when editor data also exists', async () => {
const editorData = { root: { type: 'root', children: ['parsed source'] } };
const persistedMarkdown = '<details>\n\n raw spacing\n\n</details>';
agentStoreState.agentMap = {
'agent-a': {
editorData,
systemRole: persistedMarkdown,
},
};
editorDocuments.markdown = 'normalized markdown';
render(<EditorCanvas />);
act(() => editorProps.last?.onInit());
await waitFor(() => expect(editor.setDocument).toHaveBeenCalledWith('json', editorData));
fireEvent.click(screen.getByRole('button', { name: 'settingAgent.prompt.mode.source' }));
expect(await screen.findByTestId('prompt-source-editor')).toHaveValue(persistedMarkdown);
});
it('ignores source change callbacks caused by a programmatic value sync', async () => {
permissionState.allowed = true;
const initialEditorData = { root: { type: 'root', children: ['initial'] } };
const serverEditorData = { root: { type: 'root', children: ['server'] } };
agentStoreState.agentMap = {
'agent-a': { editorData: initialEditorData, systemRole: 'initial prompt' },
};
render(<EditorCanvas />);
act(() => editorProps.last?.onInit());
fireEvent.click(screen.getByRole('button', { name: 'settingAgent.prompt.mode.source' }));
await waitFor(() =>
expect(screen.getByTestId('prompt-source-editor')).toHaveValue('initial prompt'),
);
act(() => {
agentStoreState.agentMap = {
'agent-a': { editorData: serverEditorData, systemRole: 'server prompt' },
};
agentStoreMock.listeners.forEach((listener) => listener());
});
await waitFor(() =>
expect(screen.getByTestId('prompt-source-editor')).toHaveValue('server prompt'),
);
act(() => codeEditorPaneProps.last?.onChange('server prompt'));
expect(handleContentChange).not.toHaveBeenCalled();
expect(setHasEdited).not.toHaveBeenCalled();
act(() => codeEditorPaneProps.last?.onChange('user prompt'));
expect(handleContentChange).toHaveBeenCalledWith(
'agent-a',
expect.any(Function),
editor,
'user prompt',
);
expect(setHasEdited).toHaveBeenCalledWith(true);
});
it('converts Markdown source edits into the shared rich document and autosave payload', async () => {
permissionState.allowed = true;
agentStoreState.agentMap = {
'agent-a': {
editorData: undefined,
systemRole: '# Initial role',
},
};
render(<EditorCanvas />);
act(() => editorProps.last?.onInit());
await waitFor(() =>
expect(editor.setDocument).toHaveBeenCalledWith('markdown', '# Initial role'),
);
fireEvent.click(screen.getByRole('button', { name: 'settingAgent.prompt.mode.source' }));
fireEvent.change(await screen.findByTestId('prompt-source-editor'), {
target: { value: '# Updated role\n\nFollow the user.' },
});
expect(editor.setDocument).toHaveBeenCalledWith(
'markdown',
'# Updated role\n\nFollow the user.',
);
expect(handleContentChange).toHaveBeenCalledWith(
'agent-a',
expect.any(Function),
editor,
'# Updated role\n\nFollow the user.',
);
expect(setHasEdited).toHaveBeenCalledWith(true);
});
it('does not bubble mode-switch clicks to the click-to-focus wrapper', () => {
// The profile page wraps the canvas in a click-to-focus area; a bubbled
// toggle click would focus the editor and scroll to the caret at the
// document end.
const onWrapperClick = vi.fn();
render(
<div onClick={onWrapperClick}>
<EditorCanvas />
</div>,
);
act(() => editorProps.last?.onInit());
fireEvent.click(screen.getByRole('button', { name: 'settingAgent.prompt.mode.source' }));
fireEvent.click(screen.getByRole('button', { name: 'settingAgent.prompt.mode.visual' }));
expect(onWrapperClick).not.toHaveBeenCalled();
});
it('keeps editor controls without repeating the instruction page heading and help', () => {
render(<EditorCanvas />);
expect(screen.queryByText('settingAgent.prompt.title')).not.toBeInTheDocument();
expect(screen.queryByText('settingAgent.prompt.desc')).not.toBeInTheDocument();
expect(screen.queryByTestId('prompt-description-tooltip')).not.toBeInTheDocument();
expect(
screen.getByRole('button', { name: 'settingAgent.prompt.mode.visual' }),
).toBeInTheDocument();
expect(
screen.getByRole('button', { name: 'settingAgent.prompt.mode.source' }),
).toBeInTheDocument();
const inputSurface = screen.getByTestId('profile-editor').closest('[data-variant="outlined"]');
expect(inputSurface).toBeInTheDocument();
expect(inputSurface).not.toContainElement(
screen.getByRole('button', { name: 'settingAgent.prompt.mode.visual' }),
);
expect(inputSurface).not.toContainElement(
screen.getByRole('button', { name: 'settingAgent.prompt.mode.source' }),
);
});
it('binds a draft save to its original agent and loads the next agent content', async () => {
permissionState.allowed = true;
const agentAEditorData = { root: { type: 'root', children: ['agent-a'] } };
const agentBEditorData = { root: { type: 'root', children: ['agent-b'] } };
agentStoreState.agentMap = {
'agent-a': { editorData: agentAEditorData, systemRole: 'agent-a prompt' },
'agent-b': { editorData: agentBEditorData, systemRole: 'agent-b prompt' },
};
render(<EditorCanvas />);
act(() => editorProps.last?.onInit());
await waitFor(() => expect(editor.setDocument).toHaveBeenCalledWith('json', agentAEditorData));
await act(() => new Promise((resolve) => setTimeout(resolve, 0)));
editorDocuments.json = { root: { type: 'root', children: ['agent-a edited'] } };
editorDocuments.markdown = 'agent-a edited';
act(() => editorProps.last?.onTextChange(editor));
expect(handleContentChange).toHaveBeenCalledWith('agent-a', expect.any(Function), editor);
const savePrompt = handleContentChange.mock.calls.at(-1)?.[1];
await savePrompt('agent-a', {
editorData: editorDocuments.json,
systemRole: editorDocuments.markdown,
});
expect(updateAgentConfigById).toHaveBeenCalledWith(
'agent-a',
{
editorData: editorDocuments.json,
systemRole: editorDocuments.markdown,
},
{ rethrow: true, showErrorMessage: false },
);
act(() => {
agentStoreState.activeAgentId = 'agent-b';
agentStoreMock.listeners.forEach((listener) => listener());
});
await waitFor(() => expect(flushSave).toHaveBeenCalledWith('agent-a'));
expect(editorProps.last?.content).toEqual(agentBEditorData);
act(() => editorProps.last?.onInit());
await waitFor(() => expect(editor.setDocument).toHaveBeenCalledWith('json', agentBEditorData));
});
it('replaces hydrated editor data with a later server config before local editing starts', async () => {
permissionState.allowed = true;
const hydratedEditorData = { root: { type: 'root', children: ['hydrated'] } };
const serverEditorData = { root: { type: 'root', children: ['server'] } };
agentStoreState.agentMap = {
'agent-a': { editorData: hydratedEditorData, systemRole: 'hydrated prompt' },
};
render(<EditorCanvas />);
act(() => editorProps.last?.onInit());
await waitFor(() =>
expect(editor.setDocument).toHaveBeenCalledWith('json', hydratedEditorData),
);
editor.setDocument.mockClear();
act(() => {
agentStoreState.agentMap = {
'agent-a': { editorData: serverEditorData, systemRole: 'server prompt' },
};
agentStoreMock.listeners.forEach((listener) => listener());
});
await waitFor(() => expect(editor.setDocument).toHaveBeenCalledWith('json', serverEditorData));
});
it('ignores the editor delayed callback for a programmatic document sync', async () => {
vi.useFakeTimers();
permissionState.allowed = true;
const editorData = { root: { type: 'root', children: ['programmatic'] } };
agentStoreState.agentMap = {
'agent-a': { editorData, systemRole: 'programmatic prompt' },
};
render(<EditorCanvas />);
act(() => editorProps.last?.onInit());
expect(editor.setDocument).toHaveBeenCalledWith('json', editorData);
const delayedOnTextChange = editorProps.last?.onTextChange;
setTimeout(() => delayedOnTextChange?.(editor), 100);
await act(() => vi.advanceTimersByTimeAsync(100));
expect(handleContentChange).not.toHaveBeenCalled();
expect(setHasEdited).not.toHaveBeenCalled();
});
it('does not overwrite a local draft with a later editor-data refresh', async () => {
permissionState.allowed = true;
const initialEditorData = { root: { type: 'root', children: ['initial'] } };
const serverEditorData = { root: { type: 'root', children: ['server'] } };
agentStoreState.agentMap = {
'agent-a': { editorData: initialEditorData, systemRole: 'initial prompt' },
};
render(<EditorCanvas />);
act(() => editorProps.last?.onInit());
await waitFor(() => expect(editor.setDocument).toHaveBeenCalledWith('json', initialEditorData));
await act(() => new Promise((resolve) => setTimeout(resolve, 0)));
editorDocuments.json = { root: { type: 'root', children: ['local draft'] } };
editorDocuments.markdown = 'local draft';
act(() => editorProps.last?.onTextChange(editor));
editor.setDocument.mockClear();
act(() => {
agentStoreState.agentMap = {
'agent-a': { editorData: serverEditorData, systemRole: 'server prompt' },
};
agentStoreMock.listeners.forEach((listener) => listener());
});
expect(editor.setDocument).not.toHaveBeenCalled();
expect(handleContentChange).toHaveBeenCalledWith('agent-a', expect.any(Function), editor);
});
it('shows failed Prompt save feedback with a local Retry action', () => {
permissionState.allowed = true;
profileStoreState.promptSaveStatus = 'failed';
render(<EditorCanvas />);
expect(screen.getByTestId('prompt-save-status')).toHaveTextContent('failed');
fireEvent.click(screen.getByTestId('prompt-save-status'));
expect(retryPromptSave).toHaveBeenCalledTimes(1);
});
it('falls back to a global toast when an unmounted flush fails', async () => {
permissionState.allowed = true;
flushSave.mockImplementation(async () => {
profileStoreState.promptSaveStatus = 'failed';
});
const { unmount } = render(<EditorCanvas />);
unmount();
await waitFor(() => expect(flushSave).toHaveBeenCalledWith('agent-a'));
await waitFor(() => expect(messageError).toHaveBeenCalledWith('saveAgentConfigFail'));
});
it('does not toast on unmount when the flush succeeds', async () => {
permissionState.allowed = true;
flushSave.mockImplementation(async () => {
profileStoreState.promptSaveStatus = 'saved';
});
const { unmount } = render(<EditorCanvas />);
unmount();
await waitFor(() => expect(flushSave).toHaveBeenCalledWith('agent-a'));
expect(messageError).not.toHaveBeenCalled();
});
it('hides Prompt save feedback before the first edit', () => {
render(<EditorCanvas />);
expect(screen.queryByTestId('prompt-save-status')).not.toBeInTheDocument();
});
it('falls back to systemRole when editorData is an empty root document', async () => {
const emptyEditorData = {
root: { children: [], direction: 'ltr', format: '', indent: 0, type: 'root', version: 1 },
};
agentStoreState.agentMap = {
'agent-a': { editorData: emptyEditorData, systemRole: '# API written prompt' },
};
render(<EditorCanvas />);
expect(editorProps.last?.content).toEqual(EMPTY_EDITOR_STATE);
act(() => editorProps.last?.onInit());
await waitFor(() =>
expect(editor.setDocument).toHaveBeenCalledWith('markdown', '# API written prompt'),
);
expect(editor.setDocument).not.toHaveBeenCalledWith('json', emptyEditorData);
});
it('ignores a stream inherited from the previously active agent', async () => {
permissionState.allowed = true;
const agentBEditorData = { root: { type: 'root', children: ['agent-b'] } };
agentStoreState.activeAgentId = 'agent-b';
agentStoreState.agentMap = {
'agent-b': { editorData: agentBEditorData, systemRole: 'agent-b prompt' },
};
agentStoreState.streamingSystemRole = 'agent-a stream';
agentStoreState.streamingSystemRoleAgentId = 'agent-a';
agentStoreState.streamingSystemRoleInProgress = true;
render(<EditorCanvas />);
act(() => editorProps.last?.onInit());
expect(editor.setDocument).not.toHaveBeenCalledWith('markdown', 'agent-a stream');
await waitFor(() => expect(editor.setDocument).toHaveBeenCalledWith('json', agentBEditorData));
expect(handleContentChange).not.toHaveBeenCalled();
});
});