🤯 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
✅ test(database): 100% coverage for models/task (#21851)
Arvin Xu71586499c21b
diff --git a/packages/database/src/models/__tests__/task.guards.test.ts b/packages/database/src/models/__tests__/task.guards.test.tsnew file mode 100644index 00000000000..aab8f52a6d8--- /dev/null+++ b/packages/database/src/models/__tests__/task.guards.test.ts@@ -0,0 +1,424 @@+// @vitest-environment node+import { eq, inArray } from 'drizzle-orm';+import { PgDialect } from 'drizzle-orm/pg-core';+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';++import { getTestDB } from '../../core/getTestDB';+import { documents, projects, tasks, users, workspaces } from '../../schemas';+import type { LobeChatDatabase } from '../../type';+import { TaskModel } from '../task';++const serverDB: LobeChatDatabase = await getTestDB();++const userId = 'task-guards-user';+const userId2 = 'task-guards-user-2';+const workspaceId = 'task-guards-ws';+const otherWorkspaceId = 'task-guards-ws-2';+const ownUsers = [userId, userId2];++const dialect = new PgDialect();++beforeEach(async () => {+ await serverDB.delete(users).where(inArray(users.id, ownUsers));+ await serverDB.insert(users).values(ownUsers.map((id) => ({ id })));+});++afterEach(async () => {+ vi.restoreAllMocks();+ // Tasks, projects, documents and workspaces all cascade from their owner.+ await serverDB.delete(users).where(inArray(users.id, ownUsers));+});++const createWorkspaces = () =>+ serverDB.insert(workspaces).values([+ { id: workspaceId, name: 'WS', primaryOwnerId: userId, slug: workspaceId },+ { id: otherWorkspaceId, name: 'WS 2', primaryOwnerId: userId, slug: otherWorkspaceId },+ ]);++const createDocument = async (title: string | null) => {+ const [doc] = await serverDB+ .insert(documents)+ .values({+ fileType: 'markdown',+ source: 'test',+ sourceType: 'api',+ title,+ totalCharCount: 0,+ totalLineCount: 0,+ userId,+ })+ .returning();+ return doc;+};++describe('TaskModel.create guards', () => {+ it('rejects a parent the caller cannot see', async () => {+ const model = new TaskModel(serverDB, userId);++ await expect(+ model.create({ instruction: 'child', parentTaskId: 'task_missing' }),+ ).rejects.toThrow('Task not found');+ });++ it('rejects a subtask placed in another project than its parent', async () => {+ const model = new TaskModel(serverDB, userId);+ const parent = await model.create({ instruction: 'parent' });++ await expect(+ model.create({ instruction: 'child', parentTaskId: parent.id, projectId: 'proj_other' }),+ ).rejects.toThrow('Subtask must belong to the same project as its parent');+ expect(await model.findSubtasks(parent.id)).toEqual([]);+ });++ it('gives up without inserting when no attempt is allowed', async () => {+ const model = new TaskModel(serverDB, userId);++ await expect(model.create({ instruction: 'never' }, { maxRetries: 0 })).rejects.toThrow(+ 'Failed to create task after max retries',+ );+ expect((await model.list()).total).toBe(0);+ });++ it('rethrows an insert error that is not an identifier collision', async () => {+ const model = new TaskModel(serverDB, userId);++ await expect(+ model.create({ assigneeUserId: 'task-guards-ghost', instruction: 'bad ref' }),+ ).rejects.toThrow();+ expect((await model.list()).total).toBe(0);+ });+});++describe('TaskModel.findByRepairRequest', () => {+ it('finds the task carrying the repair request id, and null otherwise', async () => {+ const model = new TaskModel(serverDB, userId);+ const task = await model.create({+ config: { acceptanceRepair: { requestId: 'repair-1' } },+ instruction: 'repair',+ });++ expect((await model.findByRepairRequest('repair-1'))?.id).toBe(task.id);+ expect(await model.findByRepairRequest('repair-2')).toBeNull();+ expect(await new TaskModel(serverDB, userId2).findByRepairRequest('repair-1')).toBeNull();+ });+});++describe('TaskModel update guards', () => {+ it('returns null when reassigning a task that is not visible', async () => {+ const task = await new TaskModel(serverDB, userId).create({ instruction: 'mine' });++ expect(+ await new TaskModel(serverDB, userId2).update(task.id, { assigneeAgentId: null }),+ ).toBeNull();+ });++ it('merges a context patch into a null context', async () => {+ const model = new TaskModel(serverDB, userId);+ const task = await model.create({ instruction: 'ctx' });+ await model.update(task.id, { context: null });++ const updated = await model.updateContext(task.id, { scheduler: { tickMessageId: 'm1' } });++ expect(updated?.context).toEqual({ scheduler: { tickMessageId: 'm1' } });+ });++ it('patches verify config onto a null config and skips undefined keys', async () => {+ const model = new TaskModel(serverDB, userId);+ const task = await model.create({ instruction: 'verify' });+ await model.update(task.id, { config: null });++ const updated = await model.updateVerifyConfig(task.id, {+ enabled: true,+ maxIterations: undefined,+ });++ expect(updated?.config).toEqual({ verify: { enabled: true } });+ });++ it('resolves no verify config for a missing task', async () => {+ expect(+ await new TaskModel(serverDB, userId).resolveVerifyConfig('task_missing'),+ ).toBeUndefined();+ });++ it('stops walking at an ancestor the caller cannot see', async () => {+ const owner = new TaskModel(serverDB, userId, workspaceId);+ await createWorkspaces();+ const parent = await owner.create({+ config: { verify: { enabled: true } },+ instruction: 'private parent',+ visibility: 'private',+ });+ const child = await owner.create({+ instruction: 'public child',+ parentTaskId: parent.id,+ visibility: 'public',+ });++ expect(await owner.resolveVerifyConfig(child.id)).toEqual({ enabled: true });+ expect(+ await new TaskModel(serverDB, userId2, workspaceId).resolveVerifyConfig(child.id),+ ).toBeUndefined();+ });+});++describe('TaskModel.updateWithLog guards', () => {+ it('returns null for a task that is not visible', async () => {+ expect(+ await new TaskModel(serverDB, userId).updateWithLog(+ 'task_missing',+ { status: 'running' },+ { userId },+ ),+ ).toBeNull();+ });++ it('applies a config patch onto a null config', async () => {+ const model = new TaskModel(serverDB, userId);+ const task = await model.create({ instruction: 'patch' });+ await model.update(task.id, { config: null });++ const updated = await model.updateWithLog(+ task.id,+ {},+ { userId },+ { configPatch: { schedule: { maxExecutions: 3 } } },+ );++ expect(updated?.config).toEqual({ schedule: { maxExecutions: 3 } });+ });++ it('logs a priority change to and from null', async () => {+ const model = new TaskModel(serverDB, userId);+ const task = await model.create({ instruction: 'priority', priority: 2 });++ await model.updateWithLog(task.id, { priority: null }, { userId });+ await model.updateWithLog(task.id, { priority: 3 }, { userId });++ const events = (await model.getActivities(task.id)).filter((row) => row.type === 'priority');+ expect(events.map((row) => row.payload)).toEqual([+ expect.objectContaining({ from: 2, to: null }),+ expect.objectContaining({ from: null, to: 3 }),+ ]);+ });+});++describe('TaskModel dependency guards', () => {+ it('rejects a dependency onto a task in a project when the task has none', async () => {+ const model = new TaskModel(serverDB, userId);+ const [project] = await serverDB+ .insert(projects)+ .values({ name: 'P', userId })+ .returning({ id: projects.id });+ const loose = await model.create({ instruction: 'loose' });+ const scoped = await model.create({ instruction: 'scoped', projectId: project.id });++ await expect(model.addDependency(loose.id, scoped.id)).rejects.toThrow(+ 'Task dependencies cannot cross project boundaries',+ );+ expect(await model.getDependencies(loose.id)).toEqual([]);+ });++ it('unlocks nothing when the completed task has no dependents', async () => {+ const model = new TaskModel(serverDB, userId);+ const done = await model.create({ instruction: 'alone' });+ await model.updateStatus(done.id, 'completed');++ expect(await model.getUnlockedTasksForMany([done.id])).toEqual([]);+ });++ it('unlocks nothing while every dependent is still blocked elsewhere', async () => {+ const model = new TaskModel(serverDB, userId);+ const done = await model.create({ instruction: 'done' });+ const pending = await model.create({ instruction: 'pending' });+ const waiting = await model.create({ instruction: 'waiting' });+ await model.addDependency(waiting.id, done.id);+ await model.addDependency(waiting.id, pending.id);+ await model.updateStatus(done.id, 'completed');++ expect(await model.getUnlockedTasksForMany([done.id])).toEqual([]);+ });+});++describe('TaskModel empty-input early returns', () => {+ it('skips the database for empty id lists', async () => {+ const model = new TaskModel(serverDB, userId);++ expect(await model.addActivities([])).toEqual([]);+ expect(await model.lockForStatusChange([])).toEqual([]);+ expect(await model.deleteByIds([])).toBe(0);+ });+});++describe('TaskModel row guards', () => {+ it('reports no other creators outside a workspace', async () => {+ const model = new TaskModel(serverDB, userId);+ const task = await model.create({ instruction: 'personal' });++ expect(await model.subtreeHasOtherCreators(task.id, userId2)).toBe(false);+ });++ it('restricts deleteAll to the caller’s own rows on request', async () => {+ await createWorkspaces();+ const mine = await new TaskModel(serverDB, userId, workspaceId).create({+ instruction: 'mine',+ visibility: 'public',+ });+ const theirs = await new TaskModel(serverDB, userId2, workspaceId).create({+ instruction: 'theirs',+ visibility: 'public',+ });++ const deleted = await new TaskModel(serverDB, userId, workspaceId).deleteAll({+ restrictToCreator: true,+ });++ expect(deleted).toBe(1);+ const left = await serverDB+ .select({ id: tasks.id })+ .from(tasks)+ .where(inArray(tasks.id, [mine.id, theirs.id]));+ expect(left).toEqual([{ id: theirs.id }]);+ });++ it('returns null when the subtree root is not visible', async () => {+ const task = await new TaskModel(serverDB, userId).create({ instruction: 'mine' });++ expect(+ await new TaskModel(serverDB, userId2).setSubtreeVisibility(task.id, [], 'private'),+ ).toBeNull();+ expect((await new TaskModel(serverDB, userId).findById(task.id))?.visibility).toBe(+ task.visibility,+ );+ });+});++describe('TaskModel pinned document guards', () => {+ it('returns a null title for an untitled document pinned since a time', async () => {+ const model = new TaskModel(serverDB, userId);+ const task = await model.create({ instruction: 'docs' });+ const doc = await createDocument(null);+ const since = new Date(Date.now() - 60_000);+ await model.pinDocument(task.id, doc.id);++ expect(await model.getDocumentsPinnedSince(task.id, since)).toEqual([+ { id: doc.id, kind: 'markdown', title: null },+ ]);+ });++ it('labels an untitled document in the pinned tree', async () => {+ const model = new TaskModel(serverDB, userId);+ const task = await model.create({ instruction: 'tree' });+ const doc = await createDocument('');+ await model.pinDocument(task.id, doc.id);++ const { nodeMap, tree } = await model.getTreePinnedDocuments(task.id);++ expect(nodeMap[doc.id].title).toBe('Untitled');+ expect(tree).toEqual([{ children: [], id: doc.id }]);+ });+});++describe('TaskModel.groupList guards', () => {+ it('requires groups or a grouping dimension', async () => {+ await expect(new TaskModel(serverDB, userId).groupList({})).rejects.toThrow(+ 'Task groups or a grouping dimension are required',+ );+ await expect(new TaskModel(serverDB, userId).groupList({ groups: [] })).rejects.toThrow(+ 'Task groups or a grouping dimension are required',+ );+ });++ it('adds an empty unassigned group to the hybrid assignee board', async () => {+ const model = new TaskModel(serverDB, userId);+ const task = await model.create({ assigneeUserId: userId2, instruction: 'member' });++ const groups = await model.groupList({ groupBy: 'assignee' });++ expect(+ groups.map(({ key, tasks, total }) => ({ ids: tasks.map((t) => t.id), key, total })),+ ).toEqual([+ { ids: [task.id], key: `assignee:user:${userId2}`, total: 1 },+ { ids: [], key: 'assignee:unassigned', total: 0 },+ ]);+ expect(groups[1]).toMatchObject({ assigneeAgentId: null, assigneeUserId: null });+ });++ it('adds an empty unassigned column to the member board', async () => {+ const model = new TaskModel(serverDB, userId);+ const task = await model.create({ assigneeUserId: userId2, instruction: 'member' });++ const groups = await model.groupList({ groupBy: 'member' });++ expect(+ groups.map(({ key, tasks, total }) => ({ ids: tasks.map((t) => t.id), key, total })),+ ).toEqual([+ { ids: [task.id], key: `member:${userId2}`, total: 1 },+ { ids: [], key: 'member:unassigned', total: 0 },+ ]);+ });++ it('returns empty status groups without reading run stats', async () => {+ const groups = await new TaskModel(serverDB, userId).groupList({+ groups: [{ key: 'running', statuses: ['running'] }],+ });++ expect(groups).toEqual([+ { hasMore: false, key: 'running', limit: 50, offset: 0, tasks: [], total: 0 },+ ]);+ });++ it('defaults run totals for a task whose stats row is gone', async () => {+ // A task deleted between the list read and the run-stats aggregate has no+ // stats row; the list still renders it with zero totals.+ const model = new TaskModel(serverDB, userId);+ const task = await model.create({ instruction: 'listed' });+ const execute = serverDB.execute.bind(serverDB);+ vi.spyOn(serverDB, 'execute').mockImplementation(((query: any) =>+ dialect.sqlToQuery(query).sql.includes('total_run_cost')+ ? Promise.resolve({ rows: [] })+ : execute(query)) as typeof serverDB.execute);++ const [group] = await model.groupList({ groups: [{ key: 'backlog', statuses: ['backlog'] }] });++ expect(group.tasks).toEqual([+ expect.objectContaining({ id: task.id, totalRunCost: 0, totalRunDuration: 0 }),+ ]);+ });+});++describe('TaskModel transfer visibility', () => {+ it('moves a subtree into a workspace with the requested visibility', async () => {+ await createWorkspaces();+ const model = new TaskModel(serverDB, userId);+ const root = await model.create({ instruction: 'root' });+ const child = await model.create({ instruction: 'child', parentTaskId: root.id });++ const { taskIds } = await model.transferTo(root.id, workspaceId, userId, 'public');++ expect(taskIds.sort()).toEqual([root.id, child.id].sort());+ const moved = await serverDB.select().from(tasks).where(inArray(tasks.id, taskIds));+ expect(moved.map((t) => [t.workspaceId, t.visibility])).toEqual([+ [workspaceId, 'public'],+ [workspaceId, 'public'],+ ]);+ });++ it('copies a subtree into a workspace with the requested visibility', async () => {+ await createWorkspaces();+ const model = new TaskModel(serverDB, userId);+ const root = await model.create({ instruction: 'root' });+ await model.create({ instruction: 'child', parentTaskId: root.id });++ const { rootId } = await model.copyToWorkspace(root.id, otherWorkspaceId, userId, 'public');++ const [copy] = await serverDB.select().from(tasks).where(eq(tasks.id, rootId));+ expect(copy).toMatchObject({ visibility: 'public', workspaceId: otherWorkspaceId });+ const copiedChildren = await new TaskModel(serverDB, userId, otherWorkspaceId).findSubtasks(+ rootId,+ );+ expect(copiedChildren).toEqual([+ expect.objectContaining({ instruction: 'child', visibility: 'public' }),+ ]);+ });+});diff --git a/packages/database/src/models/__tests__/task.helpers.test.ts b/packages/database/src/models/__tests__/task.helpers.test.tsnew file mode 100644index 00000000000..2694e1f452b--- /dev/null+++ b/packages/database/src/models/__tests__/task.helpers.test.ts@@ -0,0 +1,56 @@+// @vitest-environment node+import type { TaskItem } from '@lobechat/types';+import { describe, expect, it } from 'vitest';++import { checkpointTaskMatcher, checkpointTaskPairsMatch } from '../task/checkpoint';+import { getVerifyConfig } from '../task/config';+import { isTaskIdentifierUniqueViolation, withStaleReposCleared } from '../task/utils';++const asTask = (config: unknown) => ({ config }) as TaskItem;++describe('task checkpoint helpers', () => {+ it('treats missing legacy ids as an empty label list', () => {+ expect(checkpointTaskPairsMatch(undefined, [{ id: 'task_a', identifier: 'T-1' }])).toBe(false);+ expect(checkpointTaskPairsMatch(undefined, [])).toBe(true);+ });++ it('matches nothing when neither ids nor pairs are stored', () => {+ const matches = checkpointTaskMatcher(undefined, undefined);++ expect(matches({ id: 'task_a', identifier: 'T-1' })).toBe(false);+ });+});++describe('task getVerifyConfig', () => {+ it('carries a legacy review that only sets maxIterations', () => {+ expect(getVerifyConfig(asTask({ review: { maxIterations: 3 } }))).toEqual({+ enabled: undefined,+ maxIterations: 3,+ });+ });++ it('ignores a legacy review without gate fields', () => {+ expect(getVerifyConfig(asTask({ review: { rubrics: [] } }))).toBeUndefined();+ });+});++describe('task withStaleReposCleared', () => {+ it('leaves the write untouched when the previous config is null', () => {+ const data = { assigneeAgentId: 'agent-b' };++ expect(withStaleReposCleared({ assigneeAgentId: 'agent-a', config: null }, data)).toBe(data);+ });+});++describe('task isTaskIdentifierUniqueViolation', () => {+ it('reads a thrown non-Error value by its string form', () => {+ expect(isTaskIdentifierUniqueViolation('duplicate key value')).toBe(true);+ expect(isTaskIdentifierUniqueViolation('connection reset')).toBe(false);+ expect(isTaskIdentifierUniqueViolation(null)).toBe(false);+ });++ it('reads a code from a plain object', () => {+ expect(isTaskIdentifierUniqueViolation({ code: '23505' })).toBe(true);+ expect(isTaskIdentifierUniqueViolation({ code: '23503' })).toBe(false);+ });+});diff --git a/packages/database/src/models/task/documents.ts b/packages/database/src/models/task/documents.tsindex 15faa675697..ee77bd1e856 100644--- a/packages/database/src/models/task/documents.ts+++ b/packages/database/src/models/task/documents.ts@@ -94,6 +94,7 @@ export const getDocumentsPinnedSince = async (return rows.map((row) => ({id: row.id,+ /* v8 ignore next -- unreachable: documents.file_type is NOT NULL and the inner join always yields a document row */kind: row.fileType ?? null,title: row.title ?? null,}));diff --git a/packages/database/src/models/task/index.ts b/packages/database/src/models/task/index.tsindex c7d9216a79f..7e8f22c0e78 100644--- a/packages/database/src/models/task/index.ts+++ b/packages/database/src/models/task/index.ts@@ -133,6 +133,7 @@ export class TaskModel {async resolve(ref: string): Promise<TaskItem | null> {const result = await this.resolveRefs([ref]);+ /* v8 ignore next -- unreachable: one ref matches at most one row, ids are primary keys and identifiers are unique per scope (tasks_identifier_idx / tasks_identifier_workspace_id_unique) */if (result.ambiguous.length) throw new Error(`Ambiguous task reference: ${ref}`);return result.byRef.get(ref) ?? null;}@@ -666,6 +667,7 @@ export class TaskModel {}: data;const updated = await scoped.update(id, writeData);+ /* v8 ignore next -- unreachable: the row was just locked FOR UPDATE in this transaction under the same ownership predicate, so the write always returns it */if (!updated) return null;const events: { payload: TaskActivityLogPayload; type: TaskActivityLogType }[] = [];diff --git a/packages/database/src/models/task/queries.ts b/packages/database/src/models/task/queries.tsindex ce7832f370b..6dbebd4644c 100644--- a/packages/database/src/models/task/queries.ts+++ b/packages/database/src/models/task/queries.ts@@ -291,6 +291,7 @@ export const groupList = async (total: priorityCounts.get(group.priority) ?? 0,}));} else {+ /* v8 ignore next -- unreachable: `groupBy` is exhausted above, so the guard at the top already proved `groups` non-empty */const statusGroups = (groups ?? []).map((group) => ({...group,statuses: Array.from(new Set(group.statuses)),@@ -336,6 +337,7 @@ export const groupList = async (groupQueries.map(async (group) => {const groupTasks =group.prefetchedTasks ??+ /* v8 ignore start -- unreachable: every grouping branch above prefetches its tasks. The stop marker sits after the return because V8 charges the continuation of this never-resumed `await` to the statement that follows it */(await ctx.db.select().from(tasks)@@ -355,6 +357,7 @@ export const groupList = async (tasks: groupTasks,total: group.total,};+ /* v8 ignore stop */}),);diff --git a/packages/database/src/models/task/rows.ts b/packages/database/src/models/task/rows.tsindex b98775b3154..26784f1486a 100644--- a/packages/database/src/models/task/rows.ts+++ b/packages/database/src/models/task/rows.ts@@ -156,6 +156,7 @@ export const countTasksBlockingAgentDemotion = async (or(eq(tasks.visibility, 'public'), ne(tasks.createdByUserId, agentOwnerUserId)),),);+ /* v8 ignore next -- unreachable: an ungrouped count() always returns exactly one non-null row */return Number(row?.count ?? 0);};diff --git a/packages/database/src/models/task/transfer.ts b/packages/database/src/models/task/transfer.tsindex 4c48e4060f0..f7d7410c91c 100644--- a/packages/database/src/models/task/transfer.ts+++ b/packages/database/src/models/task/transfer.ts@@ -153,12 +153,16 @@ export const copyToWorkspace = async (while (queue.length > 0) {const currentId = queue.shift()!;+ /* v8 ignore next -- unreachable: each subtree id is queued once, by its single parent */if (seen.has(currentId)) continue;seen.add(currentId);const original = byId.get(currentId);+ /* v8 ignore next -- unreachable: the queue only holds ids taken from the subtree */if (!original) continue;+ /* v8 ignore start -- unreachable `?? null`: BFS clones a parent before its children, so idMap always holds it */const newParentId = currentId === taskId ? null : (idMap.get(original.parentTaskId!) ?? null);+ /* v8 ignore stop */const identifier = `${DEFAULT_TASK_IDENTIFIER_PREFIX}-${seq}`;const inserted = (await (trx as LobeChatDatabase)