From 7dfdcdfa859b79c338a5ca0eaf83f8fd37bd0eda Mon Sep 17 00:00:00 2001 From: margrop <1731827+margrop@users.noreply.github.com> Date: Thu, 3 Dec 2026 20:19:47 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=A0=87=E7=AD=BE=E5=BB=BA=E8=AE=AE?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=A4=8D=E7=94=A8=E7=BB=9F=E4=B8=80=E5=B9=B4?= =?UTF-8?q?=E7=BA=A7=E5=AD=A6=E6=AE=B5=E8=A7=84=E5=88=99=E4=BB=A5=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=A4=A7=E5=AD=A6=E4=B8=8E=E8=AF=BE=E6=A0=87=E9=AB=98?= =?UTF-8?q?=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/__tests__/integration/tags.test.ts | 23 ++++++++++++++ src/app/api/tags/suggestions/route.ts | 44 ++++++++++---------------- 2 files changed, 39 insertions(+), 28 deletions(-) diff --git a/src/__tests__/integration/tags.test.ts b/src/__tests__/integration/tags.test.ts index 9d8ca49c..a6d574ec 100644 --- a/src/__tests__/integration/tags.test.ts +++ b/src/__tests__/integration/tags.test.ts @@ -354,6 +354,29 @@ describe('/api/tags', () => { expect(data.suggestions).toContain('二元一次方程'); expect(data.suggestions).not.toContain('函数'); }); + + it('应该按学段过滤系统标签(含大学与课标高中根节点)并保留用户自定义标签', async () => { + const mockTags = [ + { ...createMockTag('大一', 'math', true, true), id: 'root-uni' }, + { ...createMockTag('极限与连续', 'math', true, false), parentId: 'root-uni' }, + { ...createMockTag('高中 选必一', 'math', true, true), id: 'root-sh' }, + { ...createMockTag('空间向量', 'math', true, false), parentId: 'root-sh' }, + { ...createMockTag('七年级上', 'math', true, true), id: 'root-jh' }, + { ...createMockTag('有理数', 'math', true, false), parentId: 'root-jh' }, + { ...createMockTag('我的自定义标签', 'math', false, false), userId: 'user-1' }, + ]; + mocks.mockPrismaKnowledgeTag.findMany.mockResolvedValue(mockTags); + + const uniRes = await GET_SUGGESTIONS(new Request('http://localhost/api/tags/suggestions?stage=university')); + const uniData = await uniRes.json(); + expect(uniRes.status).toBe(200); + expect(uniData.suggestions).toEqual(['极限与连续', '我的自定义标签']); + + const shRes = await GET_SUGGESTIONS(new Request('http://localhost/api/tags/suggestions?stage=senior_high')); + const shData = await shRes.json(); + expect(shRes.status).toBe(200); + expect(shData.suggestions).toEqual(['空间向量', '我的自定义标签']); + }); }); describe('POST /api/tags (创建标签)', () => { diff --git a/src/app/api/tags/suggestions/route.ts b/src/app/api/tags/suggestions/route.ts index 6479f389..16bd69cb 100644 --- a/src/app/api/tags/suggestions/route.ts +++ b/src/app/api/tags/suggestions/route.ts @@ -1,8 +1,9 @@ import { NextResponse } from "next/server"; -import { prisma } from "@/lib/prisma"; -import { authOptions } from "@/lib/auth"; import { getServerSession } from "next-auth"; +import { authOptions } from "@/lib/auth"; import { createLogger } from "@/lib/logger"; +import { isGradeInStage, isNotebookStage } from "@/lib/notebook-scope"; +import { prisma } from "@/lib/prisma"; import type { Prisma } from "@prisma/client"; const logger = createLogger('api:tags:suggestions'); @@ -69,33 +70,20 @@ export async function GET(req: Request) { let suggestions = allTags.filter(t => t.children.length === 0); - // Filter by stage if provided - if (stage) { - const allowedGradePatterns: Record = { - 'primary': ['一年级', '二年级', '三年级', '四年级', '五年级', '六年级'], - 'junior_high': ['七年级', '八年级', '九年级'], - 'senior_high': ['高一', '高二', '高三'], - }; + // 按学段过滤(复用 notebook-scope.ts 的统一年级正则,支持小学/初中/高中/大学) + if (stage && isNotebookStage(stage)) { + suggestions = suggestions.filter(tag => { + // 用户自定义标签不受系统学段树限制 + if (!tag.isSystem) { + return true; + } - const filters = allowedGradePatterns[stage]; - if (filters) { - suggestions = suggestions.filter(tag => { - // Always show user custom tags (non-system tags) regardless of stage filter - // unless we want to enforce structure on them too? Usually custom tags have flattened structure or no parent - if (!tag.isSystem) { - return true; - } - - let current = tag; - // Traverse up to find root - while (current.parentId && tagMap.get(current.parentId)) { - current = tagMap.get(current.parentId)!; - } - // Current is now the root (or top-most loaded ancestor) - const isMatch = filters.some(f => current.name.includes(f)); - return isMatch; - }); - } + let current = tag; + while (current.parentId && tagMap.get(current.parentId)) { + current = tagMap.get(current.parentId)!; + } + return isGradeInStage(current.name, stage); + }); } // Filter by query