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。

8.0 KB
Raw Blame
/**
 * 伙伴对话皮肤的接线契约。
 *
 * 这三处判定都长在超大组件里(CCAgentSessionView 5k 行 / MessageStream 5.8k 行 /
 * ChatInput 8k 行),在 jsdom 里整棵挂起来既慢又脆。真正要锁死的是**判定条件本身**:
 * 头像与收控件只能对 Bot 会话生效,普通任务的渲染必须一字不改。所以这里锁源码契约,
 * 归属与状态稳定性由 botSessionPresentation / botChatPresentation 的行为测试覆盖。
 */
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { describe, expect, it } from 'vitest';

const read = (relativePath: string): string =>
  readFileSync(resolve(__dirname, '..', '..', '..', relativePath), 'utf8').replace(/\r\n/g, '\n');

const sessionView = read('features/cc-agent/CCAgentSessionView.tsx');
const sessionHeader = read('features/cc-agent/SessionContentHeader.tsx');
const messageStream = read('components/chat/MessageStream.tsx');
const messageActionBar = read('components/chat/MessageActionBar.tsx');
const chatInput = read('components/new-chat/ChatInput.tsx');
const botSettings = read('features/bots/BotsHomeView.tsx');

describe('Bot 对话的判定条件', () => {
  it('is mounted next to the ordinary task title', () => {
    expect(sessionHeader).toContain('{!isEditing && <BotTaskOwnerChip session={session} />}');
  });

  it('气泡头像只在 Bot 对话下传给消息流', () => {
    expect(sessionView).toContain('assistantAvatar={botAssistantAvatar}');
    expect(sessionView).toContain('<BotAvatar bot={botChatIdentity} size="sm" />');
  });

  it('伙伴对话换占位符并启用轻量输入框', () => {
    expect(sessionView).toContain('botComposerPlaceholderKey(botChatIdentity.name)');
    expect(sessionView).toContain('hideRuntimeControls={Boolean(botChatIdentity)}');
    // 普通任务仍走原来的占位符。
    expect(sessionView).toContain("t('ccAgent.layout.chatPlaceholder')");
  });

  it('伙伴对话换掉任务顶栏,而不是在它旁边再加一个', () => {
    expect(sessionView).toMatch(/botChatIdentity\s*\?\s*\(\s*<BotSessionContentHeaderRegistration bot=\{botChatIdentity\}/);
    expect(sessionView).toContain('<SessionContentHeaderRegistration');
    const botHeader = sessionView.match(/<BotSessionContentHeaderRegistration\b[\s\S]*?\/>/)?.[0];
    expect(botHeader).toBeTruthy();
    expect(botHeader).not.toMatch(/\bonVoice(?:Active)?\s*=/);
  });
});

describe('消息流的头像挂载', () => {
  it('没有头像时原样返回气泡,不多包一层 DOM', () => {
    const helper = messageStream.match(/function withAssistantAvatar\([\s\S]*?\n}/)?.[0];
    expect(helper).toBeTruthy();
    expect(helper).toContain('if (!avatar) return bubble;');
  });

  it('只有 assistant 分支挂头像', () => {
    expect(messageStream).toContain('return withAssistantAvatar(\n        assistantAvatar,');
    // Assistant text shows the avatar; task and teammate-message cards plus the
    // work fold reserve the same space invisibly. User messages bypass it.
    expect(messageStream.match(/withAssistantAvatar\(/g)?.length).toBe(4);
    expect(messageStream).toMatch(/simplifiedBotConversation\s*&&\s*\(message\.systemCardType === 'bot-session-task'\s*\|\|\s*message\.systemCardType === 'bot-session-task-result'\s*\|\|\s*message\.systemCardType === 'bot-session-task-message'\s*\|\|\s*message\.systemCardType === 'bot-direct-message'\)\s*\? withAssistantAvatar\(/);
    expect(messageStream).toMatch(/<span aria-hidden="true" className="invisible">\s*\{assistantAvatar\}\s*<\/span>/);
    expect(messageStream).toContain('{replyColumnSpacer ? withAssistantAvatar(replyColumnSpacer, block) : block}');
  });
});

describe('伙伴输入框只保留对话动作', () => {
  it('语音对话入口接入输入框,并传递可用与通话状态', () => {
    expect(sessionView).toContain('onStartVoiceConversation={voiceModeAvailable ? startVoiceMode : undefined}');
    expect(sessionView).toContain('voiceConversationDisabled={voiceModeBlocked}');
    expect(sessionView).toContain('voiceConversationActive={voiceModeOpen}');
    const voiceGate = sessionView.match(/const voiceModeBlocked = Boolean\([\s\S]*?\);/)?.[0];
    expect(voiceGate).toContain('pendingInputHandoff');
  });

  it('伙伴保留标准权限 chip,仅隐藏模型选择器', () => {
    expect(chatInput.match(/!hideRuntimeControls \? \(/g)?.length).toBe(1);
    expect(chatInput.indexOf('<PermissionSelector')).toBeLessThan(chatInput.indexOf('!hideRuntimeControls ? ('));
    expect(chatInput).toContain('<PermissionSelector');
    expect(chatInput).toContain('<ModelSelector');
  });

  it('伙伴仍可使用权限快捷键,锁定任务不能切换', () => {
    expect(chatInput).toContain('settingsLocked || sharedGuest ? [] : (activeAgentCapabilities?.permissionModes ?? [])');
    expect(chatInput).not.toContain('settingsLocked || hideRuntimeControls');
  });

  it('伙伴不渲染任务目录、费用或压缩状态行', () => {
    expect(sessionView).toContain('{!botChatIdentity ? (');
    expect(sessionView).toContain('<TodaySpendChip');
    expect(sessionView).toContain('<ContextCapacityRing');
  });
});

describe('伙伴设置包含独立能力选择入口', () => {
  it('保留基本资料、按需能力选择与高级文件入口', () => {
    expect(botSettings).toContain('import { BotCapabilitySettings }');
    expect(botSettings).toContain('<BotCapabilitySettings');
    expect(botSettings).not.toContain('<BotGrowthLists');
    expect(botSettings).toContain('<BotBasicProfileFields');
    expect(botSettings).toContain("t('bots.homeFolder.title')");
  });
});

describe('伙伴消息流与普通任务同一套工作过程', () => {
  it('工作组按普通任务渲染,输入框上方只保留普通状态栏', () => {
    expect(sessionView).toContain('simplifiedBotConversation={Boolean(botChatIdentity)}');
    expect(messageStream).toContain('simplifyBotRenderItems(grouped, isSessionStreaming, visibleGeneratedFileKeys)');
    // Text/tool grouping is covered by botConversationPresentation behavioral tests.
    expect(messageStream).toContain("from '@/features/bots/botConversationPresentation'");
    expect(messageStream).toContain('compact={false}');
    expect(messageStream).not.toContain('compact={simplifiedBotConversation}');
    expect(messageStream).toContain('replyColumnSpacer={botReplyColumnSpacer}');
    expect(messageStream).not.toContain('data-testid="bot-thinking-indicator"');
    expect(sessionView).not.toContain('<BotWorkingStatus');
    expect(sessionView).toContain('<RunningStatusBar');
    expect(sessionView).toContain('visible={composerRuntimeVisible');
    expect(sessionView).not.toContain('botAssistantOutputStarted');
  });

  it('时间戳只挂在伙伴消息分组上', () => {
    expect(messageStream).toContain('collectBotMessageTimeGroups(');
    expect(messageStream).toContain('botMessageTimeGroups.get(msg.clientId)');
  });

  it('未读起点只挂在伙伴消息流,不会创建分支', () => {
    expect(sessionView).toContain(
      'botUnreadBoundaryAt={botChatIdentity ? botUnreadBoundaryAt : null}',
    );
    expect(messageStream).toContain("t('bots.chat.newMessagesBoundary')");
    expect(messageStream).toContain('botUnreadBoundaryClientId === msg.clientId');
  });

  it('伙伴消息操作栏常显,外显回复,并隐藏费用与 Fork', () => {
    expect(messageStream).toContain('simplifiedBotConversation={simplifiedBotConversation}');
    expect(messageActionBar).toContain('const replyBtn = (replyAction || (simplifiedBotConversation && onAddToChat))');
    expect(messageActionBar).toContain('const forkBtn = !simplifiedBotConversation');
    expect(messageActionBar).toContain('simplifiedBotConversation ? null : costText || tokensText');
    expect(messageActionBar).toContain('simplifiedBotConversation || visible || menuOpen');
  });

  it('伙伴身份同时接通专属成果采集与成果卡,普通任务不触发', () => {
    expect(messageStream).toContain(
      'botSessionId: simplifiedBotConversation ? sessionId : undefined',
    );
    expect(messageStream).toContain('botArtifacts={simplifiedBotConversation}');
  });
});