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。

5.9 KB
Raw Blame
import { describe, expect, it } from 'vitest';
import {
  formatQuotesForSend,
  parseLeadingBlockquotes,
  quoteSourceBasename,
  quoteSourceDisplayLabel,
} from '../lib/chatQuotes';

describe('formatQuotesForSend', () => {
  it('prefixes each quote line and separates quotes with a blank line (markdown semantics)', () => {
    expect(formatQuotesForSend([{ text: 'a\nb' }, { text: 'c' }], 'hello')).toBe(
      '> <!-- cindy-composer-quote -->\n> a\n> b\n\n> <!-- cindy-composer-quote -->\n> c\n\nhello',
    );
  });

  it('encodes intra-quote empty lines as a bare ">" (no trailing space to trim)', () => {
    expect(formatQuotesForSend([{ text: 'a\n\nb' }], 'hi')).toBe(
      '> <!-- cindy-composer-quote -->\n> a\n>\n> b\n\nhi',
    );
  });

  it('normalizes leading/trailing blank lines so the block never starts with a bare ">"', () => {
    // 开头裸 ">" 会被 parseLeadingBlockquotes 的早退守卫拒收(用户手打保护),
    // 采集侧选区吃进段落边界空行时必须在编码前剔除。
    expect(formatQuotesForSend([{ text: '\n\nselected\n' }], 'hi')).toBe(
      '> <!-- cindy-composer-quote -->\n> selected\n\nhi',
    );
    const sent = formatQuotesForSend([{ text: '\nfirst\n\nsecond\n\n', sourcePath: 'a.md' }], 'body');
    expect(sent).toBe(
      '> <!-- cindy-composer-quote -->\n> first\n>\n> second\n> — source: a.md\n\nbody',
    );
    expect(parseLeadingBlockquotes(sent)).toEqual({
      quotes: [{ text: 'first\n\nsecond', sourcePath: 'a.md' }],
      body: 'body',
    });
  });

  it('appends a source line for file quotes', () => {
    expect(formatQuotesForSend([{ text: 'a', sourcePath: 'docs/x.md' }], 'hi')).toBe(
      '> <!-- cindy-composer-quote -->\n> a\n> — source: docs/x.md\n\nhi',
    );
  });

  it('appends source line numbers for file quotes', () => {
    expect(
      formatQuotesForSend([{ text: 'a', sourcePath: 'docs/x.md', startLine: 12, endLine: 18 }], 'hi'),
    ).toBe('> <!-- cindy-composer-quote -->\n> a\n> — source: docs/x.md#L12-L18\n\nhi');
    expect(
      formatQuotesForSend([{ text: 'a', sourcePath: 'docs/x.md', startLine: 12, endLine: 12 }], 'hi'),
    ).toBe('> <!-- cindy-composer-quote -->\n> a\n> — source: docs/x.md#L12\n\nhi');
  });

  it('returns body untouched when there are no quotes', () => {
    expect(formatQuotesForSend([], 'hello')).toBe('hello');
  });

  it('trims the trailing gap for quote-only sends', () => {
    expect(formatQuotesForSend([{ text: 'a' }], '')).toBe(
      '> <!-- cindy-composer-quote -->\n> a',
    );
  });
});

describe('parseLeadingBlockquotes', () => {
  it('round-trips quotes containing internal empty lines', () => {
    const quotes = [{ text: '第一段\n\n空行后继续' }, { text: 'b', sourcePath: 'x.md' }];
    const sent = formatQuotesForSend(quotes, 'body');
    expect(parseLeadingBlockquotes(sent)).toEqual({ quotes, body: 'body' });
  });

  it('does not swallow a lone leading ">" line typed by the user', () => {
    expect(parseLeadingBlockquotes('>\nbody')).toEqual({ quotes: [], body: '>\nbody' });
  });

  it('round-trips chat and file quotes produced by formatQuotesForSend', () => {
    const quotes = [
      { text: '第一段\n跨两行' },
      { text: '文件里选的', sourcePath: 'src/app/main.ts' },
    ];
    const sent = formatQuotesForSend(quotes, '正文内容');
    expect(parseLeadingBlockquotes(sent)).toEqual({ quotes, body: '正文内容' });
  });

  it('round-trips leading indentation (whitespace-sensitive code quotes)', () => {
    const quotes = [{ text: '    if (x) {\n      return;\n    }', sourcePath: 'src/a.py' }];
    const sent = formatQuotesForSend(quotes, 'fix this');
    expect(parseLeadingBlockquotes(sent)).toEqual({ quotes, body: 'fix this' });
  });

  it('round-trips quote-only content', () => {
    const sent = formatQuotesForSend([{ text: 'only quote', sourcePath: 'a.md' }], '');
    expect(parseLeadingBlockquotes(sent)).toEqual({
      quotes: [{ text: 'only quote', sourcePath: 'a.md' }],
      body: '',
    });
  });

  it('round-trips file quote line numbers', () => {
    const quotes = [{ text: 'selected', sourcePath: 'docs/spec.md', startLine: 4, endLine: 6 }];
    const sent = formatQuotesForSend(quotes, 'fix it');
    expect(parseLeadingBlockquotes(sent)).toEqual({ quotes, body: 'fix it' });
  });

  it('does not treat a lone source-looking line as a source (needs preceding text)', () => {
    expect(parseLeadingBlockquotes('> — source: a.md\n\nbody')).toEqual({
      quotes: [{ text: '— source: a.md' }],
      body: 'body',
    });
  });

  it('returns plain content untouched', () => {
    expect(parseLeadingBlockquotes('just text\n> not leading')).toEqual({
      quotes: [],
      body: 'just text\n> not leading',
    });
  });

  it('stops the quote block at the first non-quote line', () => {
    expect(parseLeadingBlockquotes('> q\nbody\n> tail')).toEqual({
      quotes: [{ text: 'q' }],
      body: 'body\n> tail',
    });
  });
});

describe('quoteSourceBasename', () => {
  it('extracts the basename across separators', () => {
    expect(quoteSourceBasename('docs/design/spec.md')).toBe('spec.md');
    expect(quoteSourceBasename('win\\path\\a.ts')).toBe('a.ts');
    expect(quoteSourceBasename('single.md')).toBe('single.md');
  });
});

describe('quoteSourceDisplayLabel', () => {
  it('keeps file line labels visible through the renderer re-export', () => {
    expect(quoteSourceDisplayLabel({ text: 'a', sourcePath: 'docs/design/spec.md' })).toBe(
      'spec.md',
    );
    expect(
      quoteSourceDisplayLabel({
        text: 'a',
        sourcePath: 'docs/design/spec.md',
        startLine: 12,
        endLine: 18,
      }),
    ).toBe('spec.md:L12-L18');
    expect(
      quoteSourceDisplayLabel({
        text: 'a',
        sourcePath: 'docs/design/spec.md',
        startLine: 12,
        endLine: 8,
      }),
    ).toBe('spec.md:L12');
  });

  it('returns null for chat-only quotes without source path', () => {
    expect(quoteSourceDisplayLabel({ text: 'a' })).toBeNull();
  });
});