import { readFileSync } from 'node:fs'
import { describe, expect, it } from 'vitest'

const tokens = readFileSync(new URL('../src/tokens/index.css', import.meta.url), 'utf8')
const preset = readFileSync(new URL('../../config/tailwind.preset.ts', import.meta.url), 'utf8')

function colorAt(name: string, mode: 'light' | 'dark'): string {
  const block = mode === 'light'
    ? tokens.match(/:root \{([\s\S]*?)\n\}/)?.[1]
    : tokens.match(/html\.dark \{([\s\S]*?)\n\}/)?.[1]
  const value = block?.match(new RegExp(`--${name}:\\s*([^;]+)`))?.[1]
  if (!value) throw new Error(`Missing ${mode} --${name}`)
  return value.trim()
}

describe('Wave 1 measured design tokens', () => {
  it('defines the exact semantic values in both themes', () => {
    expect(colorAt('ink-faint', 'dark')).toBe('oklch(61% .025 215)')
    expect(colorAt('control-border', 'light')).toBe('oklch(62% .03 195)')
    expect(colorAt('control-border', 'dark')).toBe('oklch(49% .045 230)')
    expect(colorAt('focus-ring', 'light')).toBe('oklch(62% .08 195)')
    expect(colorAt('focus-ring', 'dark')).toBe('oklch(49% .08 195)')
  })

  it('registers all accent foreground aliases against the ink-on-accent token', () => {
    for (const alias of ['on-accent', 'accent-fg', 'accent-foreground']) {
      expect(preset).toContain(`'${alias}': 'var(--ink-on-accent)'`)
      expect(colorAt(alias, 'light')).toBe('var(--ink-on-accent)')
      expect(colorAt(alias, 'dark')).toBe('var(--ink-on-accent)')
    }
  })

  it('keeps token source in the OKLCH color space', () => {
    expect(tokens).not.toMatch(/#[0-9a-f]{3,8}|\b(?:rgb|rgba|hsl|hsla)\s*\(/i)
    expect(tokens.match(/oklch\(/g)?.length).toBeGreaterThan(20)
  })
})
