import { describe, expect, it } from 'vitest';

import { setTreePathLabels, TREE_PATH_LABEL_FIELD } from './utils';

describe('setTreePathLabels', () => {
  it('joins each node label with its ancestor labels', () => {
    const treeData: any[] = [
      {
        id: 'warehouse-1',
        fullName: '仓库1',
        children: [
          {
            id: 'area-101',
            fullName: '区域101',
            children: [
              {
                id: 'floor-101',
                fullName: '楼层 101',
                children: [
                  {
                    id: 'shelf-101',
                    fullName: '货架101',
                    children: [{ id: 'layer-101', fullName: '层数 101' }],
                  },
                ],
              },
            ],
          },
          { id: 'area-102', fullName: '区域102' },
        ],
      },
    ];

    setTreePathLabels(treeData, { children: 'children', key: 'id', label: 'fullName', value: 'id' });

    expect(treeData[0]![TREE_PATH_LABEL_FIELD]).toBe('仓库1');
    expect(treeData[0]!.children[0]![TREE_PATH_LABEL_FIELD]).toBe('仓库1/区域101');
    expect(treeData[0]!.children[0]!.children[0]!.children[0]!.children[0]![TREE_PATH_LABEL_FIELD]).toBe('仓库1/区域101/楼层 101/货架101/层数 101');
    expect(treeData[0]!.children[1]![TREE_PATH_LABEL_FIELD]).toBe('仓库1/区域102');
  });

  it('supports custom field names and ignores empty labels', () => {
    const treeData: any[] = [{ name: '仓库1', nodes: [{ name: '', nodes: [{ name: '层数 101', code: 101 }] }] }];

    setTreePathLabels(treeData, { children: 'nodes', key: 'code', label: 'name', value: 'code' });

    expect(treeData[0]!.nodes[0]!.nodes[0]![TREE_PATH_LABEL_FIELD]).toBe('仓库1/层数 101');
  });
});
