import { describe, expect, it } from 'vitest';
import { ERA_ORDER, eraSkin, highestEra, type Era } from './eras';

const eraOf = (t: string): Era | undefined =>
  ({ a: 'reel', b: 'broadcast', c: 'disc', d: 'stream', e: 'fortress' } as Record<string, Era>)[t];

describe('era skins cover the codex', () => {
  it('has a distinct skin for every era', () => {
    const bars = new Set<string>();
    for (const era of ERA_ORDER) {
      const skin = eraSkin(era);
      expect(skin.bars).toHaveLength(7); // the substrate keeps SMPTE structure; only palette ages
      expect(skin.casts).toHaveLength(7);
      bars.add(skin.bars.join(','));
    }
    expect(bars.size).toBe(ERA_ORDER.length); // no two eras look alike
  });

  it('is deterministic — the same era always paints the same substrate', () => {
    expect(eraSkin('disc')).toEqual(eraSkin('disc'));
  });

  it('runs oldest to newest, reel first and fortress last', () => {
    expect(ERA_ORDER[0]).toBe('reel');
    expect(ERA_ORDER[ERA_ORDER.length - 1]).toBe('fortress');
  });
});

describe('highest ratified era', () => {
  it('starts in the deepest stratum when nothing is ratified', () => {
    expect(highestEra([], eraOf)).toBe('reel');
  });

  it('takes the newest era present, not the last listed', () => {
    expect(highestEra(['c', 'a', 'b'], eraOf)).toBe('disc');
    expect(highestEra(['e', 'a'], eraOf)).toBe('fortress');
  });

  it('ignores tech it cannot place rather than falling over', () => {
    // LANE-DATA can add tech ahead of me; unknown ids must not crash or reset the era.
    expect(highestEra(['b', 'who-knows'], eraOf)).toBe('broadcast');
  });

  it('never goes backwards as more tech is ratified', () => {
    let prev = -1;
    const acc: string[] = [];
    for (const t of ['a', 'b', 'c', 'd', 'e']) {
      acc.push(t);
      const idx = ERA_ORDER.indexOf(highestEra(acc, eraOf));
      expect(idx).toBeGreaterThanOrEqual(prev);
      prev = idx;
    }
  });
});
