const { test } = require('node:test'); const assert = require('node:assert'); const { buildFolderTree } = require('../src/n8nTree'); test('buildFolderTree returns an empty array when there are no folders or workflows', () => { assert.deepStrictEqual(buildFolderTree([], []), []); }); test('buildFolderTree places root-level workflows (no parentFolder) directly at the top', () => { const workflows = [{ id: 'w1', name: 'ワークフローA', active: true, parentFolder: null }]; const tree = buildFolderTree([], workflows); assert.deepStrictEqual(tree, [{ type: 'workflow', id: 'w1', name: 'ワークフローA', active: true }]); }); test('buildFolderTree places root-level folders (no parentFolderId) at the top with empty children', () => { const folders = [{ id: 'f1', name: 'フォルダA', parentFolderId: null }]; const tree = buildFolderTree(folders, []); assert.deepStrictEqual(tree, [{ type: 'folder', id: 'f1', name: 'フォルダA', children: [] }]); }); test('buildFolderTree nests a workflow inside its parent folder', () => { const folders = [{ id: 'f1', name: 'フォルダA', parentFolderId: null }]; const workflows = [{ id: 'w1', name: 'ワークフローA', active: false, parentFolder: { id: 'f1' } }]; const tree = buildFolderTree(folders, workflows); assert.deepStrictEqual(tree, [ { type: 'folder', id: 'f1', name: 'フォルダA', children: [{ type: 'workflow', id: 'w1', name: 'ワークフローA', active: false }], }, ]); }); test('buildFolderTree nests a sub-folder inside its parent folder', () => { const folders = [ { id: 'f1', name: '親フォルダ', parentFolderId: null }, { id: 'f2', name: '子フォルダ', parentFolderId: 'f1' }, ]; const tree = buildFolderTree(folders, []); assert.deepStrictEqual(tree, [ { type: 'folder', id: 'f1', name: '親フォルダ', children: [{ type: 'folder', id: 'f2', name: '子フォルダ', children: [] }], }, ]); }); test('buildFolderTree sorts folders before workflows, each alphabetically by name', () => { const folders = [ { id: 'f2', name: 'Zフォルダ', parentFolderId: null }, { id: 'f1', name: 'Aフォルダ', parentFolderId: null }, ]; const workflows = [ { id: 'w2', name: 'Zワークフロー', active: true, parentFolder: null }, { id: 'w1', name: 'Aワークフロー', active: true, parentFolder: null }, ]; const tree = buildFolderTree(folders, workflows); assert.deepStrictEqual( tree.map((n) => n.name), ['Aフォルダ', 'Zフォルダ', 'Aワークフロー', 'Zワークフロー'] ); });