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

import { menuOutline } from '@/components/TopNav'
import { buildUser } from '@/test/factories'

const submenuItem = (role: 'guest' | 'member' | 'admin', section: string, item: string) => {
  const menu = menuOutline ({
    user: buildUser ({ role }),
    wikiId: section === 'Wiki' ? 10 : null,
    pathName: section === 'Wiki' ? '/wiki/page' : '/posts' })
  return menu.find (entry => entry.name === section)?.subMenu.find (
      subMenuItem => subMenuItem.name === item)
}

describe ('menuOutline', () => {
  it ('uses content-edit permission for post, material, and Wiki actions', () => {
    for (const role of ['member', 'admin'] as const)
      {
	expect (submenuItem (role, '広場', '追加')?.visible).toBe (true)
	expect (submenuItem (role, '素材', '追加')?.visible).toBe (true)
	expect (submenuItem (role, 'Wiki', '新規')?.visible).toBe (true)
	expect (submenuItem (role, 'Wiki', '編輯')?.visible).toBe (true)
      }

    expect (submenuItem ('guest', '広場', '追加')?.visible).toBe (false)
    expect (submenuItem ('guest', '素材', '追加')?.visible).toBe (false)
    expect (submenuItem ('guest', 'Wiki', '新規')?.visible).toBe (false)
    expect (submenuItem ('guest', 'Wiki', '編輯')?.visible).toBe (false)
  })

  it ('uses /posts/new as the post creation entrypoint', () => {
    expect (submenuItem ('member', '広場', '追加')?.to).toBe ('/posts/new')
  })

  it ('keeps material suppression admin-only', () => {
    expect (submenuItem ('member', '素材', '抑止')?.visible).toBe (false)
    expect (submenuItem ('admin', '素材', '抑止')?.visible).toBe (true)
  })
})
