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。

58.1 KB
Raw Blame
/**
 * Automation-generated sessions are ordinary sidebar sessions.
 *
 * The schedule itself stays in Automations. Sessions produced by a schedule run
 * must flow through the same project/dialogue grouping, redirect, tabs, and
 * notification paths as user-created sessions, with only a small source marker.
 */

import { afterEach, describe, expect, it } from 'vitest';
import { readFileSync } from 'node:fs';

import {
  DESKTOP_VISIBLE_SESSION_SOURCES,
  normalizeSessionSource,
} from '../../shared/sessionSource';
import {
  addSessionAttention,
  clearSessionAttentionMany,
  getSessionAttentionSnapshot,
  hasSessionAttention,
} from '@/lib/sessionAttentionStore';
import type { Session } from '@/lib/ccAgent.types';
import {
  getAutomationGroupChildView,
  getAutomationGroupLatestSession,
  getAutomationGroupPrimarySession,
  getVisibleAutomationGroupSessions,
  groupAutomationSidebarEntries,
  unreadSuccessScheduleRunIds,
  type AutomationScheduleSessionInfo,
} from '@/features/cc-agent/lib/automationSidebarGrouping';
import { groupSessions } from '@/features/cc-agent/lib/projectGrouping';
import {
  getAutomationSessionDisplayTitle,
  groupScheduledSessions,
  isAutomationGeneratedSession,
  isScheduledSession,
} from '@/features/cc-agent/lib/scheduledSessionGrouping';
import { getFocusedScheduleStatusFilter } from '@/features/scheduler/SchedulerPage';
import { isUnreadFailedScheduleRun, isUnreadScheduleRun } from '@/features/scheduler/lib/runUnread';
// Windows checkout(core.autocrlf)下源码是 CRLF;统一归一成 LF,含 \n 的多行片段断言才跨平台成立。
const readTextLf = (...args: Parameters<typeof readFileSync>): string =>
  String(readFileSync(...args)).replace(/\r\n/g, '\n');

const BASE_TIME = '2026-01-01T00:00:00.000Z';
const ATTENTION_TEST_IDS = ['auto-run', 'manual-run'];

const makeSession = (partial: Partial<Session> = {}): Session => {
  // 侧栏排序时间轴现在以 userSendAt 为主键(userSendAt ?? updatedAt;scheduler fire
  // 也会把 userSendAt bump 成本次 firedAt)。测试助手把 updatedAt / userSendAt 都对齐
  // 到同一个"活动时刻":调用者只给其一时,另一个同步取该值,让排序断言按预期时间轴运作。
  const updatedAt = partial.updatedAt ?? partial.userSendAt ?? BASE_TIME;
  return {
    id: 's1',
    userId: 'u1',
    title: 'Session',
    workingDir: '/repo',
    workspaceKind: 'project',
    model: 'claude-sonnet-4-6',
    effort: 'medium',
    permissionMode: 'ask',
    sdkSessionId: null,
    totalTokenUsage: 0,
    totalCostUsd: 0,
    contextTokens: 0,
    contextWindow: 0,
    fastMode: false,
    clearedAt: null,
    pinnedAt: null,
    // 默认对齐到派生的活动时刻(updatedAt),而非固定 BASE_TIME —— 否则只传 updatedAt
    // 的用例在 userSendAt 主键排序下会因 userSendAt 恒等而并列。显式 partial.userSendAt 优先。
    userSendAt: updatedAt,
    status: 'active',
    agentKind: 'cc',
    source: 'desktop',
    extraDirs: [],
    createdAt: BASE_TIME,
    updatedAt,
    ...partial,
    // 保证 updatedAt 最终为上面的推导结果(即使 partial 里显式带了 userSendAt
    // 但没带 updatedAt),不被 spread 覆盖回默认 BASE_TIME。
    ...(partial.updatedAt === undefined ? { updatedAt } : {}),
  };
};

const makeScheduleSessionInfo = (
  partial: Partial<AutomationScheduleSessionInfo> = {},
): AutomationScheduleSessionInfo => ({
  scheduleId: 'sched-1',
  scheduleName: 'Schedule',
  unreadRunIds: [],
  unreadFailedRunIds: [],
  hasUnreadRun: false,
  hasUnreadFailedRun: false,
  ...partial,
});

describe('automation-generated sessions', () => {
  afterEach(() => {
    clearSessionAttentionMany(ATTENTION_TEST_IDS);
  });

  it('keeps scheduler sessions in the desktop-visible source contract', () => {
    // 所有会生成本地会话的 IM 渠道均进入 desktop sidebar。
    // (feishu 2026-07-16 起以「对话」分组回归, 见 sessionSource.ts 注释)。
    // Bot-owned Session 仍是真实 Cindy 任务,但只由 Bots 面板投影;普通任务列表
    // 不再重复展示同一批主对话、渠道与 worker。
    expect(DESKTOP_VISIBLE_SESSION_SOURCES).toEqual([
      'side-chat',
      'desktop',
      'feishu',
      'slack',
      'telegram',
      'x',
      'discord',
      'wechat',
      'dingtalk',
      'wecom',
      'scheduler',
      'learn',
      'review',
      'shared',
      'plugin',
    ]);
    expect(DESKTOP_VISIBLE_SESSION_SOURCES).toContain('feishu');
    expect(DESKTOP_VISIBLE_SESSION_SOURCES).toContain('telegram');
    expect(DESKTOP_VISIBLE_SESSION_SOURCES).toContain('discord');
    expect(DESKTOP_VISIBLE_SESSION_SOURCES).toContain('dingtalk');
    expect(DESKTOP_VISIBLE_SESSION_SOURCES).toContain('review');
    expect(DESKTOP_VISIBLE_SESSION_SOURCES).toContain('plugin');
    expect(DESKTOP_VISIBLE_SESSION_SOURCES).not.toContain('bot');
    expect(DESKTOP_VISIBLE_SESSION_SOURCES).not.toContain('cindy-make');
    expect(DESKTOP_VISIBLE_SESSION_SOURCES).not.toContain('cindy-make-merge');

    expect(normalizeSessionSource('desktop')).toBe('desktop');
    expect(normalizeSessionSource('scheduler')).toBe('scheduler');
    expect(normalizeSessionSource('learn')).toBe('learn');
    expect(normalizeSessionSource('feishu')).toBe('feishu');
    expect(normalizeSessionSource('telegram')).toBe('telegram');
    expect(normalizeSessionSource('discord')).toBe('discord');
    expect(normalizeSessionSource('dingtalk')).toBe('dingtalk');
    expect(normalizeSessionSource('wecom')).toBe('wecom');
    expect(normalizeSessionSource('plugin')).toBe('plugin');
    expect(normalizeSessionSource(null)).toBe('desktop');
    expect(normalizeSessionSource('unknown')).toBe('desktop');
  });

  it('uses source=scheduler as the stable marker and keeps legacy title-prefix compatibility', () => {
    const schedulerSession = makeSession({ title: 'Daily summary', source: 'scheduler' });
    const legacySession = makeSession({ title: '[Schedule] Daily summary' });
    const normalSession = makeSession({ title: 'Daily summary', source: 'desktop' });

    expect(isScheduledSession(schedulerSession)).toBe(false);
    expect(isAutomationGeneratedSession(schedulerSession)).toBe(true);
    expect(getAutomationSessionDisplayTitle(schedulerSession)).toBe('Daily summary');

    expect(isScheduledSession(legacySession)).toBe(true);
    expect(isAutomationGeneratedSession(legacySession)).toBe(true);
    expect(getAutomationSessionDisplayTitle(legacySession)).toBe('Daily summary');

    expect(isScheduledSession(normalSession)).toBe(false);
    expect(isAutomationGeneratedSession(normalSession)).toBe(false);
    expect(getAutomationSessionDisplayTitle(normalSession)).toBe('Daily summary');
  });

  it('groups only automation-generated sessions by target directory and display title', () => {
    const groups = groupScheduledSessions(
      [
        makeSession({
          id: 'manual',
          title: 'Manual session',
          source: 'desktop',
          userSendAt: '2026-01-06T00:00:00.000Z',
        }),
        makeSession({
          id: 'daily-old',
          title: 'Daily summary',
          source: 'scheduler',
          workingDir: '/Users/alice/repo',
          userSendAt: '2026-01-02T00:00:00.000Z',
        }),
        makeSession({
          id: 'daily-new',
          title: 'Daily summary',
          source: 'scheduler',
          workingDir: '/Users/alice/repo',
          userSendAt: '2026-01-04T00:00:00.000Z',
          agentKind: 'codex',
        }),
        makeSession({
          id: 'legacy',
          title: '[Schedule] Legacy summary',
          workingDir: '/Users/alice/repo',
          userSendAt: '2026-01-03T00:00:00.000Z',
        }),
        makeSession({
          id: 'dialogue-run',
          title: 'Dialogue check',
          source: 'scheduler',
          workingDir: null,
          workspaceKind: 'dialogue',
          userSendAt: '2026-01-05T00:00:00.000Z',
        }),
      ],
      (key) => (key === 'ccAgent.schedule.unspecifiedDir' ? '未指定目录' : key),
    );

    expect(groups).toHaveLength(2);
    expect(groups[0]).toMatchObject({
      workingDir: null,
      displayName: '未指定目录',
    });
    expect(groups[0].schedules.map((schedule) => schedule.scheduleName)).toEqual([
      'Dialogue check',
    ]);
    expect(groups[0].schedules[0].sessions.map((session) => session.id)).toEqual(['dialogue-run']);

    expect(groups[1]).toMatchObject({
      workingDir: '/Users/alice/repo',
      displayName: 'repo',
    });
    expect(groups[1].schedules.map((schedule) => schedule.scheduleName)).toEqual([
      'Daily summary',
      'Legacy summary',
    ]);
    expect(groups[1].schedules[0]).toMatchObject({
      agentKind: 'codex',
      latestActivityMs: Date.parse('2026-01-04T00:00:00.000Z'),
    });
    expect(groups[1].schedules[0].sessions.map((session) => session.id)).toEqual([
      'daily-new',
      'daily-old',
    ]);

    const groupedSessionIds = groups.flatMap((dirGroup) =>
      dirGroup.schedules.flatMap((schedule) => schedule.sessions.map((session) => session.id)),
    );
    expect(groupedSessionIds).not.toContain('manual');
  });

  it('keeps automation-generated sessions in ordinary sidebar project and dialogue groups', () => {
    const grouped = groupSessions([
      makeSession({
        id: 'project-manual',
        title: 'Manual project session',
        source: 'desktop',
        workingDir: '/Users/alice/repo',
        userSendAt: '2026-01-02T00:00:00.000Z',
      }),
      makeSession({
        id: 'project-auto',
        title: 'Automated project session',
        source: 'scheduler',
        workingDir: '/Users/alice/repo',
        userSendAt: '2026-01-03T00:00:00.000Z',
      }),
      makeSession({
        id: 'dialogue-auto',
        title: 'Automated dialogue session',
        source: 'scheduler',
        workspaceKind: 'dialogue',
        workingDir: null,
        userSendAt: '2026-01-04T00:00:00.000Z',
      }),
    ]);

    expect(grouped.dialogues.map((session) => session.id)).toEqual(['dialogue-auto']);
    expect(grouped.projects).toHaveLength(1);
    expect(grouped.projects[0]).toMatchObject({
      workingDir: '/Users/alice/repo',
      displayName: 'repo',
    });
    expect(grouped.projects[0].sessions.map((session) => session.id)).toEqual([
      'project-auto',
      'project-manual',
    ]);
  });

  it('keeps newly bound scheduler project sessions under their project before message count refreshes', () => {
    const grouped = groupSessions([
      makeSession({
        id: 'manual-draft',
        title: 'Manual draft',
        source: 'desktop',
        workingDir: '/Users/alice/repo',
        userSendAt: null,
        _count: { messages: 0 },
      }),
      makeSession({
        id: 'scheduler-bound',
        title: '[Schedule] GitHub 巡检',
        source: 'scheduler',
        workingDir: '/Users/alice/repo',
        userSendAt: null,
        _count: { messages: 0 },
      }),
    ]);

    expect(grouped.unclassified.map((session) => session.id)).toEqual(['manual-draft']);
    expect(grouped.projects).toHaveLength(1);
    expect(grouped.projects[0]).toMatchObject({
      workingDir: '/Users/alice/repo',
      displayName: 'repo',
    });
    expect(grouped.projects[0].sessions.map((session) => session.id)).toEqual(['scheduler-bound']);
  });

  it('keeps a single automation-generated session as an ordinary sidebar row', () => {
    const session = makeSession({
      id: 'single-auto-run',
      title: 'Daily summary',
      source: 'scheduler',
    });

    const entries = groupAutomationSidebarEntries([session], {
      notifications: new Set(),
    });

    expect(entries).toEqual([{ kind: 'session', session }]);
  });

  it('groups multiple sessions from the same automation task into one sidebar entry', () => {
    const scheduleIndex = new Map<string, AutomationScheduleSessionInfo>([
      [
        'jira-1',
        makeScheduleSessionInfo({
          scheduleId: 'sched-jira',
          scheduleName: 'Jira 自动修复',
          scheduleStatus: 'active',
          scheduleSource: 'user',
          nextFireAt: 1_767_225_600_000,
          unreadRunIds: ['run-jira-1'],
          hasUnreadRun: true,
        }),
      ],
      [
        'jira-2',
        makeScheduleSessionInfo({
          scheduleId: 'sched-jira',
          scheduleName: 'Jira 自动修复',
          scheduleStatus: 'active',
          scheduleSource: 'user',
          nextFireAt: 1_767_225_600_000,
          unreadRunIds: [],
          hasUnreadRun: false,
        }),
      ],
      [
        'daily-1',
        makeScheduleSessionInfo({
          scheduleId: 'sched-daily',
          scheduleName: 'Daily summary',
          scheduleStatus: 'paused',
          scheduleSource: 'project',
          nextFireAt: 1_767_312_000_000,
          workingDir: '/repo',
          projectConfigId: 'daily-config',
          unreadRunIds: [],
          hasUnreadRun: false,
        }),
      ],
    ]);
    const sessions = [
      makeSession({
        id: 'manual',
        title: 'Manual session',
        source: 'desktop',
        userSendAt: '2026-01-05T00:00:00.000Z',
      }),
      makeSession({
        id: 'jira-2',
        title: 'BUG-2',
        source: 'scheduler',
        userSendAt: '2026-01-04T00:00:00.000Z',
      }),
      makeSession({
        id: 'jira-1',
        title: 'BUG-1',
        source: 'scheduler',
        userSendAt: '2026-01-03T00:00:00.000Z',
      }),
      makeSession({
        id: 'daily-1',
        title: 'Daily summary',
        source: 'scheduler',
        userSendAt: '2026-01-02T00:00:00.000Z',
      }),
    ];

    const entries = groupAutomationSidebarEntries(sessions, {
      notifications: new Set(['jira-1']),
      scheduleSessionIndex: scheduleIndex,
    });

    expect(entries).toHaveLength(3);
    expect(entries[0]).toMatchObject({ kind: 'session' });
    expect(entries[1]).toMatchObject({
      kind: 'automation-group',
      group: {
        id: 'schedule:sched-jira',
        scheduleId: 'sched-jira',
        scheduleStatus: 'active',
        scheduleSource: 'user',
        nextFireAt: 1_767_225_600_000,
        title: 'Jira 自动修复',
        attentionSessionIds: ['jira-1'],
      },
    });
    if (entries[1].kind !== 'automation-group') throw new Error('expected automation group');
    expect(entries[1].group.sessions.map((session) => session.id)).toEqual(['jira-2', 'jira-1']);
    expect(entries[2]).toEqual({ kind: 'session', session: sessions[3] });
  });

  it('keeps remote schedule state and the owning device on remote automation groups', () => {
    // 侧栏把远程设备镜像的索引并入分组索引:远程组拿到暂停态 / 下次运行,
    // 并记住所属设备,让分组操作发到那台电脑而不是本机。
    const remoteIndex = new Map([
      ['remote-1', makeScheduleSessionInfo({ scheduleId: 'sched-remote', scheduleName: 'Remote', scheduleStatus: 'paused', nextFireAt: 1_767_225_600_000 })],
      ['remote-2', makeScheduleSessionInfo({ scheduleId: 'sched-remote', scheduleName: 'Remote', scheduleStatus: 'paused', nextFireAt: 1_767_225_600_000 })],
    ]);
    const sessions = ['remote-2', 'remote-1'].map((id) =>
      makeSession({ id, source: 'scheduler', deviceLinkDeviceId: 'device-b' }),
    );

    const [entry] = groupAutomationSidebarEntries(sessions, {
      notifications: new Set(),
      scheduleSessionIndex: remoteIndex,
    });

    expect(entry).toMatchObject({
      kind: 'automation-group',
      group: {
        id: 'schedule:sched-remote:device:device-b',
        scheduleId: 'sched-remote',
        scheduleStatus: 'paused',
        nextFireAt: 1_767_225_600_000,
        deviceLinkDeviceId: 'device-b',
        title: 'Remote',
      },
    });
  });

  it('folds runs of one teammate routine together and leaves older-host routine rows flat', () => {
    // 伙伴例行任务不是 schedule:按「伙伴 + 规则」成组,不同规则 / 伙伴各自成组;
    // 旧主机不投影 routineId 时保持平铺,不误并。
    const routineRun = (id: string, botId: string, routineId?: string) =>
      makeSession({
        id,
        title: 'Morning brief',
        source: 'bot',
        workspaceKind: 'dialogue',
        companion: { id: botId, role: 'routine', ...(routineId ? { routineId } : {}) },
      });
    const sessions = [
      routineRun('brief-2', 'bot-a', 'brief'),
      routineRun('brief-1', 'bot-a', 'brief'),
      routineRun('other-bot', 'bot-b', 'brief'),
      routineRun('legacy-1', 'bot-a'),
      routineRun('legacy-2', 'bot-a'),
    ];

    const entries = groupAutomationSidebarEntries(sessions, { notifications: new Set() });

    expect(entries.map((entry) => entry.kind)).toEqual([
      'automation-group',
      'session',
      'session',
      'session',
    ]);
    expect(entries[0]).toMatchObject({
      kind: 'automation-group',
      group: {
        id: 'routine:bot-a:brief',
        title: 'Morning brief',
        teammateRoutine: { botId: 'bot-a', routineId: 'brief' },
      },
    });
    if (entries[0].kind !== 'automation-group') throw new Error('expected automation group');
    expect(entries[0].group.scheduleId).toBeUndefined();
    expect(entries[0].group.sessions.map((session) => session.id)).toEqual(['brief-2', 'brief-1']);
  });

  it('shows every recent run as its own row when expanded (primary not hidden into header)', () => {
    const group = {
      id: 'schedule:sched-jira',
      title: 'Jira 自动修复',
      sessions: [
        makeSession({ id: 'jira-2', title: 'BUG-2', source: 'scheduler' }),
        makeSession({ id: 'jira-1', title: 'BUG-1', source: 'scheduler' }),
      ],
      attentionSessionIds: ['jira-1'],
    };

    // 展开态:全部运行(含被组头代表的最新未读 jira-1)都各自成行,前 5 条内全显示。
    expect(
      getVisibleAutomationGroupSessions(group, {
        notifications: new Set(['jira-1']),
        showAll: false,
      }).map((session) => session.id),
    ).toEqual(['jira-2', 'jira-1']);
    // 组头点击目标仍是 primary(最新未读),与它的列表行是两个入口指向同一 session。
    expect(getAutomationGroupPrimarySession(group, new Set(['jira-1']))?.id).toBe('jira-1');
  });

  it('picks the most recently active run as the collapsed group latest session', () => {
    const group = {
      id: 'schedule:sched-jira',
      title: 'Jira 自动修复',
      sessions: [
        makeSession({
          id: 'jira-old',
          title: 'BUG-old',
          source: 'scheduler',
          updatedAt: '2026-07-01T00:00:00.000Z',
        }),
        makeSession({
          id: 'jira-new',
          title: 'BUG-new',
          source: 'scheduler',
          updatedAt: '2026-07-08T00:00:00.000Z',
        }),
        makeSession({
          id: 'jira-mid',
          title: 'BUG-mid',
          source: 'scheduler',
          updatedAt: '2026-07-05T00:00:00.000Z',
        }),
      ],
      attentionSessionIds: [],
    };
    // 组头代表「最新一条」运行:严格按活动时间取,不受列表顺序 / running / attention 影响。
    expect(getAutomationGroupLatestSession(group)?.id).toBe('jira-new');
  });

  it('uses a running automation session as the collapsed group primary before attention arrives', () => {
    const group = {
      id: 'schedule:sched-jira',
      title: 'Jira 自动修复',
      sessions: [
        makeSession({ id: 'jira-3', title: 'BUG-3', source: 'scheduler' }),
        makeSession({ id: 'jira-2', title: 'BUG-2', source: 'scheduler' }),
        makeSession({ id: 'jira-1', title: 'BUG-1', source: 'scheduler' }),
      ],
      attentionSessionIds: ['jira-1'],
    };

    expect(
      getAutomationGroupPrimarySession(group, new Set(), {
        runningSessionIds: new Set(['jira-2']),
      })?.id,
    ).toBe('jira-2');
    expect(
      getAutomationGroupPrimarySession(group, new Set(['jira-1']), {
        runningSessionIds: new Set(['jira-2']),
        preferredSessionId: 'jira-3',
      })?.id,
    ).toBe('jira-2');
    // primary = 运行中的 jira-2(组头点击目标);展开态里三条都各自成行。
    expect(
      getVisibleAutomationGroupSessions(group, {
        notifications: new Set(['jira-2', 'jira-1']),
        runningSessionIds: new Set(['jira-2']),
        showAll: false,
      }).map((session) => session.id),
    ).toEqual(['jira-3', 'jira-2', 'jira-1']);
  });

  it('keeps schedule run unread semantics shared with the run history badge', () => {
    expect(isUnreadScheduleRun({ status: 'success', readAt: undefined })).toBe(true);
    expect(isUnreadScheduleRun({ status: 'failed', readAt: undefined })).toBe(true);
    expect(isUnreadScheduleRun({ status: 'interrupted', readAt: undefined })).toBe(true);
    expect(isUnreadScheduleRun({ status: 'aborted', readAt: undefined })).toBe(false);
    expect(isUnreadScheduleRun({ status: 'running', readAt: undefined })).toBe(false);
    expect(isUnreadScheduleRun({ status: 'success', readAt: 1 })).toBe(false);
    expect(isUnreadFailedScheduleRun({ status: 'failed', readAt: undefined })).toBe(true);
    expect(isUnreadFailedScheduleRun({ status: 'interrupted', readAt: undefined })).toBe(true);
    expect(isUnreadFailedScheduleRun({ status: 'aborted', readAt: undefined })).toBe(false);
    expect(isUnreadFailedScheduleRun({ status: 'success', readAt: undefined })).toBe(false);
    expect(
      unreadSuccessScheduleRunIds({
        unreadRunIds: ['ok', 'bad'],
        unreadFailedRunIds: ['bad'],
      }),
    ).toEqual(['ok']);
  });

  it('routes historical failures to the seen receipt while keeping retry scoped to one run', () => {
    const sessionViewSource = readTextLf(
      new URL('../features/cc-agent/CCAgentSessionView.tsx', import.meta.url),
      'utf8',
    );
    const bannerSource = readTextLf(
      new URL('../components/chat/UnreadFailedScheduleBanner.tsx', import.meta.url),
      'utf8',
    );
    const zh = JSON.parse(
      readTextLf(new URL('../i18n/locales/zh-CN/common.json', import.meta.url), 'utf8'),
    );

    expect(sessionViewSource).toContain('<UnreadFailedScheduleBanner');
    expect(sessionViewSource).toContain('scheduleSessionInfo.hasFailedRun');
    expect(sessionViewSource).toContain('shouldShowFailedScheduleNotice({');
    expect(sessionViewSource).toContain('useAutomationScheduleSessionInfo(sessionId)');
    expect(sessionViewSource).not.toContain('useAutomationScheduleSessionIndex()');
    expect(sessionViewSource).toContain('latestUnreadFailedRunId');
    expect(sessionViewSource).toContain('markScheduleRunsReadAndSync([currentUnreadFailedRunId], remoteDeviceId ?? undefined)');
    expect(sessionViewSource).toContain(
      'useReadFailedScheduleRuns(unreadFailedScheduleRunIds, viewVisible && historyLoaded, remoteDeviceId ?? undefined)',
    );
    expect(bannerSource).toContain('scheduleFailureMessageKey(latestFailedRun)');
    expect(zh.chat.unreadFailedScheduleBanner.text).toBe('此前有自动运行失败。');
    expect(sessionViewSource).toContain('latestFailedRun={scheduleSessionInfo.latestFailedRun}');
  });

  it('maps a focused schedule to the status bucket that can reveal it', () => {
    const schedules = [
      { id: 'active-schedule', status: 'active' },
      { id: 'expired-schedule', status: 'expired' },
      { id: 'paused-schedule', status: 'paused' },
    ] as const;

    expect(getFocusedScheduleStatusFilter(schedules, 'active-schedule')).toBe('active');
    expect(getFocusedScheduleStatusFilter(schedules, 'expired-schedule')).toBe('active');
    expect(getFocusedScheduleStatusFilter(schedules, 'paused-schedule')).toBe('paused');
    expect(getFocusedScheduleStatusFilter(schedules, 'missing')).toBeNull();
    expect(getFocusedScheduleStatusFilter(schedules, null)).toBeNull();
  });

  it('shows all recent runs as rows in the default expanded view, consistent with show-all', () => {
    const group = {
      id: 'schedule:sched-jira',
      title: 'Jira 自动修复',
      sessions: [
        makeSession({ id: 'jira-3', title: 'BUG-3', source: 'scheduler' }),
        makeSession({ id: 'jira-2', title: 'BUG-2', source: 'scheduler' }),
        makeSession({ id: 'jira-1', title: 'BUG-1', source: 'scheduler' }),
      ],
      attentionSessionIds: ['jira-1'],
    };

    // 默认展开态:全部运行各自成行(≤5 条全显示),含被组头代表的那条。
    expect(
      getVisibleAutomationGroupSessions(group, {
        notifications: new Set(['jira-1']),
        showAll: false,
      }).map((session) => session.id),
    ).toEqual(['jira-3', 'jira-2', 'jira-1']);

    expect(
      getVisibleAutomationGroupSessions(group, {
        notifications: new Set(['jira-2', 'jira-1']),
        showAll: false,
      }).map((session) => session.id),
    ).toEqual(['jira-3', 'jira-2', 'jira-1']);
    expect(getAutomationGroupPrimarySession(group, new Set(['jira-2', 'jira-1']))?.id).toBe(
      'jira-2',
    );

    // 「显示全部」与默认展开态一致(都是全量)。
    expect(
      getVisibleAutomationGroupSessions(group, {
        notifications: new Set(['jira-1']),
        showAll: true,
      }).map((session) => session.id),
    ).toEqual(['jira-3', 'jira-2', 'jira-1']);
  });

  // review P2(#2938):上层聚合灯为远程活动点亮的子运行,不能藏在「显示全部」后。
  // 远程 running / 未读不在本地 notifications / runningSessionIds 里,父层通过
  // foldExemptSessionIds 并入,自动化组的展开态折叠也要认这份豁免。
  it('keeps remote-lamp exempt automation children visible in the expanded fold', () => {
    const sessions = Array.from({ length: 7 }, (_, index) =>
      makeSession({ id: `run-${index}`, title: `RUN-${index}`, source: 'scheduler' }),
    );
    const group = { id: 'schedule:sched-remote', title: 'Remote', sessions, attentionSessionIds: [] };

    const withoutExempt = getAutomationGroupChildView(group, {
      notifications: new Set(),
      runningSessionIds: new Set(),
      showAll: false,
    });
    expect(withoutExempt.visibleSessions.map((session) => session.id)).not.toContain('run-6');

    const view = getAutomationGroupChildView(group, {
      notifications: new Set(),
      runningSessionIds: new Set(),
      foldExemptSessionIds: new Set(['run-6']),
      showAll: false,
    });
    expect(view.visibleSessions.map((session) => session.id)).toContain('run-6');
    expect(view.isOverflowing).toBe(true);
    expect(view.hiddenCount).toBe(1);
  });

  it('merges newly exempt remote runs into a frozen layout without moving its clicked rows', () => {
    const sessions = Array.from({ length: 7 }, (_, index) =>
      makeSession({ id: `run-${index}`, source: 'scheduler' }),
    );
    const group = { id: 'schedule:remote-frozen', title: 'Remote', sessions, attentionSessionIds: [] };
    const options = { notifications: new Set<string>(), showAll: false,
      frozenVisibleSessionIds: ['run-2', 'run-0'], activeSessionId: 'run-2' };
    const before = getAutomationGroupChildView(group, options);
    expect(before.visibleSessions.map((session) => session.id)).toEqual(['run-2', 'run-0']);
    const view = getAutomationGroupChildView(group, {
      ...options, foldExemptSessionIds: new Set(['run-0', 'run-6', 'other-group']),
    });
    expect(view.visibleSessions.map((session) => session.id)).toEqual(['run-2', 'run-0', 'run-6']);
    expect(view.isOverflowing).toBe(true);
    expect(view.hiddenCount).toBe(4);
    expect(view.totalCount).toBe(7);
    expect(getAutomationGroupChildView(group, { ...options, showAll: true }).visibleSessions)
      .toEqual(sessions);
  });

  it.each(['unread', 'running'])('keeps newly %s local runs visible in frozen layouts without remote exemptions', (activity) => {
    const sessions = ['clicked', 'idle', 'active'].map((id) => makeSession({ id, source: 'scheduler' }));
    const group = { id: 'schedule:local-frozen', title: 'Local', sessions, attentionSessionIds: [] };
    const view = getAutomationGroupChildView(group, {
      notifications: new Set(activity === 'unread' ? ['clicked', 'active', 'other-group'] : []),
      runningSessionIds: new Set(activity === 'running' ? ['clicked', 'active', 'other-group'] : []),
      showAll: false, frozenVisibleSessionIds: ['clicked'],
    });
    expect(view.visibleSessions.map((session) => session.id)).toEqual(['clicked', 'active']);
    expect(view.hiddenCount).toBe(1);
    expect(view.isOverflowing).toBe(true);
  });

  it('keeps overflow available when a frozen layout still hides idle runs', () => {
    const sessions = ['visible', 'hidden'].map((id) => makeSession({ id, source: 'scheduler' }));
    const group = { id: 'schedule:frozen-overflow', title: 'Frozen', sessions, attentionSessionIds: [] };
    const view = getAutomationGroupChildView(group, {
      notifications: new Set(), showAll: false, frozenVisibleSessionIds: ['visible', 'removed'],
    });
    expect(view.visibleSessions).toEqual([sessions[0]]);
    expect(view.isOverflowing).toBe(true);
    expect(view.hiddenCount).toBe(1);
  });

  it('caps collapsed automation children at five and overflows the rest like the dialogue list', () => {
    const sessions = Array.from({ length: 7 }, (_, index) =>
      makeSession({ id: `run-${index}`, title: `RUN-${index}`, source: 'scheduler' }),
    );
    const group = { id: 'schedule:sched-cap', title: 'Cap', sessions, attentionSessionIds: [] };

    // 全部 7 条参与折叠,默认显示前 5 条(run-0..run-4),剩 run-5 / run-6 折叠。
    const view = getAutomationGroupChildView(group, {
      notifications: new Set(),
      showAll: false,
    });
    expect(view.visibleSessions.map((session) => session.id)).toEqual([
      'run-0',
      'run-1',
      'run-2',
      'run-3',
      'run-4',
    ]);
    expect(view.isOverflowing).toBe(true);
    expect(view.hiddenCount).toBe(2);
    expect(view.totalCount).toBe(7);
  });

  describe('collapsed automation group keeps unhandled alerts reachable', () => {
    const alertGroup = (count = 7) => ({
      id: 'schedule:sched-alert',
      title: 'Alert',
      sessions: Array.from({ length: count }, (_, index) =>
        makeSession({ id: `run-${index}`, title: `RUN-${index}`, source: 'scheduler' }),
      ),
      attentionSessionIds: [],
    });

    it('renders nothing extra while no run holds an alert', () => {
      const view = getAutomationGroupChildView(alertGroup(), {
        notifications: new Set(['run-0']),
        showAll: false,
        collapsed: true,
      });
      expect(view.visibleSessions).toEqual([]);
      expect(view.isOverflowing).toBe(false);
      expect(view.totalCount).toBe(0);
    });

    it('lifts only the alerting runs out of a collapsed group', () => {
      // run-3 是「组内第 3 新、turn 从未收尾」那类运行:组头代表的是 run-0,
      // 收起态若返回空列表,项目折叠头的红点就没有任何一行能对应上。
      const view = getAutomationGroupChildView(alertGroup(), {
        notifications: new Set(['run-0', 'run-3']),
        showAll: false,
        collapsed: true,
        alertSessionIds: new Set(['run-3']),
      });
      expect(view.visibleSessions.map((session) => session.id)).toEqual(['run-3']);
      expect(view.isOverflowing).toBe(false);
      expect(view.totalCount).toBe(1);
    });

    it('caps lifted alerts at five and counts only alerts in the overflow label', () => {
      const alerts = ['run-0', 'run-1', 'run-2', 'run-3', 'run-4', 'run-5'];
      const view = getAutomationGroupChildView(alertGroup(8), {
        notifications: new Set(alerts),
        showAll: false,
        collapsed: true,
        alertSessionIds: new Set(alerts),
      });
      expect(view.visibleSessions).toHaveLength(5);
      expect(view.isOverflowing).toBe(true);
      expect(view.hiddenCount).toBe(1);
      // 「显示全部 N 个」的 N 是告警数,不是整组运行数(收起态列表语义就是告警)。
      expect(view.totalCount).toBe(6);

      const expanded = getAutomationGroupChildView(alertGroup(8), {
        notifications: new Set(alerts),
        showAll: true,
        collapsed: true,
        alertSessionIds: new Set(alerts),
      });
      expect(expanded.visibleSessions.map((session) => session.id)).toEqual(alerts);
      expect(expanded.isOverflowing).toBe(false);
    });

    it('ignores a frozen expanded layout so collapsing cannot drag non-alert runs back', () => {
      const view = getAutomationGroupChildView(alertGroup(), {
        notifications: new Set(),
        showAll: false,
        collapsed: true,
        alertSessionIds: new Set(['run-4']),
        frozenVisibleSessionIds: ['run-0', 'run-1', 'run-2'],
      });
      expect(view.visibleSessions.map((session) => session.id)).toEqual(['run-4']);
    });

    it('goes back to the regular collapse cap once the group is expanded', () => {
      const view = getAutomationGroupChildView(alertGroup(), {
        notifications: new Set(),
        showAll: false,
        collapsed: false,
        alertSessionIds: new Set(['run-6']),
      });
      expect(view.visibleSessions.map((session) => session.id)).toEqual([
        'run-0',
        'run-1',
        'run-2',
        'run-3',
        'run-4',
      ]);
      expect(view.totalCount).toBe(7);
    });

    it('does not let a leftover collapsed showAll dump the whole history on expand', () => {
      // 收起态点过「显示全部」后,showAll 仍为 true;若展开分支把 collapsed 短路排在
      // showAll 之后,会一次渲染全部历史运行。组件切折叠时会清 showAll,本断言钉住
      // childView 自己也不依赖那次复位。
      const view = getAutomationGroupChildView(alertGroup(), {
        notifications: new Set(),
        showAll: true,
        collapsed: false,
        alertSessionIds: new Set(['run-0', 'run-1', 'run-2', 'run-3', 'run-4', 'run-5']),
      });
      expect(view.visibleSessions).toHaveLength(7);
      expect(view.totalCount).toBe(7);
    });
  });

  it('keeps unread automation runs visible past the collapse cap', () => {
    const sessions = Array.from({ length: 8 }, (_, index) =>
      makeSession({ id: `run-${index}`, title: `RUN-${index}`, source: 'scheduler' }),
    );
    const group = {
      id: 'schedule:sched-unread',
      title: 'Unread',
      sessions,
      attentionSessionIds: ['run-1', 'run-7'],
    };

    // 前 5 条 = run-0..run-4;run-7 在折叠区但未读 → 豁免保留;run-5 / run-6 已读且在折叠区 → 折叠。
    const view = getAutomationGroupChildView(group, {
      notifications: new Set(['run-1', 'run-7']),
      showAll: false,
    });
    expect(view.visibleSessions.map((session) => session.id)).toContain('run-7');
    expect(view.visibleSessions.map((session) => session.id)).not.toContain('run-6');
    expect(view.isOverflowing).toBe(true);
  });

  it('exempts automation runs active within the last 24h from the collapse cap', () => {
    const now = Date.parse('2026-01-10T12:00:00.000Z');
    const recentIso = new Date(now - 60 * 60 * 1000).toISOString();
    const staleIso = new Date(now - 48 * 60 * 60 * 1000).toISOString();
    const group = {
      id: 'schedule:sched-recent',
      title: 'Recent',
      sessions: [
        makeSession({ id: 'primary', source: 'scheduler', userSendAt: recentIso }),
        makeSession({ id: 'stale', source: 'scheduler', userSendAt: staleIso }),
        makeSession({ id: 'recent', source: 'scheduler', userSendAt: recentIso }),
      ],
      attentionSessionIds: [],
    };

    // minVisibleCount=1:全部 3 条参与,硬性只保留前 1 条(primary);传 now 时 recent 因 24h
    // 内有活动而豁免保留,stale(48h)被折叠;不传 now(关闭 24h 豁免)则 recent 也被折叠。
    const withNow = getAutomationGroupChildView(group, {
      notifications: new Set(),
      showAll: false,
      minVisibleCount: 1,
      nowMs: now,
    });
    expect(withNow.visibleSessions.map((session) => session.id)).toEqual(['primary', 'recent']);

    const withoutNow = getAutomationGroupChildView(group, {
      notifications: new Set(),
      showAll: false,
      minVisibleCount: 1,
    });
    expect(withoutNow.visibleSessions.map((session) => session.id)).toEqual(['primary']);
    expect(withoutNow.isOverflowing).toBe(true);
  });

  it('retains a clicked auto-exposed child during route transition and while active', () => {
    const group = {
      id: 'schedule:sched-jira',
      title: 'Jira 自动修复',
      sessions: [
        makeSession({ id: 'jira-3', title: 'BUG-3', source: 'scheduler' }),
        makeSession({ id: 'jira-2', title: 'BUG-2', source: 'scheduler' }),
        makeSession({ id: 'jira-1', title: 'BUG-1', source: 'scheduler' }),
      ],
      attentionSessionIds: ['jira-2', 'jira-1'],
    };

    expect(
      getVisibleAutomationGroupSessions(group, {
        notifications: new Set(['jira-2']),
        showAll: false,
        activeSessionId: 'manual',
        frozenVisibleSessionIds: ['jira-1'],
      }).map((session) => session.id),
    ).toEqual(['jira-1', 'jira-2']);
    expect(
      getAutomationGroupPrimarySession(group, new Set(['jira-2']), {
        preferredSessionId: 'jira-2',
      })?.id,
    ).toBe('jira-2');

    expect(
      getVisibleAutomationGroupSessions(group, {
        notifications: new Set(['jira-2']),
        showAll: false,
        activeSessionId: 'jira-1',
        frozenVisibleSessionIds: ['jira-1'],
      }).map((session) => session.id),
    ).toEqual(['jira-1', 'jira-2']);
  });

  it('freezes the group primary while the clicked automation session is active', () => {
    const group = {
      id: 'schedule:sched-jira',
      title: 'Jira 自动修复',
      sessions: [
        makeSession({ id: 'jira-3', title: 'BUG-3', source: 'scheduler' }),
        makeSession({ id: 'jira-2', title: 'BUG-2', source: 'scheduler' }),
        makeSession({ id: 'jira-1', title: 'BUG-1', source: 'scheduler' }),
      ],
      attentionSessionIds: ['jira-3', 'jira-2', 'jira-1'],
    };

    expect(getAutomationGroupPrimarySession(group, new Set(['jira-2', 'jira-1']))?.id).toBe(
      'jira-2',
    );
    expect(
      getAutomationGroupPrimarySession(group, new Set(['jira-2', 'jira-1']), {
        preferredSessionId: 'jira-3',
      })?.id,
    ).toBe('jira-3');
    expect(
      getVisibleAutomationGroupSessions(group, {
        notifications: new Set(['jira-2', 'jira-1']),
        showAll: false,
        activeSessionId: 'jira-3',
        frozenVisibleSessionIds: ['jira-2', 'jira-1'],
      }).map((session) => session.id),
    ).toEqual(['jira-2', 'jira-1']);
  });

  it('keeps the automation group title click separate from the disclosure toggle', () => {
    const source = readTextLf(
      new URL('../features/cc-agent/sidebar/AutomationSessionGroupItem.tsx', import.meta.url),
      'utf8',
    );
    const entryListSource = readTextLf(
      new URL('../features/cc-agent/sidebar/SessionEntryList.tsx', import.meta.url),
      'utf8',
    );

    expect(source).toContain('setFrozen(null)');
    // 轴 1:箭头切换的是持久化 disclosure(useAutomationGroupCollapsed),不是「显示全部」。
    expect(source).toContain('useAutomationGroupCollapsed(');
    expect(source).toContain('group.legacyId');
    expect(source).toContain('toggleCollapsed()');
    // 切折叠必须复位轴 2:showAll 被收起告警列表和展开历史列表共用。复位挂在
    // collapsed 变化上,覆盖 chevron 与父层「收起所有分组」,不只一条点击路径。
    expect(source).toMatch(/useLayoutEffect\(\(\) => \{\s+setShowAll\(false\);\s+\}, \[collapsed\]\);/);
    expect(source).toContain('const ToggleIcon = collapsed ? ChevronRight : ChevronDown');
    expect(source).toContain('aria-expanded={!collapsed}');
    // 轴 1 收起时只留组头 + 被提上来的未处理告警行,取舍统一由 childView 决定
    // (收起态曾经是硬编码的空列表,于是组内非最新一条的告警既不上组头也不成行)。
    expect(source).toContain('const visibleSessions = childView.visibleSessions;');
    expect(source).toMatch(/getAutomationGroupChildView\(group, \{[\s\S]*?collapsed,/);
    expect(source).toContain('const hasVisibleChildren = visibleSessions.length > 0');
    expect(source).toContain('{hasVisibleChildren && (');
    // 侧栏侧保留「立即运行」直点,低频的编辑 / 暂停恢复 / 删除收回 More 菜单。
    expect(source).toContain("onScheduleAction(group, 'run')");
    expect(source).toContain('const canMarkRead = collapsedAttention.tone != null');
    expect(source).toContain('{canMarkRead && (');
    expect(source).toContain("onScheduleAction(group, 'mark-read')");
    expect(source).toContain("onScheduleAction(group, 'edit')");
    expect(source).toContain("onScheduleAction(group, 'toggle-pause')");
    expect(source).toContain("onScheduleAction(group, 'delete')");
    expect(source).toContain('EllipsisVertical');
    expect(source).toContain('setMenuOpen');
    expect(source).toContain('onContextMenu={(event) => {');
    expect(source).toContain('setMenuOpen(true)');
    // Run / More 图标(lucide Play / EllipsisVertical)必须都在,按钮尺寸与普通会话行对齐。
    expect(source).toMatch(/<Play size=\{14\}/);
    expect(source).toMatch(/<EllipsisVertical size=\{14\}/);
    expect(source).toContain('ccAgent.sidebar.automationGroup.menu.runNow');
    expect(source).toContain('ccAgent.sidebar.automationGroup.menu.more');
    expect(source).toContain('ccAgent.sidebar.automationGroup.menu.markAllAsRead');
    expect(source).toContain('ccAgent.sidebar.automationGroup.menu.edit');
    expect(source).toContain('ccAgent.sidebar.automationGroup.menu.pause');
    expect(source).toContain('ccAgent.sidebar.automationGroup.menu.resume');
    expect(source).toContain('ccAgent.sidebar.automationGroup.menu.delete');
    expect(source).toContain('scheduleFocusPath(group.scheduleId)');
    // 组头点击:展开打开最新一条;收起且整组是红时打开贡献红点的那条。
    expect(source).toContain('resolveCollapsedGroupHeaderSessionId({');
    expect(source).toContain('onSessionClick(targetId)');
    expect(source).toContain('getAutomationGroupLatestSession(group)');
    expect(source).toContain('visibleSessionIds: visibleSessions.map((session) => session.id)');
    // 自动任务只是展示分组,展开后的每条运行仍须保留普通会话行的移动菜单与搜索高亮。
    expect(source).toContain('onMoveSession,');
    expect(source).toContain('projectOptions,');
    expect(source).toContain('matchIndices: matchMap?.get(session.id)');
    expect(entryListSource).toContain('onMoveSession={onMoveSession}');
    expect(entryListSource).toContain('projectOptions={projectOptions}');
    expect(entryListSource).toContain('matchMap={matchMap}');
    expect(source).toContain('originActiveSessionId: activeSessionId ?? null');
    expect(source).toContain('hasBeenActive: false');
    expect(source).toContain('hasBeenActive: true');
    expect(source).toContain('formatSidebarTime(primaryActivityIso, t)');
    expect(source).toContain('setCountdownNowMs(Date.now())');
    expect(source).toContain('window.setInterval');
    // 下次运行倒计时不再进 meta,而是作为整行 hover tooltip 的内容。
    expect(source).toContain(
      'formatSidebarFutureTime(group.nextFireAt, t, new Date(countdownNowMs))',
    );
    // rowTooltip 现在同时展示「下次运行倒计时 + 累计运行次数」;countdownText 来自
    // shouldTickCountdown 分支,runCountText 走既有 runCount i18n。
    expect(source).toContain('const countdownText = shouldTickCountdown');
    expect(source).toContain(
      "t('ccAgent.sidebar.automationGroup.runCount', { count: group.sessions.length })",
    );
    expect(source).toContain('text={rowTooltip}');
    expect(source).toContain('side="right"');
    // 点击行空白 = 点击标题:共享 openPrimarySession,行 div 挂 onClick,内部 chevron /
    // Run / More 各自 stopPropagation 不误触发。ARIA 上不给 div 加 role=button(会与
    // 内嵌 <button> 冲突),键盘可达性由内部标题 <button> 保留;因此不断言 role/tabIndex/
    // onKeyDown 存在,反过来要保证它们不出现在行 div 上。
    expect(source).toContain('const openLatestSession =');
    expect(source).toContain('onClick={openLatestSession}');
    expect(source).not.toMatch(/role=\{latestSession \? 'button'/);
    expect(source).toContain("group.scheduleStatus === 'active'");
    // 用正则容忍 prettier 折行(条件在一行还是拆三行都算通过)。
    expect(source).toMatch(
      /hasHoverActions &&\s*!menuOpen &&\s*'group-hover:opacity-0 group-focus-within\/slot:opacity-0'/,
    );
    expect(source).toContain("menuOpen && 'opacity-0'");
    expect(source).toContain('disabled={!latestSession}');
    // 自动任务组头首图标按模式复用普通任务槽:list 对齐 SessionCard 的 12px,
    // text 对齐 SessionItem 的 15px;vendor 尺寸规则保持一致。
    // vendor 经 agentKindToVendor 归一(pi 会话显示 π,而非 Claude 脸);尺寸规则:cc=13,其余(codex/pi)=12。
    expect(source).toContain("sessionVariant === 'list' ? 'w-3' : 'w-[15px]'");
    expect(source).toContain('vendor={agentKindToVendor(latestSession?.agentKind)}');
    expect(source).toContain(
      "size={agentKindToVendor(latestSession?.agentKind) === 'cc' ? 13 : 12}",
    );
    // 所有自动任务统一 Timer;暂停只叠角标,主图标和 12px 槽位不替换。
    expect(source).toContain('<AutomationTimerIcon');
    expect(source).toContain('paused={isScheduleStopped}');
    expect(source).not.toContain('<Clock');
    expect(source).not.toContain('<Pause');
    // text 沿用 vendor → Timer → 标题的 6px 节奏;list 把 Timer 排到标题后,
    // 标题与普通列表任务落在同一列。
    expect(source).toContain('className="min-w-0 truncate text-left disabled:cursor-default"');
    expect(source).toContain('className="flex min-w-0 items-center gap-1.5"');
    expect(source).toContain("sessionVariant === 'list' && 'order-2'");
    expect(source).toContain('runningSessionIds,');
    // 组头右侧的时间文字与普通任务行的信息槽(SessionInfoMeta)同款字体 / 色号,
    // 含 tabular-nums(2026-08-12 用户裁决;C 期给普通行加等宽数字时组头漏跟)。
    expect(source).toContain(
      "'col-start-1 row-start-1 flex items-center gap-1 text-xs font-medium tabular-nums'",
    );
    expect(source).toContain("'text-sidebar-action-icon'");
  });

  it('auto-collapses an expanded automation group once focus leaves it and offers show-all', () => {
    const source = readTextLf(
      new URL('../features/cc-agent/sidebar/AutomationSessionGroupItem.tsx', import.meta.url),
      'utf8',
    );

    // 展开锚点:展开瞬间记当时 active,落到组内算锚定,锚定过再离开组 → setShowAll(false)。
    expect(source).toContain('expandAnchorRef');
    expect(source).toContain('hasFocusedGroup');
    expect(source).toContain('originActiveSessionId: activeSessionId ?? null');
    expect(source).toContain('setShowAll(false)');
    // 「显示全部 N 个」复用普通对话同款文案 key,溢出时才出现。
    expect(source).toContain('childView.isOverflowing');
    expect(source).toContain(
      "t('ccAgent.sidebar.showAllSessions', { count: childView.totalCount })",
    );
  });

  it('refreshes the shared session store when a schedule binds a newly created session', () => {
    const source = readTextLf(new URL('../lib/sessionsStore.ts', import.meta.url), 'utf8');

    expect(source).toContain('maker?.schedule');
    expect(source).toContain("event.type === 'session-bound'");
    expect(source).toContain('sessionsStore.forceRefreshAll()');
  });

  it('backfills scheduler session metadata before broadcasting session-bound refreshes', () => {
    const source = readTextLf(
      new URL('../../main/scheduler-host/runner.ts', import.meta.url),
      'utf8',
    );

    const fireStart = source.indexOf(
      'async fire(schedule: Schedule, ctx: FireContext): Promise<FireResult> {',
    );
    const turnListenerStart = source.indexOf('let assistantText =', fireStart);
    expect(fireStart).toBeGreaterThanOrEqual(0);
    expect(turnListenerStart).toBeGreaterThan(fireStart);

    const fireSetupSource = source
      .slice(fireStart, turnListenerStart)
      .replace(/\/\*[\s\S]*?\*\//g, '')
      .replace(/^\s*\/\/.*$/gm, '');
    const backfillMatches = [...fireSetupSource.matchAll(/\bawait\s+backfillSessionMeta\s*\(/g)];
    const sessionBoundMatches = [
      ...fireSetupSource.matchAll(/\bawait\s+ctx\.onSessionBound\?\.\(session\.id\)/g),
    ];

    expect(backfillMatches).toHaveLength(1);
    expect(sessionBoundMatches).toHaveLength(1);
    expect(backfillMatches[0].index).toBeLessThan(sessionBoundMatches[0].index);
  });

  it('does not clear automation conversation attention just by browsing schedules', () => {
    const schedulerPageSource = readTextLf(
      new URL('../features/scheduler/SchedulerPage.tsx', import.meta.url),
      'utf8',
    );
    const taskListPaneSource = readTextLf(
      new URL('../features/scheduler/components/TaskListPane.tsx', import.meta.url),
      'utf8',
    );
    const taskListCellSource = readTextLf(
      new URL('../features/scheduler/components/TaskListCell.tsx', import.meta.url),
      'utf8',
    );
    const runHistoryCardSource = readTextLf(
      new URL('../features/scheduler/components/RunHistoryCard.tsx', import.meta.url),
      'utf8',
    );
    expect(schedulerPageSource).toContain('useScheduleUnreadRunCounts(sorted)');
    expect(taskListPaneSource).toContain('unreadRunCounts.get(s.id) ?? 0');
    expect(taskListCellSource).toContain('<AttentionDot');
    expect(schedulerPageSource).not.toContain('markScheduleRunsRead(selectedId)');
    // 标已读走 …AndSync 包装(IPC settle 后无条件本地刷新,main no-op 不广播时红点也能自愈)
    expect(runHistoryCardSource).toContain('markScheduleRunReadAndSync(run.id)');
  });

  it('uses the sidebar run index instead of a fixed per-schedule history limit', () => {
    const scheduleIndexHookSource = readTextLf(
      new URL('../features/cc-agent/hooks/useAutomationScheduleSessionIndex.ts', import.meta.url),
      'utf8',
    );
    const unreadCountsHookSource = readTextLf(
      new URL('../features/scheduler/hooks/useScheduleUnreadRunCounts.ts', import.meta.url),
      'utf8',
    );
    const storageSource = readTextLf(
      new URL('../../main/scheduler-host/storage.ts', import.meta.url),
      'utf8',
    );
    const preloadSource = readTextLf(new URL('../../preload/preload.ts', import.meta.url), 'utf8');

    // 这个 hook 取 snapshot 变体:除了 run 列表还要引擎的 in-flight 集合,用于通知抑制
    // 标记的对账(见 scheduleSidebarIndexRuns 与 scheduler.listInflightRunIds)。两者是
    // 同一条 sidebar index IPC,本用例要锁的「不走 per-schedule history limit」不变。
    expect(scheduleIndexHookSource).toContain('loadScheduleSidebarIndexSnapshot()');
    expect(scheduleIndexHookSource).not.toContain('RUNS_PER_SCHEDULE_LIMIT');
    expect(scheduleIndexHookSource).not.toContain('listRuns(');
    expect(unreadCountsHookSource).toContain('loadScheduleSidebarIndexRuns()');
    expect(unreadCountsHookSource).not.toContain('RUNS_PER_SCHEDULE_LIMIT');
    expect(unreadCountsHookSource).not.toContain('listRuns(');
    expect(storageSource).toContain('listSidebarIndexRuns');
    expect(storageSource).toContain('.from(scheduleSessionLatestRuns)');
    expect(storageSource).toContain('isNotNull(scheduleRuns.sessionId)');
    expect(storageSource).toContain('unreadTerminalRunWhere');
    expect(storageSource).toContain('nextFireAt: schedules.nextFireAt');
    expect(storageSource).toContain('listSchedulesByLegacyKey(db)');
    expect(storageSource).toContain('legacyScheduleNameFromSessionTitle(session.title)');
    expect(storageSource).toContain('runId: `${LEGACY_SESSION_RUN_ID_PREFIX}${session.id}`');
    expect(storageSource).toContain('readAt: session.updatedAt');
    expect(storageSource).toContain('linkedLegacyRows');
    expect(storageSource).toContain('scheduleByLegacyKey.set(key');
    expect(storageSource).toContain('legacyTitleWhere()');
    expect(storageSource).toContain("eq(sessions.source, 'scheduler')");
    expect(storageSource).toContain('listDirectScheduleIdsByLegacyKey');
    expect(storageSource).toContain('directScheduleId && directScheduleId !== row.id');
    expect(scheduleIndexHookSource).toContain('projectScheduleSidebarIndex(runs)');
    expect(readFileSync(new URL('../features/scheduler/lib/projectScheduleSidebarIndex.ts', import.meta.url), 'utf8')).toContain('nextFireAt: run.nextFireAt');
    expect(preloadSource).toContain('listSidebarIndexRuns');
  });

  it('keeps per-run Automation cost without loading cumulative list cost', () => {
    const schedulePageSource = readTextLf(
      new URL('../features/scheduler/SchedulerPage.tsx', import.meta.url),
      'utf8',
    );
    const taskListPaneSource = readTextLf(
      new URL('../features/scheduler/components/TaskListPane.tsx', import.meta.url),
      'utf8',
    );
    const taskListCellSource = readTextLf(
      new URL('../features/scheduler/components/TaskListCell.tsx', import.meta.url),
      'utf8',
    );
    const runHistoryCardSource = readTextLf(
      new URL('../features/scheduler/components/RunHistoryCard.tsx', import.meta.url),
      'utf8',
    );
    const runHistoryPaneSource = readTextLf(
      new URL('../features/scheduler/components/RunHistoryPane.tsx', import.meta.url),
      'utf8',
    );
    const preloadSource = readTextLf(new URL('../../preload/preload.ts', import.meta.url), 'utf8');
    const zh = JSON.parse(
      readTextLf(new URL('../i18n/locales/zh-CN/common.json', import.meta.url), 'utf8'),
    );

    expect(preloadSource).not.toContain('listCostSummaries');
    expect(schedulePageSource).not.toContain('useScheduleCostSummaries');
    expect(taskListPaneSource).not.toContain('costSummaries');
    expect(taskListCellSource).not.toContain('scheduler.cell.totalCost');
    expect(runHistoryPaneSource).toContain('groupRunsForHistory');
    expect(runHistoryPaneSource).toContain('PERSISTENT_SESSION_PREVIEW_LIMIT = 3');
    expect(runHistoryPaneSource).toContain('expandRemainingRuns');
    expect(runHistoryPaneSource).not.toContain('sessionCostMap');
    expect(runHistoryCardSource).toContain('isLegacySessionRun');
    expect(runHistoryCardSource).toContain("!isLegacySessionRun && run.status !== 'running'");
    expect(runHistoryCardSource).toContain('scheduler.runs.runCost');
    expect(runHistoryCardSource).toContain("run.costAttribution === 'legacy'");
    expect(zh.scheduler.runs.sessionCost).toBe('任务开销 {{cost}}');
    expect(zh.scheduler.runs.sessionValue).toBe('任务价值 {{value}}');
    expect(zh.scheduler.runs.runCost).toBe('本次开销 {{cost}}');
    expect(zh.scheduler.runs.legacyCostUnavailable).toBe('历史费用无法拆分');
    expect(zh.scheduler.runs.persistentSessionGroup).toBe(
      '持续任务 {{session}} · {{count}} 次运行',
    );
    expect(zh.scheduler.runs.expandRemainingRuns).toBe('展开另外 {{count}} 次');
  });

  it('routes automation group menu actions through the scheduler APIs', () => {
    const sidebarSource = readTextLf(
      new URL('../features/cc-agent/CCAgentSidebarUpper.tsx', import.meta.url),
      'utf8',
    );
    const schedulerPageSource = readTextLf(
      new URL('../features/scheduler/SchedulerPage.tsx', import.meta.url),
      'utf8',
    );

    expect(sidebarSource).toContain('/cc-agent/scheduled?focus=');
    expect(sidebarSource).toContain('&edit=');
    expect(sidebarSource).toContain('encodeURIComponent(scheduleId)');
    expect(sidebarSource).toContain('maker.schedule.runNow(scheduleId)');
    expect(sidebarSource).toContain('maker.schedule.pause(scheduleId)');
    expect(sidebarSource).toContain('maker.schedule.resume(scheduleId)');
    expect(sidebarSource).toContain('useDeleteScheduleWithSessions');
    expect(sidebarSource).toContain('requestDeleteSchedule({');
    expect(sidebarSource).toContain('knownSessionIds: group.sessions.map((session) => session.id)');
    expect(sidebarSource).toContain("if (action === 'mark-read')");
    expect(sidebarSource).toContain('unreadSuccessScheduleRunIds(info)');
    expect(sidebarSource).toContain(
      "t('ccAgent.layout.markedAsRead', { count: processed.length })",
    );
    expect(sidebarSource).not.toContain(
      "t('ccAgent.layout.markedAsRead', { count: unreadRunIds.length })",
    );
    expect(schedulerPageSource).toContain('getFocusedScheduleStatusFilter(schedules, focusId)');
    expect(schedulerPageSource).toContain('setEditing(focused)');
    expect(schedulerPageSource).toContain('setFormOpen(true)');
    expect(schedulerPageSource).toContain('requestDeleteSchedule({');
  });

  it('requires a generated-conversation disposition before deleting an automation schedule', () => {
    const deleteHookSource = readTextLf(
      new URL('../features/scheduler/hooks/useDeleteScheduleWithSessions.tsx', import.meta.url),
      'utf8',
    );

    expect(deleteHookSource).toContain(
      "export type ScheduleGeneratedSessionDisposition = 'keep' | 'archive' | 'delete'",
    );
    expect(deleteHookSource).toContain('window.electronAPI.maker.schedule.listRuns(');
    expect(deleteHookSource).toContain('RUNS_PER_DELETE_PREVIEW_LIMIT');
    expect(deleteHookSource).toContain('target.knownSessionIds ?? []');
    expect(deleteHookSource).toContain('window.electronAPI.maker.projectAutomation.removeSchedule');
    expect(deleteHookSource).toContain('window.electronAPI.maker.schedule.delete(target.id)');
    expect(deleteHookSource).toContain("if (disposition === 'keep') return []");
    expect(deleteHookSource).toContain(
      "sessionService.update(sessionId, { status: 'archived', pinnedAt: null })",
    );
    expect(deleteHookSource).toContain("sessionService.update(sessionId, { status: 'deleted' })");
    expect(deleteHookSource).toContain('window.electronAPI.cleanupSessionImages(sessionId)');
    expect(deleteHookSource).toContain('makerChatStore.closeSessionQuery(sessionId)');
    expect(deleteHookSource).toContain('discardComposerDraft(sessionId)');
    expect(deleteHookSource).toContain('scheduler.deleteDialog.option.keep.title');
    expect(deleteHookSource).toContain('scheduler.deleteDialog.option.archive.title');
    expect(deleteHookSource).toContain('scheduler.deleteDialog.option.delete.title');
    expect(deleteHookSource).not.toContain('AlertDialog.Action asChild');
  });

  it('tracks attention by ordinary session id without a separate automation badge store', () => {
    addSessionAttention('auto-run');
    addSessionAttention('manual-run');

    expect(hasSessionAttention('auto-run')).toBe(true);
    expect(hasSessionAttention('manual-run')).toBe(true);
    expect([...getSessionAttentionSnapshot()].sort()).toEqual(['auto-run', 'manual-run']);

    expect(clearSessionAttentionMany(['auto-run'])).toBe(1);
    expect(hasSessionAttention('auto-run')).toBe(false);
    expect(hasSessionAttention('manual-run')).toBe(true);
  });
});