package main

import (
	"encoding/json"
	"os"
	"path/filepath"
	"testing"
)

func TestComparisonRejectsAnIndividualRegression(t *testing.T) {
	dir := t.TempDir()
	baseline := BenchReport{Dataset: "test"}
	candidate := BenchReport{Dataset: "test"}
	for i := 0; i < 5; i++ {
		a := Sample{Workload: "user", Repeat: i, Concurrency: 1, Requests: 1000, P50MS: 2, P95MS: 3, RPS: 400, Before: Resources{CPUTicks: 10}, After: Resources{CPUTicks: 110}, Peak: Resources{PSSKB: 1000, RSSKB: 2000}}
		b := a
		b.P50MS = 1
		b.P95MS = 2
		b.RPS = 800
		b.After.CPUTicks = 60
		b.Peak.PSSKB = 500
		b.Peak.RSSKB = 1000
		baseline.Samples = append(baseline.Samples, a)
		candidate.Samples = append(candidate.Samples, b)
	}
	write := func(name string, value any) string {
		path := filepath.Join(dir, name)
		raw, _ := json.Marshal(value)
		if err := os.WriteFile(path, raw, 0600); err != nil {
			t.Fatal(err)
		}
		return path
	}
	a := write("baseline.json", baseline)
	b := write("candidate.json", candidate)
	out := filepath.Join(dir, "result.json")
	if err := compareFiles(a, b, out, ""); err != nil {
		t.Fatal(err)
	}
	if err := compareFiles(a, b, out, "typo"); err == nil {
		t.Fatal("accepted empty workload selection")
	}
	candidate.Preparation = "different-physical-state"
	b = write("candidate.json", candidate)
	if err := compareFiles(a, b, out, ""); err == nil {
		t.Fatal("accepted mismatched database preparation")
	}
	candidate.Preparation = baseline.Preparation
	for i := range candidate.Samples {
		candidate.Samples[i].Peak.PSSKB = 1100
	}
	b = write("candidate.json", candidate)
	if err := compareFiles(a, b, out, ""); err == nil {
		t.Fatal("accepted memory regression despite faster latency")
	}
}

func TestBootstrapDifference(t *testing.T) {
	lo, hi := bootstrap([]float64{10, 10, 10, 10, 10}, []float64{3, 3, 3, 3, 3})
	if lo != -7 || hi != -7 {
		t.Fatalf("unexpected fixed-sample interval [%f,%f]", lo, hi)
	}
}
