fix(ai): 收紧AI任务类型入库规则
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
||||
getDefaultCategoryByGroup,
|
||||
ensureTaskCategoryByName,
|
||||
normalizeTaskCategories,
|
||||
resolveAiTaskCategoryByName,
|
||||
resolveCategoryIdFromCode,
|
||||
} from './task-category';
|
||||
|
||||
@@ -68,3 +69,25 @@ test('creates non-system category for missing AI task type name', () => {
|
||||
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');
|
||||
});
|
||||
|
||||
@@ -123,6 +123,15 @@ export function findCategoryByCode(categories: TaskCategory[], code?: string): T
|
||||
return categories.find((c) => c.code === code);
|
||||
}
|
||||
|
||||
function findCategoryByCodeInGroup(
|
||||
categories: TaskCategory[],
|
||||
code: string | undefined,
|
||||
group: CategoryGroup,
|
||||
): TaskCategory | undefined {
|
||||
if (!code) return undefined;
|
||||
return categories.find((category) => category.group === group && category.code === code);
|
||||
}
|
||||
|
||||
export function getDefaultCategoryByGroup(categories: TaskCategory[], group: CategoryGroup): TaskCategory {
|
||||
return getCategoriesByGroup(categories, group)[0] ?? categories[0] ?? PRESET_CATEGORIES[0];
|
||||
}
|
||||
@@ -135,13 +144,24 @@ export function ensureTaskCategoryByName(
|
||||
categories: TaskCategory[],
|
||||
name: string | undefined,
|
||||
group: CategoryGroup,
|
||||
): { categories: TaskCategory[]; category: TaskCategory; created: boolean } {
|
||||
return resolveAiTaskCategoryByName(categories, name, group, { allowCreate: true });
|
||||
}
|
||||
|
||||
export function resolveAiTaskCategoryByName(
|
||||
categories: TaskCategory[],
|
||||
name: string | undefined,
|
||||
group: CategoryGroup,
|
||||
options: { allowCreate?: boolean; fallbackCode?: string } = {},
|
||||
): { categories: TaskCategory[]; category: TaskCategory; created: boolean } {
|
||||
const normalizedCategories = normalizeTaskCategories(categories);
|
||||
const trimmedName = name?.trim() ?? '';
|
||||
if (!trimmedName) {
|
||||
return {
|
||||
categories: normalizedCategories,
|
||||
category: getDefaultCategoryByGroup(normalizedCategories, group),
|
||||
category:
|
||||
findCategoryByCodeInGroup(normalizedCategories, options.fallbackCode, group) ??
|
||||
getDefaultCategoryByGroup(normalizedCategories, group),
|
||||
created: false,
|
||||
};
|
||||
}
|
||||
@@ -153,6 +173,15 @@ export function ensureTaskCategoryByName(
|
||||
if (existing) {
|
||||
return { categories: normalizedCategories, category: existing, created: false };
|
||||
}
|
||||
if (options.allowCreate === false) {
|
||||
return {
|
||||
categories: normalizedCategories,
|
||||
category:
|
||||
findCategoryByCodeInGroup(normalizedCategories, options.fallbackCode, group) ??
|
||||
getDefaultCategoryByGroup(normalizedCategories, group),
|
||||
created: false,
|
||||
};
|
||||
}
|
||||
|
||||
const codeBase = slugifyCategoryName(trimmedName, normalizedCategories.length);
|
||||
const category: TaskCategory = {
|
||||
|
||||
Reference in New Issue
Block a user