Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

54.5 KB
Raw Blame
/**
 * mainListModel — 主列表混排模型单测(sidebar-redesign D 期)。
 * 覆盖:混排口径(项目行与散排对话平级竞争位置)、任务排序、对话组开关、
 * flat 平铺、自定义项目顺序只管项目行的收窄语义。
 */

import { beforeEach, describe, expect, it, vi } from 'vitest';

const feature = vi.hoisted(() => ({ enabled: true }));
vi.mock('@cindy/maker-shared/cindy-make-feature', async (importOriginal) => ({
  ...await importOriginal<typeof import('@cindy/maker-shared/cindy-make-feature')>(),
  get CINDY_MAKE_ENABLED() { return feature.enabled; },
}));
beforeEach(() => { feature.enabled = true; });

import { LIVE_TASK_PRIORITY } from '../../shared/liveTaskPriority';
import type { Session } from '@/lib/ccAgent.types';
import type { ProjectNode } from '../features/cc-agent/lib/projectGrouping';
import {
  advanceViewedPriorityHold,
  buildMainListEntries,
  getMainListEntrySessions,
  holdViewedPriorityRank,
  sessionPriorityRecencyMs,
  sessionPriorityRank,
  sortSessionsForMainList,
  partitionCindyMakeSessions,
  onlineDeviceSectionIds,
  hasSettledOnlineDeviceSection,
  splitEntriesByDevice,
  type MainListEntry,
} from '../features/cc-agent/lib/mainListModel';

let seq = 0;
function session(overrides: Partial<Session> & { updatedAt: string }): Session {
  seq += 1;
  return {
    id: `s${seq}`,
    userId: 'u',
    title: `session ${seq}`,
    workingDir: null,
    workspaceKind: 'dialogue',
    model: 'm',
    effort: 'high',
    permissionMode: 'ask',
    providerId: null,
    sdkSessionId: null,
    totalTokenUsage: 0,
    totalCostUsd: 0,
    contextTokens: 0,
    contextWindow: 0,
    fastMode: false,
    clearedAt: null,
    pinnedAt: null,
    userSendAt: null,
    status: 'active',
    agentKind: 'cc',
    createdAt: overrides.updatedAt,
    ...overrides,
  } as Session;
}

function project(key: string, sessions: Session[]): ProjectNode {
  return {
    projectKey: `local:${key}`,
    workingDir: key,
    displayName: key,
    scope: 'local',
    sessions,
    latestActivityAt: sessions[0]?.updatedAt ?? null,
  } as unknown as ProjectNode;
}

function labels(entries: MainListEntry[]): string[] {
  return entries.map((entry) =>
    entry.kind === 'project'
      ? `p:${entry.project.displayName}`
      : entry.kind === 'dialogue-group'
        ? 'dlg-group'
        : entry.kind === 'automation-group'
          ? `auto:${entry.group.title}`
          : entry.kind === 'bot-group'
            ? `bot:${entry.bot.displayName}`
            : entry.kind === 'cindy-make-group'
              ? 'cindy-make-group'
              : `s:${entry.session.title}`,
  );
}

const NO_PRIORITY = {
  runningSessionIds: new Set<string>(),
  attentionSessionIds: new Set<string>(),
};

describe('Cindy Make sidebar group', () => {
  it('preserves empty saved projects and ordinary tasks in mixed projects while disabled', () => {
    feature.enabled = false;
    const updatedAt = '2026-10-12T00:00:00Z';
    const hidden = session({ id: 'make', source: 'cindy-make', updatedAt });
    const ordinary = session({ id: 'ordinary', source: 'desktop', updatedAt });
    const saved = project('saved', []);
    const mixed = project('mixed', [hidden, ordinary]);
    const entries = buildMainListEntries({
      projects: [saved, mixed], dialogues: [], unclassified: [], groupBy: 'project',
      groupDialogue: false, sortBy: 'recency', manualProjectOrder: [],
    });
    expect(entries.map(entry => entry.kind === 'project' ? entry.project.projectKey : entry.kind).sort()).toEqual(['local:mixed', 'local:saved']);
    expect(entries.flatMap(getMainListEntrySessions)).toEqual([ordinary]);
    expect(mixed.sessions).toEqual([hidden, ordinary]);
  });
  it.each(['project', 'flat'] as const)('hides all Make sources and their projects in %s mode when disabled', (groupBy) => {
    feature.enabled = false;
    const updatedAt = '2026-10-12T00:00:00Z';
    const make = session({ id: 'make', source: 'cindy-make', updatedAt });
    const merge = session({ id: 'merge', source: 'cindy-make-merge', updatedAt });
    const ordinary = session({ id: 'ordinary', title: 'Ordinary', source: 'desktop', updatedAt });
    const entries = buildMainListEntries({
      projects: [project('make', [make])], dialogues: [ordinary], unclassified: [merge],
      groupBy, groupDialogue: false, sortBy: 'recency', manualProjectOrder: [],
    });
    expect(labels(entries)).toEqual(['s:Ordinary']);
    expect(getMainListEntrySessions(entries[0])).toEqual([ordinary]);
  });
  it('groups a distinct upstream-merge source alongside personal-feature tasks', () => {
    const merge = session({ source: 'cindy-make-merge', workingDir: '/managed/merge-worktrees/abcd', workspaceKind: 'project', updatedAt: '2026-09-17T00:00:00Z' });
    const normal = session({ source: 'cindy-make', workingDir: '/managed/worktrees/efgh', workspaceKind: 'project', updatedAt: '2026-09-17T00:00:00Z' });
    const result = partitionCindyMakeSessions({ projects: [], dialogues: [], unclassified: [merge, normal] });
    expect(result.cindyMake).toEqual([merge, normal]);
    expect(result.unclassified).toEqual([]);
    expect(merge.source).toBe('cindy-make-merge');
  });
  const make = (id: string, updatedAt: string, extra: Partial<Session> = {}) =>
    session({
      id,
      updatedAt,
      title: id,
      source: 'cindy-make',
      workspaceKind: 'project',
      workingDir: '/managed/worktrees/' + id,
      ...extra,
    });

  it.each(['project', 'flat'] as const)(
    'collects different worktrees and newly preparing tasks under one root in %s mode',
    (groupBy) => {
      const first = make('first', '2026-09-17T01:00:00Z');
      const second = make('second', '2026-09-17T02:00:00Z');
      const waiting = make('waiting', '2026-09-17T03:00:00Z', { workingDir: null });
      const ordinary = session({ updatedAt: '2026-09-17T00:00:00Z', title: 'Cindy Make' });
      const entries = buildMainListEntries({
        projects: [project('first', [first]), project('second', [second])],
        dialogues: [ordinary],
        unclassified: [waiting],
        groupBy,
        groupDialogue: false,
        sortBy: 'recency',
        manualProjectOrder: [],
      });
      expect(labels(entries)).toEqual(['cindy-make-group', 's:Cindy Make']);
      expect(getMainListEntrySessions(entries[0]).map((value) => value.id)).toEqual([
        'waiting',
        'second',
        'first',
      ]);
      expect(first.workingDir).toBe('/managed/worktrees/first');
      expect(second.workingDir).toBe('/managed/worktrees/second');
    },
  );

  it('preserves ordinary tasks that share a directory and leaves empty saved projects intact', () => {
    const personal = make('make', '2026-09-17T02:00:00Z');
    const ordinary = session({ updatedAt: '2026-09-17T01:00:00Z', title: 'ordinary' });
    const mixed = project('shared', [personal, ordinary]);
    const empty = project('saved', []);
    const result = partitionCindyMakeSessions({
      projects: [mixed, empty],
      dialogues: [],
      unclassified: [],
    });
    expect(result.projects.map((value) => value.projectKey)).toEqual([
      'local:shared',
      'local:saved',
    ]);
    expect(result.projects[0].sessions).toEqual([ordinary]);
    expect(result.projects[1]).toBe(empty);
    expect(result.cindyMake).toEqual([personal]);
    expect(mixed.sessions).toEqual([personal, ordinary]);
  });

  it('keeps the root first while ordering its tasks by the existing attention priorities', () => {
    const waiting = make('waiting', '2026-09-17T01:00:00Z');
    const newer = make('newer', '2026-09-17T02:00:00Z');
    const newest = session({ updatedAt: '2026-09-17T03:00:00Z' });
    const entries = buildMainListEntries({
      projects: [project('waiting', [waiting]), project('newer', [newer])],
      dialogues: [newest],
      groupBy: 'project',
      groupDialogue: false,
      sortBy: 'priority',
      manualProjectOrder: [],
      priorityContext: {
        ...NO_PRIORITY,
        attentionSessionIds: new Set([waiting.id]),
        waitingSessionIds: new Set([waiting.id]),
      },
    });
    expect(entries[0].kind).toBe('cindy-make-group');
    expect(getMainListEntrySessions(entries[0])).toEqual([waiting, newer]);
  });

  it.each(['recency', 'created', 'priority'] as const)(
    'keeps idle Cindy Make first under %s, custom project order and device grouping',
    (sortBy) => {
      const local = make('local', '2026-09-17T01:00:00Z');
      const remote = make('remote', '2026-09-17T01:00:00Z', { deviceLinkDeviceId: 'remote-device' });
      const newer = session({ updatedAt: '2026-09-18T00:00:00Z' });
      const remoteNewer = session({ updatedAt: '2026-09-18T00:00:00Z', deviceLinkDeviceId: 'remote-device' });
      for (const groupBy of ['project', 'flat'] as const) {
        for (const projectOrder of ['activity', 'custom'] as const) {
          const options = {
            sortBy,
            projectOrder,
            manualProjectOrder: ['local:ordinary'],
            priorityContext: {
              ...NO_PRIORITY,
              attentionSessionIds: new Set([newer.id, remoteNewer.id]),
              waitingSessionIds: new Set([newer.id, remoteNewer.id]),
            },
          };
          const entries = buildMainListEntries({
            projects: [project('ordinary', [newer]), project('make', [local])],
            dialogues: [remoteNewer],
            unclassified: [remote],
            groupBy,
            groupDialogue: false,
            ...options,
          });
          expect(entries[0].kind).toBe('cindy-make-group');
          expect(getMainListEntrySessions(entries[0])).toHaveLength(2);
          const sections = splitEntriesByDevice(entries, ['remote-device'], options);
          expect(sections.map((section) => section.entries[0].kind)).toEqual([
            'cindy-make-group',
            'cindy-make-group',
          ]);
          expect(getMainListEntrySessions(sections[0].entries[0])).toEqual([local]);
          expect(getMainListEntrySessions(sections[1].entries[0])).toEqual([remote]);
        }
      }
    },
  );

  it('separates local and remote Cindy Make roots by device', () => {
    const local = make('local', '2026-09-17T01:00:00Z');
    const remote = make('remote', '2026-09-17T02:00:00Z', { deviceLinkDeviceId: 'remote-device' });
    const sections = splitEntriesByDevice(
      [{ kind: 'cindy-make-group', sessions: [remote, local] }],
      ['remote-device'],
    );
    expect(sections.map((section) => section.deviceId)).toEqual([null, 'remote-device']);
    expect(sections.map((section) => section.entries[0].kind)).toEqual([
      'cindy-make-group',
      'cindy-make-group',
    ]);
    expect(getMainListEntrySessions(sections[0].entries[0])).toEqual([local]);
    expect(getMainListEntrySessions(sections[1].entries[0])).toEqual([remote]);
  });
});

describe('buildMainListEntries — 混排(recency)', () => {
  it('interleaves project rows and stray dialogues by latest activity', () => {
    const projNew = project('alpha', [session({ updatedAt: '2026-08-12T10:00:00Z' })]);
    const projOld = project('beta', [session({ updatedAt: '2026-08-10T10:00:00Z' })]);
    const dlgMid = session({ updatedAt: '2026-08-11T10:00:00Z', title: 'mid-dlg' });
    const entries = buildMainListEntries({
      projects: [projOld, projNew],
      dialogues: [dlgMid],
      groupBy: 'project',
      groupDialogue: false,
      sortBy: 'recency',
      manualProjectOrder: [],
    });
    // 两分钟前活跃的对话排在昨天的项目上面——项目不是特权层级。
    expect(labels(entries)).toEqual(['p:alpha', 's:mid-dlg', 'p:beta']);
  });

  it('collects dialogues into one group entry when groupDialogue is on', () => {
    const proj = project('alpha', [session({ updatedAt: '2026-08-12T10:00:00Z' })]);
    const dlgA = session({ updatedAt: '2026-08-12T12:00:00Z', title: 'a' });
    const dlgB = session({ updatedAt: '2026-08-01T00:00:00Z', title: 'b' });
    const entries = buildMainListEntries({
      projects: [proj],
      dialogues: [dlgA, dlgB],
      groupBy: 'project',
      groupDialogue: true,
      sortBy: 'recency',
      manualProjectOrder: [],
    });
    // 对话组按组内最新活动(dlgA)参与排序 → 排在项目前。
    expect(labels(entries)).toEqual(['dlg-group', 'p:alpha']);
    const group = entries[0];
    expect(group.kind === 'dialogue-group' && group.sessions).toHaveLength(2);
  });

  it('flattens project sessions to top level when groupBy is flat', () => {
    const proj = project('alpha', [
      session({ updatedAt: '2026-08-12T10:00:00Z', title: 'in-proj' }),
    ]);
    const dlg = session({ updatedAt: '2026-08-12T11:00:00Z', title: 'dlg' });
    const entries = buildMainListEntries({
      projects: [proj],
      dialogues: [dlg],
      groupBy: 'flat',
      groupDialogue: false,
      sortBy: 'recency',
      manualProjectOrder: [],
    });
    expect(labels(entries)).toEqual(['s:dlg', 's:in-proj']);
  });

  it('keeps repeated automation runs grouped when project grouping is flat', () => {
    const olderRun = session({
      updatedAt: '2026-08-10T10:00:00Z',
      title: 'automation run 1',
      source: 'scheduler',
      workspaceKind: 'project',
      workingDir: '/repo',
    });
    const newerRun = session({
      updatedAt: '2026-08-12T10:00:00Z',
      title: 'automation run 2',
      source: 'scheduler',
      workspaceKind: 'project',
      workingDir: '/repo',
    });
    const manual = session({
      updatedAt: '2026-08-11T10:00:00Z',
      title: 'manual',
      workspaceKind: 'project',
      workingDir: '/repo',
    });
    const scheduleInfo = {
      scheduleId: 'schedule-cindy-check',
      scheduleName: '自动检查 Cindy',
      unreadRunIds: [],
      hasUnreadRun: false,
      hasUnreadFailedRun: false,
      unreadFailedRunIds: [],
    };

    const entries = buildMainListEntries({
      projects: [project('/repo', [olderRun, manual, newerRun])],
      dialogues: [],
      groupBy: 'flat',
      groupDialogue: false,
      sortBy: 'recency',
      manualProjectOrder: [],
      notifications: new Set(),
      scheduleSessionIndex: new Map([
        [olderRun.id, scheduleInfo],
        [newerRun.id, scheduleInfo],
      ]),
    });

    expect(labels(entries)).toEqual(['auto:自动检查 Cindy', 's:manual']);
    const automationGroup = entries[0];
    expect(automationGroup.kind).toBe('automation-group');
    if (automationGroup.kind !== 'automation-group') throw new Error('expected automation group');
    expect(automationGroup.group.sessions.map((item) => item.id)).toEqual([
      newerRun.id,
      olderRun.id,
    ]);
  });

  it('keeps one schedule grouped after its working directory changes', () => {
    const olderRun = session({
      updatedAt: '2026-08-10T10:00:00Z',
      title: 'automation run 1',
      source: 'scheduler',
      workspaceKind: 'project',
      workingDir: '/old-repo',
    });
    const newerRun = session({
      updatedAt: '2026-08-12T10:00:00Z',
      title: 'automation run 2',
      source: 'scheduler',
      workspaceKind: 'project',
      workingDir: '/new-repo',
    });
    const scheduleInfo = {
      scheduleId: 'schedule-cindy-check',
      scheduleName: '自动检查 Cindy',
      unreadRunIds: [],
      hasUnreadRun: false,
      hasUnreadFailedRun: false,
      unreadFailedRunIds: [],
    };

    const entries = buildMainListEntries({
      projects: [project('/old-repo', [olderRun]), project('/new-repo', [newerRun])],
      dialogues: [],
      groupBy: 'flat',
      groupDialogue: false,
      sortBy: 'recency',
      manualProjectOrder: [],
      scheduleSessionIndex: new Map([
        [olderRun.id, scheduleInfo],
        [newerRun.id, scheduleInfo],
      ]),
    });

    expect(labels(entries)).toEqual(['auto:自动检查 Cindy']);
    const automationGroup = entries[0];
    expect(automationGroup.kind).toBe('automation-group');
    if (automationGroup.kind !== 'automation-group') throw new Error('expected automation group');
    expect(automationGroup.group.sessions.map((item) => item.id)).toEqual([
      newerRun.id,
      olderRun.id,
    ]);
  });

  it('groups one schedule across project and dialogue destinations before grouping dialogues', () => {
    const olderProjectRun = session({
      updatedAt: '2026-08-10T10:00:00Z',
      title: 'project run',
      source: 'scheduler',
      workspaceKind: 'project',
      workingDir: '/repo',
    });
    const newerDialogueRun = session({
      updatedAt: '2026-08-12T10:00:00Z',
      title: 'dialogue run',
      source: 'scheduler',
      workspaceKind: 'dialogue',
      workingDir: null,
    });
    const manualDialogue = session({
      updatedAt: '2026-08-11T10:00:00Z',
      title: 'manual dialogue',
    });
    const scheduleInfo = {
      scheduleId: 'schedule-cindy-check',
      scheduleName: '自动检查 Cindy',
      unreadRunIds: [],
      hasUnreadRun: false,
      hasUnreadFailedRun: false,
      unreadFailedRunIds: [],
    };

    const entries = buildMainListEntries({
      projects: [project('/repo', [olderProjectRun])],
      dialogues: [manualDialogue, newerDialogueRun],
      groupBy: 'flat',
      groupDialogue: true,
      sortBy: 'recency',
      manualProjectOrder: [],
      scheduleSessionIndex: new Map([
        [olderProjectRun.id, scheduleInfo],
        [newerDialogueRun.id, scheduleInfo],
      ]),
    });

    expect(labels(entries)).toEqual(['auto:自动检查 Cindy', 'dlg-group']);
    const automationGroup = entries[0];
    expect(automationGroup.kind).toBe('automation-group');
    if (automationGroup.kind !== 'automation-group') throw new Error('expected automation group');
    expect(automationGroup.group.sessions.map((item) => item.id)).toEqual([
      newerDialogueRun.id,
      olderProjectRun.id,
    ]);
    const dialogueGroup = entries[1];
    expect(dialogueGroup.kind).toBe('dialogue-group');
    if (dialogueGroup.kind !== 'dialogue-group') throw new Error('expected dialogue group');
    expect(dialogueGroup.sessions.map((item) => item.id)).toEqual([manualDialogue.id]);
  });

  it('keeps matching schedule ids isolated by remote device', () => {
    const runs = ['dev-a', 'dev-b'].flatMap((deviceLinkDeviceId, deviceIndex) =>
      [1, 2].map((runIndex) =>
        session({
          updatedAt: `2026-08-${10 + deviceIndex + runIndex}T10:00:00Z`,
          title: `${deviceLinkDeviceId} run ${runIndex}`,
          source: 'scheduler',
          workspaceKind: 'project',
          workingDir: '/repo',
          deviceLinkDeviceId,
        }),
      ),
    );
    const scheduleSessionIndex = new Map(
      runs.map((run) => [
        run.id,
        {
          scheduleId: 'shared-schedule-id',
          scheduleName: '远程自动检查',
          unreadRunIds: [],
          unreadFailedRunIds: [],
          hasUnreadRun: false,
          hasUnreadFailedRun: false,
        },
      ]),
    );

    const entries = buildMainListEntries({
      projects: [project('/repo-a', runs.slice(0, 2)), project('/repo-b', runs.slice(2))],
      dialogues: [],
      groupBy: 'flat',
      groupDialogue: false,
      sortBy: 'recency',
      manualProjectOrder: [],
      scheduleSessionIndex,
    });
    const groups = entries.filter(
      (entry): entry is Extract<MainListEntry, { kind: 'automation-group' }> =>
        entry.kind === 'automation-group',
    );
    expect(groups).toHaveLength(2);
    expect(new Set(groups.map((entry) => entry.group.id)).size).toBe(2);
    expect(groups.map((entry) => entry.group.legacyId)).toEqual([
      'schedule:shared-schedule-id',
      'schedule:shared-schedule-id',
    ]);
    expect(
      groups.map((entry) => new Set(entry.group.sessions.map((item) => item.deviceLinkDeviceId))),
    ).toEqual([new Set(['dev-b']), new Set(['dev-a'])]);

    const sections = splitEntriesByDevice(entries, ['dev-a', 'dev-b'], { sortBy: 'recency' });
    expect(sections.map((section) => section.deviceId)).toEqual(['dev-a', 'dev-b']);
    expect(
      sections.map((section) =>
        section.entries.flatMap((entry) => getMainListEntrySessions(entry).map((item) => item.id)),
      ),
    ).toEqual([
      runs
        .slice(0, 2)
        .map((run) => run.id)
        .reverse(),
      runs
        .slice(2)
        .map((run) => run.id)
        .reverse(),
    ]);
  });
});

describe('buildMainListEntries — 排序口径', () => {
  // 2026-08-12 用户裁决:「最早优先」(旧 sortBy 'time')删除,时间排序只保留
  // 最近活动在前一档(recency,菜单文案「按时间排序」)。
  it("sorts newest-first under 'recency'", () => {
    const projNew = project('alpha', [session({ updatedAt: '2026-08-12T10:00:00Z' })]);
    const dlgOld = session({ updatedAt: '2026-08-01T10:00:00Z', title: 'old' });
    const entries = buildMainListEntries({
      projects: [projNew],
      dialogues: [dlgOld],
      groupBy: 'project',
      groupDialogue: false,
      sortBy: 'recency',
      manualProjectOrder: [],
    });
    expect(labels(entries)).toEqual(['p:alpha', 's:old']);
  });

  it("floats waiting > unread > running > rest under 'priority', recency within tiers", () => {
    // 四档对齐 Codex(waiting:0 / unread:1 / active:2 / idle:3,2026-08-13 裁决)。
    const waiting = session({ updatedAt: '2026-07-20T00:00:00Z', title: 'needs-input' });
    const unread = session({ updatedAt: '2026-08-01T00:00:00Z', title: 'done-unread' });
    const running = session({ updatedAt: '2026-08-05T00:00:00Z', title: 'running' });
    const idleNewest = session({ updatedAt: '2026-08-12T00:00:00Z', title: 'idle' });
    const ctx = {
      runningSessionIds: new Set([running.id]),
      attentionSessionIds: new Set([waiting.id, unread.id]),
      waitingSessionIds: new Set([waiting.id]),
    };
    expect(sessionPriorityRank(waiting, ctx)).toBe(LIVE_TASK_PRIORITY.waiting);
    expect(sessionPriorityRank(unread, ctx)).toBe(LIVE_TASK_PRIORITY.unread);
    expect(sessionPriorityRank(running, ctx)).toBe(LIVE_TASK_PRIORITY.running);
    expect(sessionPriorityRank(idleNewest, ctx)).toBe(LIVE_TASK_PRIORITY.rest);
    const entries = buildMainListEntries({
      projects: [],
      dialogues: [idleNewest, running, unread, waiting],
      groupBy: 'project',
      groupDialogue: false,
      sortBy: 'priority',
      manualProjectOrder: [],
      priorityContext: ctx,
    });
    expect(labels(entries)).toEqual(['s:needs-input', 's:done-unread', 's:running', 's:idle']);
  });

  it('keeps a just-sent session at the top of running before the agent is actually live', () => {
    // 组装层把 starting 并进 runningSessionIds。新发送比已在跑的更新,应压在运行中档顶;
    // 更新的空闲任务仍在其余档,不能因为它更新就插到运行中前面。
    const justSent = session({ updatedAt: '2026-08-12T12:00:00Z', title: 'just-sent' });
    const runningOlder = session({ updatedAt: '2026-08-12T11:00:00Z', title: 'running-older' });
    const idleNewer = session({ updatedAt: '2026-08-12T13:00:00Z', title: 'idle-newer' });
    const entries = buildMainListEntries({
      projects: [],
      dialogues: [idleNewer, runningOlder, justSent],
      groupBy: 'project',
      groupDialogue: false,
      sortBy: 'priority',
      manualProjectOrder: [],
      priorityContext: {
        runningSessionIds: new Set([justSent.id, runningOlder.id]),
        attentionSessionIds: new Set<string>(),
      },
    });
    expect(labels(entries)).toEqual(['s:just-sent', 's:running-older', 's:idle-newer']);
  });

  it('keeps running tasks in recency order when one also has stale unread attention', () => {
    const newer = session({ updatedAt: '2026-08-12T12:00:00Z', title: 'newer-running' });
    const older = session({ updatedAt: '2026-08-12T11:00:00Z', title: 'older-running' });
    const hold = {
      heldPriorityRanks: new Map<string, number>(),
      recentlyViewedAtMs: new Map<string, number>(),
    };
    const withStaleUnread = {
      runningSessionIds: new Set([newer.id, older.id]),
      attentionSessionIds: new Set([older.id]),
      waitingSessionIds: new Set<string>(),
    };

    expect(sessionPriorityRank(older, withStaleUnread)).toBe(LIVE_TASK_PRIORITY.running);
    holdViewedPriorityRank(hold, older.id, withStaleUnread);
    const runningOnly = {
      ...withStaleUnread,
      attentionSessionIds: new Set<string>(),
    };
    advanceViewedPriorityHold(hold, older.id, runningOnly, 1_000);
    expect(hold.heldPriorityRanks.get(older.id)).toBe(LIVE_TASK_PRIORITY.running);

    const beforeSwitch = buildMainListEntries({
      projects: [],
      dialogues: [older, newer],
      groupBy: 'project',
      groupDialogue: false,
      sortBy: 'priority',
      manualProjectOrder: [],
      priorityContext: { ...runningOnly, heldPriorityRanks: hold.heldPriorityRanks },
    });
    expect(labels(beforeSwitch)).toEqual(['s:newer-running', 's:older-running']);

    advanceViewedPriorityHold(hold, newer.id, runningOnly, 2_000);
    const afterSwitch = buildMainListEntries({
      projects: [],
      dialogues: [older, newer],
      groupBy: 'project',
      groupDialogue: false,
      sortBy: 'priority',
      manualProjectOrder: [],
      priorityContext: { ...runningOnly, heldPriorityRanks: hold.heldPriorityRanks },
    });
    expect(labels(afterSwitch)).toEqual(labels(beforeSwitch));
    expect(hold.recentlyViewedAtMs.has(older.id)).toBe(false);
  });

  it('does not restore an old unread hold after the viewed run finishes', () => {
    const viewed = session({ updatedAt: '2026-08-12T12:00:00Z', title: 'viewed' });
    const hold = {
      heldPriorityRanks: new Map<string, number>(),
      recentlyViewedAtMs: new Map<string, number>(),
    };
    holdViewedPriorityRank(hold, viewed.id, {
      runningSessionIds: new Set<string>(),
      attentionSessionIds: new Set([viewed.id]),
      waitingSessionIds: new Set<string>(),
    });
    const running = {
      runningSessionIds: new Set([viewed.id]),
      attentionSessionIds: new Set<string>(),
      waitingSessionIds: new Set<string>(),
    };
    advanceViewedPriorityHold(hold, viewed.id, running, 1_000);
    expect(hold.heldPriorityRanks.get(viewed.id)).toBe(LIVE_TASK_PRIORITY.running);

    const finished = {
      ...running,
      runningSessionIds: new Set<string>(),
      heldPriorityRanks: hold.heldPriorityRanks,
    };
    expect(sessionPriorityRank(viewed, finished)).toBe(LIVE_TASK_PRIORITY.running);

    advanceViewedPriorityHold(hold, undefined, finished, 2_000);
    expect(hold.recentlyViewedAtMs.has(viewed.id)).toBe(false);
    expect(sessionPriorityRank(viewed, finished)).toBe(LIVE_TASK_PRIORITY.rest);
  });

  it('still promotes a viewed running task to waiting and releases it when running resumes', () => {
    const viewed = session({ updatedAt: '2026-08-12T12:00:00Z', title: 'viewed' });
    const hold = {
      heldPriorityRanks: new Map<string, number>(),
      recentlyViewedAtMs: new Map<string, number>(),
    };
    const running = {
      runningSessionIds: new Set([viewed.id]),
      attentionSessionIds: new Set<string>(),
      waitingSessionIds: new Set<string>(),
    };
    holdViewedPriorityRank(hold, viewed.id, running);

    const waiting = {
      ...running,
      attentionSessionIds: new Set([viewed.id]),
      waitingSessionIds: new Set([viewed.id]),
    };
    advanceViewedPriorityHold(hold, viewed.id, waiting, 1_000);
    expect(hold.heldPriorityRanks.get(viewed.id)).toBe(LIVE_TASK_PRIORITY.waiting);
    expect(
      sessionPriorityRank(viewed, { ...waiting, heldPriorityRanks: hold.heldPriorityRanks }),
    ).toBe(LIVE_TASK_PRIORITY.waiting);

    advanceViewedPriorityHold(hold, viewed.id, running, 2_000);
    expect(hold.heldPriorityRanks.get(viewed.id)).toBe(LIVE_TASK_PRIORITY.running);
  });

  it('keeps the open unread task in place, then parks it at the top of the rest tier after leave', () => {
    const unread = session({ updatedAt: '2026-07-01T00:00:00Z', title: 'just-read' });
    const olderRest = session({ updatedAt: '2026-08-12T00:00:00Z', title: 'older-rest' });
    const waiting = session({ updatedAt: '2026-07-20T00:00:00Z', title: 'needs-input' });
    const hold = {
      heldPriorityRanks: new Map<string, number>(),
      recentlyViewedAtMs: new Map<string, number>(),
    };
    const opened = advanceViewedPriorityHold(
      hold,
      unread.id,
      {
        runningSessionIds: new Set<string>(),
        attentionSessionIds: new Set([unread.id, waiting.id]),
        waitingSessionIds: new Set([waiting.id]),
      },
      1_000,
    );
    expect(opened.heldPriorityRanks.get(unread.id)).toBe(1);
    const stillOpen = buildMainListEntries({
      projects: [],
      dialogues: [olderRest, unread, waiting],
      groupBy: 'project',
      groupDialogue: false,
      sortBy: 'priority',
      manualProjectOrder: [],
      priorityContext: {
        runningSessionIds: new Set<string>(),
        attentionSessionIds: new Set([waiting.id]),
        waitingSessionIds: new Set([waiting.id]),
        heldPriorityRanks: opened.heldPriorityRanks,
        recentlyViewedAtMs: opened.recentlyViewedAtMs,
      },
    });
    expect(labels(stillOpen)).toEqual(['s:needs-input', 's:just-read', 's:older-rest']);

    const leaveAt = Date.parse('2026-08-13T00:00:00Z');
    const left = advanceViewedPriorityHold(
      opened,
      olderRest.id,
      {
        runningSessionIds: new Set<string>(),
        attentionSessionIds: new Set([waiting.id]),
        waitingSessionIds: new Set([waiting.id]),
      },
      leaveAt,
    );
    expect(left.heldPriorityRanks.has(unread.id)).toBe(false);
    expect(left.recentlyViewedAtMs.get(unread.id)).toBe(leaveAt);
    const afterLeave = buildMainListEntries({
      projects: [],
      dialogues: [olderRest, unread, waiting],
      groupBy: 'project',
      groupDialogue: false,
      sortBy: 'priority',
      manualProjectOrder: [],
      priorityContext: {
        runningSessionIds: new Set<string>(),
        attentionSessionIds: new Set([waiting.id]),
        waitingSessionIds: new Set([waiting.id]),
        heldPriorityRanks: left.heldPriorityRanks,
        recentlyViewedAtMs: left.recentlyViewedAtMs,
      },
    });
    expect(labels(afterLeave)).toEqual(['s:needs-input', 's:just-read', 's:older-rest']);
    expect(
      sessionPriorityRank(unread, {
        runningSessionIds: new Set<string>(),
        attentionSessionIds: new Set([waiting.id]),
        waitingSessionIds: new Set([waiting.id]),
        recentlyViewedAtMs: left.recentlyViewedAtMs,
      }),
    ).toBe(LIVE_TASK_PRIORITY.rest);

    const sunkWithoutViewedAt = buildMainListEntries({
      projects: [],
      dialogues: [olderRest, unread, waiting],
      groupBy: 'project',
      groupDialogue: false,
      sortBy: 'priority',
      manualProjectOrder: [],
      priorityContext: {
        runningSessionIds: new Set<string>(),
        attentionSessionIds: new Set([waiting.id]),
        waitingSessionIds: new Set([waiting.id]),
      },
    });
    expect(labels(sunkWithoutViewedAt)).toEqual(['s:needs-input', 's:older-rest', 's:just-read']);

    const recencyIgnoresLeave = buildMainListEntries({
      projects: [],
      dialogues: [olderRest, unread],
      groupBy: 'project',
      groupDialogue: false,
      sortBy: 'recency',
      manualProjectOrder: [],
      priorityContext: {
        runningSessionIds: new Set<string>(),
        attentionSessionIds: new Set<string>(),
        recentlyViewedAtMs: left.recentlyViewedAtMs,
      },
    });
    expect(labels(recencyIgnoresLeave)).toEqual(['s:older-rest', 's:just-read']);
  });

  it('does not reorder already-read rest tasks when browsing among them', () => {
    const olderRest = session({ updatedAt: '2026-07-01T00:00:00Z', title: 'older-rest' });
    const newerRest = session({ updatedAt: '2026-08-12T00:00:00Z', title: 'newer-rest' });
    const hold = {
      heldPriorityRanks: new Map<string, number>(),
      recentlyViewedAtMs: new Map<string, number>(),
    };
    const restCtx = {
      runningSessionIds: new Set<string>(),
      attentionSessionIds: new Set<string>(),
      waitingSessionIds: new Set<string>(),
    };
    const openedOlder = advanceViewedPriorityHold(hold, olderRest.id, restCtx, 1_000);
    expect(openedOlder.heldPriorityRanks.get(olderRest.id)).toBe(LIVE_TASK_PRIORITY.rest);
    const leftOlder = advanceViewedPriorityHold(
      openedOlder,
      newerRest.id,
      restCtx,
      Date.parse('2026-08-13T00:00:00Z'),
    );
    expect(leftOlder.recentlyViewedAtMs.has(olderRest.id)).toBe(false);
    const afterBrowse = buildMainListEntries({
      projects: [],
      dialogues: [olderRest, newerRest],
      groupBy: 'project',
      groupDialogue: false,
      sortBy: 'priority',
      manualProjectOrder: [],
      priorityContext: {
        ...restCtx,
        heldPriorityRanks: leftOlder.heldPriorityRanks,
        recentlyViewedAtMs: leftOlder.recentlyViewedAtMs,
      },
    });
    expect(labels(afterBrowse)).toEqual(['s:newer-rest', 's:older-rest']);
    const recency = buildMainListEntries({
      projects: [],
      dialogues: [olderRest, newerRest],
      groupBy: 'project',
      groupDialogue: false,
      sortBy: 'recency',
      manualProjectOrder: [],
    });
    expect(labels(afterBrowse)).toEqual(labels(recency));
  });

  it('holds the unread rank even if attention is cleared before the first viewed render', () => {
    const unread = session({ updatedAt: '2026-07-01T00:00:00Z', title: 'just-read' });
    const olderRest = session({ updatedAt: '2026-08-12T00:00:00Z', title: 'older-rest' });
    const waiting = session({ updatedAt: '2026-07-20T00:00:00Z', title: 'needs-input' });
    const hold = {
      heldPriorityRanks: new Map<string, number>(),
      recentlyViewedAtMs: new Map<string, number>(),
    };
    holdViewedPriorityRank(hold, unread.id, {
      runningSessionIds: new Set<string>(),
      attentionSessionIds: new Set([unread.id, waiting.id]),
      waitingSessionIds: new Set([waiting.id]),
    });
    const afterClear = advanceViewedPriorityHold(
      hold,
      unread.id,
      {
        runningSessionIds: new Set<string>(),
        attentionSessionIds: new Set([waiting.id]),
        waitingSessionIds: new Set([waiting.id]),
      },
      1_000,
    );
    expect(afterClear.heldPriorityRanks.get(unread.id)).toBe(LIVE_TASK_PRIORITY.unread);
    const entries = buildMainListEntries({
      projects: [],
      dialogues: [olderRest, unread, waiting],
      groupBy: 'project',
      groupDialogue: false,
      sortBy: 'priority',
      manualProjectOrder: [],
      priorityContext: {
        runningSessionIds: new Set<string>(),
        attentionSessionIds: new Set([waiting.id]),
        waitingSessionIds: new Set([waiting.id]),
        heldPriorityRanks: afterClear.heldPriorityRanks,
      },
    });
    expect(labels(entries)).toEqual(['s:needs-input', 's:just-read', 's:older-rest']);
  });

  it('does not apply an earlier visit time while the unread rank is still held', () => {
    const viewed = session({ updatedAt: '2026-07-01T00:00:00Z', title: 'viewed' });
    const hold = {
      heldPriorityRanks: new Map<string, number>(),
      recentlyViewedAtMs: new Map([[viewed.id, Date.parse('2026-08-01T00:00:00Z')]]),
    };
    const unread = { ...NO_PRIORITY, attentionSessionIds: new Set([viewed.id]) };
    holdViewedPriorityRank(hold, viewed.id, unread);
    const beforeRead = sessionPriorityRecencyMs(viewed, { ...unread, ...hold });
    advanceViewedPriorityHold(hold, viewed.id, NO_PRIORITY, 1_000);
    expect(sessionPriorityRecencyMs(viewed, { ...NO_PRIORITY, ...hold })).toBe(beforeRead);
    const leaveAt = Date.parse('2026-09-01T00:00:00Z');
    advanceViewedPriorityHold(hold, undefined, NO_PRIORITY, leaveAt);
    expect(sessionPriorityRecencyMs(viewed, { ...NO_PRIORITY, ...hold })).toBe(leaveAt);
  });

  it('does not let leave time promote a still-waiting or running task', () => {
    const waitingOld = session({ updatedAt: '2026-07-01T00:00:00Z', title: 'waiting-old' });
    const waitingNew = session({ updatedAt: '2026-08-12T00:00:00Z', title: 'waiting-new' });
    const leaveAt = Date.parse('2026-08-13T00:00:00Z');
    const entries = buildMainListEntries({
      projects: [],
      dialogues: [waitingOld, waitingNew],
      groupBy: 'project',
      groupDialogue: false,
      sortBy: 'priority',
      manualProjectOrder: [],
      priorityContext: {
        runningSessionIds: new Set<string>(),
        attentionSessionIds: new Set([waitingOld.id, waitingNew.id]),
        waitingSessionIds: new Set([waitingOld.id, waitingNew.id]),
        recentlyViewedAtMs: new Map([[waitingOld.id, leaveAt]]),
      },
    });
    expect(labels(entries)).toEqual(['s:waiting-new', 's:waiting-old']);
  });

  it('waitingSessionIds 缺省时全部 attention 落 unread 档(老调用方零迁移)', () => {
    const attention = session({ updatedAt: '2026-08-01T00:00:00Z', title: 'attn' });
    const running = session({ updatedAt: '2026-08-05T00:00:00Z', title: 'running' });
    const ctx = {
      runningSessionIds: new Set([running.id]),
      attentionSessionIds: new Set([attention.id]),
    };
    expect(sessionPriorityRank(attention, ctx)).toBe(LIVE_TASK_PRIORITY.unread);
    expect(sessionPriorityRank(running, ctx)).toBe(LIVE_TASK_PRIORITY.running);
  });

  it('a project inherits its best session priority (group floats with its members)', () => {
    const attention = session({ updatedAt: '2026-08-01T00:00:00Z', title: 'attn-in-proj' });
    const proj = project('alpha', [attention]);
    const idleDlg = session({ updatedAt: '2026-08-12T00:00:00Z', title: 'idle-dlg' });
    const entries = buildMainListEntries({
      projects: [proj],
      dialogues: [idleDlg],
      groupBy: 'project',
      groupDialogue: false,
      sortBy: 'priority',
      manualProjectOrder: [],
      priorityContext: {
        runningSessionIds: new Set<string>(),
        attentionSessionIds: new Set([attention.id]),
      },
    });
    expect(labels(entries)).toEqual(['p:alpha', 's:idle-dlg']);
  });

  it('re-sorts project and dialogue groups by recency even when input is active-first', () => {
    // groupSessions 会先把 active 排在 archived 前。状态=全部时若组内沿用该序,
    // 刚归档的任务会沉到陈旧活跃任务下面,项目组和对话组都不符合「按时间」。
    const archivedNew = session({
      updatedAt: '2026-08-13T00:00:00Z',
      title: 'archived-new',
      status: 'archived',
      workingDir: '/alpha',
      workspaceKind: 'project',
    });
    const activeOld = session({
      updatedAt: '2026-08-01T00:00:00Z',
      title: 'active-old',
      status: 'active',
      workingDir: '/alpha',
      workspaceKind: 'project',
    });
    const dlgArchivedNew = session({
      updatedAt: '2026-08-13T00:00:00Z',
      title: 'dlg-archived-new',
      status: 'archived',
    });
    const dlgActiveOld = session({
      updatedAt: '2026-08-01T00:00:00Z',
      title: 'dlg-active-old',
      status: 'active',
    });
    const entries = buildMainListEntries({
      projects: [project('alpha', [activeOld, archivedNew])],
      dialogues: [dlgActiveOld, dlgArchivedNew],
      groupBy: 'project',
      groupDialogue: true,
      sortBy: 'recency',
      manualProjectOrder: [],
    });
    const projectEntry = entries.find((entry) => entry.kind === 'project');
    const dialogueEntry = entries.find((entry) => entry.kind === 'dialogue-group');
    expect(
      projectEntry?.kind === 'project' && projectEntry.project.sessions.map((item) => item.title),
    ).toEqual(['archived-new', 'active-old']);
    expect(
      dialogueEntry?.kind === 'dialogue-group' && dialogueEntry.sessions.map((item) => item.title),
    ).toEqual(['dlg-archived-new', 'dlg-active-old']);
  });

  it('keeps manual project-row order but still recency-sorts inside each group', () => {
    const archivedNew = session({
      updatedAt: '2026-08-13T00:00:00Z',
      title: 'archived-new',
      status: 'archived',
      workingDir: '/alpha',
      workspaceKind: 'project',
    });
    const activeOld = session({
      updatedAt: '2026-08-01T00:00:00Z',
      title: 'active-old',
      status: 'active',
      workingDir: '/alpha',
      workspaceKind: 'project',
    });
    const dlgArchivedNew = session({
      updatedAt: '2026-08-13T00:00:00Z',
      title: 'dlg-archived-new',
      status: 'archived',
    });
    const dlgActiveOld = session({
      updatedAt: '2026-08-01T00:00:00Z',
      title: 'dlg-active-old',
      status: 'active',
    });
    const entries = buildMainListEntries({
      projects: [project('alpha', [activeOld, archivedNew])],
      dialogues: [dlgActiveOld, dlgArchivedNew],
      groupBy: 'project',
      groupDialogue: true,
      sortBy: 'recency',
      projectOrder: 'custom',
      manualProjectOrder: ['local:alpha'],
    });
    const projectEntry = entries.find((entry) => entry.kind === 'project');
    const dialogueEntry = entries.find((entry) => entry.kind === 'dialogue-group');
    expect(
      projectEntry?.kind === 'project' && projectEntry.project.sessions.map((item) => item.title),
    ).toEqual(['archived-new', 'active-old']);
    expect(
      dialogueEntry?.kind === 'dialogue-group' && dialogueEntry.sessions.map((item) => item.title),
    ).toEqual(['dlg-archived-new', 'dlg-active-old']);
  });

  it('keeps manual sort scoped to project rows; dialogues follow by recency (§9.3)', () => {
    const projA = project('alpha', [session({ updatedAt: '2026-08-01T00:00:00Z' })]);
    const projB = project('beta', [session({ updatedAt: '2026-08-12T00:00:00Z' })]);
    const dlg = session({ updatedAt: '2026-08-13T00:00:00Z', title: 'newest-dlg' });
    const entries = buildMainListEntries({
      projects: [projA, projB],
      dialogues: [dlg],
      groupBy: 'project',
      groupDialogue: false,
      sortBy: 'recency',
      projectOrder: 'custom',
      manualProjectOrder: ['local:beta', 'local:alpha'],
      priorityContext: NO_PRIORITY,
    });
    // 项目按自定义顺序;最新的散排对话也排在项目之后(自定义只管项目行)。
    expect(labels(entries)).toEqual(['p:beta', 'p:alpha', 's:newest-dlg']);
  });

  it('stacks custom project order with priority sort for tasks after the project block', () => {
    const waitingDlg = session({ id: 'wait', updatedAt: '2026-08-01T00:00:00Z', title: 'wait' });
    const idleDlg = session({ id: 'idle', updatedAt: '2026-08-13T00:00:00Z', title: 'idle' });
    const projA = project('alpha', [session({ updatedAt: '2026-08-12T00:00:00Z' })]);
    const projB = project('beta', [session({ updatedAt: '2026-08-10T00:00:00Z' })]);
    const entries = buildMainListEntries({
      projects: [projA, projB],
      dialogues: [idleDlg, waitingDlg],
      groupBy: 'project',
      groupDialogue: false,
      sortBy: 'priority',
      projectOrder: 'custom',
      manualProjectOrder: ['local:beta', 'local:alpha'],
      priorityContext: {
        runningSessionIds: new Set<string>(),
        attentionSessionIds: new Set([waitingDlg.id]),
        waitingSessionIds: new Set([waitingDlg.id]),
      },
    });
    expect(labels(entries)).toEqual(['p:beta', 'p:alpha', 's:wait', 's:idle']);
  });
});

describe('splitEntriesByDevice — 拆段后按本段重排', () => {
  it('retains online empty devices in device order and removes them after going offline', () => {
    const devices = new Map([
      ['online', { online: true }],
      ['offline', { online: false }],
    ]);
    const split = () =>
      splitEntriesByDevice([], [...devices.keys()], {
        onlineDeviceIds: onlineDeviceSectionIds(devices, 'all'),
      });
    expect(split()).toEqual([
      { deviceId: null, entries: [] },
      { deviceId: 'online', entries: [] },
    ]);
    devices.set('online', { online: false });
    expect(split()).toEqual([{ deviceId: null, entries: [] }]);
    expect(splitEntriesByDevice([], [...devices.keys()])).toEqual([]);
  });

  it('only retains empty online devices inside the selected machine scope', () => {
    const devices = new Map([
      ['a', { online: true }],
      ['b', { online: true }],
      ['offline', { online: false }],
    ]);
    expect(onlineDeviceSectionIds(devices, ['b', 'offline'])).toEqual(['b']);
    expect(onlineDeviceSectionIds(devices, ['local', 'a'])).toEqual([null, 'a']);
  });

  it('does not count empty sections of remote devices still loading or failed', () => {
    const devices = new Map([
      ['a', { online: true }],
      ['b', { online: true }],
    ]);
    // 单机范围选中一台正在首次读取 / 读取失败的远程设备:空段头不能遮掉整屏提示。
    expect(hasSettledOnlineDeviceSection(devices, ['a'], new Set(['a']))).toBe(false);
    expect(hasSettledOnlineDeviceSection(devices, ['a', 'b'], new Set(['a', 'b']))).toBe(false);
    // 读取就绪后,在线空段照常算作已有内容。
    expect(hasSettledOnlineDeviceSection(devices, ['a'], new Set())).toBe(true);
    expect(hasSettledOnlineDeviceSection(devices, ['a', 'b'], new Set(['a']))).toBe(true);
    // 本机段不受远程读取状态影响。
    expect(hasSettledOnlineDeviceSection(devices, ['local'], new Set(['a']))).toBe(true);
    expect(hasSettledOnlineDeviceSection(devices, 'all', new Set(['a', 'b']))).toBe(true);
  });

  it('keeps offline tasks and avoids duplicating populated online device sections', () => {
    const entries: MainListEntry[] = ['online', 'offline'].map((deviceLinkDeviceId) => ({
      kind: 'session',
      session: session({ deviceLinkDeviceId, updatedAt: '2026-09-19T00:00:00Z' }),
    }));
    const sections = splitEntriesByDevice(entries, ['online', 'offline'], {
      onlineDeviceIds: [null, 'online'],
    });
    expect(sections.map(({ deviceId, entries }) => [deviceId, entries.length])).toEqual([
      [null, 0],
      ['online', 1],
      ['offline', 1],
    ]);
  });

  it('re-sorts each device section by its own activity after splitting a dialogue group', () => {
    const localOld = session({
      updatedAt: '2026-08-01T00:00:00Z',
      title: 'local-old',
    });
    const remoteNew = session({
      updatedAt: '2026-08-13T00:00:00Z',
      title: 'remote-new',
      deviceLinkDeviceId: 'dev-a',
    });
    const localProject = project('local-proj', [
      session({
        updatedAt: '2026-08-10T00:00:00Z',
        title: 'local-proj',
        workingDir: '/local',
        workspaceKind: 'project',
      }),
    ]);
    const entries = buildMainListEntries({
      projects: [localProject],
      dialogues: [localOld, remoteNew],
      groupBy: 'project',
      groupDialogue: true,
      sortBy: 'recency',
      manualProjectOrder: [],
    });
    const sections = splitEntriesByDevice(entries, ['dev-a'], { sortBy: 'recency' });
    const local = sections.find((section) => section.deviceId === null);
    expect(local && labels(local.entries)).toEqual(['p:local-proj', 'dlg-group']);
  });

  it('places unclassified drafts into the owning device section', () => {
    const remoteDraft = session({
      updatedAt: '2026-08-13T00:00:00Z',
      title: 'remote-draft',
      deviceLinkDeviceId: 'dev-a',
    });
    const entries = buildMainListEntries({
      projects: [],
      dialogues: [],
      unclassified: [remoteDraft],
      groupBy: 'project',
      groupDialogue: true,
      sortBy: 'recency',
      manualProjectOrder: [],
    });
    const sections = splitEntriesByDevice(entries, ['dev-a'], { sortBy: 'recency' });
    expect(sections.map((section) => section.deviceId)).toEqual(['dev-a']);
    expect(labels(sections[0].entries)).toEqual(['s:remote-draft']);
  });
});

describe('creation-time ordering', () => {
  it.each(['flat', 'project'] as const)(
    'keeps %s tasks and groups in place after activity changes',
    (groupBy) => {
      const older = session({
        id: 'older',
        title: 'older',
        createdAt: '2026-08-01T00:00:00Z',
        updatedAt: '2026-08-20T00:00:00Z',
      });
      const newer = session({
        id: 'newer',
        title: 'newer',
        createdAt: '2026-08-10T00:00:00Z',
        updatedAt: '2026-08-10T00:00:00Z',
      });
      const middle = session({
        title: 'middle',
        createdAt: '2026-08-05T00:00:00Z',
        updatedAt: '2026-08-21T00:00:00Z',
      });
      const input = {
        projects: [project('alpha', [older, newer])],
        dialogues: [middle],
        groupBy,
        groupDialogue: false,
        sortBy: 'created' as const,
        manualProjectOrder: [],
      };
      const before = buildMainListEntries(input);
      expect(labels(before)).toEqual(
        groupBy === 'flat' ? ['s:newer', 's:middle', 's:older'] : ['p:alpha', 's:middle'],
      );
      if (before[0].kind === 'project') {
        expect(before[0].project.sessions.map((s) => s.id)).toEqual(['newer', 'older']);
      }
      older.updatedAt = '2026-09-09T00:00:00Z';
      older.userSendAt = '2026-09-09T00:00:00Z';
      expect(labels(buildMainListEntries(input))).toEqual(labels(before));
      expect(labels(buildMainListEntries({ ...input, sortBy: 'recency' }))).toEqual(
        groupBy === 'flat' ? ['s:older', 's:middle', 's:newer'] : ['p:alpha', 's:middle'],
      );
    },
  );

  it('orders dialogue groups and cached remote device sections by creation time', () => {
    const old = session({
      title: 'old',
      createdAt: '2026-08-01T00:00:00Z',
      updatedAt: '2026-09-09T00:00:00Z',
      deviceLinkDeviceId: 'old-device',
    });
    const recent = session({
      title: 'recent',
      createdAt: '2026-08-10T00:00:00Z',
      updatedAt: '2026-08-10T00:00:00Z',
      deviceLinkDeviceId: 'new-device',
    });
    const entries = buildMainListEntries({
      projects: [],
      dialogues: [old, recent],
      groupBy: 'flat',
      groupDialogue: true,
      sortBy: 'created',
      manualProjectOrder: [],
    });
    expect(getMainListEntrySessions(entries[0]).map((s) => s.title)).toEqual(['recent', 'old']);
    const sections = splitEntriesByDevice(entries, [], { sortBy: 'created' });
    expect(sections.map((section) => section.deviceId)).toEqual(['new-device', 'old-device']);
  });
});

it('keeps manual project order while creation-time tasks stay stable inside each project', () => {
  const old = session({
    id: 'old',
    title: 'old',
    createdAt: '2026-08-01T00:00:00Z',
    updatedAt: '2026-08-20T00:00:00Z',
  });
  const recent = session({
    id: 'recent',
    title: 'recent',
    createdAt: '2026-08-10T00:00:00Z',
    updatedAt: '2026-08-10T00:00:00Z',
  });
  const alpha = project('alpha', [old, recent]);
  const beta = project('beta', [session({ updatedAt: '2026-07-01T00:00:00Z' })]);
  const input = {
    projects: [alpha, beta],
    dialogues: [],
    groupBy: 'project' as const,
    groupDialogue: false,
    sortBy: 'created' as const,
    projectOrder: 'custom' as const,
    manualProjectOrder: ['local:beta', 'local:alpha'],
  };
  const before = buildMainListEntries(input);
  expect(labels(before)).toEqual(['p:beta', 'p:alpha']);
  expect(getMainListEntrySessions(before[1]).map((s) => s.id)).toEqual(['recent', 'old']);
  old.userSendAt = '2026-09-09T00:00:00Z';
  old.updatedAt = '2026-09-09T00:00:00Z';
  const after = buildMainListEntries(input);
  expect(labels(after)).toEqual(['p:beta', 'p:alpha']);
  expect(getMainListEntrySessions(after[1]).map((s) => s.id)).toEqual(['recent', 'old']);
  const activityOrder = buildMainListEntries({ ...input, sortBy: 'recency' });
  expect(labels(activityOrder)).toEqual(['p:beta', 'p:alpha']);
  expect(getMainListEntrySessions(activityOrder[1]).map((s) => s.id)).toEqual(['old', 'recent']);
});

describe('splitEntriesByDevice — Bot ownership', () => {
  it('partitions local, known and cached devices without mutating Bot identity or task order', () => {
    const sessions = [
      session({ id: 'b-new', deviceLinkDeviceId: 'b', updatedAt: '2026-09-09T12:00:00Z' }),
      session({ id: 'local', updatedAt: '2026-09-09T10:00:00Z' }),
      session({ id: 'cached', deviceLinkDeviceId: 'cached', updatedAt: '2026-09-09T09:00:00Z' }),
      session({ id: 'a', deviceLinkDeviceId: 'a', updatedAt: '2026-09-09T08:00:00Z' }),
      session({ id: 'b-old', deviceLinkDeviceId: 'b', updatedAt: '2026-09-09T07:00:00Z' }),
    ];
    const bot = { botId: 'demo', displayName: 'Demo', avatar: 'avatar', avatarColor: 'color', sessions, latestActivityAt: sessions[0].updatedAt };
    const result = splitEntriesByDevice([{ kind: 'bot-group', bot }], ['a', 'b'], { sortBy: 'recency' });
    expect(result.map((section) => section.deviceId)).toEqual([null, 'a', 'b', 'cached']);
    expect(result.map((section) => section.entries.flatMap((entry) => getMainListEntrySessions(entry).map((s) => s.id))))
      .toEqual([['local'], ['a'], ['b-new', 'b-old'], ['cached']]);
    for (const section of result) {
      expect(section.entries).toHaveLength(1);
      expect(section.entries[0]).toMatchObject({ kind: 'bot-group', bot: { botId: 'demo', displayName: 'Demo', avatar: 'avatar', avatarColor: 'color' } });
    }
    expect(bot.sessions).toBe(sessions);
    expect(bot.sessions.map((s) => s.id)).toEqual(['b-new', 'local', 'cached', 'a', 'b-old']);
  });

  it('ranks each device fragment from its own sessions alongside dialogue groups', () => {
    const local = session({ id: 'local', updatedAt: '2026-09-09T01:00:00Z' });
    const remote = session({ id: 'remote', deviceLinkDeviceId: 'remote', updatedAt: '2026-09-09T12:00:00Z' });
    const entries: MainListEntry[] = [
      { kind: 'bot-group', bot: { botId: 'demo', displayName: 'Demo', avatar: '', avatarColor: '', sessions: [remote, local], latestActivityAt: remote.updatedAt } },
      { kind: 'dialogue-group', sessions: [
        session({ id: 'dlg-local', updatedAt: '2026-09-09T06:00:00Z' }),
        session({ id: 'dlg-remote', deviceLinkDeviceId: 'remote', updatedAt: '2026-09-09T06:00:00Z' }),
      ] },
    ];
    const result = splitEntriesByDevice(entries, ['remote'], { sortBy: 'recency' });
    expect(result.map((section) => labels(section.entries))).toEqual([
      ['dlg-group', 'bot:Demo'], ['bot:Demo', 'dlg-group'],
    ]);
  });
});

describe('per-sort numeric keys', () => {
  it('reads each activity key a bounded number of times and observes later in-place updates', () => {
    let reads = 0;
    const rows = Array.from({ length: 100 }, (_, i) => {
      const row = session({ id: String(i), updatedAt: new Date(i * 1000).toISOString() });
      let value = row.updatedAt;
      Object.defineProperty(row, 'updatedAt', {
        get: () => {
          reads++;
          return value;
        },
        set: (v) => {
          value = v;
        },
      });
      return row;
    });
    expect(sortSessionsForMainList(rows, 'recency')[0].id).toBe('99');
    expect(reads).toBe(100);
    rows[0].updatedAt = '2030-01-01T00:00:00Z';
    expect(sortSessionsForMainList(rows, 'recency')[0].id).toBe('0');
  });
  it('preserves stable ties, created-id ties and invalid userSendAt semantics', () => {
    const a = session({ id: 'z', updatedAt: '2026-01-01T00:00:00Z' });
    const b = session({ id: 'a', updatedAt: a.updatedAt });
    const invalid = session({
      id: 'invalid',
      updatedAt: '2030-01-01T00:00:00Z',
      userSendAt: 'invalid',
    });
    expect(sortSessionsForMainList([a, b, invalid], 'recency').map((x) => x.id)).toEqual([
      'z',
      'a',
      'invalid',
    ]);
    expect(sortSessionsForMainList([a, b], 'created').map((x) => x.id)).toEqual(['a', 'z']);
    expect(sortSessionsForMainList([a, b], 'priority').map((x) => x.id)).toEqual(['z', 'a']);
  });
});