import { describe, expect, it } from 'vitest'
import { buildNavigationEntries, filterNavigationEntries, groupByModule } from '../navigationCatalog'

describe('navigation catalogue', () => {
  it('includes the read-only component catalogue in the Design System menu', () => {
    const entries = buildNavigationEntries([
      {
        path: '/internal/ui-standard',
        meta: {
          screenId: 'SCR-DEV-001',
          module: 'Design System',
          section: 'Design System',
          title: '컴포넌트 확인',
          order: 1,
          favoriteAllowed: false,
          internalOnly: false
        }
      },
      {
        path: '/internal/wbs',
        meta: { screenId: 'SCR-DEV-002', module: 'Internal', title: 'WBS 작업공간', internalOnly: true }
      }
    ])

    expect(groupByModule(entries).find(section => section.module === 'Design System')?.entries).toEqual([
      expect.objectContaining({ path: '/internal/ui-standard', title: '컴포넌트 확인', internalOnly: false })
    ])
    expect(entries.find(entry => entry.path === '/internal/wbs')?.internalOnly).toBe(true)
  })

  it('preserves route permissions and filters navigation as a UI visibility hint', () => {
    const entries = buildNavigationEntries([
      { path: '/models', meta: { screenId: 'models', module: 'ModelOps', title: 'Models', permissions: ['model.read'] } },
      { path: '/home', meta: { screenId: 'home', module: 'Home', title: '홈' } },
    ])

    expect(entries[0].permissions).toEqual(['model.read'])
    expect(filterNavigationEntries(entries, new Set())).toEqual([])
    expect(filterNavigationEntries(entries, new Set(['model.read']))).toHaveLength(1)
  })

  it('fails closed for malformed permission metadata', () => {
    const [entry] = buildNavigationEntries([{ path: '/safe', meta: { screenId: 'safe', module: 'Operations', permissions: ['ops.read', 7] } }])
    expect(entry.permissions).toEqual([])
  })

  it('does not expose parameterized detail routes as top-level navigation', () => {
    expect(buildNavigationEntries([
      { path: '/models', meta: { screenId: 'models', module: 'ModelOps', title: 'Models' } },
      { path: '/models/:modelId', meta: { screenId: 'model-detail', module: 'ModelOps', title: 'Model Detail' } },
    ])).toEqual([expect.objectContaining({ path: '/models' })])
  })
})
