🤯 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 { beforeEach, describe, expect, it, vi } from 'vitest';
import type { LobeChatDatabase } from '@/database/type';
import type { HeteroOperationJwtClaims } from '@/libs/trpc/utils/internalJwt';
import {
HeteroOperationPrincipalError,
resolveActiveHeteroOperationPrincipal,
} from './operationPrincipal';
const { activeUser, hasMembership, hasPermission } = vi.hoisted(() => ({
activeUser: vi.fn(),
hasMembership: vi.fn(),
hasPermission: vi.fn(),
}));
vi.mock('@/libs/oidc-provider/access-control', () => ({ assertOIDCUserActive: activeUser }));
vi.mock('@/database/models/workspace', () => ({
hasActiveWorkspaceMembership: hasMembership,
}));
vi.mock('@/database/models/rbac', () => ({
RbacModel: class {
hasAnyPermission = hasPermission;
},
}));
vi.mock('@/utils/rbac', () => ({ getScopePermissions: () => ['permission'] }));
const claims: HeteroOperationJwtClaims = {
aud: 'urn:lobehub:hetero-operation',
capabilities: ['model:invoke'],
exp: 2,
iat: 1,
iss: 'urn:lobehub:internal',
jti: 'jti-1',
model: 'model-a',
operation_id: 'op-1',
provider_id: 'lobehub',
purpose: 'hetero-operation',
sub: 'user-1',
};
const activeOperation = (overrides: Record<string, unknown> = {}) => ({
id: 'op-1',
metadata: { agentType: 'kimi-code', serverDefaultHeterogeneous: true },
model: 'model-a',
provider: 'lobehub',
status: 'running',
topicId: 'tpc_run',
userId: 'user-1',
workspaceId: null,
...overrides,
});
const dbWithOperation = (operation: unknown) => {
const query = {
from: vi.fn(() => query),
limit: vi.fn(async () => (operation ? [operation] : [])),
where: vi.fn(() => query),
};
return { select: vi.fn(() => query) } as unknown as LobeChatDatabase;
};
describe('resolveActiveHeteroOperationPrincipal', () => {
beforeEach(() => {
vi.clearAllMocks();
activeUser.mockResolvedValue(undefined);
hasMembership.mockResolvedValue(true);
hasPermission.mockResolvedValue(true);
});
it.each(['kimi-code', 'opencode'])(
're-authorizes a running %s operation on each request',
async (agentType) => {
const principal = await resolveActiveHeteroOperationPrincipal({
capability: 'model:invoke',
claims,
db: dbWithOperation(
activeOperation({ metadata: { agentType, serverDefaultHeterogeneous: true } }),
),
operationId: 'op-1',
});
expect(principal).toEqual({
agentType,
operationId: 'op-1',
topicId: 'tpc_run',
userId: 'user-1',
workspaceId: undefined,
});
expect(activeUser).toHaveBeenCalledWith(expect.anything(), 'user-1');
expect(hasPermission).toHaveBeenCalledOnce();
},
);
it('rejects a token that does not grant the requested operation', async () => {
await expect(
resolveActiveHeteroOperationPrincipal({
capability: 'model:invoke',
claims,
db: dbWithOperation(null),
operationId: 'other',
}),
).rejects.toMatchObject({ status: 403 });
expect(activeUser).not.toHaveBeenCalled();
});
it('rejects a settled operation', async () => {
await expect(
resolveActiveHeteroOperationPrincipal({
capability: 'model:invoke',
claims,
db: dbWithOperation(activeOperation({ status: 'done' })),
operationId: 'op-1',
}),
).rejects.toEqual(new HeteroOperationPrincipalError('Operation has already ended', 409));
});
it('lets a terminal-tolerant callback through a settled operation, checks intact', async () => {
// The reporting callbacks (ingest refusal / finish) have to reach their
// handlers on a settled row — that row IS what they report on. Everything
// else about the token is still re-authorized.
await expect(
resolveActiveHeteroOperationPrincipal({
allowTerminalOperation: true,
capability: 'hetero:ingest',
claims: { ...claims, capabilities: ['hetero:ingest'] },
db: dbWithOperation(activeOperation({ status: 'done' })),
operationId: 'op-1',
}),
).resolves.toMatchObject({ operationId: 'op-1', userId: 'user-1' });
expect(hasPermission).toHaveBeenCalledOnce();
// The allowance is opt-in per call site, never implied by the capability:
// token renewal asks for `hetero:ingest` too and must stay strict.
await expect(
resolveActiveHeteroOperationPrincipal({
capability: 'hetero:ingest',
claims: { ...claims, capabilities: ['hetero:ingest'] },
db: dbWithOperation(activeOperation({ status: 'done' })),
operationId: 'op-1',
}),
).rejects.toEqual(new HeteroOperationPrincipalError('Operation has already ended', 409));
});
it('still rejects a terminal-tolerant callback whose token is out of scope', async () => {
await expect(
resolveActiveHeteroOperationPrincipal({
allowTerminalOperation: true,
capability: 'hetero:finish',
claims: { ...claims, capabilities: ['hetero:finish'] },
db: dbWithOperation(activeOperation({ status: 'done', userId: 'someone-else' })),
operationId: 'op-1',
}),
).rejects.toEqual(
new HeteroOperationPrincipalError('Operation is outside the token scope', 403),
);
});
it('rejects a model selection that no longer matches the operation', async () => {
await expect(
resolveActiveHeteroOperationPrincipal({
capability: 'model:invoke',
claims,
db: dbWithOperation(activeOperation({ model: 'model-b' })),
operationId: 'op-1',
}),
).rejects.toMatchObject({ status: 403 });
});
it.each([
['missing server-default marker', { agentType: 'kimi-code' }],
['missing agent type', { serverDefaultHeterogeneous: true }],
['unsupported agent type', { agentType: 'amp', serverDefaultHeterogeneous: true }],
])('rejects model invocation with %s in durable metadata', async (_label, metadata) => {
await expect(
resolveActiveHeteroOperationPrincipal({
capability: 'model:invoke',
claims,
db: dbWithOperation(activeOperation({ metadata })),
operationId: 'op-1',
}),
).rejects.toEqual(
new HeteroOperationPrincipalError('Operation token has no valid server model selection', 403),
);
});
it('rejects a model-invocation token without model and provider claims', async () => {
const { model: _model, provider_id: _providerId, ...unscopedClaims } = claims;
await expect(
resolveActiveHeteroOperationPrincipal({
capability: 'model:invoke',
claims: unscopedClaims,
db: dbWithOperation(activeOperation()),
operationId: 'op-1',
}),
).rejects.toMatchObject({ status: 403 });
});
it('rechecks workspace membership and RBAC', async () => {
hasMembership.mockResolvedValue(false);
const workspaceClaims = { ...claims, workspace_id: 'workspace-1' };
const db = dbWithOperation(activeOperation({ workspaceId: 'workspace-1' }));
await expect(
resolveActiveHeteroOperationPrincipal({
capability: 'model:invoke',
claims: workspaceClaims,
db,
operationId: 'op-1',
}),
).rejects.toMatchObject({ status: 403 });
hasMembership.mockResolvedValue(true);
hasPermission.mockResolvedValue(false);
await expect(
resolveActiveHeteroOperationPrincipal({
capability: 'model:invoke',
claims: workspaceClaims,
db,
operationId: 'op-1',
}),
).rejects.toMatchObject({ status: 403 });
});
});