import { describe, expect, it } from 'vitest';

import { buildSearchDisplayErrors, summarizeSearchProgressMessage } from '@/components/shared/searchProgressDialogUtils';

describe('searchProgressDialogUtils', () => {
  it('strips stdout and stderr details from backend error messages', () => {
    const rawMessage = [
      '物料 182cf498-538e-4697-9b84-1eba053f062e 失败: 物料搜索执行失败: exit 1',
      '[stdout]',
      '[sync] 开始处理...',
      '[stderr]',
      'Error: stack trace',
    ].join('\n');

    expect(summarizeSearchProgressMessage(rawMessage)).toBe(
      '物料 182cf498-538e-4697-9b84-1eba053f062e 失败: 物料搜索执行失败: exit 1',
    );
  });

  it('deduplicates summarized errors from progress and result', () => {
    const detailedError = [
      '搜索执行失败: exit 1',
      '[stdout]',
      'line 1',
    ].join('\n');

    expect(buildSearchDisplayErrors([detailedError, ''], detailedError)).toEqual([
      '搜索执行失败: exit 1',
    ]);
  });
});