test: Add unit test for projects/app/src/pages/api/core/dataset/paths.ts (#4349)

* Add unit tests for the getParents function in the dataset paths API.

* Update paths.ts

---------

Co-authored-by: gru-agent[bot] <185149714+gru-agent[bot]@users.noreply.github.com>
Co-authored-by: Archer <545436317@qq.com>
This commit is contained in:
gru-agent[bot]
2025-03-27 10:38:55 +08:00
committed by GitHub
parent cb29076e5b
commit 0778508908
2 changed files with 83 additions and 1 deletions

View File

@@ -0,0 +1,82 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { getParents } from '@/pages/api/core/dataset/paths';
import { MongoDataset } from '@fastgpt/service/core/dataset/schema';
vi.mock('@fastgpt/service/core/dataset/schema', () => ({
MongoDataset: {
findById: vi.fn()
},
DatasetCollectionName: 'datasets'
}));
describe('getParents', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('should return empty array if parentId is undefined', async () => {
const result = await getParents(undefined);
expect(result).toEqual([]);
});
it('should return empty array if parent not found', async () => {
vi.mocked(MongoDataset.findById).mockResolvedValueOnce(null);
const result = await getParents('non-existent-id');
expect(result).toEqual([]);
});
it('should return single parent path if no further parents', async () => {
vi.mocked(MongoDataset.findById).mockResolvedValueOnce({
name: 'Parent1',
parentId: undefined
});
const result = await getParents('parent1-id');
expect(result).toEqual([{ parentId: 'parent1-id', parentName: 'Parent1' }]);
});
it('should return full parent path for nested parents', async () => {
vi.mocked(MongoDataset.findById)
.mockResolvedValueOnce({
name: 'Child',
parentId: 'parent1-id'
})
.mockResolvedValueOnce({
name: 'Parent1',
parentId: 'parent2-id'
})
.mockResolvedValueOnce({
name: 'Parent2',
parentId: undefined
});
const result = await getParents('child-id');
expect(result).toEqual([
{ parentId: 'parent2-id', parentName: 'Parent2' },
{ parentId: 'parent1-id', parentName: 'Parent1' },
{ parentId: 'child-id', parentName: 'Child' }
]);
});
it('should handle circular references gracefully', async () => {
vi.mocked(MongoDataset.findById)
.mockResolvedValueOnce({
name: 'Node1',
parentId: 'node2-id'
})
.mockResolvedValueOnce({
name: 'Node2',
parentId: 'node1-id' // Circular reference
});
const result = await getParents('node1-id');
expect(result).toEqual([
{ parentId: 'node2-id', parentName: 'Node2' },
{ parentId: 'node1-id', parentName: 'Node1' }
]);
});
});