import React from 'react'
import { afterEach, describe, expect, it } from 'vitest'
import { cleanup, render, screen } from '@testing-library/react'
import CreatorSpotlight from './CreatorSpotlight'

describe('CreatorSpotlight related rail', () => {
  afterEach(() => {
    cleanup()
  })

  it('shows only artworks from the same author id', () => {
    render(
      <CreatorSpotlight
        artwork={{
          canonical_url: '/art/470/words',
          viewer: { id: 2 },
          user: { id: 2, name: 'psych0', username: 'psych0', profile_url: '/@psych0', followers_count: 25 },
          credits: {},
        }}
        presentSq={{ url: '/thumb/current.jpg' }}
        related={[
          {
            id: 101,
            title: 'Same author work',
            author: 'Completely different display name',
            author_id: 2,
            publisher_type: 'user',
            publisher_id: 2,
            url: '/art/101/same-author-work',
            thumb: '/thumb/101.jpg',
          },
          {
            id: 202,
            title: 'Wrong author work',
            author: 'psych0',
            author_id: 99,
            publisher_type: 'user',
            publisher_id: 99,
            url: '/art/202/wrong-author-work',
            thumb: '/thumb/202.jpg',
          },
        ]}
      />,
    )

    expect(screen.getByText(/more from psych0/i)).not.toBeNull()
    expect(screen.getByRole('link', { name: /same author work/i }).getAttribute('href')).toBe('/art/101/same-author-work')
    expect(screen.queryByRole('link', { name: /wrong author work/i })).toBeNull()
  })

  it('hides the rail when there are no same-author works', () => {
    render(
      <CreatorSpotlight
        artwork={{
          canonical_url: '/art/470/words',
          viewer: { id: 2 },
          user: { id: 2, name: 'psych0', username: 'psych0', profile_url: '/@psych0', followers_count: 25 },
          credits: {},
        }}
        presentSq={{ url: '/thumb/current.jpg' }}
        related={[
          {
            id: 202,
            title: 'Wrong author work',
            author: 'psych0',
            author_id: 99,
            publisher_type: 'user',
            publisher_id: 99,
            url: '/art/202/wrong-author-work',
            thumb: '/thumb/202.jpg',
          },
        ]}
      />,
    )

    expect(screen.queryByText(/more from psych0/i)).toBeNull()
    expect(screen.queryByRole('link', { name: /wrong author work/i })).toBeNull()
  })
})