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。

11.5 KB
Raw Blame
import { describe, expect, it } from 'vitest';
import { readFileSync } from 'node:fs';
import { appendComposerPluginTrigger, detectComposerPluginTrigger, filterComposerPlugins, placeComposerPlugin } from '@/session/composerPlugins';
import { textComposerDocument, composerDocumentProjectedText, serializeComposerDocument, type ComposerDocument } from '@/session/composerDocument';
import { expandGhostCommand, parseGhostCommandWord } from '@cindy/maker-shared/ghost-command';
import { composerPluginListSchema } from '@/session/remoteComposerPlugins';

const art = { manifest: { id: 'art', name: 'Art', command: 'art' }, enabled: true };
const old = { manifest: { id: 'old', name: 'Old', command: 'old' }, enabled: false };
const roster = [art, old];

describe('mobile composer plugin selection', () => {
  it('keeps every installed command reachable in the scrollable palette', () => {
    const plugins = Array.from({ length: 14 }, (_, index) => ({
      manifest: { id: `plugin-${index}`, name: `Plugin ${index}`, command: `plugin${index}` },
      enabled: true,
    }));
    expect(filterComposerPlugins(plugins, '')).toEqual(plugins);
    expect(filterComposerPlugins(plugins, 'plugin13')).toEqual([plugins[13]]);
  });
  it.each([['new', '\n'], ['new', '\r\n'], ['[sessionId]', '\n'], ['[sessionId]', '\r\n']])('places plugins inside the existing %s context sheet with %j line endings', (page, newline) => {
    const source = readFileSync(new URL('../../app/sessions/' + page + '.tsx', import.meta.url), 'utf8')
      .replace(/\r?\n/g, newline);
    const sheetStart = source.search(/<ContextSheet\s/);
    const sheetEnd = source.indexOf('</ContextSheet>', sheetStart);
    const plugins = source.indexOf('<ContextSheetPlugins');
    expect(sheetStart).toBeGreaterThan(0);
    expect(plugins).toBeGreaterThan(sheetStart);
    expect(plugins).toBeLessThan(sheetEnd);
  });
  it('prepends the plugin command and preserves the draft', () => {
    expect(composerDocumentProjectedText(placeComposerPlugin(textComposerDocument('draw a cat'), art, roster)))
      .toBe('$art draw a cat');
  });
  it.each(['draw a cat $ar', '$old draw a cat $ar', 'draw a cat ¥ar'])('routes a trailing selection in %s through the leading command', (text) => {
    const selected = composerDocumentProjectedText(placeComposerPlugin(textComposerDocument(text), art, roster));
    expect(selected).toBe('$art draw a cat ');
    expect(parseGhostCommandWord(selected)).toBe('art');
    expect(expandGhostCommand(selected, roster)).toContain('mcp__cindy__ghost_call');
    expect(detectComposerPluginTrigger(selected)).toBeNull();
  });
  it.each(['$OLD', '$old', '¥old', '¥old'])('replaces installed disabled command %s', (command) => {
    expect(composerDocumentProjectedText(placeComposerPlugin(textComposerDocument(command + '\nkeep this'), art, roster)))
      .toBe('$art\nkeep this');
  });
  it('keeps unknown dollar text instead of treating it as a plugin', () => {
    expect(composerDocumentProjectedText(placeComposerPlugin(textComposerDocument('$100 budget'), art, roster)))
      .toBe('$art $100 budget');
  });
  it('keeps rich mentions, quotes and pasted text atoms intact', () => {
    const document: ComposerDocument = { version: 1, nodes: [
      { type: 'text', text: '$old ' },
      { type: 'mention', kind: 'file', label: 'README', raw: '@README.md' },
      { type: 'pasted-text', text: 'long body', display: 'Pasted Text' },
      { type: 'quote', quote: { text: 'quoted body' } },
    ] };
    const next = placeComposerPlugin(document, art, roster);
    expect(next.nodes.slice(1)).toEqual(document.nodes.slice(1));
    expect(next.nodes[0]).toEqual({ type: 'text', text: '$art ' });
  });
  it.each([
    ['draw a cat $ar', '$art ', 'draw a cat '],
    ['$old draw a cat $ar', '$art', ' draw a cat '],
  ])('places the command before a leading quote in %s', (text, command, body) => {
    const quote = { type: 'quote' as const, quote: { text: 'quoted body' } };
    const selected = placeComposerPlugin({ version: 1, nodes: [quote, { type: 'text', text }] }, art, roster);
    expect(selected.nodes).toEqual([
      { type: 'text', text: command }, quote, { type: 'text', text: body },
    ]);
    const wire = serializeComposerDocument(selected).text;
    expect(parseGhostCommandWord(wire)).toBe('art');
    expect(expandGhostCommand(wire, roster)).toContain('mcp__cindy__ghost_call');
  });
  it('preserves quotes around the replaced command and their order relative to the body', () => {
    const quotes: ComposerDocument['nodes'] = [
      { type: 'quote', quote: { text: 'first quote' } },
      { type: 'quote', quote: { text: 'second quote' } },
    ];
    const selected = placeComposerPlugin({ version: 1, nodes: [
      quotes[0], { type: 'text', text: '$old' }, quotes[1], { type: 'text', text: 'draw a cat $ar' },
    ] }, art, roster);
    expect(selected.nodes).toEqual([
      { type: 'text', text: '$art ' }, ...quotes, { type: 'text', text: 'draw a cat ' },
    ]);
    expect(parseGhostCommandWord(serializeComposerDocument(selected).text)).toBe('art');
  });
  it('does not select unavailable entries', () => {
    const document = textComposerDocument('keep this');
    expect(placeComposerPlugin(document, old, roster)).toBe(document);
    expect(placeComposerPlugin(document, { manifest: { id: 'panel', name: 'Panel' }, enabled: true }, roster)).toBe(document);
  });
  it('opens a live palette for a trailing dollar query', () => {
    expect(detectComposerPluginTrigger('$ar')).toEqual({ from: 0, query: 'ar' });
    expect(detectComposerPluginTrigger('draw $ar')).toEqual({ from: 5, query: 'ar' });
    expect(detectComposerPluginTrigger('draw $ar now')).toBeNull();
    expect(filterComposerPlugins([art, old], 'ar').map((plugin) => plugin.manifest.id)).toEqual(['art']);
    expect(composerDocumentProjectedText(placeComposerPlugin(textComposerDocument('$ar'), art, roster))).toBe('$art ');
  });
  it.each([['', '$'], ['draw a cat', 'draw a cat $'], ['draw a cat ', 'draw a cat $'], ['$ar', '$ar']])('opens the palette from draft %s without duplicating the query', (draft, expected) => {
    const opened = composerDocumentProjectedText(appendComposerPluginTrigger(textComposerDocument(draft)));
    expect(opened).toBe(expected);
    expect(detectComposerPluginTrigger(opened)).not.toBeNull();
  });
  it('closes the palette when its query is removed, sent or replaced by another trigger', () => {
    const opened = appendComposerPluginTrigger(textComposerDocument('draw a cat'));
    const selected = composerDocumentProjectedText(placeComposerPlugin(opened, art, roster));
    expect(detectComposerPluginTrigger(selected)).toBeNull();
    for (const draft of ['', 'draw a cat', '/help', '@README.md', 'draw $ar now']) {
      expect(detectComposerPluginTrigger(draft)).toBeNull();
    }
  });
  it('keeps rich atoms when opening and selecting from the trailing query', () => {
    const atoms: ComposerDocument['nodes'] = [
      { type: 'mention', kind: 'file', label: 'README', raw: '@README.md' },
      { type: 'pasted-text', text: 'long body', display: 'Pasted Text' },
      { type: 'quote', quote: { text: 'quoted body' } },
    ];
    const opened = appendComposerPluginTrigger({ version: 1, nodes: atoms });
    const selected = placeComposerPlugin(opened, art, roster);
    expect(selected.nodes.filter((node) => node.type !== 'text')).toEqual(atoms);
    expect(parseGhostCommandWord(composerDocumentProjectedText(selected))).toBe('art');
  });
  it('does not delete dollar queries embedded in pasted content', () => {
    const atom = { type: 'pasted-text' as const, text: 'example $ar', display: 'Pasted Text' };
    const selected = placeComposerPlugin({ version: 1, nodes: [atom] }, art, roster);
    expect(selected.nodes).toContainEqual(atom);
  });
  it.each(['', 'draw a cat '])('removes a query split across quotes after draft %j', (body) => {
    const quotes: ComposerDocument['nodes'] = [
      { type: 'quote', quote: { text: 'first quote' } },
      { type: 'quote', quote: { text: 'second quote' } },
    ];
    const selected = placeComposerPlugin({ version: 1, nodes: [
      { type: 'text', text: body + '$' }, quotes[0],
      { type: 'text', text: 'a' }, quotes[1], { type: 'text', text: 'r' },
    ] }, art, roster);
    expect(selected.nodes).toEqual([
      { type: 'text', text: '$art ' + body }, ...quotes,
    ]);
    expect(detectComposerPluginTrigger(composerDocumentProjectedText(selected))).toBeNull();
    const wire = serializeComposerDocument(selected).text;
    expect(parseGhostCommandWord(wire)).toBe('art');
    expect(wire).not.toMatch(/\$ar(?=\s|$)/u);
    expect(wire).toContain('first quote');
    expect(wire).toContain('second quote');
  });
  it('preserves a quote at the end of a split query', () => {
    const quote = { type: 'quote' as const, quote: { text: 'quoted body' } };
    const selected = placeComposerPlugin({ version: 1, nodes: [
      { type: 'text', text: '$' }, quote, { type: 'text', text: 'ar' }, quote,
    ] }, art, roster);
    expect(selected.nodes).toEqual([{ type: 'text', text: '$art ' }, quote, quote]);
    expect(detectComposerPluginTrigger(composerDocumentProjectedText(selected))).toBeNull();
  });
  it('keeps a query that starts in a rich atom and ends in editable text', () => {
    const atom = { type: 'pasted-text' as const, text: 'example $', display: 'Pasted Text' };
    const quote = { type: 'quote' as const, quote: { text: 'quoted body' } };
    const selected = placeComposerPlugin({ version: 1, nodes: [
      atom, quote, { type: 'text', text: 'ar' },
    ] }, art, roster);
    expect(selected.nodes).toEqual([
      { type: 'text', text: '$art ' }, atom, quote, { type: 'text', text: 'ar' },
    ]);
  });

  it('keeps desktop namespace and instanceKey, and qualifies same-name commands', () => {
    const parsed = composerPluginListSchema.parse([
      { manifest: { id: 'draw', name: 'Root', command: 'draw' }, enabled: true, namespace: null, instanceKey: 'draw' },
      { manifest: { id: 'draw', name: 'Org', command: 'draw' }, enabled: true, namespace: 'acme', instanceKey: '_ns__acme__draw' },
    ]);
    expect(parsed.map((plugin) => [plugin.namespace, plugin.instanceKey])).toEqual([
      [null, 'draw'],
      ['acme', '_ns__acme__draw'],
    ]);
    const rootText = composerDocumentProjectedText(placeComposerPlugin(textComposerDocument('a cat'), parsed[0], parsed));
    const orgText = composerDocumentProjectedText(placeComposerPlugin(textComposerDocument('a cat'), parsed[1], parsed));
    expect(rootText).toBe('$draw/@root a cat');
    expect(orgText).toBe('$draw/acme a cat');
    const rootDirective = expandGhostCommand(rootText, parsed);
    const orgDirective = expandGhostCommand(orgText, parsed);
    expect(rootDirective).toContain('namespace: null');
    expect(rootDirective).not.toContain('存在多个实例');
    expect(orgDirective).toContain('namespace: acme');
    expect(orgDirective).toContain('ghost_id: draw');
    expect(orgDirective).not.toContain('存在多个实例');
  });
  it('keeps an old desktop list without namespace on the unqualified command', () => {
    const parsed = composerPluginListSchema.parse([
      { manifest: { id: 'art', name: 'Art', command: 'art' }, enabled: true },
    ]);
    expect(parsed[0].namespace).toBeUndefined();
    expect(parsed[0].instanceKey).toBeUndefined();
    const text = composerDocumentProjectedText(placeComposerPlugin(textComposerDocument('draw'), parsed[0], parsed));
    expect(text).toBe('$art draw');
    const directive = expandGhostCommand(text, parsed);
    expect(directive).toContain('(id: art)');
    expect(directive).not.toContain('ghost_id:');
    expect(directive).not.toContain('namespace: ');
  });
});