package space.animefu.starfallhaven;

import org.junit.Test;
import java.io.IOException;

import static org.junit.Assert.*;

public class ChronicleClientTest {
    @Test public void parsesBoundedProxyJson()throws Exception{
        ChronicleClient.Narrative n=ChronicleClient.parseResponse("{\"title\":\"Night Watch\",\"story\":\"A signal says \\\"hold\\\".\\nThey do.\",\"tone\":\"tense\"}");
        assertEquals("Night Watch",n.title);assertEquals("A signal says \"hold\".\nThey do.",n.story);assertEquals("tense",n.tone);
    }
    @Test public void rejectsMissingOrOversizedFields(){
        assertThrows(IOException.class,()->ChronicleClient.parseResponse("{\"title\":\"x\",\"story\":\"y\"}"));
        String huge="{\"title\":\"x\",\"story\":\""+"a".repeat(ChronicleClient.MAX_RESPONSE_BYTES)+"\",\"tone\":\"calm\"}";
        assertThrows(IOException.class,()->ChronicleClient.parseResponse(huge));
    }
    @Test public void requestIsFixedSchemaEscapedAndHistoryBounded(){
        ChronicleDirector.Snapshot snapshot=new ChronicleDirector.Snapshot(4,4,3,4,5,6,0,3,3,0,0,0,-100,0,100);
        ChronicleDirector.Outcome outcome=new ChronicleDirector().select(snapshot,new ChronicleHistory());ChronicleClient.Request r=new ChronicleClient.Request(outcome.prompt);
        String json=ChronicleClient.encodeRequest(r);
        assertTrue(json.contains("\"tension\":100"));assertTrue(json.contains("\"event\":\""+outcome.incident.name()+"\""));assertTrue(json.contains("\"colony\""));assertFalse(json.contains("incidentId"));assertTrue(json.getBytes(java.nio.charset.StandardCharsets.UTF_8).length<ChronicleClient.MAX_REQUEST_BYTES);
    }
}
