package benchmarks

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

func TestLoadAllOnTdBenchmarks(t *testing.T) {
	// Find the repo root from the test's working dir.
	wd, _ := os.Getwd()
	root := wd
	for i := 0; i < 6; i++ {
		if _, err := os.Stat(filepath.Join(root, ".td", "benchmarks")); err == nil {
			break
		}
		root = filepath.Dir(root)
	}
	dir := filepath.Join(root, ".td", "benchmarks")
	defs, err := LoadAll(dir)
	if err != nil {
		t.Fatalf("LoadAll: %v", err)
	}
	if len(defs) < 3 {
		t.Fatalf("expected ≥3 benchmark stubs in %s, got %d", dir, len(defs))
	}
	want := map[Kind]bool{KindDecomposable: false, KindCrossCutting: false, KindUnderspecified: false}
	for _, d := range defs {
		if _, ok := want[d.Kind]; ok {
			want[d.Kind] = true
		}
		if d.ID == "" || d.Title == "" || d.Body == "" {
			t.Errorf("bench %s: missing id/title/body", d.Path)
		}
	}
	for k, ok := range want {
		if !ok {
			t.Errorf("missing benchmark kind: %s", k)
		}
	}
}

func TestParseFrontmatter(t *testing.T) {
	raw := []byte("---\nid: x\nkind: decomposable\ntitle: T\n---\n\nbody body\n")
	def, err := parse(raw)
	if err != nil {
		t.Fatal(err)
	}
	if def.ID != "x" || def.Kind != KindDecomposable || def.Title != "T" {
		t.Fatalf("got %+v", def)
	}
	if def.Body != "body body\n" {
		t.Fatalf("body = %q", def.Body)
	}
}
