🤯 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 { TRPCError } from '@trpc/server';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { resolveTaskPatchInvariants } from './patchValidation';
const { assertAgentUsableByMock, findProjectMock } = vi.hoisted(() => ({
assertAgentUsableByMock: vi.fn(),
findProjectMock: vi.fn(),
}));
vi.mock('@/database/models/project', () => ({
ProjectModel: class {
findById = findProjectMock;
},
}));
vi.mock('@/database/utils/agent-access', () => ({
assertAgentUsableBy: assertAgentUsableByMock,
}));
const CALLER = 'me';
const WORKSPACE = 'ws-1';
/** The row `TaskModel.resolve` hands back for `id`. */
const storedTask = (id: string, overrides: Record<string, unknown> = {}) => ({
createdByUserId: CALLER,
id,
projectId: 'project-1',
schedulePattern: null,
scheduleTimezone: null,
visibility: 'public' as const,
...overrides,
});
/**
* The fake task model resolves by id, like the real one — a fixture that
* returned the same row for every id would make the self-parent guard fire in
* every hierarchy case and hide the check under test.
*/
const buildContext = (rows: Record<string, Record<string, unknown>> = {}) => {
const taskModel = {
findAllDescendants: vi.fn().mockResolvedValue([]),
findById: vi.fn(async (id: string) => storedTask(id, rows[id])),
hasDependencyOutsideProject: vi.fn().mockResolvedValue(false),
hasSubtaskOutsideProject: vi.fn().mockResolvedValue(false),
resolve: vi.fn(async (id: string) => storedTask(id, rows[id])),
};
const taskService = {
assertAgentVisibilityCompat: vi.fn(),
assertAssigneeUserVisibilityCompat: vi.fn(),
assertParentVisibilityCompat: vi.fn(),
};
const agentModel = { getAgentVisibility: vi.fn().mockResolvedValue('public') };
const editLockService = { getBlockingHolder: vi.fn().mockResolvedValue(null) };
return {
agentModel,
context: {
agentModel,
editLockService,
serverDB: {} as never,
taskModel: taskModel as never,
taskService: taskService as never,
userId: CALLER,
workspaceId: WORKSPACE,
},
editLockService,
taskModel,
taskService,
};
};
describe('resolveTaskPatchInvariants hierarchy', () => {
beforeEach(() => {
vi.resetAllMocks();
});
it('refuses parenting a task to itself', async () => {
const { context } = buildContext();
const patch = { parentTaskId: 'task-1' };
await expect(
resolveTaskPatchInvariants(context, { data: patch, id: 'task-1', parentTaskId: 'task-1' }),
).rejects.toThrow('Task cannot be parented to itself');
});
it('refuses parenting a task to one of its own descendants', async () => {
const { context, taskModel } = buildContext();
const patch = { parentTaskId: 'child-1' };
taskModel.findAllDescendants.mockResolvedValue([{ id: 'child-1' }]);
await expect(
resolveTaskPatchInvariants(context, { data: patch, id: 'task-1', parentTaskId: 'child-1' }),
).rejects.toThrow('Task cannot be parented to its own descendant');
});
it('refuses a parent from another project', async () => {
const { context } = buildContext({ 'parent-1': { projectId: 'project-2' } });
const patch = { parentTaskId: 'parent-1' };
await expect(
resolveTaskPatchInvariants(context, { data: patch, id: 'task-1', parentTaskId: 'parent-1' }),
).rejects.toThrow('Parent task must belong to the same project');
});
it('refuses a public child under a private parent', async () => {
const { context, taskService } = buildContext({
'task-1': { visibility: 'public' },
'parent-1': { visibility: 'private' },
});
const patch = { parentTaskId: 'parent-1' };
taskService.assertParentVisibilityCompat.mockImplementation(() => {
throw new TRPCError({ code: 'BAD_REQUEST', message: 'Subtask cannot be more public than' });
});
await expect(
resolveTaskPatchInvariants(context, { data: patch, id: 'task-1', parentTaskId: 'parent-1' }),
).rejects.toThrow('Subtask cannot be more public than');
expect(taskService.assertParentVisibilityCompat).toHaveBeenCalledWith('public', 'private');
});
it('accepts clearing the parent', async () => {
const { context, taskModel } = buildContext();
const patch = { parentTaskId: null };
const result = await resolveTaskPatchInvariants(context, {
data: patch,
id: 'task-1',
parentTaskId: null,
});
expect(result.data.parentTaskId).toBeNull();
expect(taskModel.findAllDescendants).not.toHaveBeenCalled();
});
});
describe('resolveTaskPatchInvariants assignment', () => {
beforeEach(() => {
vi.resetAllMocks();
});
it('refuses an assignee agent the caller cannot use', async () => {
const { context } = buildContext();
const patch = { assigneeAgentId: 'agent-1' };
assertAgentUsableByMock.mockRejectedValue(
new TRPCError({ code: 'NOT_FOUND', message: 'Agent not found' }),
);
await expect(
resolveTaskPatchInvariants(context, { data: patch, id: 'task-1' }),
).rejects.toThrow('Assignee agent not found');
});
it('refuses a private assignee agent on a public task', async () => {
const { context, taskService } = buildContext({
'task-1': { visibility: 'public' },
'parent-1': { visibility: 'private' },
});
const patch = { assigneeAgentId: 'agent-1' };
taskService.assertAgentVisibilityCompat.mockImplementation(() => {
throw new TRPCError({ code: 'BAD_REQUEST', message: 'Public task needs a public agent' });
});
await expect(
resolveTaskPatchInvariants(context, { data: patch, id: 'task-1' }),
).rejects.toThrow('Public task needs a public agent');
});
it('checks the assignee before resolving the task', async () => {
const { context, taskModel } = buildContext();
const patch = { assigneeAgentId: 'agent-1' };
assertAgentUsableByMock.mockRejectedValue(
new TRPCError({ code: 'NOT_FOUND', message: 'Agent not found' }),
);
await expect(
resolveTaskPatchInvariants(context, { data: patch, id: 'missing-task' }),
).rejects.toThrow('Assignee agent not found');
expect(taskModel.resolve).not.toHaveBeenCalled();
});
it('refuses a patch that would leave an unusable stored schedule', async () => {
const { context } = buildContext({ 'task-1': { scheduleTimezone: 'Legacy/Zone' } });
const patch = { automationMode: 'schedule' };
await expect(
resolveTaskPatchInvariants(context, { data: patch, id: 'task-1' }),
).rejects.toThrow('Invalid schedule');
});
});
describe('resolveTaskPatchInvariants edit lock', () => {
beforeEach(() => {
vi.resetAllMocks();
});
it('refuses a write while another member holds the edit lock', async () => {
const { context, editLockService } = buildContext();
const patch = { name: 'renamed' };
editLockService.getBlockingHolder.mockResolvedValue('other-member');
await expect(
resolveTaskPatchInvariants(context, { data: patch, id: 'task-1' }),
).rejects.toThrow('Task is being edited by another user');
});
it('does not consult the edit lock outside a workspace', async () => {
const { context, editLockService } = buildContext();
const patch = { name: 'renamed' };
const personal = { ...context, workspaceId: undefined };
await resolveTaskPatchInvariants(personal, { data: patch, id: 'task-1' });
expect(editLockService.getBlockingHolder).not.toHaveBeenCalled();
});
});
describe('resolveTaskPatchInvariants normalization', () => {
beforeEach(() => {
vi.resetAllMocks();
});
it('drops the stale rich-text mirror when only markdown is sent', async () => {
const { context } = buildContext();
const markdownOnly: { editorData?: unknown; instruction: string } = { instruction: '# done' };
const onlyMarkdown = await resolveTaskPatchInvariants(context, {
data: markdownOnly,
id: 'task-1',
});
expect(onlyMarkdown.data.editorData).toBeNull();
});
it('keeps an explicit editor state when both fields are sent', async () => {
const { context } = buildContext();
const both = { editorData: { root: {} }, instruction: '# done' };
const result = await resolveTaskPatchInvariants(context, { data: both, id: 'task-1' });
expect(result.data.editorData).toEqual({ root: {} });
});
});
describe('resolveTaskPatchInvariants project', () => {
beforeEach(() => {
assertAgentUsableByMock.mockReset();
findProjectMock.mockReset();
findProjectMock.mockImplementation(async (id: string) =>
id.startsWith('project-') ? { id } : null,
);
});
it('accepts moving a task to a project the caller can read', async () => {
const { context } = buildContext();
const { data } = await resolveTaskPatchInvariants(context, {
data: { projectId: 'project-2' },
id: 'task-1',
});
expect(data.projectId).toBe('project-2');
});
it.each(['missing', ''])('refuses the unknown project id %j as not found', async (projectId) => {
const { context } = buildContext();
await expect(
resolveTaskPatchInvariants(context, { data: { projectId }, id: 'task-1' }),
).rejects.toMatchObject({ code: 'NOT_FOUND', message: 'Project not found' });
});
it('clears the project without looking one up', async () => {
const { context } = buildContext();
const { data } = await resolveTaskPatchInvariants(context, {
data: { projectId: null },
id: 'task-1',
});
expect(data.projectId).toBeNull();
expect(findProjectMock).not.toHaveBeenCalled();
});
it('leaves the project out of the patch when it is not sent', async () => {
const { context, taskModel } = buildContext();
const { data } = await resolveTaskPatchInvariants(context, {
data: { name: 'renamed' },
id: 'task-1',
});
expect(data).not.toHaveProperty('projectId');
expect(findProjectMock).not.toHaveBeenCalled();
expect(taskModel.hasSubtaskOutsideProject).not.toHaveBeenCalled();
});
it('does not re-check links when the project stays the same', async () => {
const { context, taskModel } = buildContext({ 'task-1': { parentTaskId: 'parent-1' } });
await resolveTaskPatchInvariants(context, { data: { projectId: 'project-1' }, id: 'task-1' });
expect(taskModel.hasSubtaskOutsideProject).not.toHaveBeenCalled();
});
it.each([
['moved', 'project-2'],
['cleared', null],
])('refuses a subtask %s away from its parent', async (_label, projectId) => {
const { context } = buildContext({ 'task-1': { parentTaskId: 'parent-1' } });
await expect(
resolveTaskPatchInvariants(context, { data: { projectId }, id: 'task-1' }),
).rejects.toMatchObject({
cause: { data: { projectMoveRefusal: 'has_parent' } },
code: 'BAD_REQUEST',
message: 'Subtask must belong to the same project as its parent',
});
});
it('accepts a move that detaches the subtask in the same patch', async () => {
const { context } = buildContext({ 'task-1': { parentTaskId: 'parent-1' } });
const { data } = await resolveTaskPatchInvariants(context, {
data: { projectId: 'project-2' },
id: 'task-1',
parentTaskId: null,
});
expect(data).toMatchObject({ parentTaskId: null, projectId: 'project-2' });
});
it('judges a new parent against the project the same patch moves to', async () => {
const { context } = buildContext({ 'parent-1': { projectId: 'project-2' } });
const { data } = await resolveTaskPatchInvariants(context, {
data: { projectId: 'project-2' },
id: 'task-1',
parentTaskId: 'parent-1',
});
expect(data).toMatchObject({ parentTaskId: 'parent-1', projectId: 'project-2' });
await expect(
resolveTaskPatchInvariants(context, {
data: { projectId: 'project-3' },
id: 'task-1',
parentTaskId: 'parent-1',
}),
).rejects.toMatchObject({ message: 'Parent task must belong to the same project' });
});
it('refuses moving a task whose subtasks would stay behind', async () => {
const { context, taskModel } = buildContext();
taskModel.hasSubtaskOutsideProject.mockResolvedValue(true);
await expect(
resolveTaskPatchInvariants(context, { data: { projectId: 'project-2' }, id: 'task-1' }),
).rejects.toMatchObject({
cause: { data: { projectMoveRefusal: 'split_subtasks' } },
code: 'BAD_REQUEST',
message: expect.stringContaining('subtasks'),
});
expect(taskModel.hasSubtaskOutsideProject).toHaveBeenCalledWith('task-1', 'project-2');
});
it('refuses a move that strands a dependency', async () => {
const { context, taskModel } = buildContext();
taskModel.hasDependencyOutsideProject.mockResolvedValue(true);
await expect(
resolveTaskPatchInvariants(context, { data: { projectId: 'project-2' }, id: 'task-1' }),
).rejects.toMatchObject({
cause: { data: { projectMoveRefusal: 'external_dependency' } },
code: 'BAD_REQUEST',
message: expect.stringContaining('dependencies cannot cross project boundaries'),
});
expect(taskModel.hasDependencyOutsideProject).toHaveBeenCalledWith('task-1', 'project-2');
});
});