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

import {
  DEFAULT_TEST_CATEGORY_ID,
  PRESET_CATEGORIES,
  findCategoryByCode,
  getDefaultCategoryByGroup,
  normalizeTaskCategories,
  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, '后端接口');
});
