package space.animefu.starfallhaven;

import org.junit.Test;
import static org.junit.Assert.*;

public class ChronicleWorldIntegrationTest {
    @Test public void worldApplicationIsDeterministicAndRecordsPersistenceReadyHistory() {
        GameWorld a=new GameWorld(404),b=new GameWorld(404);a.day=b.day=12;a.food=b.food=80;a.crystal=b.crystal=20;
        ChronicleDirector.Outcome one=a.runChronicle(),two=b.runChronicle();
        assertEquals(one.incident,two.incident);assertEquals(a.wood,b.wood);assertEquals(a.food,b.food);
        assertEquals(1,a.chronicleHistory.size());
        ChronicleHistory.Entry entry=a.chronicleHistory.last();
        assertEquals(one.incident.id,entry.incidentId);assertEquals(one.selectionSeed,entry.selectionSeed);
        assertEquals(a.chronicleTension,entry.tensionAfter);
    }

    @Test public void worldApplicationClampsResourcesMoodThreatAndTension() {
        for(int seed=1;seed<=150;seed++) {
            GameWorld world=new GameWorld(seed);world.day=50;world.wood=world.stone=world.food=world.crystal=0;
            world.chronicleThreat=100;world.chronicleTension=100;
            for(GameWorld.Colonist colonist:world.colonists)colonist.mood=seed%2==0?0:100;
            world.runChronicle();
            assertTrue(world.wood>=0&&world.stone>=0&&world.food>=0&&world.crystal>=0&&world.components>=0);
            assertTrue(world.chronicleThreat>=0&&world.chronicleThreat<=100);
            assertTrue(world.chronicleTension>=0&&world.chronicleTension<=100);
            for(GameWorld.Colonist colonist:world.colonists)assertTrue(colonist.mood>=0&&colonist.mood<=100);
        }
    }

    @Test public void restoredHistoryContinuesCooldownAuthority() {
        GameWorld world=new GameWorld(8);world.day=20;
        ChronicleHistory restored=new ChronicleHistory();
        restored.add(new ChronicleHistory.Entry(19,ChronicleDirector.Incident.QUIET_DAY.id,12,10,"Quiet Orbit"));
        world.chronicleHistory=restored;
        ChronicleDirector.Outcome outcome=world.runChronicle();
        assertFalse(outcome.eligibleWeights.containsKey(ChronicleDirector.Incident.QUIET_DAY));
        assertEquals(2,world.chronicleHistory.size());
    }
}
