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

import {
  formatWorkflowElapsed,
  formatWorkflowProgressLabel,
  summarizeWorkflowActiveNodes,
} from './workflowRunProgress';

describe('workflowRunProgress', () => {
  it('shows the current active node', () => {
    const steps = [
      {
        hasInput: false,
        hasOutput: false,
        key: 'start',
        nodeId: 'start',
        nodeName: '开始节点',
        status: 'completed' as const,
        traces: [],
      },
      {
        hasInput: true,
        hasOutput: false,
        key: 'llm',
        nodeId: 'llm',
        nodeName: '内容生成',
        status: 'running' as const,
        traces: [],
      },
    ];

    expect(summarizeWorkflowActiveNodes(steps)).toBe('内容生成');
    expect(formatWorkflowProgressLabel('正在运行', steps)).toBe(
      '正在运行 · 内容生成',
    );
  });

  it('shows the latest node and active count for parallel execution', () => {
    const steps = ['检索资料', '生成摘要'].map((nodeName, index) => ({
      hasInput: true,
      hasOutput: false,
      key: String(index),
      nodeId: String(index),
      nodeName,
      status: 'running' as const,
      traces: [],
    }));

    expect(summarizeWorkflowActiveNodes(steps)).toBe('生成摘要 等 2 个节点');
  });

  it('keeps the latest node during short lifecycle gaps', () => {
    expect(summarizeWorkflowActiveNodes([], '条件判断')).toBe('条件判断');
  });

  it('formats short and long durations compactly', () => {
    expect(formatWorkflowElapsed(320)).toBe('320 ms');
    expect(formatWorkflowElapsed(1280)).toBe('1.3 s');
    expect(formatWorkflowElapsed(12_800)).toBe('13 s');
  });
});
