import { assertDemoModeAllowed, demoModeEnabled } from './env';

describe('ADR-002 — double verrou DEMO_MODE', () => {
  const saved = { ...process.env };
  afterEach(() => {
    process.env = { ...saved };
  });

  it('refuse DEMO_MODE=true en production sans DEMO_MODE_I_KNOW', () => {
    process.env.DEMO_MODE = 'true';
    process.env.NODE_ENV = 'production';
    delete process.env.DEMO_MODE_I_KNOW;
    expect(() => assertDemoModeAllowed()).toThrow(/ADR-002/);
  });

  it('accepte DEMO_MODE=true en production AVEC DEMO_MODE_I_KNOW=true (instance démo)', () => {
    process.env.DEMO_MODE = 'true';
    process.env.NODE_ENV = 'production';
    process.env.DEMO_MODE_I_KNOW = 'true';
    expect(() => assertDemoModeAllowed()).not.toThrow();
  });

  it('accepte DEMO_MODE=true hors production', () => {
    process.env.DEMO_MODE = 'true';
    process.env.NODE_ENV = 'development';
    expect(() => assertDemoModeAllowed()).not.toThrow();
  });

  it("demoModeEnabled ne s'active que sur la valeur exacte « true »", () => {
    process.env.DEMO_MODE = '1';
    expect(demoModeEnabled()).toBe(false);
    process.env.DEMO_MODE = 'true';
    expect(demoModeEnabled()).toBe(true);
    delete process.env.DEMO_MODE;
    expect(demoModeEnabled()).toBe(false);
  });
});
