import { describe, it, expect } from 'vitest';
import { createFlagRegistry } from './index.js';

describe('createFlagRegistry', () => {
  it('returns default flag values', () => {
    const registry = createFlagRegistry({
      defaults: { 'feature.a': true, 'feature.b': false },
    });
    expect(registry.isFeatureEnabled('feature.a')).toBe(true);
    expect(registry.isFeatureEnabled('feature.b')).toBe(false);
  });

  it('returns false for unknown flags', () => {
    const registry = createFlagRegistry({ defaults: {} });
    expect(registry.isFeatureEnabled('nonexistent')).toBe(false);
  });

  it('getAllFlags returns all defaults', () => {
    const registry = createFlagRegistry({
      defaults: { a: true, b: false, c: true },
    });
    expect(registry.getAllFlags()).toEqual({ a: true, b: false, c: true });
  });

  it('setFlag overrides a value', () => {
    const registry = createFlagRegistry({ defaults: { x: false } });
    expect(registry.isFeatureEnabled('x')).toBe(false);
    registry.setFlag('x', true);
    expect(registry.isFeatureEnabled('x')).toBe(true);
  });

  it('setFlag creates new flags', () => {
    const registry = createFlagRegistry({ defaults: {} });
    registry.setFlag('new.flag', true);
    expect(registry.isFeatureEnabled('new.flag')).toBe(true);
    expect(registry.getAllFlags()).toEqual({ 'new.flag': true });
  });

  it('accepts userId parameter without error', () => {
    const registry = createFlagRegistry({ defaults: { a: true } });
    expect(registry.isFeatureEnabled('a', 'user-1')).toBe(true);
  });
});
