import { describe, it, expect } from 'vitest';
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { dirname, resolve } from 'node:path';

const cssPath = resolve(dirname(fileURLToPath(import.meta.url)), '../index.css');

/** Collect `--var` names declared in the first `{...}` block after `selector`. */
function varsInBlock(css: string, selector: string): Set<string> {
  const i = css.indexOf(selector);
  if (i === -1) throw new Error(`selector not found in index.css: ${selector}`);
  const open = css.indexOf('{', i);
  const close = css.indexOf('}', open);
  const body = css.slice(open + 1, close);
  const names = new Set<string>();
  for (const m of body.matchAll(/(--[\w-]+)\s*:/g)) names.add(m[1]);
  return names;
}

describe('theme CSS var parity', () => {
  const css = readFileSync(cssPath, 'utf8');
  it(':root and [data-theme="dark"] declare identical custom properties', () => {
    const light = varsInBlock(css, '/* theme:light */');
    const dark = varsInBlock(css, '[data-theme="dark"]');
    expect([...light].filter((v) => !dark.has(v))).toEqual([]);
    expect([...dark].filter((v) => !light.has(v))).toEqual([]);
  });
  it('defines the core neutral ramp', () => {
    const light = varsInBlock(css, '/* theme:light */');
    for (const v of ['--slate-50', '--slate-500', '--slate-900', '--canvas', '--ink']) {
      expect(light.has(v)).toBe(true);
    }
  });
});
