/**
 * Education progression regression tests (audit blocker).
 *
 * Before this, c.education was set once at creation and never advanced — the
 * character never graduated, permanently locking credential-gated careers.
 * These lock in: grade advances by age, HS graduates at 18, college at 22, and
 * graduation lifecycle credit is queued.
 */
import { describe, it, expect } from 'vitest';
import { PlayerFactory } from '../../src/testing/PlayerFactory.js';
import { advanceEducation } from '../../src/services/education/education_manager.js';

function playerAt(age: number): any {
  const p: any = PlayerFactory.createAtAge(age);
  p.lifecycleQueue = [];
  p.messageQueue = [];
  return p;
}

describe('advanceEducation', () => {
  it('keeps the school grade current as the child ages', () => {
    const p = playerAt(7);
    p.c.ageYears = 8;
    advanceEducation(p);
    expect(p.c.education).toBe('3rd');
    p.c.ageYears = 15;
    advanceEducation(p);
    expect(p.c.education).toBe('10th');
  });

  it('graduates high school at 18 and queues graduation credit', () => {
    const p = playerAt(17);
    p.c.ageYears = 18;
    advanceEducation(p);
    expect(p.c.education).toBe('high_school');
    expect(p.lifecycleQueue.some((e: any) => e.type === 'graduation' && e.data?.level === 'high school')).toBe(true);
    expect(p.messageQueue.some((m: string) => /high school/i.test(m))).toBe(true);
  });

  it('graduates college to a bachelors degree at 22', () => {
    const p = playerAt(17);
    p.c.ageYears = 18; advanceEducation(p);      // HS first
    p.c.ageYears = 22; advanceEducation(p);      // then college
    expect(p.c.education).toBe('bachelors_degree');
    expect(p.lifecycleQueue.some((e: any) => e.type === 'graduation' && e.data?.level === 'college')).toBe(true);
  });

  it('is idempotent — graduation fires exactly once', () => {
    const p = playerAt(17);
    p.c.ageYears = 18; advanceEducation(p); advanceEducation(p); advanceEducation(p);
    const hsGrads = p.lifecycleQueue.filter((e: any) => e.type === 'graduation' && e.data?.level === 'high school').length;
    expect(hsGrads).toBe(1);
  });
});
