package space.animefu.starfallhaven;

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

/** A real save encoded by the tagged v0.1.0 codec must remain loadable forever. */
public final class V1SaveMigrationTest {
    @Test public void qualifiedV1FixtureLoadsWithKnownState() throws Exception {
        byte[] data;
        try(InputStream in=getClass().getResourceAsStream("/saves/v1-qualified.bin")){
            assertNotNull("missing frozen v1 fixture",in);
            ByteArrayOutputStream out=new ByteArrayOutputStream();byte[] b=new byte[4096];int n;
            while((n=in.read(b))!=-1)out.write(b,0,n);data=out.toByteArray();
        }
        GameWorld w=GameWorldSave.decode(data);
        assertEquals(51,w.wood);assertEquals(37,w.stone);assertEquals(22,w.food);assertEquals(15,w.crystal);
        assertEquals(3,w.day);assertEquals(.72f,w.timeOfDay,0f);assertEquals(2f,w.speed,0f);
        assertTrue(w.walls[18][14]);assertEquals(GameWorld.Tool.WALL,w.tool);
        assertEquals(74f,w.colonists.get(0).health,0f);assertEquals(41f,w.colonists.get(1).hunger,0f);
        assertEquals(63f,w.bondCharge,0f);
        // V2+ fields must be supplied by deterministic migration defaults, never random initialization.
        assertEquals(3,w.colonists.size());
        assertEquals("haven-1",w.colonists.get(0).id);
        assertFalse(w.colonists.get(0).traits.isEmpty());
        assertTrue(w.nextEntityId>3);
    }
}
