Skip to content

🤯 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.

15.3 KB
Raw Blame
import { describe, expect, it } from 'vitest';

import type { TaskGroupItem, TaskListItem } from '@/store/task/slices/list/initialState';

import {
  buildKanbanColumns,
  buildKanbanGroupQuery,
  canDropTaskIntoKanbanColumn,
  getKanbanAssigneeUpdate,
  getKanbanColumnCreateDefaults,
  getKanbanColumnHeaderVariant,
  getKanbanCreateTaskModalProps,
  getKanbanTaskPatch,
  getTaskGroupCreateDefaults,
  getTaskSubGroupCreateDefaults,
  moveTaskBetweenKanbanGroups,
  normalizeKanbanGroupBy,
} from './kanbanBoardModel';
import type { TaskGroupMeta } from './listViewOptions';

const task = (
  id: string,
  assigneeAgentId?: string | null,
  assigneeUserId?: string | null,
  overrides: Partial<TaskListItem> = {},
): TaskListItem =>
  ({
    assigneeAgentId,
    assigneeUserId,
    automationMode: null,
    createdByUserId: 'creator-1',
    id,
    identifier: `T-${id}`,
    priority: 0,
    status: 'backlog',
    visibility: 'public',
    ...overrides,
  }) as TaskListItem;

const group = (
  key: string,
  tasks: TaskListItem[],
  assigneeAgentId?: string | null,
  assigneeUserId?: string | null,
): TaskGroupItem =>
  ({
    assigneeAgentId,
    assigneeUserId,
    hasMore: false,
    key,
    limit: 50,
    offset: 0,
    tasks,
    total: tasks.length,
  }) as TaskGroupItem;

describe('kanbanBoardModel', () => {
  it('uses assignee, member and priority as board dimensions and falls back from no grouping', () => {
    expect(normalizeKanbanGroupBy('assignee')).toBe('assignee');
    expect(normalizeKanbanGroupBy('member')).toBe('member');
    expect(normalizeKanbanGroupBy('priority')).toBe('priority');
    expect(normalizeKanbanGroupBy('none')).toBe('status');
  });

  it('keeps the column header in skeleton mode for every loading group shape', () => {
    expect(getKanbanColumnHeaderVariant({ hasGroupMeta: false, loading: true })).toBe('loading');
    expect(getKanbanColumnHeaderVariant({ hasGroupMeta: true, loading: true })).toBe('loading');
    expect(getKanbanColumnHeaderVariant({ hasGroupMeta: true })).toBe('group');
    expect(getKanbanColumnHeaderVariant({ hasGroupMeta: false })).toBe('fallback');
  });

  it('builds assignee columns including the unassigned group', () => {
    const groups = [
      group('assignee:agent-1', [task('1', 'agent-1')], 'agent-1'),
      group('assignee:unassigned', [task('2', null)], null),
    ];

    const columns = buildKanbanColumns(groups, 'assignee');

    expect(columns.map((column) => column.key).sort()).toEqual(
      ['assignee:unassigned', 'assignee:agent-1'].sort(),
    );
    expect(
      columns.find((column) => column.key === 'assignee:unassigned')?.groupMeta?.assigneeId,
    ).toBeUndefined();
    expect(columns.find((column) => column.key === 'assignee:agent-1')?.groupMeta?.assigneeId).toBe(
      'agent-1',
    );
  });

  it('builds member columns independently from agent assignees', () => {
    const groups = [
      group('member:user-1', [task('1', 'agent-1', 'user-1')], undefined, 'user-1'),
      group('member:unassigned', [task('2', 'agent-1', null)], undefined, null),
    ];

    const columns = buildKanbanColumns(groups, 'member');

    expect(columns.map((column) => column.key).sort()).toEqual(
      ['member:unassigned', 'member:user-1'].sort(),
    );
    expect(
      columns.find((column) => column.key === 'member:user-1')?.groupMeta?.assigneeUserId,
    ).toBe('user-1');
  });

  it('patches the task assignee when moving between assignee columns', () => {
    const assignedTask = task('1', 'agent-1', 'user-1');
    const groups = [
      group('assignee:agent-1', [assignedTask], 'agent-1'),
      group('assignee:unassigned', [], null),
    ];
    const targetColumn = buildKanbanColumns(groups, 'assignee').find(
      (column) => column.key === 'assignee:unassigned',
    )!;
    const patch = getKanbanTaskPatch('assignee', targetColumn)!;

    const next = moveTaskBetweenKanbanGroups(groups, assignedTask, targetColumn.key, patch);

    expect(next.find((item) => item.key === 'assignee:agent-1')?.total).toBe(0);
    const unassigned = next.find((item) => item.key === 'assignee:unassigned');
    expect(unassigned?.total).toBe(1);
    expect(unassigned?.tasks[0].assigneeAgentId).toBeNull();
    expect(unassigned?.tasks[0].assigneeUserId).toBe('user-1');
  });

  it('patches only the member when moving between member columns', () => {
    const assignedTask = task('1', 'agent-1', 'user-1');
    const groups = [
      group('member:user-1', [assignedTask], undefined, 'user-1'),
      group('member:unassigned', [], undefined, null),
    ];
    const targetColumn = buildKanbanColumns(groups, 'member').find(
      (column) => column.key === 'member:unassigned',
    )!;
    const patch = getKanbanTaskPatch('member', targetColumn)!;

    expect(patch).toEqual({ assigneeUserId: null });
    const next = moveTaskBetweenKanbanGroups(groups, assignedTask, targetColumn.key, patch);
    const moved = next.find((item) => item.key === 'member:unassigned')?.tasks[0];
    expect(moved?.assigneeAgentId).toBe('agent-1');
    expect(moved?.assigneeUserId).toBeNull();
  });

  it('persists only the assignment dimension that changed', () => {
    expect(
      getKanbanAssigneeUpdate(task('1', null, 'user-1'), { assigneeAgentId: 'agent-1' }),
    ).toEqual({ assigneeAgentId: 'agent-1' });
    expect(getKanbanAssigneeUpdate(task('2', 'agent-1'), { assigneeUserId: 'user-1' })).toEqual({
      assigneeUserId: 'user-1',
    });
    expect(
      getKanbanAssigneeUpdate(task('3', null, 'user-1'), { assigneeUserId: 'user-1' }),
    ).toBeUndefined();
  });

  it('allows automated tasks to move between member columns', () => {
    const columns = buildKanbanColumns(
      [
        group('member:unassigned', [], undefined, null),
        group('member:user-1', [], undefined, 'user-1'),
      ],
      'member',
    );
    const unassignedColumn = columns.find((column) => column.key === 'member:unassigned')!;
    const memberColumn = columns.find((column) => column.key === 'member:user-1')!;
    const automatedTask = task('automated', null, null, { automationMode: 'schedule' });

    expect(canDropTaskIntoKanbanColumn(automatedTask, 'member', memberColumn)).toBe(true);
    expect(canDropTaskIntoKanbanColumn(automatedTask, 'member', unassignedColumn)).toBe(true);
  });

  it('only allows private tasks to be dropped into their creator member column', () => {
    const columns = buildKanbanColumns(
      [
        group('member:creator-1', [], undefined, 'creator-1'),
        group('member:user-2', [], undefined, 'user-2'),
      ],
      'member',
    );
    const creatorColumn = columns.find((column) => column.key === 'member:creator-1')!;
    const otherMemberColumn = columns.find((column) => column.key === 'member:user-2')!;
    const privateTask = task('private', null, null, { visibility: 'private' });

    expect(canDropTaskIntoKanbanColumn(privateTask, 'member', creatorColumn)).toBe(true);
    expect(canDropTaskIntoKanbanColumn(privateTask, 'member', otherMemberColumn)).toBe(false);
  });

  it('accepts drops into the running column only from runnable statuses', () => {
    const running = buildKanbanColumns([], 'status').find((column) => column.key === 'running')!;

    for (const status of ['backlog', 'completed', 'failed', 'paused'] as const) {
      expect(
        canDropTaskIntoKanbanColumn(task('1', null, null, { status }), 'status', running),
      ).toBe(true);
    }
    for (const status of ['running', 'scheduled', 'canceled'] as const) {
      expect(
        canDropTaskIntoKanbanColumn(task('1', null, null, { status }), 'status', running),
      ).toBe(false);
    }
    expect(getKanbanTaskPatch('status', running)).toEqual({ status: 'running' });
  });

  it('keeps the needs-input column closed to drops', () => {
    const needsInput = buildKanbanColumns([], 'status').find(
      (column) => column.key === 'needsInput',
    )!;

    expect(canDropTaskIntoKanbanColumn(task('1'), 'status', needsInput)).toBe(false);
  });

  it('preserves paginated group totals during an optimistic move', () => {
    const assignedTask = task('1', 'agent-1');
    const groups = [
      { ...group('assignee:agent-1', [assignedTask], 'agent-1'), total: 75 },
      { ...group('assignee:agent-2', [], 'agent-2'), total: 63 },
    ];

    const next = moveTaskBetweenKanbanGroups(groups, assignedTask, 'assignee:agent-2', {
      assigneeAgentId: 'agent-2',
    });

    expect(next.map(({ total }) => total)).toEqual([74, 64]);
  });
  describe('buildKanbanGroupQuery', () => {
    it('sends no automation filter on the "My tasks" board', () => {
      // Regression: the board pinned `automated: false` while the My tasks list
      // view sends none, so a scheduled or heartbeat task assigned to the
      // caller vanished the moment they switched that collection to board.
      const query = buildKanbanGroupQuery({ groupBy: 'status', myTaskScope: 'assigned' });

      expect(query).not.toHaveProperty('automated');
      expect(query).toEqual({ excludeStatuses: undefined, groupBy: 'status', scope: 'assigned' });
    });

    it('keeps excluding automation on the project, agent and all-agents boards', () => {
      // Those three do belong to the scheduled roll-up, so their columns stay
      // free of self-firing tasks.
      expect(buildKanbanGroupQuery({ groupBy: 'status', projectId: 'proj_1' })).toMatchObject({
        automated: false,
        projectId: 'proj_1',
      });
      expect(buildKanbanGroupQuery({ agentId: 'agt_1', groupBy: 'status' })).toMatchObject({
        agentId: 'agt_1',
        automated: false,
      });
      expect(buildKanbanGroupQuery({ groupBy: 'status' })).toMatchObject({
        allAgents: true,
        automated: false,
      });
    });

    it('prefers the My tasks scope over the other scopes', () => {
      const query = buildKanbanGroupQuery({
        agentId: 'agt_1',
        groupBy: 'status',
        myTaskScope: 'created',
        projectId: 'proj_1',
      });

      expect(query).toEqual({
        excludeStatuses: undefined,
        groupBy: 'status',
        scope: 'created',
      });
    });

    it('carries the status exclusions through every scope', () => {
      const excludeStatuses = ['completed'] as const;

      expect(
        buildKanbanGroupQuery({ excludeStatuses, groupBy: 'status', myTaskScope: 'assigned' }),
      ).toMatchObject({ excludeStatuses });
      expect(buildKanbanGroupQuery({ excludeStatuses, groupBy: 'status' })).toMatchObject({
        excludeStatuses,
      });
    });
  });

  describe('getKanbanCreateTaskModalProps', () => {
    it('prefills the column priority in the create task modal', () => {
      expect(getKanbanCreateTaskModalProps({ priority: 4 }, { projectId: 'project-1' })).toEqual({
        agentId: undefined,
        defaultPriority: 4,
        lockAssignee: false,
        projectId: 'project-1',
        showInlineToggle: false,
      });
    });

    it('prefills the column assignee over the board agent', () => {
      expect(
        getKanbanCreateTaskModalProps({ assigneeAgentId: 'agent-col' }, { agentId: 'agent-board' }),
      ).toMatchObject({ agentId: 'agent-col', lockAssignee: true });
    });

    it('falls back to the board agent and locks it on an agent-scoped board', () => {
      expect(getKanbanCreateTaskModalProps({}, { agentId: 'agent-board' })).toMatchObject({
        agentId: 'agent-board',
        defaultPriority: undefined,
        lockAssignee: true,
      });
    });
  });

  describe('getKanbanColumnCreateDefaults', () => {
    it('pins the column priority on tasks created from a priority column', () => {
      const groups = [
        { ...group('priority:2', []), priority: 2 },
        { ...group('priority:0', []), priority: 0 },
      ] as TaskGroupItem[];
      const columns = buildKanbanColumns(groups, 'priority');

      expect(
        columns.map((column) => getKanbanColumnCreateDefaults('priority', column)?.priority),
      ).toEqual([2, 0]);
    });

    it('offers status creation only from the backlog column, without pinned fields', () => {
      const columns = buildKanbanColumns([], 'status');

      expect(columns.map((column) => getKanbanColumnCreateDefaults('status', column))).toEqual([
        {},
        undefined,
        undefined,
        undefined,
        undefined,
      ]);
    });

    it('pins the column agent on tasks created from an agent column', () => {
      const [agentColumn] = buildKanbanColumns(
        [group('assignee:agent-1', [], 'agent-1')],
        'assignee',
      );

      expect(getKanbanColumnCreateDefaults('assignee', agentColumn)).toEqual({
        assigneeAgentId: 'agent-1',
      });
    });

    it('offers no create entry on the unassigned and member columns', () => {
      const [unassignedColumn] = buildKanbanColumns([group('assignee:unassigned', [])], 'assignee');
      const [memberColumn] = buildKanbanColumns(
        [group('member:user-1', [], undefined, 'user-1')],
        'member',
      );

      expect(getKanbanColumnCreateDefaults('assignee', unassignedColumn)).toBeUndefined();
      expect(getKanbanColumnCreateDefaults('member', memberColumn)).toBeUndefined();
    });
  });

  describe('getTaskGroupCreateDefaults', () => {
    const meta = (overrides: Partial<TaskGroupMeta>): TaskGroupMeta => ({
      groupBy: 'status',
      key: 'k',
      label: 'l',
      ...overrides,
    });

    it('offers creation only on the backlog status group', () => {
      expect(getTaskGroupCreateDefaults(meta({ status: 'backlog' }))).toEqual({});
      expect(getTaskGroupCreateDefaults(meta({ status: 'running' }))).toBeUndefined();
      expect(getTaskGroupCreateDefaults(meta({ status: 'completed' }))).toBeUndefined();
    });

    it('pins the group priority, including "no priority"', () => {
      expect(getTaskGroupCreateDefaults(meta({ groupBy: 'priority', priority: 2 }))).toEqual({
        priority: 2,
      });
      expect(getTaskGroupCreateDefaults(meta({ groupBy: 'priority', priority: 0 }))).toEqual({
        priority: 0,
      });
    });

    it('pins the group agent and offers nothing on the unassigned group', () => {
      expect(
        getTaskGroupCreateDefaults(meta({ assigneeId: 'agent-1', groupBy: 'assignee' })),
      ).toEqual({ assigneeAgentId: 'agent-1' });
      expect(getTaskGroupCreateDefaults(meta({ groupBy: 'assignee' }))).toBeUndefined();
    });

    it('offers nothing on member, automation and ungrouped lists', () => {
      expect(
        getTaskGroupCreateDefaults(meta({ assigneeUserId: 'user-1', groupBy: 'member' })),
      ).toBeUndefined();
      expect(
        getTaskGroupCreateDefaults(meta({ automationMode: 'schedule', groupBy: 'automationMode' })),
      ).toBeUndefined();
      expect(getTaskGroupCreateDefaults(meta({ groupBy: 'none' }))).toBeUndefined();
      expect(getTaskGroupCreateDefaults(undefined)).toBeUndefined();
    });

    it('combines a sub-group with its parent only when both offer an entry', () => {
      const backlog = meta({ status: 'backlog' });
      const high = meta({ groupBy: 'priority', priority: 2 });
      const agent = meta({ assigneeId: 'agent-1', groupBy: 'assignee' });

      expect(getTaskSubGroupCreateDefaults(backlog, high)).toEqual({ priority: 2 });
      expect(getTaskSubGroupCreateDefaults(high, agent)).toEqual({
        assigneeAgentId: 'agent-1',
        priority: 2,
      });
      expect(getTaskSubGroupCreateDefaults(meta({ status: 'running' }), high)).toBeUndefined();
      expect(getTaskSubGroupCreateDefaults(high, meta({ groupBy: 'assignee' }))).toBeUndefined();
    });
  });
});