const { test } = require('node:test'); const assert = require('node:assert'); const { buildTagTree } = require('../src/n8nTree'); test('buildTagTree returns an empty array when there are no workflows', () => { assert.deepStrictEqual(buildTagTree([]), []); }); test('buildTagTree groups a workflow under its tag name', () => { const workflows = [{ id: 'w1', name: 'ワークフローA', active: true, tags: [{ name: 'タグA' }] }]; const tree = buildTagTree(workflows); assert.deepStrictEqual(tree, [ { type: 'folder', id: 'タグA', name: 'タグA', children: [{ type: 'workflow', id: 'w1', name: 'ワークフローA', active: true }], }, ]); }); test('buildTagTree puts untagged workflows into a "未分類" group', () => { const workflows = [{ id: 'w1', name: 'ワークフローA', active: true, tags: [] }]; const tree = buildTagTree(workflows); assert.deepStrictEqual(tree, [ { type: 'folder', id: '__untagged__', name: '未分類', children: [{ type: 'workflow', id: 'w1', name: 'ワークフローA', active: true }], }, ]); }); test('buildTagTree puts a multi-tagged workflow into every one of its tag groups', () => { const workflows = [{ id: 'w1', name: 'ワークフローA', active: true, tags: [{ name: 'タグA' }, { name: 'タグB' }] }]; const tree = buildTagTree(workflows); assert.deepStrictEqual( tree.map((g) => g.name), ['タグA', 'タグB'] ); assert.strictEqual(tree[0].children.length, 1); assert.strictEqual(tree[1].children.length, 1); }); test('buildTagTree sorts workflows within a group alphabetically by name', () => { const workflows = [ { id: 'w2', name: 'Zワークフロー', active: true, tags: [{ name: 'タグA' }] }, { id: 'w1', name: 'Aワークフロー', active: true, tags: [{ name: 'タグA' }] }, ]; const tree = buildTagTree(workflows); assert.deepStrictEqual( tree[0].children.map((w) => w.name), ['Aワークフロー', 'Zワークフロー'] ); }); test('buildTagTree sorts tag groups alphabetically and puts "未分類" last', () => { const workflows = [ { id: 'w1', name: 'A', active: true, tags: [{ name: 'Zタグ' }] }, { id: 'w2', name: 'B', active: true, tags: [] }, { id: 'w3', name: 'C', active: true, tags: [{ name: 'Aタグ' }] }, ]; const tree = buildTagTree(workflows); assert.deepStrictEqual( tree.map((g) => g.name), ['Aタグ', 'Zタグ', '未分類'] ); });