import { describe, it, expect } from 'vitest';
import { getEnemyBarrier } from '../utils/room-utils';

// ─── getEnemyBarrier ──────────────────────────────────────────────────────────

describe('getEnemyBarrier', () => {
  it('should return 0 for floors below 20', () => {
    for (let floor = 1; floor < 20; floor++) {
      expect(getEnemyBarrier(floor, 'fire')).toBe(0);
    }
  });

  it('should return barrier proportionally for floor 20', () => {
    const originalRandom = Math.random;
    Math.random = () => 0; // Always succeeds barrier roll
    expect(getEnemyBarrier(20, 'fire')).toBeGreaterThan(0);
    Math.random = originalRandom;
  });

  it('should return different barriers for different elements on same floor', () => {
    const fireBarrier = getEnemyBarrier(50, 'fire');
    const waterBarrier = getEnemyBarrier(50, 'water');
    const airBarrier = getEnemyBarrier(50, 'air');
    expect(typeof fireBarrier).toBe('number');
    expect(typeof waterBarrier).toBe('number');
    expect(typeof airBarrier).toBe('number');
  });

  it('should favor barrier elements more often', () => {
    const fireHasBarrier = getEnemyBarrier(50, 'fire') > 0;
    const lightHasBarrier = getEnemyBarrier(50, 'light') > 0;
    expect(typeof fireHasBarrier).toBe('boolean');
    expect(typeof lightHasBarrier).toBe('boolean');
  });

  it('barrier should be between 0.1 and 0.4 for floor 50', () => {
    const originalRandom = Math.random;
    Math.random = () => 0; // Always succeeds barrier roll
    const barrier = getEnemyBarrier(50, 'fire');
    expect(barrier).toBeGreaterThanOrEqual(0.1);
    expect(barrier).toBeLessThanOrEqual(0.4);
    Math.random = originalRandom;
  });

  it('should return 0 when barrier roll fails', () => {
    const originalRandom = Math.random;
    Math.random = () => 0.5; // > barrier chance
    const barrier = getEnemyBarrier(50, 'fire');
    expect(barrier).toBe(0);
    Math.random = originalRandom;
  });

  it('should cap at 0.4 maximum barrier', () => {
    const originalRandom = Math.random;
    Math.random = () => 0; // Always succeeds
    const barrier = getEnemyBarrier(200, 'fire');
    expect(barrier).toBeLessThanOrEqual(0.4);
    Math.random = originalRandom;
  });

  it('should handle all elements', () => {
    const elements = ['fire', 'water', 'air', 'earth', 'light', 'dark', 'death'];
    for (const elem of elements) {
      const barrier = getEnemyBarrier(50, elem);
      expect(typeof barrier).toBe('number');
    }
  });
});
