import test from 'node:test';
import assert from 'node:assert/strict';

import { buildEffortSummaryMetrics, sumPositiveHours } from './effort-summary';

interface EffortMetricForTest {
  label: string;
  value: string;
}

test('sumPositiveHours only returns a total when at least one item has a positive value', () => {
  const total = sumPositiveHours(
    [{ hours: 0.25 }, { hours: undefined }, { hours: 1 }],
    (item: { hours?: number }) => item.hours,
  );

  assert.equal(total, 1.25);
  assert.equal(sumPositiveHours([{ hours: 0 }, { hours: undefined }], (item: { hours?: number }) => item.hours), undefined);
});

test('buildEffortSummaryMetrics keeps the shared top metric order and empty estimate placeholders', () => {
  const metrics = buildEffortSummaryMetrics({
    aiEstimateHours: 0.25,
    estimateHours: undefined,
    actualHours: 1.5,
    manhours: 2,
  });

  assert.deepEqual(metrics.map((metric: EffortMetricForTest) => metric.label), ['AI预估', '执行预估', '实际耗时', '人力投入']);
  assert.equal(metrics[0].value, '0.25h（0.01天）');
  assert.equal(metrics[1].value, '-');
  assert.equal(metrics[2].value, '1.5h（0.06天）');
  assert.equal(metrics[3].value, '2h（0.08天）');
});
