import { describe, it, expect } from 'vitest'
import { findOverflowRisks } from '../extract.js'

// Invariant under test: every `<code>` and every `font-mono` value in a `user`
// doc page carries a break utility (`break-all` / `break-words` / `overflow-wrap`).
// Without it a long unbreakable token (URL, permission key, namespace) fixes a
// wide min-content on its grid/flex cell and overflows the card — the exact
// PERMISSION / URL regression the gate now blocks.

describe('extract-doc / findOverflowRisks — overflow-prone technical tokens', () => {
  it('flags a <code> value with no break utility (the URL / permission card case)', () => {
    const tsx = `<div className="min-w-0 p-3">
  <code className="block text-xs text-[var(--text-secondary)]">{t('access.permission')}</code>
</div>`
    const hits = findOverflowRisks(tsx, 'src/pages/docs/business/platform/administration/themes/index.tsx')
    expect(hits).toHaveLength(1)
    expect(hits[0].token).toBe('code-no-break')
    expect(hits[0].line).toBe(2) // 1-based, points at the offending element
  })

  it('flags a font-mono table cell with no break utility (API endpoints table)', () => {
    const tsx = `<td className="py-2 px-3 font-mono text-xs">{ep.permission}</td>`
    const hits = findOverflowRisks(tsx, 'themes/index.tsx')
    expect(hits.map((h) => h.token)).toEqual(['mono-no-break'])
  })

  it('does NOT flag a <code> that already carries break-all', () => {
    const tsx = `<code className="block text-xs break-all">{t('access.url')}</code>`
    expect(findOverflowRisks(tsx, 'themes/index.tsx')).toEqual([])
  })

  it('does NOT flag a font-mono cell that carries break-all', () => {
    const tsx = `<td className="py-2 px-3 font-mono text-xs break-all">{ep.path}</td>`
    expect(findOverflowRisks(tsx, 'themes/index.tsx')).toEqual([])
  })

  it('accepts break-words / overflow-wrap as valid break utilities', () => {
    const tsx = `<code className="break-words">{t('a')}</code>
<span className="font-mono [overflow-wrap:anywhere]">{t('b')}</span>`
    expect(findOverflowRisks(tsx, 'themes/index.tsx')).toEqual([])
  })

  it('does NOT flag a non-mono badge span (HTTP method badge)', () => {
    const tsx = `<span className="px-2 py-0.5 rounded text-xs font-medium border">{ep.method}</span>`
    expect(findOverflowRisks(tsx, 'themes/index.tsx')).toEqual([])
  })

  it('does NOT flag a plain annotated Mock UI line', () => {
    const tsx = `<p className="text-[var(--text-secondary)] leading-relaxed">{t('objective')}</p>`
    expect(findOverflowRisks(tsx, 'themes/index.tsx')).toEqual([])
  })

  it('reports a single finding for <code className="font-mono"> (code precedence, not double)', () => {
    const tsx = `<code className="font-mono text-xs">{t('access.url')}</code>`
    const hits = findOverflowRisks(tsx, 'themes/index.tsx')
    expect(hits).toHaveLength(1)
    expect(hits[0].token).toBe('code-no-break')
  })

  it('does not scan i18n JSON (only the page TSX carries layout)', () => {
    const json = `{ "access": { "url": "/administration/uiconfiguration/themes" } }`
    expect(findOverflowRisks(json, 'src/i18n/locales/fr/docs-administration-themes.json')).toEqual([])
  })

  it('returns nothing for a fully compliant access section', () => {
    const tsx = `<div className="grid grid-cols-1 md:grid-cols-3 gap-3">
  <div className="min-w-0 p-3 rounded-lg bg-[var(--bg-secondary)]">
    <code className="block text-xs break-all">{t('access.url')}</code>
  </div>
  <div className="min-w-0 p-3 rounded-lg bg-[var(--bg-secondary)]">
    <code className="block text-xs break-all">{t('access.permission')}</code>
  </div>
</div>`
    expect(findOverflowRisks(tsx, 'themes/index.tsx')).toEqual([])
  })
})
