package eval

import (
	"context"
	"math/rand"
	"testing"

	"github.com/kami/maven/internal/phraser"
)

// TestTemplateNudges scores the hand-written Russian templates on the same
// fixture the model is scored on. No model, no network — it runs in milliseconds.
//
// The bar is every case, not most of them: the templates are hand-written, so a
// failure is a bug in one line of Russian, not model variance.
func TestTemplateNudges(t *testing.T) {
	f, err := Load()
	if err != nil {
		t.Fatalf("Load: %v", err)
	}
	// Fixed seed: the score must not depend on which variant came up.
	nt, err := phraser.NewNudgeTemplates(rand.NewSource(20260731))
	if err != nil {
		t.Fatalf("NewNudgeTemplates: %v", err)
	}
	rep, err := Score(context.Background(), "ru templates", nt, f)
	if err != nil {
		t.Fatalf("Score: %v", err)
	}
	t.Log("\n" + rep.String())
	t.Log("\n" + rep.Messages())
	if rep.Passed != rep.Total {
		t.Errorf("templates scored %d/%d, want every case:\n%s",
			rep.Passed, rep.Total, rep.Failures())
	}
}

// TestTemplateNudgesEverySeed — one seed passing could be luck. Every variant of
// every rule has to pass every check, so sweep seeds until each has been used.
func TestTemplateNudgesEverySeed(t *testing.T) {
	f, err := Load()
	if err != nil {
		t.Fatalf("Load: %v", err)
	}
	for seed := int64(0); seed < 60; seed++ {
		nt, err := phraser.NewNudgeTemplates(rand.NewSource(seed))
		if err != nil {
			t.Fatalf("NewNudgeTemplates: %v", err)
		}
		rep, err := Score(context.Background(), "ru templates", nt, f)
		if err != nil {
			t.Fatalf("Score: %v", err)
		}
		if rep.Passed != rep.Total {
			t.Errorf("seed %d: %d/%d\n%s", seed, rep.Passed, rep.Total, rep.Failures())
		}
	}
}
