Files
admin-web/src/app/router/index.test.ts
T

122 lines
5.6 KiB
TypeScript

import { describe, expect, it } from 'vitest'
import { adminNavigation, getNavigationLeaves } from '@/core/navigation/adminNavigation'
import router from './index'
describe('admin router access metadata', () => {
it('uses the root route as a permission-aware entry point and registers the formal dashboard', () => {
const home = router.getRoutes().find((route) => route.name === 'home')
expect(home?.path).toBe('/')
expect(home?.meta.permission).toBeUndefined()
const dashboard = router.getRoutes().find((route) => route.name === 'dashboard')
expect(dashboard?.path).toBe('/dashboard')
expect(dashboard?.meta.permission).toBeUndefined()
})
it('protects warehouse management with its stable list permission', () => {
const warehouseRoute = router.getRoutes().find((route) => route.name === 'warehouses')
expect(warehouseRoute?.path).toBe('/warehouses')
expect(warehouseRoute?.meta.permission).toBe('platform:warehouse:list')
})
it('protects phase-two inbound operations and master data with deployed permissions', () => {
const expected = [
['inbound-orders', '/inbounds', 'platform:firstleg:inbound:list'],
['inbound-order-detail', '/inbounds/:inboundId', 'platform:firstleg:inbound:view'],
['fba-carrier-rates', '/first-leg-pricing/carrier-rates', 'firstleg:fba-pricing:rate:view'],
['locations', '/master-data/locations', 'platform:location:list'],
['pallets', '/master-data/pallets', 'platform:pallet:list'],
]
for (const [name, path, permission] of expected) {
const route = router.getRoutes().find((item) => item.name === name)
expect(route?.path).toBe(path)
expect(route?.meta.permission).toBe(permission)
}
})
it('does not register removed duplicate pricing and FBA placeholder pages', () => {
const removedRouteNames = ['fba-customer-rules', 'general-fba', 'general-voyage']
for (const name of removedRouteNames) {
expect(router.getRoutes().some((route) => route.name === name)).toBe(false)
}
})
it('protects express company records with a stable master-data permission', () => {
const route = router.getRoutes().find((item) => item.name === 'express-companies')
expect(route?.path).toBe('/master-data/express-companies')
expect(route?.meta.permission).toBe('platform:express:list')
})
it('registers the global smart-loading route before the single-container deep link', () => {
const route = router.getRoutes().find((item) => item.name === 'global-smart-loading-workbench')
const moduleChildren = router.options.routes.find((item) => item.path === '/')?.children || []
const globalIndex = moduleChildren.findIndex((item) => item.name === 'global-smart-loading-workbench')
const detailIndex = moduleChildren.findIndex((item) => item.name === 'smart-loading-workbench')
expect(route?.path).toBe('/container-loading/smart-loading')
expect(route?.meta.permission).toBe('platform:firstleg:smart-loading:list')
expect(globalIndex).toBeGreaterThanOrEqual(0)
expect(detailIndex).toBeGreaterThan(globalIndex)
})
it('registers phase-three service center routes with their stable permissions', () => {
const expected = [
['port-schedules', '/port-schedules', 'platform:firstleg:port-schedule:list'],
['customs-cases', '/customs', 'platform:firstleg:customs:list'],
['outbound-dispatches', '/outbound-dispatches', 'platform:firstleg:outbound-dispatch:list'],
]
for (const [name, path, permission] of expected) {
const route = router.getRoutes().find((item) => item.name === name)
expect(route?.path).toBe(path)
expect(route?.meta.permission).toBe(permission)
}
})
it('protects counterparty records with a stable master-data permission', () => {
const route = router.getRoutes().find((item) => item.name === 'counterparties')
expect(route?.path).toBe('/master-data/counterparties')
expect(route?.meta.permission).toBe('platform:counterparty:list')
})
it('protects the download center with its stable list permission', () => {
const route = router.getRoutes().find((item) => item.name === 'download-center')
expect(route?.path).toBe('/downloads')
expect(route?.meta.permission).toBe('platform:download:list')
})
it('redirects the removed enterprise review route to the protected customer list', () => {
const route = router.getRoutes().find((item) => item.name === 'enterprise-reviews')
expect(route?.path).toBe('/customers/reviews')
expect(route?.redirect).toBe('/customers')
expect(route?.meta.permission).toBe('platform:customer:list')
expect(route?.meta.permissionAnyOf).toContain('platform:customer:review')
})
it('keeps forbidden inside the authenticated layout without a business permission', () => {
const authenticatedLayout = router.options.routes.find((route) => route.path === '/')
const forbidden = authenticatedLayout?.children?.find((route) => route.name === 'forbidden')
const resolved = router.resolve('/403')
expect(authenticatedLayout?.meta?.requiresAuth).toBe(true)
expect(forbidden?.path).toBe('403')
expect(forbidden?.meta?.permission).toBeUndefined()
expect(resolved.matched.some((record) => record.meta.requiresAuth)).toBe(true)
})
it('keeps every registered navigation path and permission aligned with routes', () => {
for (const item of getNavigationLeaves(adminNavigation)) {
const resolved = router.resolve(item.path)
const pageRoute = resolved.matched.at(-1)
expect(pageRoute, `missing route for ${item.path}`).toBeDefined()
expect(pageRoute?.meta.permission, `permission mismatch for ${item.path}`).toBe(item.permission)
}
})
})