package dorks

import "testing"

// TestDorkCountGuardrail enforces the DORK-02 requirement that the embedded
// corpus ships with at least 150 built-in dorks. If this test fails, a dork
// file was removed or a YAML was broken — do not lower the threshold, restore
// the missing dorks instead.
func TestDorkCountGuardrail(t *testing.T) {
	r, err := NewRegistry()
	if err != nil {
		t.Fatalf("NewRegistry: %v", err)
	}
	const minTotal = 150
	if got := len(r.List()); got < minTotal {
		t.Fatalf("dork count regression: got %d, want >= %d (see DORK-02)", got, minTotal)
	}
}

// TestDorkCountPerSource enforces per-source minimums so a partial regression
// (e.g. deleting all github dorks) is caught even if the total still clears
// 150 via other sources.
func TestDorkCountPerSource(t *testing.T) {
	r, err := NewRegistry()
	if err != nil {
		t.Fatal(err)
	}

	minimums := map[string]int{
		"github":  50,
		"google":  30,
		"shodan":  20,
		"censys":  15,
		"zoomeye": 10,
		"fofa":    10,
		"gitlab":  10,
		"bing":    5,
	}
	stats := r.Stats()
	for src, min := range minimums {
		if stats.BySource[src] < min {
			t.Errorf("source %s: got %d, want >= %d", src, stats.BySource[src], min)
		}
	}
}

// TestDorkCategoriesPresent guarantees every category in the DORK-01 schema
// has at least one representative dork in the embedded corpus.
func TestDorkCategoriesPresent(t *testing.T) {
	r, err := NewRegistry()
	if err != nil {
		t.Fatal(err)
	}
	required := []string{"frontier", "specialized", "infrastructure", "emerging", "enterprise"}
	stats := r.Stats()
	for _, c := range required {
		if stats.ByCategory[c] == 0 {
			t.Errorf("category %q missing", c)
		}
	}
}

// TestDorkIDsUnique ensures no two dork files (across sources) use the same
// ID. Duplicate IDs would cause the later entry to shadow the earlier one in
// Registry.Get lookups.
func TestDorkIDsUnique(t *testing.T) {
	r, err := NewRegistry()
	if err != nil {
		t.Fatal(err)
	}
	seen := map[string]string{}
	for _, d := range r.List() {
		if existing, ok := seen[d.ID]; ok {
			t.Errorf("duplicate dork id %q (second occurrence: %s, first: %s)", d.ID, d.Source, existing)
		}
		seen[d.ID] = d.Source
	}
}
