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

import { filterDecomposeResultByTarget } from './ai-decompose-target';
import type { AgentDecomposeResult } from '@ftb/shared';

function makeResult(): AgentDecomposeResult {
  return {
    report: { matched: [], reqOnly: [], prototypeOnly: [], ambiguous: [] },
    devTaskDrafts: [
      {
        title: '实现登录表单',
        description: '实现范围：渲染账号密码登录表单。验收点：输入合法信息后可提交。',
        taskTypeName: '前端开发',
        categoryCode: 'frontend_development',
        priority: 'P2',
        aiEstimateHours: 0.5,
        references: [{ type: 'requirement', id: 'REQ001', label: 'REQ001 登录' }],
      },
    ],
    testCaseDrafts: [
      {
        title: '验证登录成功',
        description: '输入正确账号密码后进入首页',
        taskTypeName: '功能测试',
        categoryCode: 'test_functional',
        priority: 'P2',
        aiEstimateHours: 0.2,
        references: [{ type: 'requirement', id: 'REQ001', label: 'REQ001 登录' }],
      },
    ],
  };
}

test('keeps only dev task drafts for dev task decomposition target', () => {
  const original = makeResult();
  const filtered = filterDecomposeResultByTarget(original, 'dev_tasks');

  assert.equal(filtered.devTaskDrafts.length, 1);
  assert.equal(filtered.testCaseDrafts.length, 0);
  assert.equal(original.testCaseDrafts.length, 1);
});

test('keeps only test case drafts for test case decomposition target', () => {
  const filtered = filterDecomposeResultByTarget(makeResult(), 'test_cases');

  assert.equal(filtered.devTaskDrafts.length, 0);
  assert.equal(filtered.testCaseDrafts.length, 1);
});

test('keeps both draft groups for all decomposition target', () => {
  const filtered = filterDecomposeResultByTarget(makeResult(), 'all');

  assert.equal(filtered.devTaskDrafts.length, 1);
  assert.equal(filtered.testCaseDrafts.length, 1);
});
