// @vitest-environment happy-dom
import { describe, expect, it, vi } from 'vitest'
import { render, screen } from '@testing-library/react'

// The sidebar fetches forms unread counts on mount; stub it so these brand
// tests stay offline and deterministic.
vi.mock('../../lib/forms-service.js', () => ({
  fetchFormsSidebarCounts: vi.fn(async () => []),
}))

// Drive the brand lockup directly: a branded install (initials derived from a
// site title) with no uploaded logo. happy-dom's localStorage stub is
// unreliable here, so we mock the theme hook rather than hydrating it.
vi.mock('../../components/ThemeProvider.js', () => ({
  useTheme: () => ({
    adminBrand: { primaryUrl: null, markUrl: null, darkUrl: null, initials: 'AC' },
    resolvedTheme: 'light',
    density: 'default',
  }),
}))

const { Sidebar } = await import('../../layout/Sidebar.js')

/**
 * Brand lockup fallback behaviour. When an install is branded (a site name is
 * set, so initials exist) but no logo has been uploaded, the sidebar must show
 * the neutral logo placeholder — NOT a filled initials badge, which reads as a
 * user avatar (the real account avatar already lives top-right).
 */
function renderSidebar(collapsed: boolean, config?: unknown) {
  return render(
    <Sidebar
      collapsed={collapsed}
      onToggleCollapse={() => {}}
      currentPath="/"
      onNavigate={() => {}}
      config={config}
    />,
  )
}

describe('Sidebar brand placeholder', () => {
  it('renders the logo placeholder (not an initials avatar) when branded with no logo — expanded', () => {
    renderSidebar(false, { admin: { branding: { name: 'Acme Co' } } })

    // Placeholder slot is present and the brand name shows beside it.
    expect(screen.getByText('Acme Co')).toBeTruthy()
    // The old avatar-style initials badge must not render.
    expect(screen.queryByText('AC')).toBeNull()
  })

  it('renders the logo placeholder slot when branded with no logo — collapsed', () => {
    renderSidebar(true)

    expect(screen.getByRole('img', { name: 'Logo placeholder' })).toBeTruthy()
    expect(screen.queryByText('AC')).toBeNull()
  })
})
