import { describe, it, expect } from 'vitest'
import {
  ThemeValidationError, ThemeColorFormatError, ThemeResolutionError,
  ThemeOverrideError, isThemeError,
} from './errors'

describe('theme-engine errors', () => {
  it('carry a stable name + THEME_-prefixed code', () => {
    expect(new ThemeValidationError('palettes', 'must be >=1').code).toBe('THEME_VALIDATION')
    expect(new ThemeColorFormatError('accent', 'oklch(...)').code).toBe('THEME_COLOR_FORMAT')
    expect(new ThemeResolutionError('unknown theme: x').code).toBe('THEME_RESOLUTION')
    expect(new ThemeOverrideError('radius-zz').code).toBe('THEME_OVERRIDE')
    expect(new ThemeValidationError('f', 'd').name).toBe('ThemeValidationError')
  })
  it('isThemeError matches structurally by code prefix (not instanceof)', () => {
    expect(isThemeError(new ThemeOverrideError('k'))).toBe(true)
    // a STRUCTURAL clone from a separate module copy still passes:
    expect(isThemeError({ code: 'THEME_VALIDATION', message: 'x' })).toBe(true)
    expect(isThemeError({ code: 'COMMENT_VALIDATION' })).toBe(false)
    expect(isThemeError(new Error('plain'))).toBe(false)
    expect(isThemeError(null)).toBe(false)
    expect(isThemeError('THEME_VALIDATION')).toBe(false)
  })
  it('field/detail are retained for actionable reporting', () => {
    const e = new ThemeColorFormatError('accent', 'not-hex')
    expect(e.field).toBe('accent')
    expect(e.detail).toBe('not-hex')
    expect(e.message).toContain('accent')
  })
})
