🤯 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
import { act, renderHook } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { useAgentIdentityForm } from './useAgentIdentityForm';
const mocks = vi.hoisted(() => ({
agentMap: {} as Record<
string,
{ description?: string; name?: string; slug?: string; title?: string }
>,
confirmAgentSlug: vi.fn(),
refreshAgentConfig: vi.fn(),
updateAgentMeta: vi.fn(),
updateAgentSlug: vi.fn(),
}));
vi.mock('@/store/home', () => ({
useHomeStore: (selector: (state: unknown) => unknown) =>
selector({ updateAgentMeta: mocks.updateAgentMeta }),
}));
vi.mock('@/services/agent', () => ({
agentService: {
updateAgentSlug: (...args: unknown[]) => mocks.updateAgentSlug(...args),
},
}));
vi.mock('@/store/agent', () => ({
useAgentStore: (selector: (state: unknown) => unknown) =>
selector({
agentMap: mocks.agentMap,
internal_confirmAgentSlug: mocks.confirmAgentSlug,
internal_refreshAgentConfig: mocks.refreshAgentConfig,
}),
}));
vi.mock('@/store/agent/selectors', () => ({
agentSelectors: {
getAgentMetaById: (agentId: string) => (state: { agentMap: typeof mocks.agentMap }) =>
state.agentMap[agentId] || {},
getAgentSlugById: (agentId: string) => (state: { agentMap: typeof mocks.agentMap }) =>
state.agentMap[agentId]?.slug,
},
}));
const setup = () => {
const onSaved = vi.fn();
const view = renderHook(() => useAgentIdentityForm({ agentId: 'agent-a', onSaved }));
return { onSaved, ...view };
};
beforeEach(() => {
vi.clearAllMocks();
mocks.agentMap = {
'agent-a': {
description: 'Tracks sleep and workouts',
name: 'Alice',
slug: 'old-slug',
title: 'Health Assistant',
},
};
mocks.updateAgentSlug.mockResolvedValue({ success: true });
mocks.updateAgentMeta.mockResolvedValue(undefined);
});
describe('useAgentIdentityForm', () => {
it('seeds every field from the agent it edits', () => {
const { result } = setup();
expect(result.current.name).toBe('Alice');
expect(result.current.title).toBe('Health Assistant');
expect(result.current.slug).toBe('old-slug');
expect(result.current.description).toBe('Tracks sleep and workouts');
});
it('saves name and role through the meta patch, leaving an unchanged slug alone', async () => {
const { result, onSaved } = setup();
act(() => result.current.setName('小艾'));
await act(async () => {
await result.current.save();
});
expect(mocks.updateAgentMeta).toHaveBeenCalledExactlyOnceWith('agent-a', {
description: 'Tracks sleep and workouts',
name: '小艾',
title: 'Health Assistant',
});
expect(mocks.updateAgentSlug).not.toHaveBeenCalled();
expect(onSaved).toHaveBeenCalled();
});
it('saves the trimmed description with the rest of the identity', async () => {
const { result } = setup();
act(() => result.current.setDescription(' Reviews pull requests\n'));
await act(async () => {
await result.current.save();
});
expect(mocks.updateAgentMeta).toHaveBeenCalledExactlyOnceWith('agent-a', {
description: 'Reviews pull requests',
name: 'Alice',
title: 'Health Assistant',
});
});
it('clears the description when the field is emptied', async () => {
const { result } = setup();
act(() => result.current.setDescription(' '));
await act(async () => {
await result.current.save();
});
expect(mocks.updateAgentMeta).toHaveBeenCalledWith(
'agent-a',
expect.objectContaining({ description: '' }),
);
});
it('routes a slug change to its own endpoint, normalized to lowercase', async () => {
const { result, onSaved } = setup();
act(() => result.current.setSlug('New-Slug'));
await act(async () => {
await result.current.save();
});
expect(mocks.updateAgentSlug).toHaveBeenCalledExactlyOnceWith('agent-a', 'new-slug');
// The local `slug → id` copy follows the rename, so neither slug route paints a stale agent.
expect(mocks.confirmAgentSlug).toHaveBeenCalledExactlyOnceWith('agent-a', 'new-slug', {
// The old slug's row may only be persisted, and the rename keeps its scope.
previousSlug: 'old-slug',
scope: expect.any(String),
});
expect(mocks.refreshAgentConfig).toHaveBeenCalledWith('agent-a');
expect(onSaved).toHaveBeenCalled();
});
it('keeps the form open with a reason when the slug is rejected', async () => {
mocks.updateAgentSlug.mockResolvedValue({ reason: 'taken', success: false });
const { result, onSaved } = setup();
act(() => result.current.setSlug('taken-slug'));
await act(async () => {
await result.current.save();
});
expect(result.current.error).toBe('settingAgent.slug.error.taken');
// A rejected slug must not leave name/role persisted behind a closed form.
expect(mocks.updateAgentMeta).not.toHaveBeenCalled();
expect(mocks.confirmAgentSlug).not.toHaveBeenCalled();
expect(onSaved).not.toHaveBeenCalled();
});
describe('builtin agents', () => {
beforeEach(() => {
mocks.agentMap = { 'agent-a': { slug: 'inbox', title: 'Lobe AI' } };
});
it('locks the slug of a builtin agent', () => {
const { result } = setup();
expect(result.current.slugLocked).toBe(true);
});
it('leaves an ordinary agent unlocked', () => {
mocks.agentMap = { 'agent-a': { slug: 'my-agent', title: 'Agent' } };
const { result } = setup();
expect(result.current.slugLocked).toBe(false);
});
// The lock is what the user sees, but the field could still be driven (a
// stale render, a paste before the disabled prop lands) — an edit must never
// reach the endpoint, or the builtin lookup would resolve to nothing and
// mint a second, empty inbox.
it('never sends a slug change for a builtin agent', async () => {
const { result, onSaved } = setup();
act(() => result.current.setSlug('my-inbox'));
await act(async () => {
await result.current.save();
});
expect(mocks.updateAgentSlug).not.toHaveBeenCalled();
expect(mocks.updateAgentMeta).toHaveBeenCalledExactlyOnceWith('agent-a', {
description: '',
name: '',
title: 'Lobe AI',
});
expect(onSaved).toHaveBeenCalled();
});
});
it('clears a stale error as soon as the slug is edited again', async () => {
mocks.updateAgentSlug.mockResolvedValue({ reason: 'taken', success: false });
const { result } = setup();
act(() => result.current.setSlug('taken-slug'));
await act(async () => {
await result.current.save();
});
expect(result.current.error).toBeDefined();
act(() => result.current.setSlug('another-slug'));
expect(result.current.error).toBeUndefined();
});
});