import { describe, expect, it } from 'vitest'
import {
  TermConflictError,
  TermCycleError,
  TermHasChildrenError,
  TermNotFoundError,
  TermValidationError,
  contentTaxonomyMigrationSql,
  MAX_TERM_DEPTH,
} from './taxonomy.js'

describe('taxonomy errors', () => {
  it('TermConflictError has structural name', () => {
    expect(new TermConflictError('tag', 'x', null).name).toBe('TermConflictError')
  })
  it('TermCycleError has structural name', () => {
    expect(new TermCycleError('a', 'b').name).toBe('TermCycleError')
  })
  it('TermHasChildrenError has structural name', () => {
    expect(new TermHasChildrenError('a').name).toBe('TermHasChildrenError')
  })
  it('TermNotFoundError has structural name', () => {
    expect(new TermNotFoundError('id=x').name).toBe('TermNotFoundError')
  })
  it('TermValidationError has structural name', () => {
    expect(new TermValidationError('slug', 'bad').name).toBe('TermValidationError')
  })

  it('exports MAX_TERM_DEPTH = 6', () => {
    expect(MAX_TERM_DEPTH).toBe(6)
  })
})

describe('contentTaxonomyMigrationSql', () => {
  it('emits idempotent CREATE TABLE + indexes only', () => {
    const ddl = contentTaxonomyMigrationSql()
    expect(ddl).toContain('CREATE TABLE IF NOT EXISTS content_terms')
    expect(ddl).toContain('CREATE TABLE IF NOT EXISTS content_entry_terms')
    expect(ddl).toContain('content_terms_sibling_slug_uq')
    expect(ddl).toContain('COALESCE(parent_id')
    expect(ddl).toContain('term_ids jsonb')
    expect(ddl).not.toMatch(/DROP COLUMN/i)
  })
})
