Add API router, index
This commit is contained in:
1 parent
fea861f042
commit
223ab79d21
4 files changed
+94
-2
No files matched your search
@@ -0,0 +1,35 @@
|
||||
import { describe, it } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { buildCategoryTree, CategoryNode } from './tree.utility.ts';
|
||||
|
||||
describe('Tree Utility - buildCategoryTree()', () => {
|
||||
it('should transform a flat list of ltree rows into a nested JSON structure', () => {
|
||||
const flatNodes: CategoryNode[] = [
|
||||
{ id: 1, name: 'Root', path: 'Top' },
|
||||
{ id: 3, name: 'Astronomy', path: 'Top.Science.Astronomy' },
|
||||
{ id: 2, name: 'Science', path: 'Top.Science' },
|
||||
{ id: 4, name: 'Gardening', path: 'Top.Hobbies.Gardening' },
|
||||
{ id: 5, name: 'Hobbies', path: 'Top.Hobbies' }
|
||||
];
|
||||
|
||||
const tree = buildCategoryTree(flatNodes);
|
||||
|
||||
// Verify root nodes count
|
||||
assert.equal(tree.length, 1);
|
||||
assert.equal(tree[0].name, 'Root');
|
||||
|
||||
// Verify second level ("Science" and "Hobbies")
|
||||
const rootChildren = tree[0].children;
|
||||
assert.equal(rootChildren.length, 2);
|
||||
|
||||
const scienceNode = rootChildren.find(c => c.name === 'Science');
|
||||
const hobbiesNode = rootChildren.find(c => c.name === 'Hobbies');
|
||||
|
||||
assert.ok(scienceNode);
|
||||
assert.ok(hobbiesNode);
|
||||
|
||||
// Verify third level deeply nested child ("Astronomy")
|
||||
assert.equal(scienceNode.children.length, 1);
|
||||
assert.equal(scienceNode.children[0].name, 'Astronomy');
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user