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

import {
  calculateStandardizationTaskProgressPercent,
  formatStandardizationTaskProgressSummary,
  getStandardizationTaskProgressStageLabel,
} from '@/lib/standardizationTaskProgress';

describe('standardizationTaskProgress', () => {
  it('keeps online progress based on processed ratio', () => {
    const snapshot = {
      execution_mode: 'online' as const,
      status: 'processing' as const,
      provider_status: '',
      progress_current: 5,
      progress_total: 10,
      current_phase: '分析中',
    };

    expect(calculateStandardizationTaskProgressPercent(snapshot)).toBe(50);
    expect(getStandardizationTaskProgressStageLabel(snapshot)).toBe('分析中');
    expect(formatStandardizationTaskProgressSummary(snapshot)).toBe('已处理 5/10');
  });

  it('maps offline validating stage to non-zero monitor progress', () => {
    const snapshot = {
      execution_mode: 'offline' as const,
      status: 'processing' as const,
      provider_status: 'validating',
      progress_current: 0,
      progress_total: 20,
      current_phase: '离线批处理 · validating',
    };

    expect(calculateStandardizationTaskProgressPercent(snapshot)).toBe(20);
    expect(getStandardizationTaskProgressStageLabel(snapshot)).toBe('校验中');
    expect(formatStandardizationTaskProgressSummary(snapshot)).toBe('已回执 0/20');
  });

  it('scales offline in-progress stage by acknowledged rows', () => {
    const snapshot = {
      execution_mode: 'offline' as const,
      status: 'processing' as const,
      provider_status: 'in_progress',
      progress_current: 10,
      progress_total: 20,
      current_phase: '离线批处理 · in_progress',
    };

    expect(calculateStandardizationTaskProgressPercent(snapshot)).toBe(60);
    expect(getStandardizationTaskProgressStageLabel(snapshot)).toBe('执行中');
  });

  it('treats writeback as near-complete even before task completion', () => {
    const snapshot = {
      execution_mode: 'offline' as const,
      status: 'processing' as const,
      provider_status: '写回结果',
      progress_current: 20,
      progress_total: 20,
      current_phase: '离线批处理 · 写回结果',
    };

    expect(calculateStandardizationTaskProgressPercent(snapshot)).toBe(98);
    expect(getStandardizationTaskProgressStageLabel(snapshot)).toBe('写回结果');
  });

  it('marks completed offline tasks as 100 percent', () => {
    const snapshot = {
      execution_mode: 'offline' as const,
      status: 'completed' as const,
      provider_status: 'completed',
      progress_current: 20,
      progress_total: 20,
      current_phase: '离线批处理 · completed',
    };

    expect(calculateStandardizationTaskProgressPercent(snapshot)).toBe(100);
    expect(getStandardizationTaskProgressStageLabel(snapshot)).toBe('已完成');
  });
});