From f483832749b1ebebdd1f820b9ffab9c9d0115c3a Mon Sep 17 00:00:00 2001 From: "gru-agent[bot]" <185149714+gru-agent[bot]@users.noreply.github.com> Date: Thu, 27 Mar 2025 10:41:24 +0800 Subject: [PATCH] test: Add unit test for projects/app/src/pages/api/core/dataset/collection/paths.ts (#4350) * Add unit tests for getDatasetCollectionPaths function and modify paths.ts to export handler. * Update paths.ts --------- Co-authored-by: gru-agent[bot] <185149714+gru-agent[bot]@users.noreply.github.com> Co-authored-by: Archer <545436317@qq.com> --- .../api/core/dataset/collection/paths.ts | 2 +- .../api/core/dataset/collection/paths.test.ts | 87 +++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 test/cases/pages/api/core/dataset/collection/paths.test.ts diff --git a/projects/app/src/pages/api/core/dataset/collection/paths.ts b/projects/app/src/pages/api/core/dataset/collection/paths.ts index 1d7cd357b..6c3fe01b3 100644 --- a/projects/app/src/pages/api/core/dataset/collection/paths.ts +++ b/projects/app/src/pages/api/core/dataset/collection/paths.ts @@ -5,7 +5,7 @@ import { ParentTreePathItemType } from '@fastgpt/global/common/parentFolder/type import { MongoDatasetCollection } from '@fastgpt/service/core/dataset/collection/schema'; import { NextAPI } from '@/service/middleware/entry'; -async function handler(req: NextApiRequest) { +export async function handler(req: NextApiRequest) { const { parentId } = req.query as { parentId: string }; if (!parentId) { diff --git a/test/cases/pages/api/core/dataset/collection/paths.test.ts b/test/cases/pages/api/core/dataset/collection/paths.test.ts new file mode 100644 index 000000000..ae5d35cd3 --- /dev/null +++ b/test/cases/pages/api/core/dataset/collection/paths.test.ts @@ -0,0 +1,87 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest'; +import { getDatasetCollectionPaths } from '@/pages/api/core/dataset/collection/paths'; +import { MongoDatasetCollection } from '@fastgpt/service/core/dataset/collection/schema'; + +vi.mock('@fastgpt/service/core/dataset/collection/schema', () => ({ + MongoDatasetCollection: { + findOne: vi.fn() + }, + DatasetColCollectionName: 'dataset_collections' +})); + +describe('getDatasetCollectionPaths', () => { + beforeEach(() => { + vi.clearAllMocks(); + }); + + it('should return empty array for empty parentId', async () => { + const result = await getDatasetCollectionPaths({}); + expect(result).toEqual([]); + }); + + it('should return empty array if collection not found', async () => { + vi.mocked(MongoDatasetCollection.findOne).mockResolvedValueOnce(null); + + const result = await getDatasetCollectionPaths({ parentId: 'nonexistent-id' }); + expect(result).toEqual([]); + }); + + it('should return single path for collection without parent', async () => { + vi.mocked(MongoDatasetCollection.findOne).mockResolvedValueOnce({ + _id: 'col1', + name: 'Collection 1', + parentId: '' + }); + + const result = await getDatasetCollectionPaths({ parentId: 'col1' }); + expect(result).toEqual([{ parentId: 'col1', parentName: 'Collection 1' }]); + }); + + it('should return full path for nested collections', async () => { + vi.mocked(MongoDatasetCollection.findOne) + .mockResolvedValueOnce({ + _id: 'col3', + name: 'Collection 3', + parentId: 'col2' + }) + .mockResolvedValueOnce({ + _id: 'col2', + name: 'Collection 2', + parentId: 'col1' + }) + .mockResolvedValueOnce({ + _id: 'col1', + name: 'Collection 1', + parentId: '' + }); + + const result = await getDatasetCollectionPaths({ parentId: 'col3' }); + + expect(result).toEqual([ + { parentId: 'col1', parentName: 'Collection 1' }, + { parentId: 'col2', parentName: 'Collection 2' }, + { parentId: 'col3', parentName: 'Collection 3' } + ]); + }); + + it('should handle circular references gracefully', async () => { + vi.mocked(MongoDatasetCollection.findOne) + .mockResolvedValueOnce({ + _id: 'col1', + name: 'Collection 1', + parentId: 'col2' + }) + .mockResolvedValueOnce({ + _id: 'col2', + name: 'Collection 2', + parentId: 'col1' + }); + + const result = await getDatasetCollectionPaths({ parentId: 'col1' }); + + expect(result).toEqual([ + { parentId: 'col2', parentName: 'Collection 2' }, + { parentId: 'col1', parentName: 'Collection 1' } + ]); + }); +});