package ai.nektron.grownet.tests;

import ai.nektron.grownet.debug.GrowNetTraceMain;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class BenchmarkRunnerTests {

    @Test
    public void textBenchmarkEmitsStartResultAndSummary() throws Exception {
        String output = GrowNetTraceMain.runForTest(
                "--lang", "java",
                "--benchmark",
                "--scenario", "single_neuron",
                "--benchmark-warmup", "1",
                "--benchmark-iterations", "2",
                "--benchmark-repeat", "2"
        );
        assertTrue(output.contains("[benchmark.start]"));
        assertTrue(output.contains("[benchmark.result]"));
        assertTrue(output.contains("[benchmark.summary]"));
        assertTrue(output.contains("min_us="));
        assertTrue(output.contains("p50_us="));
        assertTrue(output.contains("p95_us="));
        assertTrue(output.contains("p99_us="));
        assertTrue(output.contains("max_us="));
        assertTrue(output.contains("mean_us="));
        assertTrue(output.contains("total_elapsed_ms="));
        assertTrue(output.contains("throughput_per_sec="));
    }

    @Test
    public void jsonlBenchmarkEmitsRequiredFields() throws Exception {
        String output = GrowNetTraceMain.runForTest(
                "--lang", "java",
                "--benchmark",
                "--benchmark-format", "jsonl",
                "--scenario", "two_layer",
                "--benchmark-warmup", "1",
                "--benchmark-iterations", "2",
                "--benchmark-repeat", "1"
        );
        String[] lines = output.split("\\R");
        assertEquals(3, lines.length);
        assertTrue(lines[0].contains("\"event\":\"benchmark.start\""));
        assertTrue(lines[0].contains("\"warmup\":1"));
        assertTrue(lines[0].contains("\"iterations\":2"));
        assertTrue(lines[0].contains("\"repeat\":1"));
        assertTrue(lines[1].contains("\"event\":\"benchmark.result\""));
        assertTrue(lines[1].contains("\"count\":2"));
        assertTrue(lines[1].contains("\"min_us\":"));
        assertTrue(lines[1].contains("\"p50_us\":"));
        assertTrue(lines[1].contains("\"throughput_per_sec\":"));
        assertTrue(lines[2].contains("\"event\":\"benchmark.summary\""));
        assertTrue(lines[2].contains("\"overall_mean_us\":"));
        assertTrue(lines[2].contains("\"best_p50_us\":"));
        assertTrue(lines[2].contains("\"best_p99_us\":"));
        assertTrue(lines[2].contains("\"best_throughput_per_sec\":"));
    }
}
