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

import {
  DEFAULT_TEST_CATEGORY_ID,
  PRESET_CATEGORIES,
  findCategoryByCode,
  getDefaultCategoryByGroup,
  ensureTaskCategoryByName,
  normalizeTaskCategories,
  resolveAiTaskCategoryByName,
  resolveCategoryIdFromCode,
} from './task-category';

test('preset categories include stable codes and testing group', () => {
  assert.ok(PRESET_CATEGORIES.some((c) => c.code === 'test_functional' && c.group === 'testing'));
  for (const code of [
    'test_ui_interaction',
    'test_form_validation',
    'test_data_consistency',
    'test_permission',
    'test_boundary',
    'test_state_flow',
    'test_regression',
  ]) {
    assert.ok(PRESET_CATEGORIES.some((c) => c.code === code && c.group === 'testing'), code);
  }
  assert.ok(PRESET_CATEGORIES.every((c) => c.code.length > 0));
});

test('normalizes legacy categories without code', () => {
  const normalized = normalizeTaskCategories([
    { id: 'cat-1', name: '前端开发', group: 'development', sortOrder: 1, isSystem: true },
  ]);

  assert.equal(normalized[0].code, 'frontend_development');
});

test('resolves category id from stable code', () => {
  const id = resolveCategoryIdFromCode(PRESET_CATEGORIES, 'test_functional', 'testing');
  assert.equal(id, DEFAULT_TEST_CATEGORY_ID);
});

test('falls back to default group category for unknown code', () => {
  const category = getDefaultCategoryByGroup(PRESET_CATEGORIES, 'testing');
  assert.equal(resolveCategoryIdFromCode(PRESET_CATEGORIES, 'unknown_code', 'testing'), category.id);
});

test('finds category by code', () => {
  assert.equal(findCategoryByCode(PRESET_CATEGORIES, 'backend_api')?.name, '后端接口');
});

test('returns existing category when AI task type name already exists in same group', () => {
  const result = ensureTaskCategoryByName(PRESET_CATEGORIES, '  前端开发  ', 'development');

  assert.equal(result.created, false);
  assert.equal(result.category.id, 'cat-1');
  assert.equal(result.categories.length, PRESET_CATEGORIES.length);
});

test('creates non-system category for missing AI task type name', () => {
  const result = ensureTaskCategoryByName(PRESET_CATEGORIES, '人员名片交互', 'development');

  assert.equal(result.created, true);
  assert.equal(result.category.name, '人员名片交互');
  assert.equal(result.category.group, 'development');
  assert.equal(result.category.isSystem, false);
  assert.equal(result.category.code, '人员名片交互');
  assert.ok(result.category.id.startsWith('cat-ai-'));
  assert.equal(result.categories.at(-1)?.id, result.category.id);
});

test('does not create document-specific testing type when auto creation is disabled', () => {
  const result = resolveAiTaskCategoryByName(PRESET_CATEGORIES, '排行榜测试', 'testing', {
    allowCreate: false,
    fallbackCode: 'test_data_consistency',
  });

  assert.equal(result.created, false);
  assert.equal(result.category.code, 'test_data_consistency');
  assert.equal(result.categories.length, PRESET_CATEGORIES.length);
});

test('keeps testing fallback within the testing category group', () => {
  const result = resolveAiTaskCategoryByName(PRESET_CATEGORIES, '排行榜测试', 'testing', {
    allowCreate: false,
    fallbackCode: 'frontend_development',
  });

  assert.equal(result.created, false);
  assert.equal(result.category.id, DEFAULT_TEST_CATEGORY_ID);
  assert.equal(result.category.group, 'testing');
});
