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.

36.6 KB
Raw Blame
import type { ChatTopic } from '@lobechat/types';
import dayjs from 'dayjs';
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';

import {
  filterTopics,
  getTopicWorkingDirectoryEffectivePath,
  getTopicWorkingDirectorySourcePath,
  groupTopicsByAgent,
  groupTopicsByDevice,
  groupTopicsByProject,
  groupTopicsByStatus,
  groupTopicsByTime,
  groupTopicsByUpdatedTime,
} from './topic';

// Mock current date to ensure consistent test results
const NOW = '2024-01-15T12:00:00Z';

beforeAll(() => {
  // Mock the current date
  vi.useFakeTimers();
  vi.setSystemTime(new Date(NOW));
});

describe('groupTopicsByTime', () => {
  afterAll(() => {
    vi.useRealTimers();
  });

  // Helper function to create test topics
  const createTopic = (createdAt: number, title: string = 'Test Topic'): ChatTopic => ({
    id: createdAt.toString(),
    title,
    createdAt,
    updatedAt: createdAt,
  });

  it('should return empty array for empty input', () => {
    expect(groupTopicsByTime([])).toEqual([]);
  });

  it('should group topics created today', () => {
    const today = dayjs().valueOf();
    const topics = [createTopic(today)];

    const result = groupTopicsByTime(topics);

    expect(result).toHaveLength(1);
    expect(result[0]).toEqual({
      id: 'today',
      children: topics,
    });
  });

  it('should group topics created yesterday', () => {
    const yesterday = dayjs().subtract(1, 'day').valueOf();
    const topics = [createTopic(yesterday)];

    const result = groupTopicsByTime(topics);

    expect(result).toHaveLength(1);
    expect(result[0]).toEqual({
      id: 'yesterday',
      children: topics,
    });
  });

  it('should group topics created within the week', () => {
    const threeDaysAgo = dayjs().subtract(3, 'day').valueOf();
    const topics = [createTopic(threeDaysAgo)];

    const result = groupTopicsByTime(topics);

    expect(result).toHaveLength(1);
    expect(result[0]).toEqual({
      id: 'week',
      children: topics,
    });
  });

  it('should group topics created this month', () => {
    const thisMonth = dayjs().startOf('month').add(1, 'day').valueOf();
    const topics = [createTopic(thisMonth)];

    const result = groupTopicsByTime(topics);

    expect(result).toHaveLength(1);
    expect(result[0]).toEqual({
      id: 'month',
      children: topics,
    });
  });

  it('should group topics from previous years', () => {
    const lastYear = dayjs().subtract(1, 'year').valueOf();
    const topics = [createTopic(lastYear)];

    const result = groupTopicsByTime(topics);

    expect(result).toHaveLength(1);
    expect(result[0]).toEqual({
      id: dayjs(lastYear).year().toString(),
      children: topics,
    });
  });

  it('should sort groups in correct order', () => {
    const today = dayjs().valueOf();
    const yesterday = dayjs().subtract(1, 'day').valueOf();
    const lastWeek = dayjs().subtract(5, 'day').valueOf();
    const lastMonth = dayjs().subtract(1, 'month').valueOf();
    const lastYear = dayjs().subtract(1, 'year').valueOf();

    const topics = [
      createTopic(lastYear, 'Last Year'),
      createTopic(lastMonth, 'Last Month'),
      createTopic(lastWeek, 'Last Week'),
      createTopic(yesterday, 'Yesterday'),
      createTopic(today, 'Today'),
    ];

    const result = groupTopicsByTime(topics);

    // Verify order of groups
    expect(result.map((g) => g.id)).toEqual([
      'today',
      'yesterday',
      'week',
      dayjs(lastYear).year().toString(),
    ]);
  });

  it('should sort topics within groups by createdAt in descending order', () => {
    const today1 = dayjs().hour(9).valueOf();
    const today2 = dayjs().hour(10).valueOf();
    const today3 = dayjs().hour(11).valueOf();

    const topics = [
      createTopic(today1, 'Morning'),
      createTopic(today2, 'Midday'),
      createTopic(today3, 'Afternoon'),
    ];

    const result = groupTopicsByTime(topics);

    expect(result).toHaveLength(1);
    expect(result[0].id).toBe('today');
    expect(result[0].children.map((t) => t.title)).toEqual(['Afternoon', 'Midday', 'Morning']);
  });
});

describe('groupTopicsByUpdatedTime', () => {
  afterAll(() => {
    vi.useRealTimers();
  });

  const createTopic = (
    createdAt: number,
    updatedAt: number,
    title: string = 'Test Topic',
  ): ChatTopic => ({
    id: `${createdAt}-${updatedAt}`,
    title,
    createdAt,
    updatedAt,
  });

  it('should return empty array for empty input', () => {
    expect(groupTopicsByUpdatedTime([])).toEqual([]);
  });

  it('should group topics by updatedAt instead of createdAt', () => {
    const lastYear = dayjs().subtract(1, 'year').valueOf();
    const today = dayjs().valueOf();

    // Topic created last year but updated today
    const topics = [createTopic(lastYear, today, 'Old but recently updated')];

    const result = groupTopicsByUpdatedTime(topics);

    expect(result).toHaveLength(1);
    expect(result[0].id).toBe('today');
    expect(result[0].children[0].title).toBe('Old but recently updated');
  });

  it('should sort topics within groups by updatedAt in descending order', () => {
    const createdAt = dayjs().subtract(1, 'month').valueOf();
    const updatedAt1 = dayjs().hour(9).valueOf();
    const updatedAt2 = dayjs().hour(10).valueOf();
    const updatedAt3 = dayjs().hour(11).valueOf();

    const topics = [
      createTopic(createdAt, updatedAt1, 'Morning update'),
      createTopic(createdAt, updatedAt2, 'Midday update'),
      createTopic(createdAt, updatedAt3, 'Afternoon update'),
    ];

    const result = groupTopicsByUpdatedTime(topics);

    expect(result).toHaveLength(1);
    expect(result[0].id).toBe('today');
    expect(result[0].children.map((t) => t.title)).toEqual([
      'Afternoon update',
      'Midday update',
      'Morning update',
    ]);
  });

  it('should produce different grouping than groupTopicsByTime when updatedAt differs from createdAt', () => {
    const lastYear = dayjs().subtract(1, 'year').valueOf();
    const yesterday = dayjs().subtract(1, 'day').valueOf();

    // Created last year, updated yesterday
    const topics = [createTopic(lastYear, yesterday, 'Migrated topic')];

    const byCreated = groupTopicsByTime(topics);
    const byUpdated = groupTopicsByUpdatedTime(topics);

    // By createdAt: grouped under last year
    expect(byCreated[0].id).toBe(dayjs(lastYear).year().toString());

    // By updatedAt: grouped under yesterday
    expect(byUpdated[0].id).toBe('yesterday');
  });

  it('should group and sort by sortUpdatedAt (activity time) when present, ignoring updatedAt', () => {
    const lastYear = dayjs().subtract(1, 'year').valueOf();
    const today = dayjs().valueOf();

    // Row was edited last year (updatedAt) but had message activity today
    // (sortUpdatedAt) — the sidebar must group it under "today".
    const topic: ChatTopic = {
      id: 'active',
      title: 'Recently active',
      createdAt: lastYear,
      updatedAt: lastYear,
      sortUpdatedAt: today,
    };

    const result = groupTopicsByUpdatedTime([topic]);

    expect(result).toHaveLength(1);
    expect(result[0].id).toBe('today');
  });

  it('should order rows by sortUpdatedAt (activity) over updatedAt (row edit time)', () => {
    const base = dayjs().subtract(1, 'month').valueOf();
    // `a` has an older row edit time but newer activity → must sort first.
    const a: ChatTopic = {
      id: 'a',
      title: 'Older edit, newer activity',
      createdAt: base,
      updatedAt: dayjs().hour(9).valueOf(),
      sortUpdatedAt: dayjs().hour(11).valueOf(),
    };
    const b: ChatTopic = {
      id: 'b',
      title: 'Newer edit, older activity',
      createdAt: base,
      updatedAt: dayjs().hour(10).valueOf(),
      sortUpdatedAt: dayjs().hour(10).valueOf(),
    };

    const result = groupTopicsByUpdatedTime([b, a]);

    expect(result[0].children.map((t) => t.id)).toEqual(['a', 'b']);
  });
});

describe('working directory topic helpers', () => {
  const createTopic = (
    id: string,
    metadata: ChatTopic['metadata'],
    updatedAt: number = 0,
  ): ChatTopic => ({
    createdAt: updatedAt,
    id,
    metadata,
    title: id,
    updatedAt,
  });

  it('preserves source and effective paths for worktree topics', () => {
    const topic = createTopic('worktree', {
      workingDirectory: '/repo-fix',
      workingDirectoryConfig: {
        git: { activeWorktree: '/repo-fix', branch: 'fix', isWorktree: true },
        path: '/repo',
        repoType: 'git',
      },
    });

    expect(getTopicWorkingDirectorySourcePath(topic)).toBe('/repo');
    expect(getTopicWorkingDirectoryEffectivePath(topic)).toBe('/repo-fix');
  });

  it('tolerates a legacy object-form workingDirectory without crashing', () => {
    // Some heterogeneous (Claude Code) topics persisted a `WorkingDirConfig`
    // object into `workingDirectory` even though the field is typed as a string.
    // The helpers must extract its path instead of calling `dir.trim()` on it.
    const topic = createTopic('legacy-object', {
      workingDirectory: { path: '/Users/xxx/项目目录', repoType: 'git' },
    } as unknown as ChatTopic['metadata']);

    expect(getTopicWorkingDirectorySourcePath(topic)).toBe('/Users/xxx/项目目录');
    expect(getTopicWorkingDirectoryEffectivePath(topic)).toBe('/Users/xxx/项目目录');
    expect(() => groupTopicsByProject([topic], 'updatedAt')).not.toThrow();
    expect(groupTopicsByProject([topic], 'updatedAt')[0]).toMatchObject({
      id: 'project:/Users/xxx/项目目录',
      title: '项目目录',
    });
  });

  /** @example Identical basenames expose only the distinguishing suffix, retaining membership. */
  it('disambiguates duplicate project names without changing group identity or topic order', () => {
    // ROOT CAUSE:
    //
    // groupTopicsByProject used only the final path segment for each title.
    // Separate IDs therefore rendered as the same "lobehub" in GroupedAccordion.
    // Expanding colliding names to a unique path suffix fixes the visible labels
    // while preserving the source-path IDs and topic sorting.
    const gitPath = '/Users/me/Git/lobehub/lobehub';
    const workPath = '/Users/me/work/lobehub/lobehub';
    const topics = [
      createTopic('git-old', { workingDirectory: gitPath }, 1),
      createTopic('work', { workingDirectory: workPath }, 3),
      createTopic('git-new', { workingDirectory: gitPath }, 4),
      createTopic('unique', { workingDirectory: '/Users/me/private/other' }, 2),
      createTopic('none', undefined, 5),
    ];

    const result = groupTopicsByProject(topics, 'updatedAt');

    /** @example The user's two checkouts keep their IDs, sorted children, and private ancestors hidden. */
    expect(result).toEqual([
      { children: [topics[2], topics[0]], id: `project:${gitPath}`, title: 'Git/lobehub/lobehub' },
      { children: [topics[1]], id: `project:${workPath}`, title: 'work/lobehub/lobehub' },
      { children: [topics[3]], id: 'project:/Users/me/private/other', title: 'other' },
      { children: [topics[4]], id: 'no-project', title: undefined },
    ]);
    /** @example Reversing input does not change the labels or time-sorted groups. */
    expect(groupTopicsByProject([...topics].reverse(), 'updatedAt')).toEqual(result);
  });

  /** @example A manager with 1,000 same-name projects retains unique labels and exact topic ownership. */
  it('disambiguates a large project list without changing membership', () => {
    // ROOT CAUSE:
    //
    // Each label previously split every other project's path, making the growing
    // manager list quadratic. Shared suffix counts remove that repeated work;
    // this case protects the resulting labels and membership at the reported scale.
    const topics = Array.from({ length: 1000 }, (_, index) =>
      createTopic(String(index), { workingDirectory: `/private/parent-${index}/repo` }, index),
    );
    const result = groupTopicsByProject(topics, 'updatedAt');

    /** @example All 1,000 independently owned projects remain visible and distinguishable. */
    expect(new Set(result.map(({ title }) => title)).size).toBe(1000);
    /** @example Each label reveals only its distinguishing parent and keeps the original ID and child. */
    expect(result).toEqual(
      [...topics].reverse().map((topic) => ({
        children: [topic],
        id: `project:/private/parent-${topic.id}/repo`,
        title: `parent-${topic.id}/repo`,
      })),
    );
  });

  /** @example A lone checkout keeps its basename even after a collision disappears. */
  it('keeps a unique project name compact', () => {
    const topics = [createTopic('single', { workingDirectory: '/Users/me/Git/lobehub/lobehub' })];

    /** @example No parent path is exposed without another same-name project. */
    expect(groupTopicsByProject(topics, 'createdAt')).toEqual([
      { children: topics, id: 'project:/Users/me/Git/lobehub/lobehub', title: 'lobehub' },
    ]);
  });

  /** @example Windows spelling remains part of identity; trailing separators retain existing normalization. */
  it('retains case-sensitive path identities and normalizes trailing separators', () => {
    const topics = [
      createTopic('upper', { workingDirectory: 'C:/Work/repo/' }),
      createTopic('lower', { workingDirectory: 'c:/work/repo' }),
      createTopic('same', { workingDirectory: ' C:/Work/repo// ' }),
    ];

    /** @example Display suffixes distinguish parents while existing matching paths stay together. */
    expect(groupTopicsByProject(topics, 'createdAt')).toEqual([
      { children: [topics[0], topics[2]], id: 'project:C:/Work/repo', title: 'Work/repo' },
      { children: [topics[1]], id: 'project:c:/work/repo', title: 'work/repo' },
    ]);
  });

  /** @example Drive letters and UNC server names remain available when nearer ancestors collide. */
  it('disambiguates Windows drives and remote shares', () => {
    const paths = [
      String.raw`C:\work\repo`,
      'D:/work/repo',
      String.raw`\\server-a\share\repo`,
      String.raw`\\server-b\share\repo`,
    ];
    const topics = paths.map((path, index) =>
      createTopic(String(index), { workingDirectory: path }),
    );

    /** @example Separators are consistent for display without altering path identity. */
    expect(
      groupTopicsByProject(topics, 'createdAt').map(({ id, title }) => ({ id, title })),
    ).toEqual([
      { id: `project:${paths[0]}`, title: 'C:/work/repo' },
      { id: `project:${paths[1]}`, title: 'D:/work/repo' },
      { id: `project:${paths[2]}`, title: 'server-a/share/repo' },
      { id: `project:${paths[3]}`, title: 'server-b/share/repo' },
    ]);
  });

  /** @example A path that is itself another path's suffix still has a distinct visible label. */
  it('preserves raw path distinctions when no unique segment suffix exists', () => {
    const paths = ['/repo', '/home/repo', 'home/repo', String.raw`home\repo`];
    const topics = paths.map((path, index) =>
      createTopic(String(index), { workingDirectory: path }),
    );

    /** @example The absolute root and separator spelling distinguish otherwise identical suffixes. */
    expect(groupTopicsByProject(topics, 'createdAt').map((group) => group.title)).toEqual(paths);
  });

  /** @example Worktree locations must not determine the source project's display suffix. */
  it('uses source repository paths when disambiguating worktree projects', () => {
    const topics = [
      createTopic('worktree', {
        workingDirectory: '/tmp/checkout',
        workingDirectoryConfig: {
          git: { activeWorktree: '/tmp/checkout', branch: 'fix', isWorktree: true },
          path: '/Git/repo',
          repoType: 'git',
        },
      }),
      createTopic('source', { workingDirectory: '/Git/repo' }),
      createTopic('other', { workingDirectory: '/work/repo' }),
    ];

    /** @example The worktree stays with its source; only the two project labels expand. */
    expect(groupTopicsByProject(topics, 'createdAt')).toEqual([
      { children: [topics[0], topics[1]], id: 'project:/Git/repo', title: 'Git/repo' },
      { children: [topics[2]], id: 'project:/work/repo', title: 'work/repo' },
    ]);
  });

  it('groups worktree topics under the source project', () => {
    const topics = [
      createTopic(
        'worktree',
        {
          workingDirectory: '/repo-fix',
          workingDirectoryConfig: {
            git: { activeWorktree: '/repo-fix', branch: 'fix', isWorktree: true },
            path: '/repo',
            repoType: 'git',
          },
        },
        2,
      ),
      createTopic('source', { workingDirectory: '/repo' }, 1),
    ];

    const result = groupTopicsByProject(topics, 'updatedAt');

    expect(result).toHaveLength(1);
    expect(result[0]).toMatchObject({ id: 'project:/repo', title: 'repo' });
    expect(result[0].children.map((topic) => topic.id)).toEqual(['worktree', 'source']);
  });
});

describe('groupTopicsByStatus', () => {
  const createTopic = (
    id: string,
    status: ChatTopic['status'],
    updatedAt: number = 0,
  ): ChatTopic => ({
    id,
    title: id,
    createdAt: updatedAt,
    status,
    updatedAt,
  });

  it('should return empty array for empty input', () => {
    expect(groupTopicsByStatus([], 'updatedAt')).toEqual([]);
  });

  it('should order groups by fixed priority: pending, running, then active', () => {
    const topics = [
      createTopic('a', 'active'),
      createTopic('r', 'running'),
      createTopic('w', 'waitingForHuman'),
    ];

    const result = groupTopicsByStatus(topics, 'updatedAt');

    expect(result.map((g) => g.id)).toEqual(['pending', 'running', 'active']);
  });

  it('should collapse waitingForHuman and failed into the pending bucket', () => {
    const topics = [
      createTopic('w', 'waitingForHuman', 2),
      createTopic('f', 'failed', 1),
      createTopic('a', 'active'),
    ];

    const result = groupTopicsByStatus(topics, 'updatedAt');

    expect(result.map((g) => g.id)).toEqual(['pending', 'active']);
    expect(result[0].children.map((t) => t.id)).toEqual(['w', 'f']);
  });

  it('should bucket an unread completion as pending while read completions stay completed', () => {
    const topics = [createTopic('unread', 'unread'), createTopic('read', 'completed')];

    const result = groupTopicsByStatus(topics, 'updatedAt');

    expect(result.map((g) => g.id)).toEqual(['pending', 'completed']);
    expect(result[0].children.map((t) => t.id)).toEqual(['unread']);
    expect(result[1].children.map((t) => t.id)).toEqual(['read']);
  });

  it('should bucket topics without a status as active', () => {
    const topics = [createTopic('1', undefined), createTopic('2', null)];

    const result = groupTopicsByStatus(topics, 'updatedAt');

    expect(result).toHaveLength(1);
    expect(result[0].id).toBe('active');
    expect(result[0].children.map((t) => t.id)).toEqual(['1', '2']);
  });

  it('should only emit non-empty groups and keep the remaining states below the priority ones', () => {
    const topics = [
      createTopic('c', 'completed'),
      createTopic('w', 'waitingForHuman'),
      createTopic('a', 'archived'),
    ];

    const result = groupTopicsByStatus(topics, 'updatedAt');

    expect(result.map((g) => g.id)).toEqual(['pending', 'completed', 'archived']);
  });

  it('should sort topics inside a group by the chosen field desc', () => {
    const topics = [
      createTopic('old', 'running', 1),
      createTopic('new', 'running', 100),
      createTopic('mid', 'running', 50),
    ];

    const result = groupTopicsByStatus(topics, 'updatedAt');

    expect(result[0].children.map((t) => t.id)).toEqual(['new', 'mid', 'old']);
  });

  it('should bucket a topic that is streaming on this client (loadingTopicIds) as running', () => {
    const topics = [createTopic('loading', 'active'), createTopic('idle', 'active')];

    const result = groupTopicsByStatus(topics, 'updatedAt', new Set(['loading']));

    expect(result.map((g) => g.id)).toEqual(['running', 'active']);
    expect(result[0].children.map((t) => t.id)).toEqual(['loading']);
    expect(result[1].children.map((t) => t.id)).toEqual(['idle']);
  });

  it('should keep a loading topic in pending (it outranks the running overlay)', () => {
    const topics = [createTopic('waiting', 'waitingForHuman')];

    const result = groupTopicsByStatus(topics, 'updatedAt', new Set(['waiting']));

    expect(result.map((g) => g.id)).toEqual(['pending']);
  });
});

it('keeps project directory bindings and device identities separate', () => {
  const base = { createdAt: 1, updatedAt: 1, title: 'Work' };
  const topics: ChatTopic[] = [
    {
      ...base,
      id: 'a',
      projectWorkingDirectoryId: 'directory-a',
      metadata: { workingDirectory: '/repo' },
    },
    {
      ...base,
      id: 'b',
      projectWorkingDirectoryId: 'directory-b',
      metadata: { workingDirectory: '/repo' },
    },
    { ...base, id: 'c', metadata: { workingDirectory: '/repo', boundDeviceId: 'device-a' } },
    { ...base, id: 'd', metadata: { workingDirectory: '/repo', boundDeviceId: 'device-b' } },
  ];
  expect(groupTopicsByProject(topics, 'updatedAt')).toHaveLength(4);
});

describe('groupTopicsByProject on one machine with several device identities', () => {
  const row = (id: string, updatedAt: number, boundDeviceId?: string): ChatTopic => ({
    createdAt: updatedAt,
    id,
    metadata: { boundDeviceId, workingDirectory: '/home/me/workspaces/cpc' },
    title: id,
    updatedAt,
  });

  it('merges one folder used through both identities of this machine', () => {
    const topics = [row('desk', 3, 'local-personal'), row('shared', 2, 'local-workspace')];

    expect(
      groupTopicsByProject(topics, 'updatedAt', {
        localDeviceIds: ['local-personal', 'local-workspace'],
      }),
    ).toEqual([
      {
        children: [topics[0], topics[1]],
        id: 'project:local-personal:/home/me/workspaces/cpc',
        title: 'cpc',
      },
    ]);
  });

  it('keeps the folder of another machine apart, naming its device to tell them apart', () => {
    const topics = [row('desk', 3, 'local-personal'), row('remote', 2, 'other-machine')];

    expect(
      groupTopicsByProject(topics, 'updatedAt', { localDeviceIds: ['local-personal'] }).map(
        ({ deviceId, id, title }) => ({ deviceId, id, title }),
      ),
    ).toEqual([
      {
        deviceId: 'local-personal',
        id: 'project:local-personal:/home/me/workspaces/cpc',
        title: 'cpc',
      },
      {
        deviceId: 'other-machine',
        id: 'project:other-machine:/home/me/workspaces/cpc',
        title: 'cpc',
      },
    ]);
  });

  it('names no device when the folder title is already unambiguous', () => {
    const [group] = groupTopicsByProject([row('only', 1, 'other-machine')], 'updatedAt');

    expect(group.title).toBe('cpc');
    expect(group).not.toHaveProperty('deviceId');
  });

  it('keeps the short name when a project registers the same folder', () => {
    const projectTopic: ChatTopic = {
      ...row('project', 4, 'local-personal'),
      projectId: 'pro_1',
      projectWorkingDirectoryId: 'dir_1',
    };
    const topics = [projectTopic, row('desk', 3, 'local-personal'), row('remote', 2, 'other')];

    expect(
      groupTopicsByProject(topics, 'updatedAt').map(({ id, title }) => ({ id, title })),
    ).toEqual([
      { id: 'project-id:pro_1', title: 'cpc' },
      { id: 'project:local-personal:/home/me/workspaces/cpc', title: 'cpc' },
      { id: 'project:other:/home/me/workspaces/cpc', title: 'cpc' },
    ]);
  });

  it('still lengthens the name of different folders that share a basename', () => {
    const other: ChatTopic = {
      ...row('elsewhere', 1, 'local-personal'),
      metadata: { boundDeviceId: 'local-personal', workingDirectory: '/srv/cpc' },
    };

    expect(
      groupTopicsByProject([row('desk', 3, 'local-personal'), other], 'updatedAt').map(
        (group) => group.title,
      ),
    ).toEqual(['workspaces/cpc', 'srv/cpc']);
  });
});

describe('groupTopicsByProject project merging', () => {
  const base = { createdAt: 0, title: 'Work', updatedAt: 0 };

  /** @example Two directories of one project must collapse into a single sidebar group. */
  it('merges directories of the same project into one project-id group', () => {
    // ROOT CAUSE:
    //
    // Directory-bound topics were grouped per `projectWorkingDirectoryId`, so
    // two directories bound to the same project rendered as two identically
    // named groups in the sidebar's "by project" mode. Grouping by the
    // topic's `projectId` first merges them into a single group.
    const topics: ChatTopic[] = [
      {
        ...base,
        createdAt: 3,
        updatedAt: 3,
        id: 'recent',
        projectId: 'project-1',
        projectWorkingDirectoryId: 'directory-a',
        metadata: { workingDirectory: '/work/lobehub' },
      },
      {
        ...base,
        createdAt: 1,
        updatedAt: 1,
        id: 'older',
        projectId: 'project-1',
        projectWorkingDirectoryId: 'directory-b',
        metadata: { workingDirectory: '/home/lobehub' },
      },
    ];

    expect(groupTopicsByProject(topics, 'updatedAt')).toEqual([
      {
        children: [topics[0], topics[1]],
        id: 'project-id:project-1',
        // Same-basename directories collide, so the label expands to the
        // distinguishing parent of the most recently active directory.
        title: 'work/lobehub',
      },
    ]);
  });

  /** @example A project conversation without a working directory still belongs to its project group. */
  it('merges pathless project topics into their project group', () => {
    const topics: ChatTopic[] = [
      {
        ...base,
        createdAt: 2,
        updatedAt: 2,
        id: 'with-directory',
        projectId: 'project-1',
        projectWorkingDirectoryId: 'directory-a',
        metadata: { workingDirectory: '/work/lobehub' },
      },
      { ...base, createdAt: 1, updatedAt: 1, id: 'conversation-only', projectId: 'project-1' },
    ];

    expect(groupTopicsByProject(topics, 'updatedAt')).toEqual([
      {
        children: [topics[0], topics[1]],
        id: 'project-id:project-1',
        title: 'lobehub',
      },
    ]);
  });

  /** @example Topics without a project binding keep their per-directory groups. */
  it('keeps directory grouping when topics have no project binding', () => {
    const topics: ChatTopic[] = [
      {
        ...base,
        id: 'a',
        projectWorkingDirectoryId: 'directory-a',
        metadata: { workingDirectory: '/repo-a' },
      },
      {
        ...base,
        id: 'b',
        projectWorkingDirectoryId: 'directory-b',
        metadata: { workingDirectory: '/repo-b' },
      },
    ];

    expect(groupTopicsByProject(topics, 'updatedAt')).toEqual([
      {
        children: [topics[0]],
        id: 'project-directory:directory-a',
        title: 'repo-a',
      },
      {
        children: [topics[1]],
        id: 'project-directory:directory-b',
        title: 'repo-b',
      },
    ]);
  });

  /** @example Distinct projects never merge, and project groups stay above unbound topics. */
  it('keeps distinct projects separate and sorts the no-project bucket last', () => {
    const topics: ChatTopic[] = [
      { ...base, createdAt: 1, updatedAt: 1, id: 'unbound' },
      {
        ...base,
        createdAt: 2,
        updatedAt: 2,
        id: 'mobile',
        projectId: 'project-2',
        metadata: { workingDirectory: '/home/lobehub_mobile' },
      },
      {
        ...base,
        createdAt: 3,
        updatedAt: 3,
        id: 'main',
        projectId: 'project-1',
        metadata: { workingDirectory: '/home/lobehub' },
      },
    ];

    expect(groupTopicsByProject(topics, 'updatedAt').map((group) => group.id)).toEqual([
      'project-id:project-1',
      'project-id:project-2',
      'no-project',
    ]);
  });

  /** @example A stale explicit project still leads a freshly active directory group. */
  it('sorts explicit-project groups ahead of directory groups regardless of activity', () => {
    // ROOT CAUSE:
    //
    // Groups sorted purely by most recent activity, so a project the user
    // deliberately bound topics to could sink below an inferred directory
    // group. Explicit bindings are stronger signals and lead the list; only
    // groups within the same tier compete on activity.
    const topics: ChatTopic[] = [
      {
        ...base,
        createdAt: 1,
        updatedAt: 1,
        id: 'bound-stale',
        projectId: 'project-1',
        metadata: { workingDirectory: '/home/lobehub' },
      },
      {
        ...base,
        createdAt: 3,
        updatedAt: 3,
        id: 'unbound-recent',
        metadata: { workingDirectory: '/tmp/scratch' },
      },
      { ...base, createdAt: 2, updatedAt: 2, id: 'unbound' },
    ];

    expect(groupTopicsByProject(topics, 'updatedAt').map((group) => group.id)).toEqual([
      'project-id:project-1',
      'project:/tmp/scratch',
      'no-project',
    ]);
  });

  /** @example Within the explicit-project tier the most recently active project still leads. */
  it('keeps most-recent-activity order inside the explicit-project tier', () => {
    const topics: ChatTopic[] = [
      { ...base, createdAt: 1, updatedAt: 1, id: 'older', projectId: 'project-1' },
      { ...base, createdAt: 2, updatedAt: 2, id: 'newer', projectId: 'project-2' },
    ];

    expect(groupTopicsByProject(topics, 'updatedAt').map((group) => group.id)).toEqual([
      'project-id:project-2',
      'project-id:project-1',
    ]);
  });
});

describe('groupTopicsByAgent', () => {
  type AgentRow = ChatTopic & {
    agentId?: string | null;
    agentName?: string | null;
    agentTitle?: string | null;
  };

  const createRow = (
    id: string,
    updatedAt: number,
    agent?: { agentId?: string | null; agentName?: string | null; agentTitle?: string | null },
  ): AgentRow => ({ agentId: null, createdAt: updatedAt, id, title: id, updatedAt, ...agent });

  it('should return empty array for empty input', () => {
    expect(groupTopicsByAgent([], 'updatedAt')).toEqual([]);
  });

  it('groups topics by agent with titles from agent metadata', () => {
    const topics = [
      createRow('a1', 2, { agentId: 'agent-x', agentTitle: 'LobeHub' }),
      createRow('a2', 1, { agentId: 'agent-x', agentTitle: 'LobeHub' }),
      createRow('b1', 3, { agentId: 'agent-y', agentName: 'lobehub_mobile' }),
    ];

    expect(groupTopicsByAgent(topics, 'updatedAt')).toEqual([
      { children: [topics[2]], id: 'agent:agent-y', title: 'lobehub_mobile' },
      { children: [topics[0], topics[1]], id: 'agent:agent-x', title: 'LobeHub' },
    ]);
  });

  it('titles the group with agentName over agentTitle', () => {
    const topics = [
      createRow('a1', 1, {
        agentId: 'agent-x',
        agentName: 'Cook',
        agentTitle: '公共CC(美国 100刀)',
      }),
    ];

    expect(groupTopicsByAgent(topics, 'updatedAt')[0]).toEqual({
      children: topics,
      id: 'agent:agent-x',
      title: 'Cook',
    });
  });

  it('falls back to agentTitle when agentName is blank', () => {
    const topics = [
      createRow('a1', 1, { agentId: 'agent-x', agentName: ' ', agentTitle: 'LobeHub' }),
    ];

    expect(groupTopicsByAgent(topics, 'updatedAt')[0].title).toBe('LobeHub');
  });

  it('buckets rows without agent attribution as a title-less no-agent group', () => {
    const topics = [createRow('a', 2), createRow('b', 1)];

    expect(groupTopicsByAgent(topics, 'updatedAt')).toEqual([
      { children: topics, id: 'no-agent', title: undefined },
    ]);
  });

  it('sorts children and groups by the chosen field descending', () => {
    const topics = [
      createRow('old', 1, { agentId: 'agent-x' }),
      createRow('new', 5, { agentId: 'agent-x' }),
      createRow('other-recent', 4, { agentId: 'agent-y' }),
    ];

    const result = groupTopicsByAgent(topics, 'createdAt');

    expect(result.map((group) => group.id)).toEqual(['agent:agent-x', 'agent:agent-y']);
    expect(result[0].children.map((topic) => topic.id)).toEqual(['new', 'old']);
  });
});

describe('groupTopicsByDevice', () => {
  const createRow = (id: string, updatedAt: number, extra?: Partial<ChatTopic>): ChatTopic => ({
    createdAt: updatedAt,
    id,
    title: id,
    updatedAt,
    ...extra,
  });

  it('should return empty array for empty input', () => {
    expect(groupTopicsByDevice([], 'updatedAt')).toEqual([]);
  });

  it('groups topics by metadata.boundDeviceId', () => {
    const topics = [
      createRow('a1', 2, { metadata: { boundDeviceId: 'device-x' } }),
      createRow('a2', 1, { metadata: { boundDeviceId: 'device-x' } }),
      createRow('b1', 3, { metadata: { boundDeviceId: 'device-y' } }),
    ];

    const result = groupTopicsByDevice(topics, 'updatedAt');

    expect(result.map((group) => group.id)).toEqual(['device:device-y', 'device:device-x']);
    expect(result[1].children.map((topic) => topic.id)).toEqual(['a1', 'a2']);
  });

  it('resolves the device from the topic directory when boundDeviceId is absent', () => {
    const topics = [
      createRow('a', 2, { projectWorkingDirectoryId: 'dir-1' }),
      createRow('b', 1, { projectWorkingDirectoryId: 'dir-2' }),
    ];
    const directories = [
      { deviceId: 'device-x', id: 'dir-1' },
      { deviceId: 'device-y', id: 'dir-2' },
    ];

    expect(groupTopicsByDevice(topics, 'updatedAt', directories).map((group) => group.id)).toEqual([
      'device:device-x',
      'device:device-y',
    ]);
  });

  it('prefers metadata.boundDeviceId over the directory device', () => {
    const topics = [
      createRow('a', 1, {
        metadata: { boundDeviceId: 'device-bound' },
        projectWorkingDirectoryId: 'dir-1',
      }),
    ];
    const directories = [{ deviceId: 'device-x', id: 'dir-1' }];

    expect(groupTopicsByDevice(topics, 'updatedAt', directories)[0].id).toBe('device:device-bound');
  });

  it('buckets rows without device attribution as a title-less no-device group', () => {
    const topics = [
      createRow('a', 2),
      createRow('b', 1, { projectWorkingDirectoryId: 'dir-missing' }),
    ];

    expect(groupTopicsByDevice(topics, 'updatedAt', [])).toEqual([
      { children: topics, id: 'no-device' },
    ]);
  });

  it("merges this machine's device identities into one group under its first id", () => {
    const topics = [
      createRow('desk', 3, { metadata: { boundDeviceId: 'local-personal' } }),
      createRow('shared', 2, { projectWorkingDirectoryId: 'dir-shared' }),
      createRow('remote', 1, { metadata: { boundDeviceId: 'other-machine' } }),
    ];

    expect(
      groupTopicsByDevice(
        topics,
        'updatedAt',
        [{ deviceId: 'local-workspace', id: 'dir-shared' }],
        {
          localDeviceIds: ['local-personal', 'local-workspace'],
        },
      ),
    ).toEqual([
      { children: [topics[0], topics[1]], id: 'device:local-personal' },
      { children: [topics[2]], id: 'device:other-machine' },
    ]);
  });

  it('sorts children and groups by the chosen field descending', () => {
    const topics = [
      createRow('old', 1, { metadata: { boundDeviceId: 'device-x' } }),
      createRow('new', 5, { metadata: { boundDeviceId: 'device-x' } }),
      createRow('other-recent', 4, { metadata: { boundDeviceId: 'device-y' } }),
    ];

    const result = groupTopicsByDevice(topics, 'createdAt');

    expect(result.map((group) => group.id)).toEqual(['device:device-x', 'device:device-y']);
    expect(result[0].children.map((topic) => topic.id)).toEqual(['new', 'old']);
  });
});

describe('filterTopics', () => {
  const row = (id: string, extra: Partial<ChatTopic> = {}): ChatTopic =>
    ({ createdAt: 1, id, title: id, updatedAt: 1, ...extra }) as ChatTopic;
  const topics = [
    row('waiting', { status: 'waitingForHuman', metadata: { boundDeviceId: 'device-x' } }),
    row('running', { projectWorkingDirectoryId: 'dir-1', status: 'running' }),
    row('idle'),
  ];
  const directories = [{ deviceId: 'device-y', id: 'dir-1' }];

  it('returns the same list when nothing is filtered', () => {
    expect(filterTopics(topics, {})).toBe(topics);
  });

  it('keeps the topics in the chosen status bucket, needs-attention included', () => {
    expect(filterTopics(topics, { status: 'pending' }).map((topic) => topic.id)).toEqual([
      'waiting',
    ]);
    expect(filterTopics(topics, { status: 'active' }).map((topic) => topic.id)).toEqual(['idle']);
  });

  it('counts a topic streaming on this client as running', () => {
    expect(
      filterTopics(topics, { status: 'running' }, { loadingTopicIds: new Set(['idle']) }).map(
        (topic) => topic.id,
      ),
    ).toEqual(['running', 'idle']);
  });

  it('attributes devices like device grouping, and keeps unattributed rows under none', () => {
    expect(
      filterTopics(topics, { device: 'device-y' }, { directories }).map((topic) => topic.id),
    ).toEqual(['running']);
    expect(
      filterTopics(topics, { device: 'none' }, { directories }).map((topic) => topic.id),
    ).toEqual(['idle']);
  });

  it('combines status and device', () => {
    expect(
      filterTopics(topics, { device: 'device-x', status: 'running' }, { directories }),
    ).toEqual([]);
  });
});