Files
ragflow/test/unit_test/api/db/test_template_utils.py
bitloi 853021ff2a feat: support multiple canvas_types for agent templates and remove duplicate files (#14030)
### What problem does this PR solve?

Closes #13907

The template catalog had duplicate files (e.g. `*_r.json`) only to place
the same template into multiple sidebar groups.
This increases maintenance cost and makes template updates error-prone.

This PR adds first-class support for multiple template categories in a
single file via `canvas_types`, then removes duplicate template files.

What changed:
- Added `canvas_types` to `CanvasTemplate` model and DB migration.
- Added normalization logic when loading templates:
  - accepts legacy `canvas_type`
  - accepts new `canvas_types`
  - merges/deduplicates values
- preserves backward compatibility by keeping `canvas_type` as first
normalized value.
- Updated template import flow to load only `.json` files and in stable
sorted order.
- Updated frontend template filtering to match on `canvas_types` first,
with fallback to legacy `canvas_type`.
- Consolidated duplicated template pairs into single files and removed:
  - `deep_search_r.json`
  - `reflective_academic_paper_generator_r.json`
  - `seo_article_writer_r.json`
- Added regression/edge-case tests for category normalization and route
serialization expectations.

### Type of change

- [ ] Bug Fix (non-breaking change which fixes an issue)
- [x] New Feature (non-breaking change which adds functionality)
- [ ] Documentation Update
- [ ] Refactoring
- [ ] Performance Improvement
- [ ] Other (please describe):
2026-04-13 20:26:30 +08:00

67 lines
2.1 KiB
Python

#
# Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import pytest
from api.db.template_utils import normalize_canvas_template_categories
@pytest.mark.p2
def test_normalize_canvas_template_categories_legacy_canvas_type():
payload = {"id": 1, "canvas_type": "Recommended"}
normalized = normalize_canvas_template_categories(payload)
assert normalized["canvas_type"] == "Recommended"
assert normalized["canvas_types"] == ["Recommended"]
@pytest.mark.p2
def test_normalize_canvas_template_categories_with_canvas_types_only():
payload = {
"id": 1,
"canvas_types": ["Recommended", "Agent", "Agent", " ", 1, None],
}
normalized = normalize_canvas_template_categories(payload)
assert normalized["canvas_type"] == "Recommended"
assert normalized["canvas_types"] == ["Recommended", "Agent"]
@pytest.mark.p2
def test_normalize_canvas_template_categories_merges_legacy_and_new_field():
payload = {
"id": 1,
"canvas_type": "Marketing",
"canvas_types": ["Recommended", "Marketing", "Agent"],
}
normalized = normalize_canvas_template_categories(payload)
assert normalized["canvas_type"] == "Marketing"
assert normalized["canvas_types"] == ["Marketing", "Recommended", "Agent"]
@pytest.mark.p2
def test_normalize_canvas_template_categories_no_valid_categories():
payload = {"id": 1, "canvas_type": " ", "canvas_types": [None, 3, " "]}
normalized = normalize_canvas_template_categories(payload)
assert normalized["canvas_type"] is None
assert normalized["canvas_types"] == []