import assert from 'node:assert/strict';
import { test } from 'node:test';
import { normalizeObjectContext } from '../src/service/api/object-context-normalize';

const projectDomainConfig = {
  domainKey: 'project',
  mode: 'object-context',
  objectType: 'project',
  routePathPrefixes: ['/project'],
  entryRouteKey: 'project_list',
  entryRoutePath: '/project/list',
  fallbackDefaultRouteKey: 'project_project_overview',
  fallbackDefaultRoutePath: '/project/project/overview',
  contextApiPath: '/admin-api/project/project/{id}/context',
  contextApiObjectIdParamKey: 'id',
  contextApiObjectIdPlacement: 'path',
  objectIdQueryKey: 'objectId'
} satisfies App.ObjectContext.DomainConfig;

test('normalizes project context from legacy currentProject and navs payload', () => {
  const context = normalizeObjectContext(projectDomainConfig, '2049677988131078145', {
    contextScopedMenus: [],
    currentProject: {
      id: '2049677988131078145',
      projectCode: 'CNPJ2026001',
      projectName: '研发内部管理系统',
      projectType: 'mainline',
      productId: '2045487172243963906',
      managerUserId: '2034588597520314370',
      statusCode: 'pending'
    },
    currentRole: {
      roleId: '2049372079479685122',
      roleCode: 'project_manager',
      roleName: '项目经理',
      guestFlag: false
    },
    navs: [
      {
        id: '2049679882031243266',
        name: '概览',
        path: '/project/project/overview',
        icon: 'mdi:home-outline',
        sort: 1
      }
    ],
    buttons: ['project:project:update']
  });

  assert.equal(context.objectId, '2049677988131078145');
  assert.equal(context.objectName, '研发内部管理系统');
  assert.equal(context.contextScopedMenus.length, 1);
  assert.equal(context.contextScopedMenus[0].label, '概览');
  assert.equal(context.contextScopedMenus[0].routePath, '/project/project/overview');
  assert.deepEqual(context.buttonCodes, ['project:project:update']);
});
