/**
 * Regression test for Bug #377:
 * "All 22 mana types auto-unlocked on first tick via conversion system"
 *
 * Expected: Only 'transference' should be unlocked at game start.
 * The conversion system must NOT auto-unlock elements that have non-zero rates.
 */
import { describe, it, expect, beforeEach } from 'vitest';
import { useManaStore } from '../stores/manaStore';
import { resetAllStores, tickN } from './cross-module-helpers';
import { BASE_UNLOCKED_ELEMENTS } from '../constants/elements';

function getUnlockedElements(): string[] {
  const elements = useManaStore.getState().elements;
  return Object.entries(elements)
    .filter(([, v]) => v.unlocked)
    .map(([k]) => k);
}

describe('Bug #377 — mana types must not auto-unlock on first tick', () => {
  beforeEach(() => {
    resetAllStores();
  });

  it('only transference is unlocked at game start', () => {
    const unlocked = getUnlockedElements();
    expect(unlocked).toEqual(BASE_UNLOCKED_ELEMENTS);
    expect(unlocked).toHaveLength(1);
    expect(unlocked).toContain('transference');
  });

  it('running one tick does not unlock any new elements', () => {
    const before = getUnlockedElements();
    expect(before).toEqual(['transference']);

    // Run a single game tick
    tickN(1);

    const after = getUnlockedElements();
    expect(after).toEqual(['transference']);
    expect(after).toHaveLength(1);
  });

  it('running multiple ticks does not unlock exotic elements', () => {
    // Run 20 ticks (simulating ~4 seconds of game time)
    tickN(20);

    const unlocked = getUnlockedElements();
    expect(unlocked).toEqual(['transference']);

    // Explicitly verify exotic elements remain locked
    const elements = useManaStore.getState().elements;
    const exoticElements = ['crystal', 'stellar', 'void', 'soul', 'time', 'plasma'];
    for (const elem of exoticElements) {
      expect(elements[elem]?.unlocked).toBe(false);
    }
  });

  it('conversion rates for locked elements do not cause unlock after many ticks', () => {
    // Run 50 ticks
    tickN(50);

    const unlocked = getUnlockedElements();
    expect(unlocked).toEqual(['transference']);
  });
});
