import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import * as roleResourceTree from '../src/views/system/role/modules/role-resource-tree';

type MenuNode = {
  id: string;
  children?: MenuNode[];
};

type NormalizeRoleMenuCheckedIds = (input: { menuTree: MenuNode[]; checkedIds: string[] }) => string[];

const menuTree: MenuNode[] = [
  {
    id: 'personal',
    children: [
      {
        id: 'weekly',
        children: [{ id: 'weeklyDetail' }]
      },
      {
        id: 'monthly',
        children: [{ id: 'monthlyDetail' }]
      }
    ]
  },
  {
    id: 'infra',
    children: [{ id: 'stateMachine' }, { id: 'cmd' }]
  }
];

const normalizeRoleMenuCheckedIds = (roleResourceTree as { normalizeRoleMenuCheckedIds?: NormalizeRoleMenuCheckedIds })
  .normalizeRoleMenuCheckedIds;

describe('normalizeRoleMenuCheckedIds', () => {
  it('removes partially covered parent ids before tree rendering', () => {
    assert.equal(typeof normalizeRoleMenuCheckedIds, 'function');

    const result = normalizeRoleMenuCheckedIds?.({
      menuTree,
      checkedIds: ['personal', 'weekly', 'weeklyDetail']
    });

    assert.deepEqual(result, ['weekly', 'weeklyDetail']);
  });

  it('keeps parent ids when the backend uses a parent-only full-branch representation', () => {
    assert.equal(typeof normalizeRoleMenuCheckedIds, 'function');

    const result = normalizeRoleMenuCheckedIds?.({
      menuTree,
      checkedIds: ['personal']
    });

    assert.deepEqual(result, ['personal']);
  });
});
