// tests/token-exports-classify.test.ts
import { describe, it, expect } from 'vitest'
import { classify } from '../figma-sync/generate-token-exports.mjs'

const t = (name, value, section = '') => ({ name, value, scope: 'dark', section })

describe('classify', () => {
  it('palette color', () => expect(classify(t('color-grey-8', '#595959'))).toEqual({ layer: 'palette', $type: 'color' }))
  it('semantic color literal', () => expect(classify(t('bg-layer1', '#141414'))).toEqual({ layer: 'semantic', $type: 'color' }))
  it('semantic color via var', () => expect(classify(t('input-line-border', 'var(--color-grey-8)'))).toEqual({ layer: 'semantic', $type: 'color' }))
  it('spacing dimension', () => expect(classify(t('sp-m', '16px'))).toEqual({ layer: 'nonColor', $type: 'dimension' }))
  it('font family', () => expect(classify(t('font-family-en', '-apple-system, sans-serif'))).toEqual({ layer: 'nonColor', $type: 'fontFamily' }))

  // --- Regression guards for the 4 corrections over the plan draft (from the 293-token audit) ---

  // Change 1: layer gated on $type==='color'. A size token whose prefix
  // (`input-`) is a semantic-color prefix must NOT land in `semantic`.
  it('size token under a color prefix → nonColor/dimension (not semantic)', () =>
    expect(classify(t('input-number-min-width', '100px'))).toEqual({ layer: 'nonColor', $type: 'dimension' }))

  // Change 2: widened SEMANTIC_PREFIX (control-/msg-/radio-/…).
  it('widened prefix color literal (control-) → semantic/color', () =>
    expect(classify(t('control-default-border', '#595959'))).toEqual({ layer: 'semantic', $type: 'color' }))
  it('widened prefix color literal (msg-) → semantic/color', () =>
    expect(classify(t('msg-bg-red', '#3b1c1a'))).toEqual({ layer: 'semantic', $type: 'color' }))

  // Change 3: var()-alias $type resolved by the REFERENCED name's prefix, both directions.
  it('var alias to a color-prefixed name → semantic/color', () =>
    expect(classify(t('control-default-border', 'var(--icon-placeholder)'))).toEqual({ layer: 'semantic', $type: 'color' }))
  it('var alias to a non-color-prefixed name → nonColor/other (guards over-matching)', () =>
    expect(classify(t('slider-track-h-m', 'var(--sp-xs)'))).toEqual({ layer: 'nonColor', $type: 'other' }))

  // Change 4: color/dimension checks run before the section-based shadow check,
  // so non-shadow tokens sharing the "Effect Style" section are not mistyped.
  const EFFECT = 'Effect Style tokens — figma 真源 (3-layered drop shadows)'
  it('color token in the Effect section → semantic/color (not shadow)', () =>
    expect(classify(t('mask-overlay', 'rgba(0, 0, 0, 0.6)', EFFECT))).toEqual({ layer: 'semantic', $type: 'color' }))
  it('dimension token in the Effect section → nonColor/dimension (not shadow)', () =>
    expect(classify(t('mask-overlay-blur', '8px', EFFECT))).toEqual({ layer: 'nonColor', $type: 'dimension' }))
  it('real shadow token in the Effect section → nonColor/shadow', () =>
    expect(classify(t('shadow-l1', '0 5px 12px 4px #00000017, 0 3px 6px 0 #0000001f, 0 1px 2px -2px #00000029', EFFECT)))
      .toEqual({ layer: 'nonColor', $type: 'shadow' }))
})
