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

describe('InMemoryFeatureFlagStore', () => {
  it('reflects seeded state', () => {
    const store = createFlagStore({ new_checkout: true, beta_search: false });
    expect(store.isEnabled('new_checkout')).toBe(true);
    expect(store.isEnabled('beta_search')).toBe(false);
  });

  it('defaults unknown flags to OFF (fail-safe)', () => {
    const store = createFlagStore();
    expect(store.isEnabled('never_heard_of_it')).toBe(false);
  });

  it('matches flag names case-insensitively', () => {
    const store = createFlagStore({ New_Checkout: true });
    expect(store.isEnabled('new_checkout')).toBe(true);
    expect(store.isEnabled('NEW_CHECKOUT')).toBe(true);
  });

  it('flips state at runtime without a new store (no redeploy)', () => {
    const store = createFlagStore({ risky_path: false });
    expect(store.isEnabled('risky_path')).toBe(false);

    store.setEnabled('risky_path', true);
    expect(store.isEnabled('risky_path')).toBe(true);

    store.setEnabled('risky_path', false);
    expect(store.isEnabled('risky_path')).toBe(false);
  });

  it('skips a guarded path when the flag is off', () => {
    const store = createFlagStore({ risky_path: false });
    let ran = false;
    if (store.isEnabled('risky_path')) {
      ran = true;
    }
    expect(ran).toBe(false);

    store.setEnabled('risky_path', true);
    if (store.isEnabled('risky_path')) {
      ran = true;
    }
    expect(ran).toBe(true);
  });
});
