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。

4.7 KB
Raw Blame
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import ts from 'typescript';
import { describe, expect, it, vi } from 'vitest';
import type { RemoteSessionListItem } from '@cindy/maker-shared/session-list';
import { resolveMobileSessionRowStatus } from '../session/sessionRightStatus';

const source = readFileSync(resolve(process.cwd(), 'src/session/HomeSurface.tsx'), 'utf8').replace(/\r\n/g, '\n');
const start = source.indexOf('  const openGroupPrimary = () => {');
const body = source.slice(start, source.indexOf('  return (', start));
// Execute the actual row handler while keeping native rendering out of this unit test.
const handler = new Function('group', 'statusTarget', 'attention', 'rightStatus', 'groupExpanded',
  'selectionMode', 'onPressSelection', 'onOpenSession', 'onToggleAutomationGroup', 'item',
  'sharedRole', 't',
  ts.transpileModule(`${body}\nreturn handlePress;`, {
    compilerOptions: { target: ts.ScriptTarget.ES2022 },
  }).outputText);
const translate = (key: string) => key;
const row = (id: string, time: number): RemoteSessionListItem => ({
  session: { id, status: 'active', createdAt: new Date(time).toISOString(), updatedAt: new Date(time).toISOString() },
  pendingInteractionCount: 0,
} as RemoteSessionListItem);

describe('automation group row press', () => {
  it.each([false, true])('opens the persisted interruption only while collapsed (expanded=%s)', (expanded) => {
    const interrupted = row('interrupted', 1000);
    Object.assign(interrupted.session, { activeTurnStartedAt: 1000, interruptedTurnStartedAt: 1000, lastTurnEndedAt: 900 });
    const latest = row('latest', 2000);
    const item = { ...latest, automationGroup: { key: 'group', items: [interrupted, latest], sessionCount: 2 } } as RemoteSessionListItem;
    const { status, target } = resolveMobileSessionRowStatus(item, false, expanded);
    const open = vi.fn(); const toggle = vi.fn();
    handler(item.automationGroup, target, false, status, expanded, false, undefined, open, toggle, item, undefined, translate)();
    if (expanded) { expect(toggle).toHaveBeenCalledWith('group'); expect(open).not.toHaveBeenCalled(); }
    else { expect(open).toHaveBeenCalledWith(interrupted); expect(toggle).not.toHaveBeenCalled(); }
    Object.assign(interrupted.session, { lastTurnEndedAt: 1000 });
    const resolved = resolveMobileSessionRowStatus(item, false, false);
    open.mockClear(); toggle.mockClear();
    handler(item.automationGroup, resolved.target, false, resolved.status, false, false, undefined, open, toggle, item, undefined, translate)();
    expect(toggle).toHaveBeenCalledWith('group'); expect(open).not.toHaveBeenCalled();
  });

  it.each(['time', 'running', 'done', 'awaiting', 'error'])('preserves selection, expansion and existing attention for %s', (status) => {
    const item = row('latest', 2000); const group = { key: 'group' };
    for (const attention of [false, true]) for (const expanded of [false, true]) {
      const open = vi.fn(); const toggle = vi.fn(); const select = vi.fn();
      handler(group, item, attention, status, expanded, true, select, open, toggle, item, undefined, translate)();
      expect(select).toHaveBeenCalledOnce(); expect(open).not.toHaveBeenCalled(); expect(toggle).not.toHaveBeenCalled();
      handler(group, item, attention, status, expanded, false, select, open, toggle, item, undefined, translate)();
      expect(open).toHaveBeenCalledTimes(!expanded && (attention || status === 'error') ? 1 : 0);
      expect(toggle).toHaveBeenCalledTimes(expanded || (!attention && status !== 'error') ? 1 : 0);
    }
  });

  it('keeps the arrow independent from row navigation', () => {
    const tree = ts.createSourceFile('HomeSurface.tsx', source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
    let callback: ts.ArrowFunction | undefined;
    function visit(node: ts.Node) {
      if (ts.isArrowFunction(node) && node.parameters[0]?.name.getText(tree) === 'event'
        && node.body.getText(tree).includes('event.stopPropagation();')
        && node.body.getText(tree).includes('onToggleAutomationGroup')) callback = node;
      ts.forEachChild(node, visit);
    }
    visit(tree);
    if (!callback) throw new Error('Missing production automation arrow callback');
    const onToggleAutomationGroup = vi.fn();
    const stopPropagation = vi.fn();
    const arrow = new Function('group', 'onToggleAutomationGroup', `return (${callback.getText(tree)});`)({ key: 'group' }, onToggleAutomationGroup);
    arrow({ stopPropagation });
    expect(stopPropagation).toHaveBeenCalledOnce();
    expect(onToggleAutomationGroup).toHaveBeenCalledExactlyOnceWith('group');
    expect(stopPropagation.mock.invocationCallOrder[0]).toBeLessThan(onToggleAutomationGroup.mock.invocationCallOrder[0]!);
  });
});