import { screen } from '@testing-library/react'
import { describe, expect, it } from 'vitest'

import TagLink from '@/components/TagLink'
import { buildTag } from '@/test/factories'
import { renderWithProviders } from '@/test/render'

describe ('TagLink', () => {
  it ('links tag names to post search and shows counts', () => {
    renderWithProviders (
        <TagLink tag={buildTag ({ name: '虹 夏', postCount: 4 })}/>,
    )

    expect (screen.getByRole ('link', { name: '虹 夏' })).toHaveAttribute (
        'href',
        '/posts?tags=%E8%99%B9+%E5%A4%8F',
    )
    expect (screen.getByText ('4')).toBeInTheDocument ()
  })

  it ('does not append deprecated state to the rendered tag name', () => {
    renderWithProviders (
        <TagLink
            tag={buildTag ({
                name:         '旧タグ',
                deprecatedAt: '2026-06-01T00:00:00.000Z',
            })}
            withWiki={false}
            withCount={false}/>,
    )

    expect (screen.getByRole ('link', { name: '旧タグ' })).toBeInTheDocument ()
    expect (screen.queryByText ('（廃止）')).not.toBeInTheDocument ()
  })

  it ('links wiki markers to the correct detail route', () => {
    renderWithProviders (
        <TagLink tag={buildTag ({ hasWiki: true, name: 'a/b' })}/>,
    )

    expect (screen.getByRole ('link', { name: '?' })).toHaveAttribute (
        'href',
        '/wiki/a%2Fb',
    )
  })

  it ('renders aliases and non-link tags when requested', () => {
    renderWithProviders (
        <TagLink
            tag={buildTag ({ matchedAlias: '別名', name: '正式名' })}
            linkFlg={false}
            withWiki={false}
            withCount={false}/>,
    )

    expect (screen.getByText ('別名')).toBeInTheDocument ()
    expect (screen.getByText ('正式名')).toBeInTheDocument ()
    expect (screen.queryByRole ('link')).not.toBeInTheDocument ()
  })
})
