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

import { getEstimateHours, type DevTask } from './dev-task';

function makeTask(patch: Partial<DevTask>): DevTask {
  return {
    id: 'task-1',
    taskNo: 'DEV-001',
    requirementId: 'req-1',
    title: '实现筛选',
    categoryId: 'cat-frontend',
    assigneeId: '张三',
    priority: 'P2',
    expectedStartAt: '',
    expectedEndAt: '',
    status: 'todo',
    isBlocked: false,
    createdBy: 'tester',
    createdAt: '2026-06-26T01:00:00.000Z',
    updatedAt: '2026-06-26T01:00:00.000Z',
    ...patch,
  };
}

test('getEstimateHours prefers executor estimate over AI estimate', () => {
  const task = makeTask({
    estimateHours: 1.5,
    aiEstimateHours: 0.5,
  });

  assert.equal(getEstimateHours(task), 1.5);
});

test('getEstimateHours falls back to AI estimate before schedule-derived estimate', () => {
  const task = makeTask({
    aiEstimateHours: 0.25,
    expectedStartAt: '2026-06-26T01:00:00.000Z',
    expectedEndAt: '2026-06-26T03:00:00.000Z',
  });

  assert.equal(getEstimateHours(task), 0.25);
});

test('getEstimateHours uses schedule when no executor or AI estimate exists', () => {
  const task = makeTask({
    expectedStartAt: '2026-06-26T01:00:00.000Z',
    expectedEndAt: '2026-06-26T03:00:00.000Z',
  });

  assert.equal(getEstimateHours(task), 2);
});
