🤯 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 node
import { MarketAPIError } from '@lobehub/market-sdk';
import { TRPCError } from '@trpc/server';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { agentGroupRouter } from './agentGroup';
const { mockMarketSDK } = vi.hoisted(() => ({
mockMarketSDK: {
agentGroups: {
deprecate: vi.fn(),
getAgentGroupDetail: vi.fn(),
},
headers: {} as Record<string, string>,
},
}));
vi.mock('@/business/server/trpc-middlewares/rbacPermission', () => ({
withScopedPermission: vi.fn(function () {
return (opts: any) => opts.next({ ctx: opts.ctx });
}),
}));
vi.mock('@/database/models/user', () => ({
UserModel: vi.fn(function () {
return {
getUserState: vi.fn(async () => ({ settings: {} })),
};
}),
}));
vi.mock('@/libs/trpc/lambda/middleware', () => ({
marketSDK: vi.fn(function (opts: any) {
return opts.next({
ctx: {
...opts.ctx,
marketSDK: mockMarketSDK,
},
});
}),
marketUserInfo: vi.fn(function (opts: any) {
return opts.next({
ctx: {
...opts.ctx,
marketUserInfo: { email: '[email protected]', name: 'Actor', userId: 'user-1' },
},
});
}),
serverDatabase: vi.fn(function (opts: any) {
return opts.next({ ctx: opts.ctx });
}),
}));
vi.mock('@/libs/trusted-client', () => ({
generateTrustedClientToken: vi.fn(function () {
return 'trust-token';
}),
}));
describe('agentGroupRouter.forkAgentGroup', () => {
let fetchSpy: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
vi.clearAllMocks();
mockMarketSDK.headers = {};
fetchSpy = vi.spyOn(globalThis, 'fetch' as never);
fetchSpy.mockResolvedValue(
new Response(
JSON.stringify({
group: { identifier: 'forked-group' },
groupVersion: { versionNumber: 1 },
memberAgents: [],
}),
{ status: 200 },
),
);
});
it('attributes workspace forks to the acting organization account', async () => {
const caller = agentGroupRouter.createCaller({ serverDB: {}, userId: 'user-1' } as any);
await caller.forkAgentGroup({
actAs: 321,
identifier: 'forked-group',
name: 'Forked Group',
sourceIdentifier: 'source-group',
status: 'published',
visibility: 'public',
} as unknown as Parameters<typeof caller.forkAgentGroup>[0]);
const [, requestInit] = fetchSpy.mock.calls[0] as [string, RequestInit];
expect((requestInit.headers as Record<string, string>)['x-lobe-owner-account-id']).toBe('321');
});
});
describe('agentGroupRouter.checkOwnership', () => {
let fetchSpy: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
vi.clearAllMocks();
mockMarketSDK.agentGroups.getAgentGroupDetail.mockResolvedValue({
group: {
avatar: '👥',
identifier: 'workspace-group',
name: 'Workspace Group',
ownerId: 321,
},
});
fetchSpy = vi.spyOn(globalThis, 'fetch' as never);
fetchSpy.mockResolvedValue(
new Response(JSON.stringify({ accountId: 999, sub: 'user-1' }), {
status: 200,
}),
);
});
it('uses the acting organization account when checking workspace-owned groups', async () => {
const caller = agentGroupRouter.createCaller({ serverDB: {}, userId: 'user-1' } as any);
const result = await caller.checkOwnership({
actAs: 321,
identifier: 'workspace-group',
} as unknown as Parameters<typeof caller.checkOwnership>[0]);
expect(result).toMatchObject({
exists: true,
isOwner: true,
originalGroup: null,
});
});
});
describe('agentGroupRouter.deprecateAgentGroup', () => {
beforeEach(() => {
vi.clearAllMocks();
mockMarketSDK.agentGroups.deprecate.mockResolvedValue({ success: true });
});
it('deprecates the requested group and returns the Market result', async () => {
const caller = agentGroupRouter.createCaller({ serverDB: {}, userId: 'user-1' } as any);
const result = await caller.deprecateAgentGroup({ identifier: 'group-1' });
expect(mockMarketSDK.agentGroups.deprecate).toHaveBeenCalledWith('group-1');
expect(result).toEqual({ success: true });
});
it('maps a Market 404 to NOT_FOUND', async () => {
mockMarketSDK.agentGroups.deprecate.mockRejectedValue(new MarketAPIError(404, 'Not Found'));
const caller = agentGroupRouter.createCaller({ serverDB: {}, userId: 'user-1' } as any);
const error = await caller.deprecateAgentGroup({ identifier: 'missing' }).catch((e) => e);
expect(error).toBeInstanceOf(TRPCError);
expect(error.code).toBe('NOT_FOUND');
});
});