package report

import (
	"strings"
	"testing"

	"github.com/Armur-Ai/Pentest-Swarm-AI/internal/pipeline"
)

func TestOrderFindings_SeverityThenVerifiedFirst(t *testing.T) {
	fs := []pipeline.ReportFinding{
		{Title: "unverified-high", Severity: pipeline.SeverityHigh},
		{Title: "verified-high", Severity: pipeline.SeverityHigh, Verified: true},
		{Title: "verified-critical", Severity: pipeline.SeverityCritical, Verified: true},
		{Title: "low", Severity: pipeline.SeverityLow},
	}
	got := orderFindings(fs)
	order := []string{got[0].Title, got[1].Title, got[2].Title, got[3].Title}
	want := []string{"verified-critical", "verified-high", "unverified-high", "low"}
	if strings.Join(order, ",") != strings.Join(want, ",") {
		t.Errorf("order = %v, want %v", order, want)
	}
}

// Rendering leads with the proven critical.
func TestToMarkdown_LeadsWithVerified(t *testing.T) {
	rep := &pipeline.PentestReport{
		Findings: []pipeline.ReportFinding{
			{Title: "noise-high", Severity: pipeline.SeverityHigh},
			{Title: "proven-crit", Severity: pipeline.SeverityCritical, Verified: true, VerifiedBy: "oob-callback"},
		},
	}
	md, err := NewRenderer().ToMarkdown(rep)
	if err != nil {
		t.Fatal(err)
	}
	s := string(md)
	if strings.Index(s, "proven-crit") > strings.Index(s, "noise-high") {
		t.Error("the proven critical should render before the unverified high")
	}
}
